From 16392b75814a71a4d78b3598cd3364baeb0b446e Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Thu, 9 Dec 2021 23:23:46 +0530 Subject: diceware: add Die class and tests --- .../dev/msfjarvis/aps/passgen/diceware/DieTest.kt | 47 ++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 passgen/diceware/src/test/kotlin/dev/msfjarvis/aps/passgen/diceware/DieTest.kt (limited to 'passgen/diceware/src/test/kotlin/dev') diff --git a/passgen/diceware/src/test/kotlin/dev/msfjarvis/aps/passgen/diceware/DieTest.kt b/passgen/diceware/src/test/kotlin/dev/msfjarvis/aps/passgen/diceware/DieTest.kt new file mode 100644 index 00000000..69a355c5 --- /dev/null +++ b/passgen/diceware/src/test/kotlin/dev/msfjarvis/aps/passgen/diceware/DieTest.kt @@ -0,0 +1,47 @@ +/* + * Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved. + * SPDX-License-Identifier: GPL-3.0-only + */ + +package dev.msfjarvis.aps.passgen.diceware + +import kotlin.random.Random +import kotlin.test.Test +import kotlin.test.assertEquals + +class DieTest { + + /** Pre-seeded [Random] instance to ensure tests are deterministic. */ + private val random = Random(1_00_000) + + private val intGenerator = RandomIntGenerator { it.random(random) } + + @Test + fun test_one_roll() { + val die = Die(6, intGenerator) + assertEquals(5, die.roll()) + } + + @Test + fun test_multiple_rolls() { + val die = Die(6, intGenerator) + assertEquals(526242, die.rollMultiple(6)) + } + + @Test + fun test_consecutive_rolls() { + val die = Die(6, intGenerator) + assertEquals(5, die.roll()) + assertEquals(2, die.roll()) + assertEquals(6, die.roll()) + assertEquals(2, die.roll()) + assertEquals(4, die.roll()) + assertEquals(2, die.roll()) + } + + @Test + fun test_100_sides() { + val die = Die(100, intGenerator) + assertEquals(67, die.roll()) + } +} -- cgit v1.2.3