Imported Upstream version 1.12.0
[platform/core/ml/nnfw.git] / tests / nnfw_api / src / ValidationTestSessionCreated.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 #include "fixtures.h"
19
20 TEST_F(ValidationTestSessionCreated, load_session_001)
21 {
22   // Existing model must
23   ASSERT_EQ(nnfw_load_model_from_file(
24               _session, NNPackages::get().getModelAbsolutePath(NNPackages::ADD).c_str()),
25             NNFW_STATUS_NO_ERROR);
26 }
27
28 TEST_F(ValidationTestSessionCreated, close_and_create_again)
29 {
30   NNFW_ENSURE_SUCCESS(nnfw_close_session(_session));
31   NNFW_ENSURE_SUCCESS(nnfw_create_session(&_session));
32
33   SUCCEED();
34 }
35
36 TEST_F(ValidationTestSessionCreated, neg_load_session_1)
37 {
38   ASSERT_EQ(nnfw_load_model_from_file(
39               _session, NNPackages::get().getModelAbsolutePath("nonexisting_directory").c_str()),
40             NNFW_STATUS_ERROR);
41 }
42
43 TEST_F(ValidationTestSessionCreated, neg_load_session_2)
44 {
45   ASSERT_EQ(nnfw_load_model_from_file(_session, nullptr), NNFW_STATUS_UNEXPECTED_NULL);
46 }
47
48 TEST_F(ValidationTestSessionCreated, neg_load_session_3)
49 {
50   // Too long path
51   const std::string long_path(1024, 'x');
52   ASSERT_EQ(nnfw_load_model_from_file(
53               _session, NNPackages::get().getModelAbsolutePath(long_path.c_str()).c_str()),
54             NNFW_STATUS_ERROR);
55 }
56
57 TEST_F(ValidationTestSessionCreated, neg_load_invalid_package_1)
58 {
59   ASSERT_EQ(
60     nnfw_load_model_from_file(
61       _session, NNPackages::get().getModelAbsolutePath(NNPackages::ADD_NO_MANIFEST).c_str()),
62     NNFW_STATUS_ERROR);
63   ASSERT_EQ(nnfw_prepare(_session), NNFW_STATUS_INVALID_STATE);
64 }
65
66 TEST_F(ValidationTestSessionCreated, neg_load_invalid_package_2)
67 {
68   ASSERT_EQ(
69     nnfw_load_model_from_file(
70       _session, NNPackages::get().getModelAbsolutePath(NNPackages::ADD_INVALID_MANIFEST).c_str()),
71     NNFW_STATUS_ERROR);
72   ASSERT_EQ(nnfw_prepare(_session), NNFW_STATUS_INVALID_STATE);
73 }
74
75 TEST_F(ValidationTestSessionCreated, neg_prepare_001)
76 {
77   // nnfw_load_model_from_file was not called
78   ASSERT_EQ(nnfw_prepare(_session), NNFW_STATUS_INVALID_STATE);
79 }
80
81 TEST_F(ValidationTestSessionCreated, neg_run_001)
82 {
83   // nnfw_load_model_from_file and nnfw_prepare was not called
84   ASSERT_EQ(nnfw_run(_session), NNFW_STATUS_INVALID_STATE);
85 }
86
87 TEST_F(ValidationTestSessionCreated, neg_set_input_001)
88 {
89   ASSERT_EQ(nnfw_set_input(_session, 0, NNFW_TYPE_TENSOR_FLOAT32, nullptr, 0),
90             NNFW_STATUS_INVALID_STATE);
91 }
92
93 TEST_F(ValidationTestSessionCreated, neg_set_output_001)
94 {
95   ASSERT_EQ(nnfw_set_output(_session, 0, NNFW_TYPE_TENSOR_FLOAT32, nullptr, 0),
96             NNFW_STATUS_INVALID_STATE);
97 }
98
99 TEST_F(ValidationTestSessionCreated, neg_get_input_size)
100 {
101   uint32_t size = 10000;
102   ASSERT_EQ(nnfw_input_size(_session, &size), NNFW_STATUS_INVALID_STATE);
103   ASSERT_EQ(size, 10000); // Remain unchanged
104 }
105
106 TEST_F(ValidationTestSessionCreated, neg_get_output_size)
107 {
108   uint32_t size = 10000;
109   ASSERT_EQ(nnfw_output_size(_session, &size), NNFW_STATUS_INVALID_STATE);
110   ASSERT_EQ(size, 10000); // Remain unchanged
111 }
112
113 TEST_F(ValidationTestSessionCreated, neg_output_tensorinfo)
114 {
115   nnfw_tensorinfo tensor_info;
116   // model is not loaded
117   ASSERT_EQ(nnfw_output_tensorinfo(_session, 0, &tensor_info), NNFW_STATUS_INVALID_STATE);
118   // model is not loaded and tensor_info is null
119   ASSERT_EQ(nnfw_output_tensorinfo(_session, 0, nullptr), NNFW_STATUS_INVALID_STATE);
120 }
121
122 TEST_F(ValidationTestSessionCreated, neg_internal_set_config)
123 {
124   // All arguments are valid, but the session state is wrong
125   ASSERT_EQ(nnfw_set_config(_session, "TRACE_FILEPATH", ""), NNFW_STATUS_INVALID_STATE);
126   ASSERT_EQ(nnfw_set_config(_session, "GRAPH_DOT_DUMP", "0"), NNFW_STATUS_INVALID_STATE);
127 }