Imported Upstream version 1.19.0
[platform/core/ml/nnfw.git] / compiler / luci / import / src / CircleReader.test.cpp
1 /*
2  * Copyright (c) 2021 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 "luci/Import/CircleReader.h"
18
19 #include <gtest/gtest.h>
20
21 TEST(VectorWrapperTest, basic_pattern)
22 {
23   auto fb_builder = flatbuffers::FlatBufferBuilder();
24
25   const std::vector<int32_t> data = {1, 4, 2, 0, 7};
26   auto const vec_offset = fb_builder.CreateVector(data.data(), data.size());
27   auto const vec_pointer = GetTemporaryPointer(fb_builder, vec_offset);
28
29   auto const wrapper = luci::wrap(vec_pointer);
30
31   ASSERT_EQ(wrapper.size(), data.size());
32   ASSERT_TRUE(std::equal(wrapper.begin(), wrapper.end(), data.begin()));
33 }
34
35 TEST(VectorWrapperTest, wrong_data_NEG)
36 {
37   auto fb_builder = flatbuffers::FlatBufferBuilder();
38
39   std::vector<int32_t> data = {1, 4, 2, 0, 7};
40   auto const vec_offset = fb_builder.CreateVector(data.data(), data.size());
41   auto const vec_pointer = GetTemporaryPointer(fb_builder, vec_offset);
42
43   auto const wrapper = luci::wrap(vec_pointer);
44
45   // change data
46   std::reverse(data.begin(), data.end());
47
48   ASSERT_EQ(wrapper.size(), data.size());
49   ASSERT_FALSE(std::equal(wrapper.begin(), wrapper.end(), data.begin()));
50 }
51
52 TEST(VectorWrapperTest, null_pointer)
53 {
54   flatbuffers::Vector<int32_t> *vec_pointer = nullptr;
55   auto const wrapper = luci::wrap(vec_pointer);
56
57   ASSERT_TRUE(wrapper.null());
58   ASSERT_TRUE(wrapper.empty());
59 }
60
61 TEST(VectorWrapperTest, prohibited_access_NEG)
62 {
63   flatbuffers::Vector<uint8_t> *vec_pointer = nullptr;
64   auto const wrapper = luci::wrap(vec_pointer);
65
66   ASSERT_ANY_THROW(wrapper.at(0));
67 }