• 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 Timestamp}. */
28 @RunWith(JUnit4.class)
29 public class TimestampTest {
30   @Rule public ExpectedException thrown = ExpectedException.none();
31 
32   @Test
timestampCreate()33   public void timestampCreate() {
34     assertThat(Timestamp.create(24, 42).getSeconds()).isEqualTo(24);
35     assertThat(Timestamp.create(24, 42).getNanos()).isEqualTo(42);
36     assertThat(Timestamp.create(-24, 42).getSeconds()).isEqualTo(-24);
37     assertThat(Timestamp.create(-24, 42).getNanos()).isEqualTo(42);
38     assertThat(Timestamp.create(315576000000L, 999999999).getSeconds()).isEqualTo(315576000000L);
39     assertThat(Timestamp.create(315576000000L, 999999999).getNanos()).isEqualTo(999999999);
40     assertThat(Timestamp.create(-315576000000L, 999999999).getSeconds()).isEqualTo(-315576000000L);
41     assertThat(Timestamp.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     Timestamp.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     Timestamp.create(315576000001L, 0);
56   }
57 
58   @Test
create_NanosTooLow_PositiveTime()59   public void create_NanosTooLow_PositiveTime() {
60     thrown.expect(IllegalArgumentException.class);
61     thrown.expectMessage("'nanos' is less than zero: -1");
62     Timestamp.create(1, -1);
63   }
64 
65   @Test
create_NanosTooHigh_PositiveTime()66   public void create_NanosTooHigh_PositiveTime() {
67     thrown.expect(IllegalArgumentException.class);
68     thrown.expectMessage("'nanos' is greater than maximum (999999999): 1000000000");
69     Timestamp.create(1, 1000000000);
70   }
71 
72   @Test
create_NanosTooLow_NegativeTime()73   public void create_NanosTooLow_NegativeTime() {
74     thrown.expect(IllegalArgumentException.class);
75     thrown.expectMessage("'nanos' is less than zero: -1");
76     Timestamp.create(-1, -1);
77   }
78 
79   @Test
create_NanosTooHigh_NegativeTime()80   public void create_NanosTooHigh_NegativeTime() {
81     thrown.expect(IllegalArgumentException.class);
82     thrown.expectMessage("'nanos' is greater than maximum (999999999): 1000000000");
83     Timestamp.create(-1, 1000000000);
84   }
85 
86   @Test
timestampFromMillis()87   public void timestampFromMillis() {
88     assertThat(Timestamp.fromMillis(0)).isEqualTo(Timestamp.create(0, 0));
89     assertThat(Timestamp.fromMillis(987)).isEqualTo(Timestamp.create(0, 987000000));
90     assertThat(Timestamp.fromMillis(3456)).isEqualTo(Timestamp.create(3, 456000000));
91   }
92 
93   @Test
timestampFromMillis_Negative()94   public void timestampFromMillis_Negative() {
95     assertThat(Timestamp.fromMillis(-1)).isEqualTo(Timestamp.create(-1, 999000000));
96     assertThat(Timestamp.fromMillis(-999)).isEqualTo(Timestamp.create(-1, 1000000));
97     assertThat(Timestamp.fromMillis(-3456)).isEqualTo(Timestamp.create(-4, 544000000));
98   }
99 
100   @Test
fromMillis_TooLow()101   public void fromMillis_TooLow() {
102     thrown.expect(IllegalArgumentException.class);
103     thrown.expectMessage("'seconds' is less than minimum (-315576000000): -315576000001");
104     Timestamp.fromMillis(-315576000001000L);
105   }
106 
107   @Test
fromMillis_TooHigh()108   public void fromMillis_TooHigh() {
109     thrown.expect(IllegalArgumentException.class);
110     thrown.expectMessage("'seconds' is greater than maximum (315576000000): 315576000001");
111     Timestamp.fromMillis(315576000001000L);
112   }
113 
114   @Test
timestampAddNanos()115   public void timestampAddNanos() {
116     Timestamp timestamp = Timestamp.create(1234, 223);
117     assertThat(timestamp.addNanos(0)).isEqualTo(timestamp);
118     assertThat(timestamp.addNanos(999999777)).isEqualTo(Timestamp.create(1235, 0));
119     assertThat(timestamp.addNanos(1300200500)).isEqualTo(Timestamp.create(1235, 300200723));
120     assertThat(timestamp.addNanos(1999999777)).isEqualTo(Timestamp.create(1236, 0));
121     assertThat(timestamp.addNanos(9876543789L)).isEqualTo(Timestamp.create(1243, 876544012));
122     assertThat(timestamp.addNanos(Long.MAX_VALUE))
123         .isEqualTo(Timestamp.create(1234L + 9223372036L, 223 + 854775807));
124   }
125 
126   @Test
timestampAddNanos_Negative()127   public void timestampAddNanos_Negative() {
128     Timestamp timestamp = Timestamp.create(1234, 223);
129     assertThat(timestamp.addNanos(-223)).isEqualTo(Timestamp.create(1234, 0));
130     assertThat(timestamp.addNanos(-1000000223)).isEqualTo(Timestamp.create(1233, 0));
131     assertThat(timestamp.addNanos(-1300200500)).isEqualTo(Timestamp.create(1232, 699799723));
132     assertThat(timestamp.addNanos(-4123456213L)).isEqualTo(Timestamp.create(1229, 876544010));
133     assertThat(timestamp.addNanos(Long.MIN_VALUE))
134         .isEqualTo(Timestamp.create(1234L - 9223372036L - 1, 223 + 145224192));
135   }
136 
137   @Test
timestampAddDuration()138   public void timestampAddDuration() {
139     Timestamp timestamp = Timestamp.create(1234, 223);
140     assertThat(timestamp.addDuration(Duration.create(1, 0))).isEqualTo(Timestamp.create(1235, 223));
141     assertThat(timestamp.addDuration(Duration.create(0, 1))).isEqualTo(Timestamp.create(1234, 224));
142     assertThat(timestamp.addDuration(Duration.create(1, 1))).isEqualTo(Timestamp.create(1235, 224));
143     assertThat(timestamp.addDuration(Duration.create(1, 999999900)))
144         .isEqualTo(Timestamp.create(1236, 123));
145   }
146 
147   @Test
timestampAddDuration_Negative()148   public void timestampAddDuration_Negative() {
149     Timestamp timestamp = Timestamp.create(1234, 223);
150     assertThat(timestamp.addDuration(Duration.create(-1234, -223)))
151         .isEqualTo(Timestamp.create(0, 0));
152     assertThat(timestamp.addDuration(Duration.create(-1, 0)))
153         .isEqualTo(Timestamp.create(1233, 223));
154     assertThat(timestamp.addDuration(Duration.create(-1, -1)))
155         .isEqualTo(Timestamp.create(1233, 222));
156     assertThat(timestamp.addDuration(Duration.create(-1, -323)))
157         .isEqualTo(Timestamp.create(1232, 999999900));
158     assertThat(timestamp.addDuration(Duration.create(-33, -999999999)))
159         .isEqualTo(Timestamp.create(1200, 224));
160   }
161 
162   @Test
timestampSubtractTimestamp()163   public void timestampSubtractTimestamp() {
164     Timestamp timestamp = Timestamp.create(1234, 223);
165     assertThat(timestamp.subtractTimestamp(Timestamp.create(0, 0)))
166         .isEqualTo(Duration.create(1234, 223));
167     assertThat(timestamp.subtractTimestamp(Timestamp.create(1233, 223)))
168         .isEqualTo(Duration.create(1, 0));
169     assertThat(timestamp.subtractTimestamp(Timestamp.create(1233, 222)))
170         .isEqualTo(Duration.create(1, 1));
171     assertThat(timestamp.subtractTimestamp(Timestamp.create(1232, 999999900)))
172         .isEqualTo(Duration.create(1, 323));
173     assertThat(timestamp.subtractTimestamp(Timestamp.create(1200, 224)))
174         .isEqualTo(Duration.create(33, 999999999));
175   }
176 
177   @Test
timestampSubtractTimestamp_NegativeResult()178   public void timestampSubtractTimestamp_NegativeResult() {
179     Timestamp timestamp = Timestamp.create(1234, 223);
180     assertThat(timestamp.subtractTimestamp(Timestamp.create(1235, 223)))
181         .isEqualTo(Duration.create(-1, 0));
182     assertThat(timestamp.subtractTimestamp(Timestamp.create(1234, 224)))
183         .isEqualTo(Duration.create(0, -1));
184     assertThat(timestamp.subtractTimestamp(Timestamp.create(1235, 224)))
185         .isEqualTo(Duration.create(-1, -1));
186     assertThat(timestamp.subtractTimestamp(Timestamp.create(1236, 123)))
187         .isEqualTo(Duration.create(-1, -999999900));
188   }
189 
190   @Test
timestamp_CompareTo()191   public void timestamp_CompareTo() {
192     assertThat(Timestamp.create(0, 0).compareTo(Timestamp.create(0, 0))).isEqualTo(0);
193     assertThat(Timestamp.create(24, 42).compareTo(Timestamp.create(24, 42))).isEqualTo(0);
194     assertThat(Timestamp.create(-24, 42).compareTo(Timestamp.create(-24, 42))).isEqualTo(0);
195     assertThat(Timestamp.create(25, 42).compareTo(Timestamp.create(24, 42))).isEqualTo(1);
196     assertThat(Timestamp.create(24, 45).compareTo(Timestamp.create(24, 42))).isEqualTo(1);
197     assertThat(Timestamp.create(24, 42).compareTo(Timestamp.create(25, 42))).isEqualTo(-1);
198     assertThat(Timestamp.create(24, 42).compareTo(Timestamp.create(24, 45))).isEqualTo(-1);
199     assertThat(Timestamp.create(-25, 42).compareTo(Timestamp.create(-24, 42))).isEqualTo(-1);
200     assertThat(Timestamp.create(-24, 45).compareTo(Timestamp.create(-24, 42))).isEqualTo(1);
201     assertThat(Timestamp.create(-24, 42).compareTo(Timestamp.create(-25, 42))).isEqualTo(1);
202     assertThat(Timestamp.create(-24, 42).compareTo(Timestamp.create(-24, 45))).isEqualTo(-1);
203   }
204 
205   @Test
timestamp_Equal()206   public void timestamp_Equal() {
207     // Positive tests.
208     assertThat(Timestamp.create(0, 0)).isEqualTo(Timestamp.create(0, 0));
209     assertThat(Timestamp.create(24, 42)).isEqualTo(Timestamp.create(24, 42));
210     assertThat(Timestamp.create(-24, 42)).isEqualTo(Timestamp.create(-24, 42));
211     // Negative tests.
212     assertThat(Timestamp.create(25, 42)).isNotEqualTo(Timestamp.create(24, 42));
213     assertThat(Timestamp.create(24, 43)).isNotEqualTo(Timestamp.create(24, 42));
214     assertThat(Timestamp.create(-25, 42)).isNotEqualTo(Timestamp.create(-24, 42));
215     assertThat(Timestamp.create(-24, 43)).isNotEqualTo(Timestamp.create(-24, 42));
216   }
217 }
218