Imported Upstream version 1.7.0
[platform/core/ml/nnfw.git] / compiler / locomotiv / src / Node / Push.test.cpp
1 /*
2  * Copyright (c) 2019 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 "NodeExecution.h"
18
19 #include "locomotiv/NodeData.h"
20 #include "NodeDataImpl.h"
21 #include "NodeDomain.h"
22
23 #include <nncc/core/ADT/tensor/Shape.h>
24 #include <nncc/core/ADT/tensor/Buffer.h>
25 #include <nncc/core/ADT/tensor/LexicalLayout.h>
26
27 #include <gtest/gtest.h>
28
29 using nncc::core::ADT::tensor::Index;
30 using nncc::core::ADT::tensor::Shape;
31 using nncc::core::ADT::tensor::LexicalLayout;
32 using nncc::core::ADT::tensor::make_buffer;
33
34 TEST(NodeExecution_Push, s32)
35 {
36   // Make pull-push graph
37   auto g = loco::make_graph();
38   auto pull = g->nodes()->create<loco::Pull>();
39   pull->dtype(loco::DataType::S32);
40   pull->shape({1});
41   auto push = g->nodes()->create<loco::Push>();
42   push->from(pull);
43
44   // Make and assign data to pull node
45   auto pull_buf = make_buffer<int32_t, LexicalLayout>(Shape{1});
46   pull_buf.at(Index{0}) = 42;
47   auto pull_data = locomotiv::make_data(pull_buf);
48   locomotiv::annot_data(pull, std::move(pull_data));
49   locomotiv::annot_domain(pull, loco::Domain::Tensor);
50
51   locomotiv::NodeExecution::get().run(push);
52
53   auto push_data = locomotiv::annot_data(push);
54   ASSERT_NE(push_data, nullptr);
55   ASSERT_EQ(loco::DataType::S32, push_data->dtype());
56   ASSERT_EQ(Shape{1}, *(push_data->shape()));
57   ASSERT_EQ(pull_buf.at(Index{0}), push_data->as_s32_bufptr()->at(Index{0}));
58
59   ASSERT_EQ(loco::Domain::Tensor, locomotiv::annot_domain(push));
60 }
61
62 TEST(NodeExecution_Push, f32)
63 {
64   // Make pull-push graph
65   auto g = loco::make_graph();
66   auto pull = g->nodes()->create<loco::Pull>();
67   pull->dtype(loco::DataType::FLOAT32);
68   pull->shape({1});
69   auto push = g->nodes()->create<loco::Push>();
70   push->from(pull);
71
72   // Make and assign data to pull node
73   auto pull_buf = make_buffer<float, LexicalLayout>(Shape{1});
74   pull_buf.at(Index{0}) = 3.14f;
75   auto pull_data = locomotiv::make_data(pull_buf);
76   locomotiv::annot_data(pull, std::move(pull_data));
77   locomotiv::annot_domain(pull, loco::Domain::Tensor);
78
79   locomotiv::NodeExecution::get().run(push);
80
81   auto push_data = locomotiv::annot_data(push);
82   ASSERT_NE(push_data, nullptr);
83   ASSERT_EQ(loco::DataType::FLOAT32, push_data->dtype());
84   ASSERT_EQ(Shape{1}, *(push_data->shape()));
85   ASSERT_FLOAT_EQ(pull_buf.at(Index{0}), push_data->as_f32_bufptr()->at(Index{0}));
86
87   ASSERT_EQ(loco::Domain::Tensor, locomotiv::annot_domain(push));
88 }