aboutsummaryrefslogtreecommitdiff
path: root/src/armnn/test/TestLayerVisitor.cpp
blob: d5f705f0dab8ed72ca99bc449f6f900f51803e47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
//
// Copyright © 2017 Arm Ltd. All rights reserved.
// SPDX-License-Identifier: MIT
//

#include "TestLayerVisitor.hpp"

#include <doctest/doctest.h>

namespace armnn
{

void TestLayerVisitor::CheckLayerName(const char* name)
{
    if (name == nullptr)
    {
        CHECK(m_LayerName == nullptr);
    }
    else if (m_LayerName == nullptr)
    {
        CHECK(name == nullptr);
    }
    else
    {
        CHECK_EQ(std::string(m_LayerName), std::string(name));
    }
}

void TestLayerVisitor::CheckLayerPointer(const IConnectableLayer* layer)
{
    CHECK(layer != nullptr);
}

void TestLayerVisitor::CheckConstTensors(const ConstTensor& expected, const ConstTensor& actual)
{
    CHECK(expected.GetInfo() == actual.GetInfo());
    CHECK(expected.GetNumDimensions() == actual.GetNumDimensions());
    CHECK(expected.GetNumElements() == actual.GetNumElements());
    CHECK(expected.GetNumBytes() == actual.GetNumBytes());
    if (expected.GetNumBytes() == actual.GetNumBytes())
    {
        //check data is the same byte by byte
        const unsigned char* expectedPtr = static_cast<const unsigned char*>(expected.GetMemoryArea());
        const unsigned char* actualPtr = static_cast<const unsigned char*>(actual.GetMemoryArea());
        for (unsigned int i = 0; i < expected.GetNumBytes(); i++)
        {
            CHECK(*(expectedPtr + i) == *(actualPtr + i));
        }
    }
}

void TestLayerVisitor::CheckConstTensors(const ConstTensor& expected, const ConstTensorHandle& actual)
{
    auto& actualInfo = actual.GetTensorInfo();
    CHECK(expected.GetInfo() == actualInfo);
    CHECK(expected.GetNumDimensions() == actualInfo.GetNumDimensions());
    CHECK(expected.GetNumElements() == actualInfo.GetNumElements());
    CHECK(expected.GetNumBytes() == actualInfo.GetNumBytes());
    if (expected.GetNumBytes() == actualInfo.GetNumBytes())
    {
        //check data is the same byte by byte
        const unsigned char* expectedPtr = static_cast<const unsigned char*>(expected.GetMemoryArea());
        const unsigned char* actualPtr = static_cast<const unsigned char*>(actual.Map(true));
        for (unsigned int i = 0; i < expected.GetNumBytes(); i++)
        {
            CHECK(*(expectedPtr + i) == *(actualPtr + i));
        }
        actual.Unmap();
    }
}

void TestLayerVisitor::CheckConstTensorPtrs(const std::string& name,
                                            const ConstTensor* expected,
                                            const std::shared_ptr<ConstTensorHandle> actual)
{
    if (expected == nullptr)
    {
        CHECK_MESSAGE(actual == nullptr, name + " actual should have been a nullptr");
    }
    else
    {
        CHECK_MESSAGE(actual != nullptr, name + " actual should have been set");
        if (actual != nullptr)
        {
            CheckConstTensors(*expected, *actual);
        }
    }
}

void TestLayerVisitor::CheckConstTensorPtrs(const std::string& name,
                                            const ConstTensor* expected,
                                            const ConstTensor* actual)
{
    if (expected == nullptr)
    {
        CHECK_MESSAGE(actual == nullptr, name + " actual should have been a nullptr");
    }
    else
    {
        CHECK_MESSAGE(actual != nullptr, name + " actual should have been set");
        if (actual != nullptr)
        {
            CheckConstTensors(*expected, *actual);
        }
    }
}

void TestLayerVisitor::CheckOptionalConstTensors(const Optional<ConstTensor>& expected,
                                                 const Optional<ConstTensor>& actual)
{
    CHECK(expected.has_value() == actual.has_value());
    if (expected.has_value() && actual.has_value())
    {
        CheckConstTensors(expected.value(), actual.value());
    }
}

} //namespace armnn