1 /*
2 * Copyright 2019 The Android Open Source Project
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 /*
18 * Test Histogram
19 */
20
21 #include <iostream>
22
23 #include <gtest/gtest.h>
24
25 #include <audio_utils/Histogram.h>
26
27 using namespace android::audio_utils;
28
29 static constexpr int32_t kBinWidth = 10;
30 static constexpr int32_t kNumBins = 20;
31
TEST(test_histogram,module_sinki16)32 TEST(test_histogram, module_sinki16) {
33 Histogram histogram(kNumBins, kBinWidth);
34 ASSERT_EQ(kNumBins, histogram.getNumBinsInRange());
35
36 // Is it clear initially?
37 for (int i = 0; i < kNumBins; i++) {
38 ASSERT_EQ(0, histogram.getCount(i));
39 }
40 ASSERT_EQ(0, histogram.getCountBelowRange());
41 ASSERT_EQ(0, histogram.getCountAboveRange());
42 ASSERT_EQ(0, histogram.getCount());
43
44 // Add some items.
45 histogram.add(27);
46 histogram.add(53);
47 histogram.add(171);
48 histogram.add(23);
49
50 // Did they count correctly.
51 ASSERT_EQ(2, histogram.getCount(2)); // For items 27 and 23
52 ASSERT_EQ(3, histogram.getLastItemNumber(2)); // Item 23 was the 0,1,2,3th item added.
53 ASSERT_EQ(1, histogram.getCount(5)); // For item 53
54 ASSERT_EQ(1, histogram.getLastItemNumber(5)); // item 53 was the second item added.
55 ASSERT_EQ(1, histogram.getCount(17)); // For item 171
56 ASSERT_EQ(4, histogram.getCount()); // A total of four items were added.
57
58 // Add values out of range.
59 histogram.add(-5);
60 ASSERT_EQ(1, histogram.getCountBelowRange()); // -5 is below zero.
61 ASSERT_EQ(0, histogram.getCountAboveRange());
62 ASSERT_EQ(5, histogram.getCount());
63
64 histogram.add(200);
65 ASSERT_EQ(1, histogram.getCountBelowRange());
66 ASSERT_EQ(1, histogram.getCountAboveRange()); // 200 is above top bin
67 ASSERT_EQ(6, histogram.getCount());
68
69 // Try to read values out of range. Should not crash.
70 // Legal index range is 0 to numBins-1
71 histogram.add(-1);
72 histogram.add(kNumBins);
73 ASSERT_EQ(0, histogram.getCount(-1)); // edge
74 ASSERT_EQ(0, histogram.getCount(kNumBins)); // edge
75 ASSERT_EQ(0, histogram.getCount(-1234)); // extreme
76 ASSERT_EQ(0, histogram.getCount(98765)); // extreme
77 ASSERT_EQ(0, histogram.getLastItemNumber(-1));
78 ASSERT_EQ(0, histogram.getLastItemNumber(kNumBins));
79
80 // Clear all the counts.
81 histogram.clear();
82 // Is it clear?
83 for (int i = 0; i < kNumBins; i++) {
84 ASSERT_EQ(0, histogram.getCount(i));
85 }
86 ASSERT_EQ(0, histogram.getCountBelowRange());
87 ASSERT_EQ(0, histogram.getCountAboveRange());
88 ASSERT_EQ(0, histogram.getCount());
89 }
90