aboutsummaryrefslogtreecommitdiff
path: root/api/src/test/java/io/opencensus/common/DurationTest.java
blob: ea636ca0b58b6e29951a9acf6066892fd3533c43 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
 * Copyright 2017, OpenCensus Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.opencensus.common;

import static com.google.common.truth.Truth.assertThat;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/** Unit tests for {@link Duration}. */
@RunWith(JUnit4.class)
public class DurationTest {
  @Rule public ExpectedException thrown = ExpectedException.none();

  @Test
  public void testDurationCreate() {
    assertThat(Duration.create(24, 42).getSeconds()).isEqualTo(24);
    assertThat(Duration.create(24, 42).getNanos()).isEqualTo(42);
    assertThat(Duration.create(-24, -42).getSeconds()).isEqualTo(-24);
    assertThat(Duration.create(-24, -42).getNanos()).isEqualTo(-42);
    assertThat(Duration.create(315576000000L, 999999999).getSeconds()).isEqualTo(315576000000L);
    assertThat(Duration.create(315576000000L, 999999999).getNanos()).isEqualTo(999999999);
    assertThat(Duration.create(-315576000000L, -999999999).getSeconds()).isEqualTo(-315576000000L);
    assertThat(Duration.create(-315576000000L, -999999999).getNanos()).isEqualTo(-999999999);
  }

  @Test
  public void create_SecondsTooLow() {
    thrown.expect(IllegalArgumentException.class);
    thrown.expectMessage("'seconds' is less than minimum (-315576000000): -315576000001");
    Duration.create(-315576000001L, 0);
  }

  @Test
  public void create_SecondsTooHigh() {
    thrown.expect(IllegalArgumentException.class);
    thrown.expectMessage("'seconds' is greater than maximum (315576000000): 315576000001");
    Duration.create(315576000001L, 0);
  }

  @Test
  public void create_NanosTooLow() {
    thrown.expect(IllegalArgumentException.class);
    thrown.expectMessage("'nanos' is less than minimum (-999999999): -1000000000");
    Duration.create(0, -1000000000);
  }

  @Test
  public void create_NanosTooHigh() {
    thrown.expect(IllegalArgumentException.class);
    thrown.expectMessage("'nanos' is greater than maximum (999999999): 1000000000");
    Duration.create(0, 1000000000);
  }

  @Test
  public void create_NegativeSecondsPositiveNanos() {
    thrown.expect(IllegalArgumentException.class);
    thrown.expectMessage("'seconds' and 'nanos' have inconsistent sign: seconds=-1, nanos=1");
    Duration.create(-1, 1);
  }

  @Test
  public void create_PositiveSecondsNegativeNanos() {
    thrown.expect(IllegalArgumentException.class);
    thrown.expectMessage("'seconds' and 'nanos' have inconsistent sign: seconds=1, nanos=-1");
    Duration.create(1, -1);
  }

  @Test
  public void testDurationFromMillis() {
    assertThat(Duration.fromMillis(0)).isEqualTo(Duration.create(0, 0));
    assertThat(Duration.fromMillis(987)).isEqualTo(Duration.create(0, 987000000));
    assertThat(Duration.fromMillis(3456)).isEqualTo(Duration.create(3, 456000000));
  }

  @Test
  public void testDurationFromMillisNegative() {
    assertThat(Duration.fromMillis(-1)).isEqualTo(Duration.create(0, -1000000));
    assertThat(Duration.fromMillis(-999)).isEqualTo(Duration.create(0, -999000000));
    assertThat(Duration.fromMillis(-1000)).isEqualTo(Duration.create(-1, 0));
    assertThat(Duration.fromMillis(-3456)).isEqualTo(Duration.create(-3, -456000000));
  }

  @Test
  public void fromMillis_TooLow() {
    thrown.expect(IllegalArgumentException.class);
    thrown.expectMessage("'seconds' is less than minimum (-315576000000): -315576000001");
    Duration.fromMillis(-315576000001000L);
  }

  @Test
  public void fromMillis_TooHigh() {
    thrown.expect(IllegalArgumentException.class);
    thrown.expectMessage("'seconds' is greater than maximum (315576000000): 315576000001");
    Duration.fromMillis(315576000001000L);
  }

  @Test
  public void duration_CompareLength() {
    assertThat(Duration.create(0, 0).compareTo(Duration.create(0, 0))).isEqualTo(0);
    assertThat(Duration.create(24, 42).compareTo(Duration.create(24, 42))).isEqualTo(0);
    assertThat(Duration.create(-24, -42).compareTo(Duration.create(-24, -42))).isEqualTo(0);
    assertThat(Duration.create(25, 42).compareTo(Duration.create(24, 42))).isEqualTo(1);
    assertThat(Duration.create(24, 45).compareTo(Duration.create(24, 42))).isEqualTo(1);
    assertThat(Duration.create(24, 42).compareTo(Duration.create(25, 42))).isEqualTo(-1);
    assertThat(Duration.create(24, 42).compareTo(Duration.create(24, 45))).isEqualTo(-1);
    assertThat(Duration.create(-24, -45).compareTo(Duration.create(-24, -42))).isEqualTo(-1);
    assertThat(Duration.create(-24, -42).compareTo(Duration.create(-25, -42))).isEqualTo(1);
    assertThat(Duration.create(24, 42).compareTo(Duration.create(-24, -42))).isEqualTo(1);
  }

  @Test
  public void testDurationEqual() {
    // Positive tests.
    assertThat(Duration.create(0, 0)).isEqualTo(Duration.create(0, 0));
    assertThat(Duration.create(24, 42)).isEqualTo(Duration.create(24, 42));
    assertThat(Duration.create(-24, -42)).isEqualTo(Duration.create(-24, -42));
    // Negative tests.
    assertThat(Duration.create(25, 42)).isNotEqualTo(Duration.create(24, 42));
    assertThat(Duration.create(24, 43)).isNotEqualTo(Duration.create(24, 42));
    assertThat(Duration.create(-25, -42)).isNotEqualTo(Duration.create(-24, -42));
    assertThat(Duration.create(-24, -43)).isNotEqualTo(Duration.create(-24, -42));
  }

  @Test
  public void toMillis() {
    assertThat(Duration.create(10, 0).toMillis()).isEqualTo(10000L);
    assertThat(Duration.create(10, 1000).toMillis()).isEqualTo(10000L);
    assertThat(Duration.create(0, (int) 1e6).toMillis()).isEqualTo(1L);
    assertThat(Duration.create(0, 0).toMillis()).isEqualTo(0L);
    assertThat(Duration.create(-10, 0).toMillis()).isEqualTo(-10000L);
    assertThat(Duration.create(-10, -1000).toMillis()).isEqualTo(-10000L);
    assertThat(Duration.create(0, -(int) 1e6).toMillis()).isEqualTo(-1L);
  }
}