Imported Upstream version 1.25.0
[platform/core/ml/nnfw.git] / compiler / mio-tflite2121 / src / Helper.test.cpp
1 /*
2  * Copyright (c) 2023 Samsung Electronics Co., Ltd. All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *    http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "mio_tflite2121/Helper.h"
18
19 #include <flatbuffers/flatbuffers.h>
20 #include <gtest/gtest.h>
21
22 #include <vector>
23
24 class mio_tflite2121_helper_test : public ::testing::Test
25 {
26 protected:
27   void initialization_finish(void)
28   {
29     _fbb.Finish(tflite::CreateModelDirect(_fbb, 0, &_opcodes_vec));
30   }
31
32 protected:
33   void add_operator_code(int8_t deprecated_builtin_code, const char *custom_code,
34                          tflite::BuiltinOperator builtin_code)
35   {
36     _opcodes_vec.push_back(tflite::CreateOperatorCodeDirect(
37       _fbb, deprecated_builtin_code, custom_code, 1 /* version */, builtin_code));
38   }
39
40   const tflite::OperatorCode *get_operator_code(uint8_t idx)
41   {
42     return tflite::GetModel(_fbb.GetBufferPointer())->operator_codes()->Get(idx);
43   }
44
45 private:
46   flatbuffers::FlatBufferBuilder _fbb;
47   std::vector<flatbuffers::Offset<tflite::OperatorCode>> _opcodes_vec;
48 };
49
50 /**
51  * Extended 'builtin_code' is not in TFLite schema v3.
52  *
53  * Thus it is filled with 0(BuiltinOperator_ADD) in schame v3. Please refer to
54  * https://github.com/tensorflow/tensorflow/blob/1ab788fa8d08430be239ab970980b891ad7af494/tensorflow/lite/schema/schema_utils.cc#L28-L31
55  */
56 TEST_F(mio_tflite2121_helper_test, v3)
57 {
58   // BuiltinOperator_ADD = 0
59   // BuiltinOperator_CONV_2D = 3
60   add_operator_code(3, "", tflite::BuiltinOperator_ADD);
61   initialization_finish();
62
63   ASSERT_TRUE(mio::tflite::is_valid(get_operator_code(0)));
64   ASSERT_EQ(mio::tflite::builtin_code_neutral(get_operator_code(0)),
65             tflite::BuiltinOperator_CONV_2D);
66   ASSERT_FALSE(mio::tflite::is_custom(get_operator_code(0)));
67 }
68
69 TEST_F(mio_tflite2121_helper_test, v3_custom)
70 {
71   // BuiltinOperator_ADD = 0
72   // BuiltinOperator_CUSTOM = 32
73   add_operator_code(32, "custom", tflite::BuiltinOperator_ADD);
74   initialization_finish();
75
76   ASSERT_TRUE(mio::tflite::is_valid(get_operator_code(0)));
77   ASSERT_EQ(mio::tflite::builtin_code_neutral(get_operator_code(0)),
78             tflite::BuiltinOperator_CUSTOM);
79   ASSERT_TRUE(mio::tflite::is_custom(get_operator_code(0)));
80 }
81
82 TEST_F(mio_tflite2121_helper_test, v3_NEG)
83 {
84   // BuiltinOperator_ADD = 0
85   // BuiltinOperator_CUMSUM = 128
86   // deprecated_builtin_code cannot be negative value
87   add_operator_code(128, "", tflite::BuiltinOperator_ADD);
88   initialization_finish();
89
90   ASSERT_FALSE(mio::tflite::is_valid(get_operator_code(0)));
91 }
92
93 TEST_F(mio_tflite2121_helper_test, v3a_under127)
94 {
95   // BuiltinOperator_CONV_2D = 3
96   add_operator_code(3, "", tflite::BuiltinOperator_CONV_2D);
97   initialization_finish();
98
99   ASSERT_TRUE(mio::tflite::is_valid(get_operator_code(0)));
100   ASSERT_EQ(mio::tflite::builtin_code_neutral(get_operator_code(0)),
101             tflite::BuiltinOperator_CONV_2D);
102   ASSERT_FALSE(mio::tflite::is_custom(get_operator_code(0)));
103 }
104
105 TEST_F(mio_tflite2121_helper_test, v3a_under127_NEG)
106 {
107   // BuiltinOperator_CONV_2D = 3
108   // BuiltinOperator_CUMSUM = 128
109   // deprecated_builtin_code cannot be negative value
110   add_operator_code(128, "", tflite::BuiltinOperator_CONV_2D);
111   initialization_finish();
112
113   ASSERT_FALSE(mio::tflite::is_valid(get_operator_code(0)));
114 }
115
116 TEST_F(mio_tflite2121_helper_test, v3a_custom)
117 {
118   // BuiltinOperator_CUSTOM = 32
119   add_operator_code(32, "custom", tflite::BuiltinOperator_CUSTOM);
120   initialization_finish();
121
122   ASSERT_TRUE(mio::tflite::is_valid(get_operator_code(0)));
123   ASSERT_EQ(mio::tflite::builtin_code_neutral(get_operator_code(0)),
124             tflite::BuiltinOperator_CUSTOM);
125   ASSERT_TRUE(mio::tflite::is_custom(get_operator_code(0)));
126 }
127
128 TEST_F(mio_tflite2121_helper_test, v3a_custom_NEG)
129 {
130   // BuiltinOperator_CUMSUM = 128
131   // deprecated_builtin_code cannot be negative value
132   add_operator_code(128, "custom", tflite::BuiltinOperator_CUSTOM);
133   initialization_finish();
134
135   ASSERT_FALSE(mio::tflite::is_valid(get_operator_code(0)));
136 }
137
138 TEST_F(mio_tflite2121_helper_test, v3a_over127)
139 {
140   // BuiltinOperator_PLACEHOLDER_FOR_GREATER_OP_CODES = 127
141   // BuiltinOperator_CUMSUM = 128
142   add_operator_code(127, "", tflite::BuiltinOperator_CUMSUM);
143   initialization_finish();
144
145   ASSERT_TRUE(mio::tflite::is_valid(get_operator_code(0)));
146   ASSERT_EQ(mio::tflite::builtin_code_neutral(get_operator_code(0)),
147             tflite::BuiltinOperator_CUMSUM);
148   ASSERT_FALSE(mio::tflite::is_custom(get_operator_code(0)));
149 }
150
151 TEST_F(mio_tflite2121_helper_test, v3a_over127_NEG)
152 {
153   // BuiltinOperator_CUMSUM = 128
154   // deprecated_builtin_code cannot be negative value
155   add_operator_code(128, "", tflite::BuiltinOperator_CUMSUM);
156   initialization_finish();
157
158   ASSERT_FALSE(mio::tflite::is_valid(get_operator_code(0)));
159 }