• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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 package com.android.modules.expresslog;
17 
18 import androidx.test.filters.SmallTest;
19 
20 import static org.junit.Assert.assertEquals;
21 
22 import org.junit.Test;
23 import org.junit.runner.RunWith;
24 import org.junit.runners.JUnit4;
25 
26 @RunWith(JUnit4.class)
27 @SmallTest
28 public class UniformOptionsTest {
29     private static final String TAG = UniformOptionsTest.class.getSimpleName();
30 
31     @Test
testGetBinsCount()32     public void testGetBinsCount() {
33         Histogram.UniformOptions options1 = new Histogram.UniformOptions(1, 100, 1000);
34         assertEquals(3, options1.getBinsCount());
35 
36         Histogram.UniformOptions options10 = new Histogram.UniformOptions(10, 100, 1000);
37         assertEquals(12, options10.getBinsCount());
38     }
39 
40     @Test(expected = IllegalArgumentException.class)
testConstructZeroBinsCount()41     public void testConstructZeroBinsCount() {
42         new Histogram.UniformOptions(0, 100, 1000);
43     }
44 
45     @Test(expected = IllegalArgumentException.class)
testConstructNegativeBinsCount()46     public void testConstructNegativeBinsCount() {
47         new Histogram.UniformOptions(-1, 100, 1000);
48     }
49 
50     @Test(expected = IllegalArgumentException.class)
testConstructMaxValueLessThanMinValue()51     public void testConstructMaxValueLessThanMinValue() {
52         new Histogram.UniformOptions(10, 1000, 100);
53     }
54 
55     @Test
testBinIndexForRangeEqual1()56     public void testBinIndexForRangeEqual1() {
57         Histogram.UniformOptions options = new Histogram.UniformOptions(10, 1, 11);
58         for (int i = 0, bins = options.getBinsCount(); i < bins; i++) {
59             assertEquals(i, options.getBinForSample(i));
60         }
61     }
62 
63     @Test
testBinIndexForRangeEqual2()64     public void testBinIndexForRangeEqual2() {
65         Histogram.UniformOptions options = new Histogram.UniformOptions(10, 1, 21);
66         for (int i = 0, bins = options.getBinsCount(); i < bins; i++) {
67             assertEquals(i, options.getBinForSample(i * 2));
68             assertEquals(i, options.getBinForSample(i * 2 - 1));
69         }
70     }
71 
72     @Test
testBinIndexForRangeEqual5()73     public void testBinIndexForRangeEqual5() {
74         Histogram.UniformOptions options = new Histogram.UniformOptions(2, 0, 10);
75         assertEquals(4, options.getBinsCount());
76         for (int i = 0; i < 2; i++) {
77             for (int sample = 0; sample < 5; sample++) {
78                 assertEquals(i + 1, options.getBinForSample(i * 5 + sample));
79             }
80         }
81     }
82 
83     @Test
testBinIndexForRangeEqual10()84     public void testBinIndexForRangeEqual10() {
85         Histogram.UniformOptions options = new Histogram.UniformOptions(10, 1, 101);
86         assertEquals(0, options.getBinForSample(0));
87         assertEquals(options.getBinsCount() - 2, options.getBinForSample(100));
88         assertEquals(options.getBinsCount() - 1, options.getBinForSample(101));
89 
90         final float binSize = (101 - 1) / 10f;
91         for (int i = 1, bins = options.getBinsCount() - 1; i < bins; i++) {
92             assertEquals(i, options.getBinForSample(i * binSize));
93         }
94     }
95 
96     @Test
testBinIndexForRangeEqual90()97     public void testBinIndexForRangeEqual90() {
98         final int binCount = 10;
99         final int minValue = 100;
100         final int maxValue = 100000;
101 
102         Histogram.UniformOptions options = new Histogram.UniformOptions(binCount, minValue,
103                 maxValue);
104 
105         // logging underflow sample
106         assertEquals(0, options.getBinForSample(minValue - 1));
107 
108         // logging overflow sample
109         assertEquals(binCount + 1, options.getBinForSample(maxValue));
110         assertEquals(binCount + 1, options.getBinForSample(maxValue + 1));
111 
112         // logging min edge sample
113         assertEquals(1, options.getBinForSample(minValue));
114 
115         // logging max edge sample
116         assertEquals(binCount, options.getBinForSample(maxValue - 1));
117 
118         // logging single valid sample per bin
119         final int binSize = (maxValue - minValue) / binCount;
120 
121         for (int i = 0; i < binCount; i++) {
122             assertEquals(i + 1, options.getBinForSample(minValue + binSize * i));
123         }
124     }
125 }
126