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