Imported Upstream version 1.25.0
[platform/core/ml/nnfw.git] / compiler / mio-circle06 / 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_circle/Helper.h"
18
19 #include <flatbuffers/flatbuffers.h>
20 #include <gtest/gtest.h>
21
22 #include <vector>
23
24 class mio_circle06_helper_test : public ::testing::Test
25 {
26 protected:
27   void initialization_finish(void)
28   {
29     _fbb.Finish(circle::CreateModelDirect(_fbb, 0, &_opcodes_vec));
30   }
31
32 protected:
33   void add_operator_code(int8_t deprecated_builtin_code, const char *custom_code,
34                          circle::BuiltinOperator builtin_code)
35   {
36     _opcodes_vec.push_back(circle::CreateOperatorCodeDirect(
37       _fbb, deprecated_builtin_code, custom_code, 1 /* version */, builtin_code));
38   }
39
40   const circle::OperatorCode *get_operator_code(uint8_t idx)
41   {
42     return circle::GetModel(_fbb.GetBufferPointer())->operator_codes()->Get(idx);
43   }
44
45 private:
46   flatbuffers::FlatBufferBuilder _fbb;
47   std::vector<flatbuffers::Offset<circle::OperatorCode>> _opcodes_vec;
48 };
49
50 TEST_F(mio_circle06_helper_test, v06)
51 {
52   // BuiltinOperator_ADD = 0
53   // BuiltinOperator_CONV_2D = 3
54   add_operator_code(3, "", circle::BuiltinOperator_ADD);
55   initialization_finish();
56
57   ASSERT_TRUE(mio::circle::is_valid(get_operator_code(0)));
58   ASSERT_EQ(mio::circle::builtin_code_neutral(get_operator_code(0)),
59             circle::BuiltinOperator_CONV_2D);
60   ASSERT_FALSE(mio::circle::is_custom(get_operator_code(0)));
61 }
62
63 TEST_F(mio_circle06_helper_test, v06_custom_old)
64 {
65   // BuiltinOperator_ADD = 0
66   // BuiltinOperator_CUSTOM = 32
67   add_operator_code(32, "custom", circle::BuiltinOperator_ADD);
68   initialization_finish();
69
70   ASSERT_TRUE(mio::circle::is_valid(get_operator_code(0)));
71   ASSERT_EQ(mio::circle::builtin_code_neutral(get_operator_code(0)),
72             circle::BuiltinOperator_CUSTOM);
73   ASSERT_TRUE(mio::circle::is_custom(get_operator_code(0)));
74 }
75
76 TEST_F(mio_circle06_helper_test, v06_NEG)
77 {
78   // BuiltinOperator_ADD = 0
79   // BuiltinOperator_CUMSUM = 128
80   // deprecated_builtin_code cannot be negative value
81   add_operator_code(128, "", circle::BuiltinOperator_ADD);
82   initialization_finish();
83
84   ASSERT_FALSE(mio::circle::is_valid(get_operator_code(0)));
85 }
86
87 TEST_F(mio_circle06_helper_test, v06_under127)
88 {
89   // BuiltinOperator_CONV_2D = 3
90   add_operator_code(3, "", circle::BuiltinOperator_CONV_2D);
91   initialization_finish();
92
93   ASSERT_TRUE(mio::circle::is_valid(get_operator_code(0)));
94   ASSERT_EQ(mio::circle::builtin_code_neutral(get_operator_code(0)),
95             circle::BuiltinOperator_CONV_2D);
96   ASSERT_FALSE(mio::circle::is_custom(get_operator_code(0)));
97 }
98
99 TEST_F(mio_circle06_helper_test, v06_under127_NEG)
100 {
101   // BuiltinOperator_CONV_2D = 3
102   // BuiltinOperator_CUMSUM = 128
103   // deprecated_builtin_code cannot be negative value
104   add_operator_code(128, "", circle::BuiltinOperator_CONV_2D);
105   initialization_finish();
106
107   ASSERT_FALSE(mio::circle::is_valid(get_operator_code(0)));
108 }
109
110 TEST_F(mio_circle06_helper_test, v06_custom)
111 {
112   // BuiltinOperator_CUSTOM = 32
113   add_operator_code(32, "custom", circle::BuiltinOperator_CUSTOM);
114   initialization_finish();
115
116   ASSERT_TRUE(mio::circle::is_valid(get_operator_code(0)));
117   ASSERT_EQ(mio::circle::builtin_code_neutral(get_operator_code(0)),
118             circle::BuiltinOperator_CUSTOM);
119   ASSERT_TRUE(mio::circle::is_custom(get_operator_code(0)));
120 }
121
122 TEST_F(mio_circle06_helper_test, v06_custom_NEG)
123 {
124   // BuiltinOperator_CUMSUM = 128
125   // deprecated_builtin_code cannot be negative value
126   add_operator_code(128, "custom", circle::BuiltinOperator_CUSTOM);
127   initialization_finish();
128
129   ASSERT_FALSE(mio::circle::is_valid(get_operator_code(0)));
130 }
131
132 TEST_F(mio_circle06_helper_test, v06_over127)
133 {
134   // BuiltinOperator_PLACEHOLDER_FOR_GREATER_OP_CODES = 127
135   // BuiltinOperator_CUMSUM = 128
136   add_operator_code(127, "", circle::BuiltinOperator_CUMSUM);
137   initialization_finish();
138
139   ASSERT_TRUE(mio::circle::is_valid(get_operator_code(0)));
140   ASSERT_EQ(mio::circle::builtin_code_neutral(get_operator_code(0)),
141             circle::BuiltinOperator_CUMSUM);
142   ASSERT_FALSE(mio::circle::is_custom(get_operator_code(0)));
143 }
144
145 TEST_F(mio_circle06_helper_test, v06_over127_NEG)
146 {
147   // BuiltinOperator_CUMSUM = 128
148   // deprecated_builtin_code cannot be negative value
149   add_operator_code(128, "", circle::BuiltinOperator_CUMSUM);
150   initialization_finish();
151
152   ASSERT_FALSE(mio::circle::is_valid(get_operator_code(0)));
153 }