1 /*
2  * Copyright (C) 2021 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 package com.android.net.module.util
18 
19 import androidx.test.filters.SmallTest
20 import androidx.test.runner.AndroidJUnit4
21 import kotlin.test.assertEquals
22 import kotlin.test.assertFailsWith
23 import org.junit.Test
24 import org.junit.runner.RunWith
25 
26 @RunWith(AndroidJUnit4::class)
27 @SmallTest
28 class NetworkStatsUtilsTest {
29     @Test
testMultiplySafeByRationalnull30     fun testMultiplySafeByRational() {
31         // Verify basic cases that the method equals to a * b / c.
32         assertEquals(3 * 5 / 2, NetworkStatsUtils.multiplySafeByRational(3, 5, 2))
33 
34         // Verify input with zeros.
35         assertEquals(0 * 7 / 3, NetworkStatsUtils.multiplySafeByRational(0, 7, 3))
36         assertEquals(7 * 0 / 3, NetworkStatsUtils.multiplySafeByRational(7, 0, 3))
37         assertEquals(0 * 0 / 1, NetworkStatsUtils.multiplySafeByRational(0, 0, 1))
38         assertEquals(0, NetworkStatsUtils.multiplySafeByRational(0, Long.MAX_VALUE, Long.MAX_VALUE))
39         assertEquals(0, NetworkStatsUtils.multiplySafeByRational(Long.MAX_VALUE, 0, Long.MAX_VALUE))
40         assertFailsWith<ArithmeticException> {
41             NetworkStatsUtils.multiplySafeByRational(7, 3, 0)
42         }
43         assertFailsWith<ArithmeticException> {
44             NetworkStatsUtils.multiplySafeByRational(0, 0, 0)
45         }
46 
47         // Verify cases where a * b overflows.
48         assertEquals(101, NetworkStatsUtils.multiplySafeByRational(
49                 101, Long.MAX_VALUE, Long.MAX_VALUE))
50         assertEquals(721, NetworkStatsUtils.multiplySafeByRational(
51                 Long.MAX_VALUE, 721, Long.MAX_VALUE))
52         assertEquals(Long.MAX_VALUE, NetworkStatsUtils.multiplySafeByRational(
53                 Long.MAX_VALUE, Long.MAX_VALUE, Long.MAX_VALUE))
54         assertFailsWith<ArithmeticException> {
55             NetworkStatsUtils.multiplySafeByRational(Long.MAX_VALUE, Long.MAX_VALUE, 0)
56         }
57     }
58 
59     @Test
testConstrainnull60     fun testConstrain() {
61         assertFailsWith<IllegalArgumentException> {
62             NetworkStatsUtils.constrain(5, 6, 3) // low > high
63         }
64         assertEquals(3, NetworkStatsUtils.constrain(5, 1, 3))
65         assertEquals(3, NetworkStatsUtils.constrain(3, 1, 3))
66         assertEquals(2, NetworkStatsUtils.constrain(2, 1, 3))
67         assertEquals(1, NetworkStatsUtils.constrain(1, 1, 3))
68         assertEquals(1, NetworkStatsUtils.constrain(0, 1, 3))
69 
70         assertEquals(11, NetworkStatsUtils.constrain(15, 11, 11))
71         assertEquals(11, NetworkStatsUtils.constrain(11, 11, 11))
72         assertEquals(11, NetworkStatsUtils.constrain(1, 11, 11))
73     }
74 }