• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2017, 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 /** Unit tests for {@link Duration}. */
28 @RunWith(JUnit4.class)
29 public class DurationTest {
30   @Rule public ExpectedException thrown = ExpectedException.none();
31 
32   @Test
testDurationCreate()33   public void testDurationCreate() {
34     assertThat(Duration.create(24, 42).getSeconds()).isEqualTo(24);
35     assertThat(Duration.create(24, 42).getNanos()).isEqualTo(42);
36     assertThat(Duration.create(-24, -42).getSeconds()).isEqualTo(-24);
37     assertThat(Duration.create(-24, -42).getNanos()).isEqualTo(-42);
38     assertThat(Duration.create(315576000000L, 999999999).getSeconds()).isEqualTo(315576000000L);
39     assertThat(Duration.create(315576000000L, 999999999).getNanos()).isEqualTo(999999999);
40     assertThat(Duration.create(-315576000000L, -999999999).getSeconds()).isEqualTo(-315576000000L);
41     assertThat(Duration.create(-315576000000L, -999999999).getNanos()).isEqualTo(-999999999);
42   }
43 
44   @Test
create_SecondsTooLow()45   public void create_SecondsTooLow() {
46     thrown.expect(IllegalArgumentException.class);
47     thrown.expectMessage("'seconds' is less than minimum (-315576000000): -315576000001");
48     Duration.create(-315576000001L, 0);
49   }
50 
51   @Test
create_SecondsTooHigh()52   public void create_SecondsTooHigh() {
53     thrown.expect(IllegalArgumentException.class);
54     thrown.expectMessage("'seconds' is greater than maximum (315576000000): 315576000001");
55     Duration.create(315576000001L, 0);
56   }
57 
58   @Test
create_NanosTooLow()59   public void create_NanosTooLow() {
60     thrown.expect(IllegalArgumentException.class);
61     thrown.expectMessage("'nanos' is less than minimum (-999999999): -1000000000");
62     Duration.create(0, -1000000000);
63   }
64 
65   @Test
create_NanosTooHigh()66   public void create_NanosTooHigh() {
67     thrown.expect(IllegalArgumentException.class);
68     thrown.expectMessage("'nanos' is greater than maximum (999999999): 1000000000");
69     Duration.create(0, 1000000000);
70   }
71 
72   @Test
create_NegativeSecondsPositiveNanos()73   public void create_NegativeSecondsPositiveNanos() {
74     thrown.expect(IllegalArgumentException.class);
75     thrown.expectMessage("'seconds' and 'nanos' have inconsistent sign: seconds=-1, nanos=1");
76     Duration.create(-1, 1);
77   }
78 
79   @Test
create_PositiveSecondsNegativeNanos()80   public void create_PositiveSecondsNegativeNanos() {
81     thrown.expect(IllegalArgumentException.class);
82     thrown.expectMessage("'seconds' and 'nanos' have inconsistent sign: seconds=1, nanos=-1");
83     Duration.create(1, -1);
84   }
85 
86   @Test
testDurationFromMillis()87   public void testDurationFromMillis() {
88     assertThat(Duration.fromMillis(0)).isEqualTo(Duration.create(0, 0));
89     assertThat(Duration.fromMillis(987)).isEqualTo(Duration.create(0, 987000000));
90     assertThat(Duration.fromMillis(3456)).isEqualTo(Duration.create(3, 456000000));
91   }
92 
93   @Test
testDurationFromMillisNegative()94   public void testDurationFromMillisNegative() {
95     assertThat(Duration.fromMillis(-1)).isEqualTo(Duration.create(0, -1000000));
96     assertThat(Duration.fromMillis(-999)).isEqualTo(Duration.create(0, -999000000));
97     assertThat(Duration.fromMillis(-1000)).isEqualTo(Duration.create(-1, 0));
98     assertThat(Duration.fromMillis(-3456)).isEqualTo(Duration.create(-3, -456000000));
99   }
100 
101   @Test
fromMillis_TooLow()102   public void fromMillis_TooLow() {
103     thrown.expect(IllegalArgumentException.class);
104     thrown.expectMessage("'seconds' is less than minimum (-315576000000): -315576000001");
105     Duration.fromMillis(-315576000001000L);
106   }
107 
108   @Test
fromMillis_TooHigh()109   public void fromMillis_TooHigh() {
110     thrown.expect(IllegalArgumentException.class);
111     thrown.expectMessage("'seconds' is greater than maximum (315576000000): 315576000001");
112     Duration.fromMillis(315576000001000L);
113   }
114 
115   @Test
duration_CompareLength()116   public void duration_CompareLength() {
117     assertThat(Duration.create(0, 0).compareTo(Duration.create(0, 0))).isEqualTo(0);
118     assertThat(Duration.create(24, 42).compareTo(Duration.create(24, 42))).isEqualTo(0);
119     assertThat(Duration.create(-24, -42).compareTo(Duration.create(-24, -42))).isEqualTo(0);
120     assertThat(Duration.create(25, 42).compareTo(Duration.create(24, 42))).isEqualTo(1);
121     assertThat(Duration.create(24, 45).compareTo(Duration.create(24, 42))).isEqualTo(1);
122     assertThat(Duration.create(24, 42).compareTo(Duration.create(25, 42))).isEqualTo(-1);
123     assertThat(Duration.create(24, 42).compareTo(Duration.create(24, 45))).isEqualTo(-1);
124     assertThat(Duration.create(-24, -45).compareTo(Duration.create(-24, -42))).isEqualTo(-1);
125     assertThat(Duration.create(-24, -42).compareTo(Duration.create(-25, -42))).isEqualTo(1);
126     assertThat(Duration.create(24, 42).compareTo(Duration.create(-24, -42))).isEqualTo(1);
127   }
128 
129   @Test
testDurationEqual()130   public void testDurationEqual() {
131     // Positive tests.
132     assertThat(Duration.create(0, 0)).isEqualTo(Duration.create(0, 0));
133     assertThat(Duration.create(24, 42)).isEqualTo(Duration.create(24, 42));
134     assertThat(Duration.create(-24, -42)).isEqualTo(Duration.create(-24, -42));
135     // Negative tests.
136     assertThat(Duration.create(25, 42)).isNotEqualTo(Duration.create(24, 42));
137     assertThat(Duration.create(24, 43)).isNotEqualTo(Duration.create(24, 42));
138     assertThat(Duration.create(-25, -42)).isNotEqualTo(Duration.create(-24, -42));
139     assertThat(Duration.create(-24, -43)).isNotEqualTo(Duration.create(-24, -42));
140   }
141 
142   @Test
toMillis()143   public void toMillis() {
144     assertThat(Duration.create(10, 0).toMillis()).isEqualTo(10000L);
145     assertThat(Duration.create(10, 1000).toMillis()).isEqualTo(10000L);
146     assertThat(Duration.create(0, (int) 1e6).toMillis()).isEqualTo(1L);
147     assertThat(Duration.create(0, 0).toMillis()).isEqualTo(0L);
148     assertThat(Duration.create(-10, 0).toMillis()).isEqualTo(-10000L);
149     assertThat(Duration.create(-10, -1000).toMillis()).isEqualTo(-10000L);
150     assertThat(Duration.create(0, -(int) 1e6).toMillis()).isEqualTo(-1L);
151   }
152 }
153