ebae2ea26d4301dec5dcabc657a19a98021116ec
[platform/core/ml/nnfw.git] / tests / nnfw_api / src / NNPackages.cc
1 /*
2  * Copyright (c) 2020 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 "NNPackages.h"
18
19 #include <unistd.h>
20 #include <libgen.h>
21 #include <string.h>
22 #include <dirent.h>
23 #include <assert.h>
24 #include <stdexcept>
25
26 // NOTE Must match `enum TestPackages`
27 const char *TEST_PACKAGE_NAMES[] = {
28     // for validation test
29     "add", "add_no_manifest", "add_invalid_manifest",
30
31     // for dynamic tensor test
32     "input_reshaping_add", "dynamic_tensor_reshape", "unknown_dim_input_concat",
33     "add_unspecified_rank_inputs", "neg", "while_dynamic", "if_dynamic",
34 };
35
36 NNPackages &NNPackages::get()
37 {
38   static NNPackages instance;
39   return instance;
40 }
41
42 void NNPackages::init(const char *argv0)
43 {
44   char raw_dir[1024];
45   char cwd[1024];
46   strncpy(raw_dir, argv0, sizeof(raw_dir) - 1);
47   dirname(raw_dir);
48   if (raw_dir[0] == '/')
49   {
50     // If it is an absolute path, just use it
51     _base_path = raw_dir;
52   }
53   else
54   {
55     // If it is a relative path, prepend CWD
56     getcwd(cwd, sizeof(cwd));
57     _base_path = cwd;
58     _base_path += "/";
59     _base_path += raw_dir;
60   }
61 }
62
63 void NNPackages::checkAll()
64 {
65   assert(!_base_path.empty());
66
67   for (int i = 0; i < NNPackages::COUNT; i++)
68   {
69     std::string package_name = TEST_PACKAGE_NAMES[i];
70     std::string path = getModelAbsolutePath(i);
71
72     DIR *dir = opendir(path.c_str());
73     if (!dir)
74     {
75       std::string msg = "missing nnpackage: " + package_name + ", path: " + path;
76       throw std::runtime_error{msg};
77     }
78     closedir(dir);
79   }
80 }
81
82 std::string NNPackages::getModelAbsolutePath(int package_no)
83 {
84   if (package_no < 0 || package_no >= NNPackages::COUNT)
85   {
86     throw std::runtime_error{"Invalid package_no: " + std::to_string(package_no)};
87   }
88
89   const char *package_dir = TEST_PACKAGE_NAMES[package_no];
90   return getModelAbsolutePath(package_dir);
91 }
92
93 std::string NNPackages::getModelAbsolutePath(const char *package_name)
94 {
95   return _base_path + "/nnfw_api_gtest_models/" + package_name + "/" + package_name;
96 }