Release 18.08
[platform/upstream/armnn.git] / src / armnnTfLiteParser / test / GetBuffer.cpp
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // See LICENSE file in the project root for full license information.
4 //
5
6 #include <boost/test/unit_test.hpp>
7 #include "ParserFlatbuffersFixture.hpp"
8 #include "../TfLiteParser.hpp"
9 #include <sstream>
10
11 using armnnTfLiteParser::TfLiteParser;
12
13 BOOST_AUTO_TEST_SUITE(TensorflowLiteParser)
14
15 struct GetBufferFixture : public ParserFlatbuffersFixture
16 {
17     explicit GetBufferFixture()
18     {
19         m_JsonString = R"(
20             {
21                 "version": 3,
22                 "operator_codes": [ { "builtin_code": "CONV_2D" } ],
23                 "subgraphs": [ {
24                     "tensors": [
25                         {
26                             "shape": [ 1, 3, 3, 1 ],
27                             "type": "UINT8",
28                             "buffer": 0,
29                             "name": "inputTensor",
30                             "quantization": {
31                                 "min": [ 0.0 ],
32                                 "max": [ 255.0 ],
33                                 "scale": [ 1.0 ],
34                                 "zero_point": [ 0 ],
35                             }
36                         },
37                         {
38                             "shape": [ 1, 1, 1, 1 ],
39                             "type": "UINT8",
40                             "buffer": 1,
41                             "name": "outputTensor",
42                             "quantization": {
43                                 "min": [ 0.0 ],
44                                 "max": [ 511.0 ],
45                                 "scale": [ 2.0 ],
46                                 "zero_point": [ 0 ],
47                             }
48                         },
49                         {
50                             "shape": [ 1, 3, 3, 1 ],
51                             "type": "UINT8",
52                             "buffer": 2,
53                             "name": "filterTensor",
54                             "quantization": {
55                                 "min": [ 0.0 ],
56                                 "max": [ 255.0 ],
57                                 "scale": [ 1.0 ],
58                                 "zero_point": [ 0 ],
59                             }
60                         }
61                     ],
62                     "inputs": [ 0 ],
63                     "outputs": [ 1 ],
64                     "operators": [
65                         {
66                             "opcode_index": 0,
67                             "inputs": [ 0, 2 ],
68                             "outputs": [ 1 ],
69                             "builtin_options_type": "Conv2DOptions",
70                             "builtin_options": {
71                                 "padding": "VALID",
72                                 "stride_w": 1,
73                                 "stride_h": 1,
74                                 "fused_activation_function": "NONE"
75                             },
76                             "custom_options_format": "FLEXBUFFERS"
77                         }
78                     ],
79                 } ],
80                 "buffers" : [
81                     { },
82                     { },
83                     { "data": [ 2,1,0,  6,2,1, 4,1,2 ], },
84                     { },
85                 ]
86             }
87         )";
88         ReadStringToBinary();
89     }
90
91     void CheckBufferContents(const TfLiteParser::ModelPtr& model,
92                              std::vector<int32_t> bufferValues, size_t bufferIndex)
93     {
94         for(long unsigned int i=0; i<bufferValues.size(); i++)
95         {
96             BOOST_CHECK_EQUAL(TfLiteParser::GetBuffer(model, bufferIndex)->data[i], bufferValues[i]);
97         }
98     }
99 };
100
101 BOOST_FIXTURE_TEST_CASE(GetBufferCheckContents, GetBufferFixture)
102 {
103     //Check contents of buffer are correct
104     TfLiteParser::ModelPtr model = TfLiteParser::LoadModelFromBinary(m_GraphBinary.data(), m_GraphBinary.size());
105     std::vector<int32_t> bufferValues = {2,1,0,6,2,1,4,1,2};
106     CheckBufferContents(model, bufferValues, 2);
107 }
108
109 BOOST_FIXTURE_TEST_CASE(GetBufferCheckEmpty, GetBufferFixture)
110 {
111     //Check if test fixture buffers are empty or not
112     TfLiteParser::ModelPtr model = TfLiteParser::LoadModelFromBinary(m_GraphBinary.data(), m_GraphBinary.size());
113     BOOST_CHECK(TfLiteParser::GetBuffer(model, 0)->data.empty());
114     BOOST_CHECK(TfLiteParser::GetBuffer(model, 1)->data.empty());
115     BOOST_CHECK(!TfLiteParser::GetBuffer(model, 2)->data.empty());
116     BOOST_CHECK(TfLiteParser::GetBuffer(model, 3)->data.empty());
117 }
118
119 BOOST_FIXTURE_TEST_CASE(GetBufferCheckParseException, GetBufferFixture)
120 {
121     //Check if armnn::ParseException thrown when invalid buffer index used
122     TfLiteParser::ModelPtr model = TfLiteParser::LoadModelFromBinary(m_GraphBinary.data(), m_GraphBinary.size());
123     BOOST_CHECK_THROW(TfLiteParser::GetBuffer(model, 4)->data.empty(), armnn::Exception);
124 }
125
126 BOOST_AUTO_TEST_SUITE_END()