• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 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 package libcore.android.system;
18 
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertFalse;
21 
22 import android.system.StructTimeval;
23 
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 
27 import junitparams.JUnitParamsRunner;
28 import junitparams.Parameters;
29 
30 
31 /**
32  * Unit test for {@link StructTimeval}
33  */
34 @RunWith(JUnitParamsRunner.class)
35 public class StructTimevalTest {
36 
37     private static final long MS_PER_SEC = 1000L;
38     private static final long US_PER_MS = 1000L;
39     private static final long US_PER_SEC = US_PER_MS * MS_PER_SEC;
40 
interestingMillisValues()41     public static Object[][] interestingMillisValues() {
42         // An array of { testMillisValue, expectedSeconds, expectedMicros }
43         return new Object[][] {
44                 { 0L, 0L, 0L },
45                 { 1000L, 1L, 0L },
46                 { -1000L, -1L, 0L },
47 
48                 // +ve and -ve cases close to zero seconds.
49                 { 23L, 0L, 23L * US_PER_MS /* 23000 */ },
50                 { -23L, -1L, US_PER_SEC - (23L * US_PER_MS) /* 977000 */ },
51 
52                 // +ve and -ve cases with non-zero seconds.
53                 { 2003L, 2L, 3L * US_PER_MS /* 3000 */ },
54                 { -2003L, -3L, US_PER_SEC - (3L * US_PER_MS) /* 997000 */ },
55 
56                 // Check for overflow.
57                 { Long.MAX_VALUE, /* 9223372036854775807 */
58                         Long.MAX_VALUE / MS_PER_SEC, /* 9223372036854775 */
59                         (Long.MAX_VALUE % MS_PER_SEC) * US_PER_MS, /* 807000 */
60                 },
61 
62                 // Check for underflow. [Note: In Java (-ve % +ve) generates a -ve result]
63                 { Long.MIN_VALUE, /* -9223372036854775808 */
64                         (Long.MIN_VALUE / MS_PER_SEC) - 1L, /* -9223372036854776 */
65                         US_PER_SEC - (-(Long.MIN_VALUE % MS_PER_SEC) * US_PER_MS), /* 192000 */
66                 },
67         };
68     }
69 
70     @Parameters(method = "interestingMillisValues")
71     @Test
fromToMillis(long millisValue, long expectedSeconds, long expectedMicros)72     public void fromToMillis(long millisValue, long expectedSeconds, long expectedMicros) {
73         StructTimeval val = StructTimeval.fromMillis(millisValue);
74 
75         assertEquals(expectedSeconds, val.tv_sec);
76         assertEquals(expectedMicros, val.tv_usec);
77 
78         assertEquals(millisValue, val.toMillis());
79     }
80 
81     @Test
testEqualsAndHashcode()82     public void testEqualsAndHashcode() {
83         Object[][] millisValues = interestingMillisValues();
84         StructTimeval[] timeVals = new StructTimeval[millisValues.length];
85 
86         for (int i = 0; i < millisValues.length; i++) {
87             long millisValue = (long) millisValues[i][0];
88             StructTimeval value1 = StructTimeval.fromMillis(millisValue);
89             StructTimeval value2 = StructTimeval.fromMillis(millisValue);
90 
91             assertEquals("value1.equals(value1)", value1, value1);
92             assertEquals("value1.equals(value2)", value1, value2);
93             assertEquals("value2.equals(value1)", value2, value1);
94 
95             assertEquals("value1.hashCode() == value2.hashCode()",
96                     value1.hashCode(), value2.hashCode());
97 
98             timeVals[i] = value1;
99         }
100 
101         for (int i = 0; i < millisValues.length; i++) {
102             StructTimeval iVal = timeVals[i];
103             for (int j = i + 1; j < millisValues.length; j++) {
104                 StructTimeval jVal = timeVals[j];
105                 assertFalse(iVal.equals(jVal));
106                 assertFalse(jVal.equals(iVal));
107             }
108         }
109     }
110 }
111