Deprecate nGraph v0 ops and builders (#1856)
[platform/upstream/dldt.git] / ngraph / test / backend / round.in.cpp
1 //*****************************************************************************
2 // Copyright 2017-2020 Intel Corporation
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 #include "ngraph/ngraph.hpp"
19 #include "ngraph/runtime/tensor.hpp"
20 #include "runtime/backend.hpp"
21 #include "util/all_close.hpp"
22 #include "util/all_close_f.hpp"
23 #include "util/ndarray.hpp"
24 #include "util/test_control.hpp"
25 #include "util/test_tools.hpp"
26
27 NGRAPH_SUPPRESS_DEPRECATED_START
28
29 using namespace std;
30 using namespace ngraph;
31
32 static string s_manifest = "${MANIFEST}";
33
34 NGRAPH_TEST(${BACKEND_NAME}, round)
35 {
36     Shape shape{5};
37     auto A = make_shared<op::Parameter>(element::f32, shape);
38     auto f = make_shared<Function>(make_shared<op::Round>(A), ParameterVector{A});
39
40     auto backend = runtime::Backend::create("${BACKEND_NAME}");
41
42     auto a = backend->create_tensor(element::f32, shape);
43     copy_data(a, vector<float>{0.9f, 2.5f, 2.3f, 1.5f, -4.5f});
44     auto result = backend->create_tensor(element::f32, shape);
45
46     auto handle = backend->compile(f);
47     handle->call_with_validate({result}, {a});
48     EXPECT_TRUE(test::all_close_f((vector<float>{1.0f, 2.0f, 2.0f, 2.0f, -4.0f}),
49                                   read_vector<float>(result),
50                                   MIN_FLOAT_TOLERANCE_BITS));
51 }
52
53 NGRAPH_TEST(${BACKEND_NAME}, round_2D)
54 {
55     Shape shape{3, 5};
56     auto A = make_shared<op::Parameter>(element::f32, shape);
57     auto f = make_shared<Function>(make_shared<op::Round>(A), ParameterVector{A});
58
59     auto backend = runtime::Backend::create("${BACKEND_NAME}");
60
61     auto a = backend->create_tensor(element::f32, shape);
62     copy_data(a,
63               vector<float>{0.1f,
64                             0.5f,
65                             0.9f,
66                             1.2f,
67                             1.5f,
68                             1.8f,
69                             2.3f,
70                             2.5f,
71                             2.7f,
72                             -1.1f,
73                             -1.5f,
74                             -1.9f,
75                             -2.2f,
76                             -2.5f,
77                             -2.8f});
78     auto result = backend->create_tensor(element::f32, shape);
79
80     auto handle = backend->compile(f);
81     handle->call_with_validate({result}, {a});
82     EXPECT_TRUE(test::all_close_f(
83         (vector<float>{
84             0.f, 0.f, 1.f, 1.f, 2.f, 2.f, 2.f, 2.f, 3.f, -1.f, -2.f, -2.f, -2.f, -2.f, -3.f}),
85         read_vector<float>(result),
86         MIN_FLOAT_TOLERANCE_BITS));
87 }
88
89 NGRAPH_TEST(${BACKEND_NAME}, round_int64)
90 {
91     // This tests large numbers that will not fit in a double
92     Shape shape{3};
93     auto A = make_shared<op::Parameter>(element::i64, shape);
94     auto f = make_shared<Function>(make_shared<op::Round>(A), ParameterVector{A});
95
96     auto backend = runtime::Backend::create("${BACKEND_NAME}");
97
98     auto a = backend->create_tensor(element::i64, shape);
99     vector<int64_t> expected{0, 1, 0x4000000000000001};
100     copy_data(a, expected);
101     auto result = backend->create_tensor(element::i64, shape);
102
103     auto handle = backend->compile(f);
104     handle->call_with_validate({result}, {a});
105     EXPECT_EQ(expected, read_vector<int64_t>(result));
106 }