2 * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "RecordFunction.h"
22 #include <gtest/gtest.h>
24 namespace record_minmax
27 #define EXPECT_FLOAT_NEAR(exp, val) EXPECT_NEAR(exp, val, 1e-5 + 1e-5 * std::abs(exp))
29 TEST(GetNthPercentileTest, Edge)
31 std::vector<float> input{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
33 EXPECT_FLOAT_NEAR(0, getNthPercentile(input, 0));
34 EXPECT_FLOAT_NEAR(9, getNthPercentile(input, 100));
39 TEST(GetNthPercentileTest, Simple)
41 std::vector<float> input{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
43 for (float i = 1; i <= 99; i++)
45 EXPECT_FLOAT_NEAR(0.09 * i, getNthPercentile(input, i));
48 for (float i = 0.5; i <= 99.5; i++)
50 EXPECT_FLOAT_NEAR(0.09 * std::floor(i) + 0.045, getNthPercentile(input, i));
56 TEST(GetNthPercentileTest, Float)
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};
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));
72 TEST(GetNthPercentileTest, FloatWithNegative)
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};
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));
88 TEST(GetNthPercentileTest, SigleElement)
90 std::vector<float> input{33};
92 EXPECT_FLOAT_NEAR(33, getNthPercentile(input, 0));
93 EXPECT_FLOAT_NEAR(33, getNthPercentile(input, 50));
94 EXPECT_FLOAT_NEAR(33, getNthPercentile(input, 100));
99 TEST(GetNthPercentileTest, OutOfBoundary_NEG)
101 std::vector<float> input{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
103 EXPECT_THROW(getNthPercentile(input, -1), std::runtime_error);
104 EXPECT_THROW(getNthPercentile(input, 101), std::runtime_error);
109 TEST(GetNthPercentileTest, EmptyVector_NEG)
111 std::vector<float> input;
113 EXPECT_THROW(getNthPercentile(input, 10), std::runtime_error);
118 } // namespace record_minmax