1 /* sane - Scanner Access Now Easy.
2 
3    Copyright (C) 2019 Povilas Kanapickas <povilas@radix.lt>
4 
5    This file is part of the SANE package.
6 
7    This program is free software; you can redistribute it and/or
8    modify it under the terms of the GNU General Public License as
9    published by the Free Software Foundation; either version 2 of the
10    License, or (at your option) any later version.
11 
12    This program is distributed in the hope that it will be useful, but
13    WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    General Public License for more details.
16 
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <https://www.gnu.org/licenses/>.
19 */
20 
21 #define DEBUG_DECLARE_ONLY
22 
23 #include "tests.h"
24 #include "tests_printers.h"
25 #include "minigtest.h"
26 
27 #include "../../../backend/genesys/utilities.h"
28 
29 namespace genesys {
30 
test_utilities_compute_array_percentile_approx_empty()31 void test_utilities_compute_array_percentile_approx_empty()
32 {
33     std::vector<std::uint16_t> data;
34     data.resize(1, 0);
35 
36     ASSERT_RAISES(compute_array_percentile_approx(data.data(), data.data(), 0, 0, 0.0f),
37                   SaneException);
38 }
39 
test_utilities_compute_array_percentile_approx_single_line()40 void test_utilities_compute_array_percentile_approx_single_line()
41 {
42     std::vector<std::uint16_t> data = {
43         0, 1, 2, 3, 4, 5, 6, 7, 8, 9
44     };
45     std::vector<std::uint16_t> expected = data;
46     std::vector<std::uint16_t> result;
47     result.resize(data.size(), 0);
48 
49     compute_array_percentile_approx(result.data(), data.data(), 1, data.size(), 0.5f);
50     ASSERT_EQ(result, expected);
51 }
52 
test_utilities_compute_array_percentile_approx_multiple_lines()53 void test_utilities_compute_array_percentile_approx_multiple_lines()
54 {
55     std::vector<std::uint16_t> data = {
56          5, 17,  4, 14,  3,  9,  9,  5, 10,  1,
57          6,  1,  0, 18,  8,  5, 11, 11, 15, 12,
58          6,  8,  7,  3,  2, 15,  5, 12,  3,  3,
59          6, 12, 17,  6,  7,  7,  1,  6,  3, 18,
60         10,  5,  8,  0, 14,  3,  3,  7, 10,  5,
61         18,  7,  3, 11,  0, 14, 12, 19, 18, 11,
62          5, 16,  2,  9,  8,  2,  7,  6, 11, 18,
63         16,  5,  2,  2, 14, 18, 19, 13, 16,  1,
64          5,  9, 14,  6, 17, 16,  1,  1, 16,  0,
65         19, 18,  4, 12,  0,  7, 15,  3,  2,  6,
66     };
67     std::vector<std::uint16_t> result;
68     result.resize(10, 0);
69 
70     std::vector<std::uint16_t> expected = {
71         5, 1, 0, 0, 0, 2, 1, 1, 2, 0,
72     };
73     compute_array_percentile_approx(result.data(), data.data(), 10, 10, 0.0f);
74     ASSERT_EQ(result, expected);
75 
76     expected = {
77         5, 5, 2, 3, 2, 5, 3, 5, 3, 1,
78     };
79     compute_array_percentile_approx(result.data(), data.data(), 10, 10, 0.25f);
80     ASSERT_EQ(result, expected);
81 
82     expected = {
83         6, 9, 4, 9, 8, 9, 9, 7, 11, 6,
84     };
85     compute_array_percentile_approx(result.data(), data.data(), 10, 10, 0.5f);
86     ASSERT_EQ(result, expected);
87 
88     expected = {
89         16, 16, 8, 12, 14, 15, 12, 12, 16, 12,
90     };
91     compute_array_percentile_approx(result.data(), data.data(), 10, 10, 0.75f);
92     ASSERT_EQ(result, expected);
93 
94     expected = {
95         19, 18, 17, 18, 17, 18, 19, 19, 18, 18,
96     };
97     compute_array_percentile_approx(result.data(), data.data(), 10, 10, 1.0f);
98     ASSERT_EQ(result, expected);
99 }
100 
test_utilities()101 void test_utilities()
102 {
103     test_utilities_compute_array_percentile_approx_empty();
104     test_utilities_compute_array_percentile_approx_single_line();
105     test_utilities_compute_array_percentile_approx_multiple_lines();
106 }
107 
108 } // namespace genesys
109