Imported Upstream version 1.8.0
[platform/core/ml/nnfw.git] / compiler / record-minmax / tests / RecordFunction.test.cpp
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 "RecordFunction.h"
18
19 #include <vector>
20 #include <cmath>
21
22 #include <gtest/gtest.h>
23
24 namespace record_minmax
25 {
26
27 #define EXPECT_FLOAT_NEAR(exp, val) EXPECT_NEAR(exp, val, 1e-5 + 1e-5 * std::abs(exp))
28
29 TEST(GetNthPercentileTest, Edge)
30 {
31   std::vector<float> input{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
32
33   EXPECT_FLOAT_NEAR(0, getNthPercentile(input, 0));
34   EXPECT_FLOAT_NEAR(9, getNthPercentile(input, 100));
35
36   SUCCEED();
37 }
38
39 TEST(GetNthPercentileTest, Simple)
40 {
41   std::vector<float> input{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
42
43   for (float i = 1; i <= 99; i++)
44   {
45     EXPECT_FLOAT_NEAR(0.09 * i, getNthPercentile(input, i));
46   }
47
48   for (float i = 0.5; i <= 99.5; i++)
49   {
50     EXPECT_FLOAT_NEAR(0.09 * std::floor(i) + 0.045, getNthPercentile(input, i));
51   }
52
53   SUCCEED();
54 }
55
56 TEST(GetNthPercentileTest, Float)
57 {
58   std::vector<float> input{8.48424583,  89.39998456, 65.83323245, 87.85243858, 68.85414866,
59                            98.40591775, 16.74266565, 25.09415131, 74.54084952, 29.70536481,
60                            49.26803928, 79.49602425, 53.69395631, 73.73140271, 99.81245733,
61                            46.76997646, 78.37688474, 10.43076744, 30.39480496, 14.30875609,
62                            86.72073486, 17.97364969, 14.66724564, 0.47818459,  17.77138025,
63                            85.68981239, 22.18322696, 78.81541331, 93.04085581, 40.2147895};
64
65   EXPECT_FLOAT_NEAR(2.799942346802177, getNthPercentile(input, 1));
66   EXPECT_FLOAT_NEAR(7.768503955476342, getNthPercentile(input, 3.14));
67   EXPECT_FLOAT_NEAR(99.40456084968194, getNthPercentile(input, 99));
68
69   SUCCEED();
70 }
71
72 TEST(GetNthPercentileTest, FloatWithNegative)
73 {
74   std::vector<float> input{-41.51575417, 39.39998456,  15.83323245,  37.85243858,  18.85414866,
75                            48.40591775,  -33.25733435, -24.90584869, 24.54084952,  -20.29463519,
76                            -0.73196072,  29.49602425,  3.69395631,   23.73140271,  49.81245733,
77                            -3.23002354,  28.37688474,  -39.56923256, -19.60519504, -35.69124391,
78                            36.72073486,  -32.02635031, -35.33275436, -49.52181541, -32.22861975,
79                            35.68981239,  -27.81677304, 28.81541331,  43.04085581,  -9.7852105};
80
81   EXPECT_FLOAT_NEAR(-47.20005765319782, getNthPercentile(input, 1));
82   EXPECT_FLOAT_NEAR(-42.23149604452366, getNthPercentile(input, 3.14));
83   EXPECT_FLOAT_NEAR(49.40456084968194, getNthPercentile(input, 99));
84
85   SUCCEED();
86 }
87
88 TEST(GetNthPercentileTest, SigleElement)
89 {
90   std::vector<float> input{33};
91
92   EXPECT_FLOAT_NEAR(33, getNthPercentile(input, 0));
93   EXPECT_FLOAT_NEAR(33, getNthPercentile(input, 50));
94   EXPECT_FLOAT_NEAR(33, getNthPercentile(input, 100));
95
96   SUCCEED();
97 }
98
99 TEST(GetNthPercentileTest, OutOfBoundary_NEG)
100 {
101   std::vector<float> input{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
102
103   EXPECT_THROW(getNthPercentile(input, -1), std::runtime_error);
104   EXPECT_THROW(getNthPercentile(input, 101), std::runtime_error);
105
106   SUCCEED();
107 }
108
109 TEST(GetNthPercentileTest, EmptyVector_NEG)
110 {
111   std::vector<float> input;
112
113   EXPECT_THROW(getNthPercentile(input, 10), std::runtime_error);
114
115   SUCCEED();
116 }
117
118 } // namespace record_minmax