• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 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.tradefed.util;
18 
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertTrue;
21 import static org.junit.Assert.fail;
22 import org.junit.Test;
23 import org.junit.runner.RunWith;
24 import org.junit.runners.JUnit4;
25 
26 /** Unit tests for {@link TimeVal}. */
27 @RunWith(JUnit4.class)
28 public class TimeValTest {
29 
30     @Test
testBasic()31     public void testBasic() {
32         assertEquals(0, TimeVal.fromString("0"));
33         assertEquals(1, TimeVal.fromString("1"));
34 
35         assertEquals(1000, TimeVal.fromString("1000"));
36         assertEquals(1000, TimeVal.fromString("1000ms"));
37         assertEquals(1000, TimeVal.fromString("1000Ms"));
38         assertEquals(1000, TimeVal.fromString("1000mS"));
39         assertEquals(1000, TimeVal.fromString("1000MS"));
40 
41         assertEquals(1000, TimeVal.fromString("1s"));
42         assertEquals(1000, TimeVal.fromString("1S"));
43 
44         assertEquals(1000 * 60, TimeVal.fromString("1m"));
45         assertEquals(1000 * 60, TimeVal.fromString("1M"));
46 
47         assertEquals(1000 * 3600, TimeVal.fromString("1h"));
48         assertEquals(1000 * 3600, TimeVal.fromString("1H"));
49 
50         assertEquals(1000 * 86400, TimeVal.fromString("1d"));
51         assertEquals(1000 * 86400, TimeVal.fromString("1D"));
52     }
53 
54     @Test
testComposition()55     public void testComposition() {
56         assertEquals(1303, TimeVal.fromString("1s303"));
57         assertEquals(1000 * 60 + 303, TimeVal.fromString("1m303"));
58         assertEquals(1000 * 3600 + 303, TimeVal.fromString("1h303ms"));
59         assertEquals(1000 * 86400 + 303, TimeVal.fromString("1d303MS"));
60 
61         assertEquals(4 * 1000 * 86400 + 5 * 1000 * 3600 + 303, TimeVal.fromString("4D5h303mS"));
62         assertEquals(5 + 1000 * (4 + 60 * (3 + 60 * (2 + 24 * 1))),
63                 TimeVal.fromString("1d2h3m4s5ms"));
64     }
65 
66     @Test
testWhitespace()67     public void testWhitespace() {
68         assertEquals(1, TimeVal.fromString("1 ms"));
69         assertEquals(1, TimeVal.fromString("  1  ms  "));
70 
71         assertEquals(1002, TimeVal.fromString("1s2"));
72         assertEquals(1002, TimeVal.fromString("1s2 ms"));
73         assertEquals(1002, TimeVal.fromString(" 1s2ms"));
74         assertEquals(1002, TimeVal.fromString("1s 2 ms"));
75         // This is non-ideal, but is a side-effect of discarding all whitespace prior to parsing
76         assertEquals(3 * 1000 * 60 + 1002, TimeVal.fromString(" 3 m 1 s 2 m s"));
77 
78         assertEquals(5 + 1000 * (4 + 60 * (3 + 60 * (2 + 24 * 1))),
79                 TimeVal.fromString("1d 2h 3m 4s 5ms"));
80     }
81 
82     @Test
testInvalid()83     public void testInvalid() {
84         assertInvalid("-1");
85         assertInvalid("1m1h");
86         assertInvalid("+5");
87     }
88 
89     /**
90      * Because of TimeVal's multiplication features, it can be easier for a user to unexpectedly
91      * trigger an overflow without realizing that their value has become quite so large. Here, we
92      * verify that various kinds of overflows and ensure that we detect them.
93      */
94     @Test
testOverflow()95     public void testOverflow() {
96         // 2**63 - 1
97         assertEquals(Long.MAX_VALUE, TimeVal.fromString("9223372036854775807"));
98         // 2**63
99         assertInvalid("9223372036854775808");
100 
101         // (2**63 - 1).divmod(1000 * 86400) = [106751991167, 25975807] (days, msecs)
102         // This should be the greatest number of days that does not overflow
103         assertEquals(106751991167L * 1000L * 86400L, TimeVal.fromString("106751991167d"));
104         // This should be 2**63 - 1
105         assertEquals(Long.MAX_VALUE, TimeVal.fromString("106751991167d 25975807ms"));
106         // Adding 1 more ms should cause an overflow.
107         assertInvalid("106751991167d 25975808ms");
108 
109         // 2**64 + 1 should be a positive value after an overflow.  Make sure we can still detect
110         // this non-negative overflow
111         long l = 1L<<62;
112         l *= 2;
113         l *= 2;
114         l += 1;
115         assertTrue(l > 0);
116         // 2**64 + 1 == 18446744073709551617
117         assertInvalid("18446744073709551617ms");
118     }
119 
assertInvalid(String input)120     private void assertInvalid(String input) {
121         try {
122             final long val = TimeVal.fromString(input);
123             fail(String.format("Did not reject input: %s.  Produced value: %d", input, val));
124         } catch (NumberFormatException e) {
125             // expected
126         }
127     }
128 }
129