-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathUUIDFeatureSpec.scala
155 lines (111 loc) · 4.67 KB
/
UUIDFeatureSpec.scala
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
153
154
155
package test.io.jvm.uuid
import io.jvm.uuid._
import scala.util.Try
class UUIDFeatureSpec
extends org.specs2.Specification {
def is = s2"""
Primary constructor
from two longs $fromTwoLongs
String constructors
from strict $fromStrictString
from non-strict (fail) $fromNonStrictString
from strict $fromStrictStringWithCheck
from non-strict with check (fail) $fromNonStrictStringWithCheck
from strict without check $fromStrictStringWithoutCheck
from non-strict without check $fromNonStrictStringWithoutCheck
Array constructors
from long array $fromLongArray
from int array $fromIntArray
from short array $fromShortArray
from byte array $fromByteArray
Accessors
to msb, lsb longs $longAccessors
to long array $toLongArray
to int array $toIntArray
to short array $toShortArray
to byte array $toByteArray
to char array $toCharArray
to string $toDefaultCaseString
to lower-case string $toLowerCaseString
to upper-case string $toUpperCaseString
Extractors
uuid extractor $uuidExtractor
uuid extractor (failure) $uuidExtractorFailure
Comparisons
signed comparison $signedComparsion
signed comparison (via ordering) $explictSignedComparsion
unsigned comparison by default $unsignedComparsion
unsigned comparison (via ordering) $explicitUnsignedComparsion
equal comparison $equalComparsion
"""
private val u159bf = java.util.UUID.fromString("1-5-9-b-f")
// Primary constructor
def fromTwoLongs =
UUID(0x100050009L, 0xb00000000000fL) ==== u159bf
// String constructors
def fromStrictString =
UUID("00000001-0005-0009-000b-00000000000f") ==== u159bf
def fromNonStrictString =
Try { UUID("1-5-9-b-f") ==== u159bf } isFailure
def fromStrictStringWithCheck =
UUID("00000001-0005-0009-000b-00000000000f", true) ==== u159bf
def fromNonStrictStringWithCheck =
Try { UUID("1-5-9-b-f", true) } isFailure
def fromStrictStringWithoutCheck =
UUID("00000001-0005-0009-000b-00000000000f", false) ==== u159bf
def fromNonStrictStringWithoutCheck =
UUID("1-5-9-b-f", false) ==== u159bf
// Array constructors
def fromLongArray =
UUID(Array(0x100050009L, 0xb00000000000fL)) ==== u159bf
def fromIntArray =
UUID(Array(1, 0x50009, 0xb0000, 0xf)) ==== u159bf
def fromShortArray =
UUID(Array[Short](0, 1, 5, 9, 0xb, 0, 0, 0xf)) ==== u159bf
def fromByteArray =
UUID(Array[Byte](0, 0, 0, 1, 0, 5, 0, 9, 0, 0xb, 0, 0, 0, 0, 0, 0xf)) ==== u159bf
def fromCharArray =
UUID("00000001-0005-0009-000b-00000000000f".toCharArray) ==== u159bf
// Accessors
def longAccessors =
u159bf.mostSigBits ==== 0x100050009L &&
u159bf.leastSigBits ==== 0xb00000000000fL
def toLongArray =
u159bf.longArray ==== Array(0x100050009L, 0xb00000000000fL)
def toIntArray =
u159bf.intArray ==== Array(1, 0x50009, 0xb0000, 0xf)
def toShortArray =
u159bf.shortArray ==== Array[Short](0, 1, 5, 9, 0xb, 0, 0, 0xf)
def toByteArray =
u159bf.byteArray ==== Array[Byte](0, 0, 0, 1, 0, 5, 0, 9, 0, 0xb, 0, 0, 0, 0, 0, 0xf)
def toCharArray =
u159bf.charArray ==== "00000001-0005-0009-000b-00000000000f".toCharArray
def toDefaultCaseString =
u159bf.string ==== "00000001-0005-0009-000b-00000000000f"
def toLowerCaseString =
u159bf.toLowerCase ==== "00000001-0005-0009-000b-00000000000f"
def toUpperCaseString =
u159bf.toUpperCase ==== "00000001-0005-0009-000B-00000000000F"
// -- Extractors --
def uuidExtractor = {
val UUID(uuidValue) = "00000001-0005-0009-000b-00000000000f" // will fail if not strict!
uuidValue ==== u159bf
}
def uuidExtractorFailure = {
"1-5-9-b-f" match {
case UUID(_) => sys.error("Should not match - it wasn't strict!")
case _ => true
}
}
// -- Comparison --
def signedComparsion =
(UUID("FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF") compareTo u159bf) ==== -1
def explictSignedComparsion =
UUID.signedOrdering.compare(UUID("FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF"), u159bf) ==== -1
def unsignedComparsion =
(UUID("FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF") compare u159bf) ==== 1
def explicitUnsignedComparsion =
UUID.unsignedOrdering.compare(UUID("FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF"), u159bf) ==== 1
def equalComparsion =
(u159bf compare u159bf) ==== 0
}