aboutsummaryrefslogtreecommitdiff
path: root/javatests/com/android/modules/expresslog/ScaledRangeOptionsTest.java
blob: 8defce798ac75a42fdc6490353253fd1fee9b23e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
 * Copyright (C) 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.modules.expresslog;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import androidx.test.filters.SmallTest;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
@SmallTest
public class ScaledRangeOptionsTest {
    static {
        System.loadLibrary("expresslog_test_jni");
    }

    private static final String TAG = ScaledRangeOptionsTest.class.getSimpleName();

    @Test
    public void testGetBinsCount() {
        Histogram.ScaledRangeOptions options1 = new Histogram.ScaledRangeOptions(1, 100, 100, 2);
        assertEquals(3, options1.getBinsCount());

        Histogram.ScaledRangeOptions options10 = new Histogram.ScaledRangeOptions(10, 100, 100, 2);
        assertEquals(12, options10.getBinsCount());
    }

    @Test(expected = IllegalArgumentException.class)
    public void testConstructZeroBinsCount() {
        new Histogram.ScaledRangeOptions(0, 100, 100, 2);
    }

    @Test(expected = IllegalArgumentException.class)
    public void testConstructNegativeBinsCount() {
        new Histogram.ScaledRangeOptions(-1, 100, 100, 2);
    }

    @Test(expected = IllegalArgumentException.class)
    public void testConstructNegativeFirstBinWidth() {
        new Histogram.ScaledRangeOptions(10, 100, -100, 2);
    }

    @Test(expected = IllegalArgumentException.class)
    public void testConstructTooSmallFirstBinWidth() {
        new Histogram.ScaledRangeOptions(10, 100, 0.5f, 2);
    }

    @Test(expected = IllegalArgumentException.class)
    public void testConstructNegativeScaleFactor() {
        new Histogram.ScaledRangeOptions(10, 100, 100, -2);
    }

    @Test(expected = IllegalArgumentException.class)
    public void testConstructTooSmallScaleFactor() {
        new Histogram.ScaledRangeOptions(10, 100, 100, 0.5f);
    }

    @Test(expected = IllegalArgumentException.class)
    public void testConstructTooBigScaleFactor() {
        new Histogram.ScaledRangeOptions(10, 100, 100, 500.f);
    }

    @Test(expected = IllegalArgumentException.class)
    public void testConstructTooBigBinRange() {
        new Histogram.ScaledRangeOptions(100, 100, 100, 10.f);
    }

    @Test
    public void testBinIndexForRangeEqual1() {
        Histogram.ScaledRangeOptions options = new Histogram.ScaledRangeOptions(10, 1, 1, 1);
        assertEquals(12, options.getBinsCount());

        assertEquals(11, options.getBinForSample(11));

        for (int i = 0, bins = options.getBinsCount(); i < bins; i++) {
            assertEquals(i, options.getBinForSample(i));
        }
    }

    @Test
    public void testBinIndexForRangeEqual2() {
        // this should produce bin otpions similar to linear histogram with bin width 2
        Histogram.ScaledRangeOptions options = new Histogram.ScaledRangeOptions(10, 1, 2, 1);
        assertEquals(12, options.getBinsCount());

        for (int i = 0, bins = options.getBinsCount(); i < bins; i++) {
            assertEquals(i, options.getBinForSample(i * 2));
            assertEquals(i, options.getBinForSample(i * 2 - 1));
        }
    }

    @Test
    public void testBinIndexForRangeEqual5() {
        Histogram.ScaledRangeOptions options = new Histogram.ScaledRangeOptions(2, 0, 5, 1);
        assertEquals(4, options.getBinsCount());
        for (int i = 0; i < 2; i++) {
            for (int sample = 0; sample < 5; sample++) {
                assertEquals(i + 1, options.getBinForSample(i * 5 + sample));
            }
        }
    }

    @Test
    public void testBinIndexForRangeEqual10() {
        Histogram.ScaledRangeOptions options = new Histogram.ScaledRangeOptions(10, 1, 10, 1);
        assertEquals(0, options.getBinForSample(0));
        assertEquals(options.getBinsCount() - 2, options.getBinForSample(100));
        assertEquals(options.getBinsCount() - 1, options.getBinForSample(101));

        final float binSize = (101 - 1) / 10f;
        for (int i = 1, bins = options.getBinsCount() - 1; i < bins; i++) {
            assertEquals(i, options.getBinForSample(i * binSize));
        }
    }

    @Test
    public void testBinIndexForScaleFactor2() {
        final int binsCount = 10;
        final int minValue = 10;
        final int firstBinWidth = 5;
        final int scaledFactor = 2;

        Histogram.ScaledRangeOptions options = new Histogram.ScaledRangeOptions(
                binsCount, minValue, firstBinWidth, scaledFactor);
        assertEquals(binsCount + 2, options.getBinsCount());
        long[] binCounts = new long[10];

        // precalculate max valid value - start value for the overflow bin
        int lastBinStartValue = minValue; //firstBinMin value
        int lastBinWidth = firstBinWidth;
        for (int binIdx = 2; binIdx <= binsCount + 1; binIdx++) {
            lastBinStartValue = lastBinStartValue + lastBinWidth;
            lastBinWidth *= scaledFactor;
        }

        // underflow bin
        for (int i = 1; i < minValue; i++) {
            assertEquals(0, options.getBinForSample(i));
        }

        for (int i = 10; i < lastBinStartValue; i++) {
            assertTrue(options.getBinForSample(i) > 0);
            assertTrue(options.getBinForSample(i) <= binsCount);
            binCounts[options.getBinForSample(i) - 1]++;
        }

        // overflow bin
        assertEquals(binsCount + 1, options.getBinForSample(lastBinStartValue));

        for (int i = 1; i < binsCount; i++) {
            assertEquals(binCounts[i], binCounts[i - 1] * 2L);
        }
    }
}