1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5
6 #include "TestLayerVisitor.hpp"
7
8 #include <doctest/doctest.h>
9
10 namespace armnn
11 {
12
CheckLayerName(const char * name)13 void TestLayerVisitor::CheckLayerName(const char* name)
14 {
15 if (name == nullptr)
16 {
17 CHECK(m_LayerName == nullptr);
18 }
19 else if (m_LayerName == nullptr)
20 {
21 CHECK(name == nullptr);
22 }
23 else
24 {
25 CHECK_EQ(std::string(m_LayerName), std::string(name));
26 }
27 }
28
CheckLayerPointer(const IConnectableLayer * layer)29 void TestLayerVisitor::CheckLayerPointer(const IConnectableLayer* layer)
30 {
31 CHECK(layer != nullptr);
32 }
33
CheckConstTensors(const ConstTensor & expected,const ConstTensor & actual)34 void TestLayerVisitor::CheckConstTensors(const ConstTensor& expected, const ConstTensor& actual)
35 {
36 CHECK(expected.GetInfo() == actual.GetInfo());
37 CHECK(expected.GetNumDimensions() == actual.GetNumDimensions());
38 CHECK(expected.GetNumElements() == actual.GetNumElements());
39 CHECK(expected.GetNumBytes() == actual.GetNumBytes());
40 if (expected.GetNumBytes() == actual.GetNumBytes())
41 {
42 //check data is the same byte by byte
43 const unsigned char* expectedPtr = static_cast<const unsigned char*>(expected.GetMemoryArea());
44 const unsigned char* actualPtr = static_cast<const unsigned char*>(actual.GetMemoryArea());
45 for (unsigned int i = 0; i < expected.GetNumBytes(); i++)
46 {
47 CHECK(*(expectedPtr + i) == *(actualPtr + i));
48 }
49 }
50 }
51
CheckConstTensors(const ConstTensor & expected,const ConstTensorHandle & actual)52 void TestLayerVisitor::CheckConstTensors(const ConstTensor& expected, const ConstTensorHandle& actual)
53 {
54 auto& actualInfo = actual.GetTensorInfo();
55 CHECK(expected.GetInfo() == actualInfo);
56 CHECK(expected.GetNumDimensions() == actualInfo.GetNumDimensions());
57 CHECK(expected.GetNumElements() == actualInfo.GetNumElements());
58 CHECK(expected.GetNumBytes() == actualInfo.GetNumBytes());
59 if (expected.GetNumBytes() == actualInfo.GetNumBytes())
60 {
61 //check data is the same byte by byte
62 const unsigned char* expectedPtr = static_cast<const unsigned char*>(expected.GetMemoryArea());
63 const unsigned char* actualPtr = static_cast<const unsigned char*>(actual.Map(true));
64 for (unsigned int i = 0; i < expected.GetNumBytes(); i++)
65 {
66 CHECK(*(expectedPtr + i) == *(actualPtr + i));
67 }
68 actual.Unmap();
69 }
70 }
71
CheckConstTensorPtrs(const std::string & name,const ConstTensor * expected,const std::shared_ptr<ConstTensorHandle> actual)72 void TestLayerVisitor::CheckConstTensorPtrs(const std::string& name,
73 const ConstTensor* expected,
74 const std::shared_ptr<ConstTensorHandle> actual)
75 {
76 if (expected == nullptr)
77 {
78 CHECK_MESSAGE(actual == nullptr, (name + " actual should have been a nullptr"));
79 }
80 else
81 {
82 CHECK_MESSAGE(actual != nullptr, (name + " actual should have been set"));
83 if (actual != nullptr)
84 {
85 CheckConstTensors(*expected, *actual);
86 }
87 }
88 }
89
CheckConstTensorPtrs(const std::string & name,const ConstTensor * expected,const ConstTensor * actual)90 void TestLayerVisitor::CheckConstTensorPtrs(const std::string& name,
91 const ConstTensor* expected,
92 const ConstTensor* actual)
93 {
94 if (expected == nullptr)
95 {
96 CHECK_MESSAGE(actual == nullptr, (name + " actual should have been a nullptr"));
97 }
98 else
99 {
100 CHECK_MESSAGE(actual != nullptr, (name + " actual should have been set"));
101 if (actual != nullptr)
102 {
103 CheckConstTensors(*expected, *actual);
104 }
105 }
106 }
107
CheckOptionalConstTensors(const Optional<ConstTensor> & expected,const Optional<ConstTensor> & actual)108 void TestLayerVisitor::CheckOptionalConstTensors(const Optional<ConstTensor>& expected,
109 const Optional<ConstTensor>& actual)
110 {
111 CHECK(expected.has_value() == actual.has_value());
112 if (expected.has_value() && actual.has_value())
113 {
114 CheckConstTensors(expected.value(), actual.value());
115 }
116 }
117
118 } //namespace armnn
119