-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathSimpleTimestampSpec.scala
More file actions
88 lines (75 loc) · 2.95 KB
/
Copy pathSimpleTimestampSpec.scala
File metadata and controls
88 lines (75 loc) · 2.95 KB
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
package net.orfjackal.dimdwarf.domain
import org.junit.runner.RunWith
import net.orfjackal.specsy._
import org.junit.Assert._
import org.hamcrest.Matchers._
import org.hamcrest.MatcherAssert.assertThat
import org.scalatest.Assertions
@RunWith(classOf[Specsy])
class SimpleTimestampSpec extends Spec with Assertions {
"Timestamps are value objects" >> {
val ts1a = SimpleTimestamp(1L)
val ts1b = SimpleTimestamp(1L)
val ts2 = SimpleTimestamp(2L)
assertThat(ts1a, equalTo(ts1b))
assertThat(ts1a, not(equalTo(ts2)))
assertThat(ts2, not(equalTo(ts1a)))
assertThat(ts1a.hashCode, equalTo(ts1b.hashCode))
assertThat(ts1a.hashCode, not(equalTo(ts2.hashCode)))
}
"Timestamps are shown in hexadecimal format" >> {
assertThat(SimpleTimestamp(0L).toString, is("{00000000-00000000}"))
assertThat(SimpleTimestamp(1L).toString, is("{00000000-00000001}"))
assertThat(SimpleTimestamp(Long.MaxValue).toString, is("{7fffffff-ffffffff}"))
assertThat(SimpleTimestamp(Long.MinValue).toString, is("{80000000-00000000}"))
assertThat(SimpleTimestamp(-1L).toString, is("{ffffffff-ffffffff}"))
}
"Timestamps can be incremented" >> {
assertThat(SimpleTimestamp(0L).next, is(SimpleTimestamp(1L)))
assertThat(SimpleTimestamp(1L).next, is(SimpleTimestamp(2L)))
assertThat(SimpleTimestamp(Long.MaxValue).next, is(SimpleTimestamp(Long.MinValue)))
assertThat(SimpleTimestamp(-2L).next, is(SimpleTimestamp(-1L)))
}
"Timestamps cannot overflow" >> {
val e = intercept[IllegalStateException] {
SimpleTimestamp(-1L).next
}
assertThat(e.getMessage, containsString("overflow"))
}
"Timestamps are ordered" >> {
assertComparableInOrder(
SimpleTimestamp(0L),
SimpleTimestamp(1L),
SimpleTimestamp(Int.MaxValue - 1L),
SimpleTimestamp(Int.MaxValue),
SimpleTimestamp(Int.MaxValue + 1L),
SimpleTimestamp(Long.MaxValue - 1L),
SimpleTimestamp(Long.MaxValue),
SimpleTimestamp(Long.MinValue),
SimpleTimestamp(Long.MinValue + 1L),
SimpleTimestamp(Int.MinValue - 1L),
SimpleTimestamp(Int.MinValue),
SimpleTimestamp(Int.MinValue + 1L),
SimpleTimestamp(-1L))
}
private def assertComparableInOrder(ordered: Timestamp*) {
shareSideEffects() // performance optimization
for (i <- 0 until ordered.length) {
val lesser = ordered(i)
assertEqualToItself(lesser)
for (j <- i + 1 until ordered.length) {
val greater = ordered(j)
"Case: " + lesser + " < " + greater >> {
assertLessThan(lesser, greater)
}
}
}
}
private def assertEqualToItself(ts: Timestamp) {
assertTrue("did not satisfy: " + ts + " == " + ts, ts.compareTo(ts) == 0)
}
private def assertLessThan(lesser: Timestamp, greater: Timestamp) {
assertTrue("did not satisfy: " + lesser + " < " + greater, lesser.compareTo(greater) < 0)
assertTrue("did not satisfy: " + greater + " > " + lesser, greater.compareTo(lesser) > 0)
}
}