ArmNN
 21.11
TestLayerVisitor.cpp
Go to the documentation of this file.
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 
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 
30 {
31  CHECK(layer != nullptr);
32 }
33 
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 
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 
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 
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 
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
void CheckLayerName(const char *name)
Interface for a layer that is connectable to other layers via InputSlots and OutputSlots.
Definition: INetwork.hpp:61
virtual const void * Map(bool) const override
Map the tensor data for access.
void CheckLayerPointer(const IConnectableLayer *layer)
unsigned int GetNumElements() const
Definition: Tensor.hpp:303
MemoryType GetMemoryArea() const
Definition: Tensor.hpp:305
Copyright (c) 2021 ARM Limited and Contributors.
void CheckConstTensors(const ConstTensor &expected, const ConstTensor &actual)
const TensorInfo & GetTensorInfo() const
bool has_value() const noexcept
Definition: Optional.hpp:53
A tensor defined by a TensorInfo (shape and data type) and an immutable backing store.
Definition: Tensor.hpp:327
const TensorInfo & GetInfo() const
Definition: Tensor.hpp:295
void CheckConstTensorPtrs(const std::string &name, const ConstTensor *expected, const ConstTensor *actual)
unsigned int GetNumDimensions() const
Definition: Tensor.hpp:301
virtual void Unmap() const override
Unmap the tensor data.
void CheckOptionalConstTensors(const Optional< ConstTensor > &expected, const Optional< ConstTensor > &actual)
unsigned int GetNumBytes() const
Definition: Tensor.hpp:302