Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package org.whispersystems.signalservice.api.util

import junit.framework.TestCase.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNotEquals
import org.junit.Assert.assertTrue
import org.junit.Test
import org.whispersystems.signalservice.api.util.OptionalUtil.byteArrayEquals
import org.whispersystems.signalservice.api.util.OptionalUtil.byteArrayHashCode
import org.whispersystems.signalservice.api.util.OptionalUtil.or
import java.util.Optional

class OptionalUtilTest {

@Test
fun absent_are_equal() {
assertTrue(byteArrayEquals(Optional.empty(), Optional.empty()))
}

@Test
fun first_non_absent_not_equal() {
assertFalse(byteArrayEquals(Optional.of(ByteArray(1)), Optional.empty()))
}

@Test
fun second_non_absent_not_equal() {
assertFalse(byteArrayEquals(Optional.empty(), Optional.of(ByteArray(1))))
}

@Test
fun equal_contents() {
val contentsA = byteArrayOf(1, 2, 3)
val contentsB = contentsA.copyOf()
val a: Optional<ByteArray> = Optional.of(contentsA)
val b: Optional<ByteArray> = Optional.of(contentsB)
assertTrue(byteArrayEquals(a, b))
assertEquals(byteArrayHashCode(a), byteArrayHashCode(b))
}

@Test
fun in_equal_contents() {
val contentsA = byteArrayOf(1, 2, 3)
val contentsB = byteArrayOf(4, 5, 6)
val a: Optional<ByteArray> = Optional.of(contentsA)
val b: Optional<ByteArray> = Optional.of(contentsB)
assertFalse(byteArrayEquals(a, b))
assertNotEquals(byteArrayHashCode(a), byteArrayHashCode(b))
}

@Test
fun hash_code_absent() {
assertEquals(0, byteArrayHashCode(Optional.empty()))
}

@Test
fun or_singleAbsent() {
assertFalse(or(Optional.empty()).isPresent)
}

@Test
fun or_multipleAbsent() {
assertFalse(or(Optional.empty(), Optional.empty()).isPresent)
}

@Test
fun or_firstHasValue() {
assertEquals(5, or(Optional.of(5), Optional.empty()).get())
}

@Test
fun or_secondHasValue() {
assertEquals(5, or(Optional.empty(), Optional.of(5)).get())
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package org.whispersystems.signalservice.api.util

import junit.framework.TestCase.assertEquals
import org.junit.Test
import org.whispersystems.signalservice.api.util.Uint64Util.bigIntegerToUInt64
import org.whispersystems.signalservice.api.util.Uint64Util.uint64ToBigInteger
import java.math.BigInteger

class Uint64UtilTest {

@Test
fun long_zero_to_bigInteger() {
val bigInteger = uint64ToBigInteger(0)
assertEquals("0", bigInteger.toString())
}

@Test
fun long_to_bigInteger() {
val bigInteger = uint64ToBigInteger(12345L)
assertEquals("12345", bigInteger.toString())
}

@Test
fun bigInteger_zero_to_long() {
val uint64 = bigIntegerToUInt64(BigInteger.ZERO)
assertEquals(0L, uint64)
}

@Test
fun first_uint64_value_to_bigInteger() {
val bigInteger = uint64ToBigInteger(0x8000000000000000UL.toLong())
assertEquals("9223372036854775808", bigInteger.toString())
}

@Test
fun bigInteger_to_first_uint64_value() {
val uint64 = bigIntegerToUInt64(BigInteger("9223372036854775808"))
assertEquals(0x8000000000000000UL.toLong(), uint64)
}

@Test
fun large_uint64_value_to_bigInteger() {
val bigInteger = uint64ToBigInteger(0xa523f21e412c14d2UL.toLong())
assertEquals("11899620852199331026", bigInteger.toString())
}

@Test
fun bigInteger_to_large_uint64_value() {
val uint64 = bigIntegerToUInt64(BigInteger("11899620852199331026"))
assertEquals(0xa523f21e412c14d2UL.toLong(), uint64)
}

@Test
fun largest_uint64_value_to_bigInteger() {
val bigInteger = uint64ToBigInteger(0xffffffffffffffffUL.toLong())
assertEquals("18446744073709551615", bigInteger.toString())
}

@Test
fun bigInteger_to_largest_uint64_value() {
val uint64 = bigIntegerToUInt64(BigInteger("18446744073709551615"))
assertEquals(0xffffffffffffffffUL.toLong(), uint64)
}

@Test(expected = Uint64RangeException::class)
fun too_big_by_one() {
bigIntegerToUInt64(BigInteger("18446744073709551616"))
}

@Test(expected = Uint64RangeException::class)
fun too_small_by_one() {
bigIntegerToUInt64(BigInteger("-1"))
}

@Test(expected = Uint64RangeException::class)
fun too_big_by_a_lot() {
bigIntegerToUInt64(BigInteger("1844674407370955161623"))
}

@Test(expected = Uint64RangeException::class)
fun too_small_by_a_lot() {
bigIntegerToUInt64(BigInteger("-1844674407370955161623"))
}
}

This file was deleted.

Loading