blob: bd435e139aff4a549dc835bb0268568968babfec [file] [log] [blame]
Colm Donelan0dfb2652023-06-22 10:19:17 +01001//
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 Donelan0dfb2652023-06-22 10:19:17 +01008#include <fstream>
Francis Murtagh22c8a4b2023-07-04 13:35:32 +01009#include <ghc/filesystem.hpp>
10
Colm Donelan0dfb2652023-06-22 10:19:17 +010011namespace
12{
13
Francis Murtagh22c8a4b2023-07-04 13:35:32 +010014namespace fs = ghc::filesystem;
Colm Donelan0dfb2652023-06-22 10:19:17 +010015
16TEST_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 Murtagh22c8a4b2023-07-04 13:35:32 +010038 ghc::filesystem::path fileName = fs::temp_directory_path().append("ComparisonFileIsEmpty.tmp");
Colm Donelan0dfb2652023-06-22 10:19:17 +010039 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 Murtagh22c8a4b2023-07-04 13:35:32 +010048 ghc::filesystem::remove(fileName);
Colm Donelan0dfb2652023-06-22 10:19:17 +010049 }
50
51 TEST_CASE("ComparisonFileHasValidHeaderAndData")
52 {
Francis Murtagh22c8a4b2023-07-04 13:35:32 +010053 ghc::filesystem::path fileName = fs::temp_directory_path().append("ComparisonFileHasValidHeaderAndData.tmp");
Colm Donelan0dfb2652023-06-22 10:19:17 +010054 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 Murtagh22c8a4b2023-07-04 13:35:32 +010064 ghc::filesystem::remove(fileName);
Colm Donelan0dfb2652023-06-22 10:19:17 +010065 // 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