Skip to content

Commit 2c8b061

Browse files
Convert OptionalUtilTest, Uint64UtilTest and UuidUtilTest classes to Kotlin
1 parent 6805826 commit 2c8b061

File tree

6 files changed

+209
-228
lines changed

6 files changed

+209
-228
lines changed

libsignal-service/src/test/java/org/whispersystems/signalservice/api/util/OptionalUtilTest.java

Lines changed: 0 additions & 73 deletions
This file was deleted.
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package org.whispersystems.signalservice.api.util
2+
3+
import junit.framework.TestCase.assertEquals
4+
import org.junit.Assert.assertFalse
5+
import org.junit.Assert.assertNotEquals
6+
import org.junit.Assert.assertTrue
7+
import org.junit.Test
8+
import org.whispersystems.signalservice.api.util.OptionalUtil.byteArrayEquals
9+
import org.whispersystems.signalservice.api.util.OptionalUtil.byteArrayHashCode
10+
import org.whispersystems.signalservice.api.util.OptionalUtil.or
11+
import java.util.Optional
12+
13+
class OptionalUtilTest {
14+
15+
@Test
16+
fun absent_are_equal() {
17+
assertTrue(byteArrayEquals(Optional.empty(), Optional.empty()))
18+
}
19+
20+
@Test
21+
fun first_non_absent_not_equal() {
22+
assertFalse(byteArrayEquals(Optional.of(ByteArray(1)), Optional.empty()))
23+
}
24+
25+
@Test
26+
fun second_non_absent_not_equal() {
27+
assertFalse(byteArrayEquals(Optional.empty(), Optional.of(ByteArray(1))))
28+
}
29+
30+
@Test
31+
fun equal_contents() {
32+
val contentsA = byteArrayOf(1, 2, 3)
33+
val contentsB = contentsA.copyOf()
34+
val a: Optional<ByteArray> = Optional.of(contentsA)
35+
val b: Optional<ByteArray> = Optional.of(contentsB)
36+
assertTrue(byteArrayEquals(a, b))
37+
assertEquals(byteArrayHashCode(a), byteArrayHashCode(b))
38+
}
39+
40+
@Test
41+
fun in_equal_contents() {
42+
val contentsA = byteArrayOf(1, 2, 3)
43+
val contentsB = byteArrayOf(4, 5, 6)
44+
val a: Optional<ByteArray> = Optional.of(contentsA)
45+
val b: Optional<ByteArray> = Optional.of(contentsB)
46+
assertFalse(byteArrayEquals(a, b))
47+
assertNotEquals(byteArrayHashCode(a), byteArrayHashCode(b))
48+
}
49+
50+
@Test
51+
fun hash_code_absent() {
52+
assertEquals(0, byteArrayHashCode(Optional.empty()))
53+
}
54+
55+
@Test
56+
fun or_singleAbsent() {
57+
assertFalse(or(Optional.empty()).isPresent)
58+
}
59+
60+
@Test
61+
fun or_multipleAbsent() {
62+
assertFalse(or(Optional.empty(), Optional.empty()).isPresent)
63+
}
64+
65+
@Test
66+
fun or_firstHasValue() {
67+
assertEquals(5, or(Optional.of(5), Optional.empty()).get())
68+
}
69+
70+
@Test
71+
fun or_secondHasValue() {
72+
assertEquals(5, or(Optional.empty(), Optional.of(5)).get())
73+
}
74+
}

libsignal-service/src/test/java/org/whispersystems/signalservice/api/util/Uint64UtilTest.java

Lines changed: 0 additions & 95 deletions
This file was deleted.
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package org.whispersystems.signalservice.api.util
2+
3+
import junit.framework.TestCase.assertEquals
4+
import org.junit.Test
5+
import org.whispersystems.signalservice.api.util.Uint64Util.bigIntegerToUInt64
6+
import org.whispersystems.signalservice.api.util.Uint64Util.uint64ToBigInteger
7+
import java.math.BigInteger
8+
9+
class Uint64UtilTest {
10+
11+
@Test
12+
fun long_zero_to_bigInteger() {
13+
val bigInteger = uint64ToBigInteger(0)
14+
assertEquals("0", bigInteger.toString())
15+
}
16+
17+
@Test
18+
fun long_to_bigInteger() {
19+
val bigInteger = uint64ToBigInteger(12345L)
20+
assertEquals("12345", bigInteger.toString())
21+
}
22+
23+
@Test
24+
fun bigInteger_zero_to_long() {
25+
val uint64 = bigIntegerToUInt64(BigInteger.ZERO)
26+
assertEquals(0L, uint64)
27+
}
28+
29+
@Test
30+
fun first_uint64_value_to_bigInteger() {
31+
val bigInteger = uint64ToBigInteger(0x8000000000000000UL.toLong())
32+
assertEquals("9223372036854775808", bigInteger.toString())
33+
}
34+
35+
@Test
36+
fun bigInteger_to_first_uint64_value() {
37+
val uint64 = bigIntegerToUInt64(BigInteger("9223372036854775808"))
38+
assertEquals(0x8000000000000000UL.toLong(), uint64)
39+
}
40+
41+
@Test
42+
fun large_uint64_value_to_bigInteger() {
43+
val bigInteger = uint64ToBigInteger(0xa523f21e412c14d2UL.toLong())
44+
assertEquals("11899620852199331026", bigInteger.toString())
45+
}
46+
47+
@Test
48+
fun bigInteger_to_large_uint64_value() {
49+
val uint64 = bigIntegerToUInt64(BigInteger("11899620852199331026"))
50+
assertEquals(0xa523f21e412c14d2UL.toLong(), uint64)
51+
}
52+
53+
@Test
54+
fun largest_uint64_value_to_bigInteger() {
55+
val bigInteger = uint64ToBigInteger(0xffffffffffffffffUL.toLong())
56+
assertEquals("18446744073709551615", bigInteger.toString())
57+
}
58+
59+
@Test
60+
fun bigInteger_to_largest_uint64_value() {
61+
val uint64 = bigIntegerToUInt64(BigInteger("18446744073709551615"))
62+
assertEquals(0xffffffffffffffffUL.toLong(), uint64)
63+
}
64+
65+
@Test(expected = Uint64RangeException::class)
66+
fun too_big_by_one() {
67+
bigIntegerToUInt64(BigInteger("18446744073709551616"))
68+
}
69+
70+
@Test(expected = Uint64RangeException::class)
71+
fun too_small_by_one() {
72+
bigIntegerToUInt64(BigInteger("-1"))
73+
}
74+
75+
@Test(expected = Uint64RangeException::class)
76+
fun too_big_by_a_lot() {
77+
bigIntegerToUInt64(BigInteger("1844674407370955161623"))
78+
}
79+
80+
@Test(expected = Uint64RangeException::class)
81+
fun too_small_by_a_lot() {
82+
bigIntegerToUInt64(BigInteger("-1844674407370955161623"))
83+
}
84+
}

libsignal-service/src/test/java/org/whispersystems/signalservice/api/util/UuidUtilTest.java

Lines changed: 0 additions & 60 deletions
This file was deleted.

0 commit comments

Comments
 (0)