Imported Upstream version 1.21.0
[platform/core/ml/nnfw.git] / compiler / circle-operator-test / src / circle-operator.test.cpp
1 /*
2  * Copyright (c) 2022 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 <gtest/gtest.h>
18
19 #include <cstdlib>
20 #include <fstream>
21 #include <vector>
22
23 class cirlce_operator_test : public ::testing::Test
24 {
25 protected:
26   bool initialize(void);
27   bool run(const std::string &command);
28
29 protected:
30   bool load(const std::string &file);
31
32 protected:
33   std::string _artifacts_path;
34   std::string _circle_operator_path;
35   std::string _result;
36 };
37
38 bool cirlce_operator_test::initialize(void)
39 {
40   char *path = std::getenv("ARTIFACTS_PATH");
41   if (path == nullptr)
42   {
43     std::cerr << "ARTIFACTS_PATH not found" << std::endl;
44     return false;
45   }
46   _artifacts_path = path;
47
48   path = std::getenv("CIRCLE_OPERATOR_PATH");
49   if (path == nullptr)
50   {
51     std::cerr << "ARTIFACTS_BIN_PATH not found" << std::endl;
52     return false;
53   }
54   _circle_operator_path = path;
55
56   return true;
57 }
58
59 bool cirlce_operator_test::run(const std::string &command)
60 {
61   std::vector<char> buffer(260);
62   std::string result = "";
63   std::string cmd_err = command + " 2>&1";
64   FILE *pipe = popen(cmd_err.c_str(), "r");
65   if (!pipe)
66   {
67     return false;
68   }
69   try
70   {
71     while (fgets(&buffer[0], buffer.size(), pipe) != NULL)
72     {
73       result += &buffer[0];
74     }
75   }
76   catch (...)
77   {
78     pclose(pipe);
79     return false;
80   }
81   pclose(pipe);
82   _result = result;
83
84   std::cout << _result << std::endl;
85
86   return true;
87 }
88
89 bool cirlce_operator_test::load(const std::string &file)
90 {
91   std::ifstream tmp(file.c_str());
92   if (tmp.fail())
93     return false;
94
95   std::stringstream buffer;
96   buffer << tmp.rdbuf();
97   _result = buffer.str();
98   return true;
99 }
100
101 TEST_F(cirlce_operator_test, valid_names)
102 {
103   if (!initialize())
104   {
105     FAIL();
106     return;
107   }
108
109   std::string model = _artifacts_path + "/Add_000.circle";
110   std::string command = _circle_operator_path + " --name " + model;
111   if (!run(command))
112   {
113     FAIL();
114     return;
115   }
116
117   const auto pos = _result.find("ofm");
118   ASSERT_NE(std::string::npos, pos);
119 }
120
121 TEST_F(cirlce_operator_test, valid_codes)
122 {
123   if (!initialize())
124   {
125     FAIL();
126     return;
127   }
128
129   std::string model = _artifacts_path + "/Add_000.circle";
130   std::string command = _circle_operator_path + " --code " + model;
131   if (!run(command))
132   {
133     FAIL();
134     return;
135   }
136
137   const auto pos = _result.find("ADD");
138   ASSERT_NE(std::string::npos, pos);
139 }
140
141 TEST_F(cirlce_operator_test, invalid_option_NEG)
142 {
143   if (!initialize())
144   {
145     FAIL();
146     return;
147   }
148
149   std::string model = _artifacts_path + "/Add_000.circle";
150   std::string command = _circle_operator_path + " --opname " + model;
151   if (!run(command))
152   {
153     FAIL();
154     return;
155   }
156
157   const auto pos = _result.find("Invalid argument");
158   ASSERT_NE(std::string::npos, pos);
159 }
160
161 TEST_F(cirlce_operator_test, check_code_name)
162 {
163   if (!initialize())
164   {
165     FAIL();
166     return;
167   }
168
169   std::string model = _artifacts_path + "/Add_000.circle";
170   std::string command = _circle_operator_path + " --code --name " + model;
171   if (!run(command))
172   {
173     FAIL();
174     return;
175   }
176
177   const auto pos = _result.find("ofm");
178   ASSERT_NE(std::string::npos, pos);
179   const auto pos2 = _result.find("ADD");
180   ASSERT_NE(std::string::npos, pos2);
181 }
182
183 TEST_F(cirlce_operator_test, nonexist_file_NEG)
184 {
185   if (!initialize())
186   {
187     FAIL();
188     return;
189   }
190
191   std::string model = _artifacts_path + "/non_exist_file.foo";
192   std::string command = _circle_operator_path + " --name " + model;
193   if (!run(command))
194   {
195     FAIL();
196     return;
197   }
198
199   const auto pos = _result.find("ERROR");
200   ASSERT_NE(std::string::npos, pos);
201 }
202
203 TEST_F(cirlce_operator_test, invalid_file_NEG)
204 {
205   if (!initialize())
206   {
207     FAIL();
208     return;
209   }
210
211   std::string model = _artifacts_path + "/Add_000.recipe";
212   std::string command = _circle_operator_path + " --name " + model;
213   if (!run(command))
214   {
215     FAIL();
216     return;
217   }
218
219   const auto pos = _result.find("ERROR");
220   ASSERT_NE(std::string::npos, pos);
221 }
222
223 TEST_F(cirlce_operator_test, output_file)
224 {
225   if (!initialize())
226   {
227     FAIL();
228     return;
229   }
230
231   std::string fileName("/tmp/a.txt");
232   std::remove(fileName.c_str());
233   std::string model = _artifacts_path + "/Add_000.circle";
234   std::string command = _circle_operator_path + " --code --output_path " + fileName + " " + model;
235   if (!run(command))
236   {
237     FAIL();
238     return;
239   }
240   if (!load(fileName))
241   {
242     FAIL();
243     return;
244   }
245
246   const auto pos = _result.find("ADD");
247   ASSERT_NE(std::string::npos, pos);
248 }