Colm Donelan | 0dfb265 | 2023-06-22 10:19:17 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2023 Arm Ltd and Contributors. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include <ExecuteNetwork/FileComparisonExecutor.hpp> |
| 7 | #include <doctest/doctest.h> |
Colm Donelan | 0dfb265 | 2023-06-22 10:19:17 +0100 | [diff] [blame] | 8 | #include <fstream> |
Francis Murtagh | 22c8a4b | 2023-07-04 13:35:32 +0100 | [diff] [blame] | 9 | #include <ghc/filesystem.hpp> |
| 10 | |
Colm Donelan | 0dfb265 | 2023-06-22 10:19:17 +0100 | [diff] [blame] | 11 | namespace |
| 12 | { |
| 13 | |
Francis Murtagh | 22c8a4b | 2023-07-04 13:35:32 +0100 | [diff] [blame] | 14 | namespace fs = ghc::filesystem; |
Colm Donelan | 0dfb265 | 2023-06-22 10:19:17 +0100 | [diff] [blame] | 15 | |
| 16 | TEST_SUITE("FileComparisonExecutorTests") |
| 17 | { |
| 18 | |
| 19 | TEST_CASE("EmptyComparisonThrowsException") |
| 20 | { |
| 21 | ExecuteNetworkParams params; |
| 22 | FileComparisonExecutor classToTest(params); |
| 23 | // The comparison file is not set in the parameters. This should throw an exception. |
| 24 | CHECK_THROWS_AS(classToTest.Execute(), armnn::InvalidArgumentException); |
| 25 | } |
| 26 | |
| 27 | TEST_CASE("InvalidComparisonFilesThrowsException") |
| 28 | { |
| 29 | ExecuteNetworkParams params; |
| 30 | params.m_ComparisonFile = "Balh,Blah,Blah"; |
| 31 | FileComparisonExecutor classToTest(params); |
| 32 | // None of the files in the parameter exist. |
| 33 | CHECK_THROWS_AS(classToTest.Execute(), armnn::FileNotFoundException); |
| 34 | } |
| 35 | |
| 36 | TEST_CASE("ComparisonFileIsEmpty") |
| 37 | { |
Francis Murtagh | 22c8a4b | 2023-07-04 13:35:32 +0100 | [diff] [blame] | 38 | ghc::filesystem::path fileName = fs::temp_directory_path().append("ComparisonFileIsEmpty.tmp"); |
Colm Donelan | 0dfb265 | 2023-06-22 10:19:17 +0100 | [diff] [blame] | 39 | std::fstream tmpFile; |
| 40 | tmpFile.open(fileName, std::ios::out); |
| 41 | ExecuteNetworkParams params; |
| 42 | params.m_ComparisonFile = fileName; |
| 43 | FileComparisonExecutor classToTest(params); |
| 44 | // The comparison file is empty. This exception should happen in ExtractHeader when it realises it |
| 45 | // can't read a header. |
| 46 | CHECK_THROWS_AS(classToTest.Execute(), armnn::ParseException); |
| 47 | tmpFile.close(); |
Francis Murtagh | 22c8a4b | 2023-07-04 13:35:32 +0100 | [diff] [blame] | 48 | ghc::filesystem::remove(fileName); |
Colm Donelan | 0dfb265 | 2023-06-22 10:19:17 +0100 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | TEST_CASE("ComparisonFileHasValidHeaderAndData") |
| 52 | { |
Francis Murtagh | 22c8a4b | 2023-07-04 13:35:32 +0100 | [diff] [blame] | 53 | ghc::filesystem::path fileName = fs::temp_directory_path().append("ComparisonFileHasValidHeaderAndData.tmp"); |
Colm Donelan | 0dfb265 | 2023-06-22 10:19:17 +0100 | [diff] [blame] | 54 | std::fstream tmpFile; |
| 55 | tmpFile.open(fileName, std::ios::out); |
| 56 | // Write a valid header. |
| 57 | tmpFile << "TensorName, Float32 : 1.1000"; |
| 58 | tmpFile.close(); |
| 59 | ExecuteNetworkParams params; |
| 60 | params.m_ComparisonFile = fileName; |
| 61 | FileComparisonExecutor classToTest(params); |
| 62 | // The read in tensor should consist of 1 float. |
| 63 | std::vector<const void*> results = classToTest.Execute(); |
Francis Murtagh | 22c8a4b | 2023-07-04 13:35:32 +0100 | [diff] [blame] | 64 | ghc::filesystem::remove(fileName); |
Colm Donelan | 0dfb265 | 2023-06-22 10:19:17 +0100 | [diff] [blame] | 65 | // Should be one tensor in the data. |
| 66 | CHECK_EQ(1, results.size()); |
| 67 | // We expect there to be 1 element of value 1.1f. |
| 68 | const float* floatPtr = static_cast<const float*>(results[0]); |
| 69 | CHECK_EQ(*floatPtr, 1.1f); |
| 70 | } |
| 71 | |
| 72 | |
| 73 | } // End of TEST_SUITE("FileComparisonExecutorTests") |
| 74 | |
| 75 | } // anonymous namespace |