1 /* 2 * Copyright 2018, OpenCensus Authors 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 io.opencensus.common; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import org.junit.Rule; 22 import org.junit.Test; 23 import org.junit.rules.ExpectedException; 24 import org.junit.runner.RunWith; 25 import org.junit.runners.JUnit4; 26 27 /** Tests for {@link TimeUtils}. */ 28 @RunWith(JUnit4.class) 29 public final class TimeUtilsTest { 30 31 @Rule public ExpectedException thrown = ExpectedException.none(); 32 33 @Test compareLongs()34 public void compareLongs() { 35 assertThat(TimeUtils.compareLongs(-1L, 1L)).isLessThan(0); 36 assertThat(TimeUtils.compareLongs(10L, 10L)).isEqualTo(0); 37 assertThat(TimeUtils.compareLongs(1L, 0L)).isGreaterThan(0); 38 } 39 40 @Test checkedAdd_TooLow()41 public void checkedAdd_TooLow() { 42 thrown.expect(ArithmeticException.class); 43 thrown.expectMessage("Long sum overflow: x=-9223372036854775807, y=-2"); 44 TimeUtils.checkedAdd(Long.MIN_VALUE + 1, -2); 45 } 46 47 @Test checkedAdd_TooHigh()48 public void checkedAdd_TooHigh() { 49 thrown.expect(ArithmeticException.class); 50 thrown.expectMessage("Long sum overflow: x=9223372036854775806, y=2"); 51 TimeUtils.checkedAdd(Long.MAX_VALUE - 1, 2); 52 } 53 54 @Test checkedAdd_Valid()55 public void checkedAdd_Valid() { 56 assertThat(TimeUtils.checkedAdd(1, 2)).isEqualTo(3); 57 assertThat(TimeUtils.checkedAdd(Integer.MAX_VALUE, Integer.MAX_VALUE)) 58 .isEqualTo(2L * Integer.MAX_VALUE); 59 } 60 } 61