From e8044a7c702396d7a31705c932fe31d65cdd10d8 Mon Sep 17 00:00:00 2001 From: Fabian Zobrist Date: Wed, 19 Aug 2020 16:27:55 +0200 Subject: [PATCH] Add new class files with Unit tests for tracking the history. --- .../java/me/zobrist/tichucounter/History.kt | 55 +++++++++++ .../java/me/zobrist/tichucounter/Round.kt | 36 ++++++++ .../zobrist/tichucounter/ExampleUnitTest.kt | 29 ------ .../zobrist/tichucounter/HistoryUnitTest.kt | 53 +++++++++++ .../me/zobrist/tichucounter/RoundUnitTest.kt | 92 +++++++++++++++++++ 5 files changed, 236 insertions(+), 29 deletions(-) create mode 100644 app/src/main/java/me/zobrist/tichucounter/History.kt create mode 100644 app/src/main/java/me/zobrist/tichucounter/Round.kt delete mode 100644 app/src/test/java/me/zobrist/tichucounter/ExampleUnitTest.kt create mode 100644 app/src/test/java/me/zobrist/tichucounter/HistoryUnitTest.kt create mode 100644 app/src/test/java/me/zobrist/tichucounter/RoundUnitTest.kt diff --git a/app/src/main/java/me/zobrist/tichucounter/History.kt b/app/src/main/java/me/zobrist/tichucounter/History.kt new file mode 100644 index 0000000..df23329 --- /dev/null +++ b/app/src/main/java/me/zobrist/tichucounter/History.kt @@ -0,0 +1,55 @@ +package me.zobrist.tichucounter + +class History { + private var scores: ArrayList = ArrayList() + + fun getScoreA(): Int { + var tempScore = 0 + scores.forEach { + tempScore += it.scoreA + } + return tempScore + } + + fun getScoreB(): Int { + var tempScore = 0 + scores.forEach { + tempScore += it.scoreB + } + return tempScore + } + + fun getHistoryA(): String { + var tempHistory = String() + scores.forEach { + tempHistory = tempHistory.plus(it.scoreA.toString()).plus("\n") + } + return tempHistory + } + + fun getHistoryB(): String { + var tempHistory = String() + scores.forEach { + tempHistory = tempHistory.plus(it.scoreB.toString()).plus("\n") + } + return tempHistory + } + + fun logRound(round: Round) { + scores.add(round) + } + + fun revertLastRound() { + if (scores.isNotEmpty()) { + scores.removeAt(scores.size - 1) + } + } + + fun clearAll() { + scores.clear() + } + + fun isEmpty(): Boolean { + return scores.isEmpty() + } +} \ No newline at end of file diff --git a/app/src/main/java/me/zobrist/tichucounter/Round.kt b/app/src/main/java/me/zobrist/tichucounter/Round.kt new file mode 100644 index 0000000..fad6b61 --- /dev/null +++ b/app/src/main/java/me/zobrist/tichucounter/Round.kt @@ -0,0 +1,36 @@ +package me.zobrist.tichucounter + +class Round() { + var scoreA: Int = 0 + var scoreB: Int = 0 + + constructor(score: Int, isScoreA: Boolean) : this() { + if (isScoreA) { + scoreA = score + scoreB = calculateOtherScore(scoreA) + } else { + scoreB = score + scoreA = calculateOtherScore(scoreB) + } + } + + constructor(scoreA: Int, scoreB: Int) : this() { + this.scoreA = scoreA + this.scoreB = scoreB + } + + private fun calculateOtherScore(score: Int): Int { + if (isMultipleOf100(score)) { + return 0 + } + return 100 - (score % 100) + } + + private fun isMultipleOf100(score: Int): Boolean { + return (score / 100) >= 1 && (score % 100) == 0 + } + + fun isValidRound(): Boolean { + return (scoreA % 5 == 0) && (scoreB % 5 == 0) && ((scoreA + scoreB) % 100 == 0) + } +} \ No newline at end of file diff --git a/app/src/test/java/me/zobrist/tichucounter/ExampleUnitTest.kt b/app/src/test/java/me/zobrist/tichucounter/ExampleUnitTest.kt deleted file mode 100644 index 51adcd2..0000000 --- a/app/src/test/java/me/zobrist/tichucounter/ExampleUnitTest.kt +++ /dev/null @@ -1,29 +0,0 @@ -package me.zobrist.tichucounter - -import org.junit.Test - -import org.junit.Assert.* - -/** - * Example local unit test, which will execute on the development machine (host). - * - * See [testing documentation](http://d.android.com/tools/testing). - */ -class RoundUnitTest { - @Test - fun addition_isCorrect() { - var inputScoreA: Int - var inputScoreB: Int - - inputScoreA = 125 - inputScoreB = -25 - - while(inputScoreA <= -25){ - assertEquals(inputScoreB, Round(inputScoreA, true).scoreB) - assertEquals(inputScoreB, Round(inputScoreA, false).scoreA) - - inputScoreA -= 5 - inputScoreB += 5 - } - } -} \ No newline at end of file diff --git a/app/src/test/java/me/zobrist/tichucounter/HistoryUnitTest.kt b/app/src/test/java/me/zobrist/tichucounter/HistoryUnitTest.kt new file mode 100644 index 0000000..576abe4 --- /dev/null +++ b/app/src/test/java/me/zobrist/tichucounter/HistoryUnitTest.kt @@ -0,0 +1,53 @@ +package me.zobrist.tichucounter + +import org.junit.Assert.assertEquals +import org.junit.Assert.assertNotEquals +import org.junit.Test + +/** + * Example local unit test, which will execute on the development machine (host). + * + * See [testing documentation](http://d.android.com/tools/testing). + */ +class HistoryUnitTest { + @Test + fun calculation_isCorrect() { + val history = History() + + history.revertLastRound() + history.getHistoryA() + history.getHistoryB() + history.getScoreA() + history.getScoreB() + + history.logRound(Round(10, 10)) + history.logRound(Round(10, 10)) + history.logRound(Round(10, 10)) + history.logRound(Round(10, 10)) + history.logRound(Round(10, 10)) + history.logRound(Round(10, 10)) + history.logRound(Round(10, 10)) + history.logRound(Round(10, 10)) + history.logRound(Round(10, 10)) + history.logRound(Round(10, 10)) + + assertEquals(100, history.getScoreA()) + assertEquals(100, history.getScoreB()) + + history.revertLastRound() + + assertEquals(90, history.getScoreA()) + assertEquals(90, history.getScoreB()) + + assertNotEquals("", history.getHistoryA()) + assertNotEquals("", history.getHistoryB()) + + history.clearAll() + assertEquals(0, history.getScoreA()) + assertEquals(0, history.getScoreB()) + + + assertEquals("", history.getHistoryA()) + assertEquals("", history.getHistoryB()) + } +} \ No newline at end of file diff --git a/app/src/test/java/me/zobrist/tichucounter/RoundUnitTest.kt b/app/src/test/java/me/zobrist/tichucounter/RoundUnitTest.kt new file mode 100644 index 0000000..f7626c6 --- /dev/null +++ b/app/src/test/java/me/zobrist/tichucounter/RoundUnitTest.kt @@ -0,0 +1,92 @@ +package me.zobrist.tichucounter + +import org.junit.Assert.* +import org.junit.Test + +/** + * Example local unit test, which will execute on the development machine (host). + * + * See [testing documentation](http://d.android.com/tools/testing). + */ +class RoundUnitTest { + @Test + fun calculation_isCorrect() { + + var inputScoreA = 125 + var inputScoreB = -25 + var temp: Round + + // Normal round range -25 to 125 as input + while (inputScoreB >= 125) { + temp = Round(inputScoreA, true) + assertEquals(inputScoreB, temp.scoreB) + assertTrue(temp.isValidRound()) + + temp = Round(inputScoreA, false) + assertEquals(inputScoreB, temp.scoreA) + assertTrue(temp.isValidRound()) + + inputScoreA -= 5 + inputScoreB += 5 + } + + // Team a +100 points for Tichu + inputScoreA = 125 + 100 + inputScoreB = -25 + + // Normal round range -25 to 125 as input + while (inputScoreB >= 125) { + temp = Round(inputScoreA, true) + assertEquals(inputScoreB, temp.scoreB) + assertTrue(temp.isValidRound()) + + temp = Round(inputScoreA, false) + assertEquals(inputScoreB, temp.scoreA) + assertTrue(temp.isValidRound()) + + inputScoreA -= 5 + inputScoreB += 5 + } + + // Double win + temp = Round(200, true) + assertEquals(0, temp.scoreB) + assertTrue(temp.isValidRound()) + + temp = Round(200, false) + assertEquals(0, temp.scoreA) + assertTrue(temp.isValidRound()) + + // Double win with Tichu + temp = Round(300, true) + assertEquals(0, temp.scoreB) + assertTrue(temp.isValidRound()) + + temp = Round(300, false) + assertEquals(0, temp.scoreA) + assertTrue(temp.isValidRound()) + + // Double win with Grand Tichu + temp = Round(400, true) + assertEquals(0, temp.scoreB) + assertTrue(temp.isValidRound()) + + temp = Round(400, false) + assertEquals(0, temp.scoreA) + assertTrue(temp.isValidRound()) + + //Good rounds + temp = Round(0, 0) + assertTrue(temp.isValidRound()) + + //Bad rounds + temp = Round(5, 12) + assertFalse(temp.isValidRound()) + + temp = Round(12, 5) + assertFalse(temp.isValidRound()) + + temp = Round(5, 55) + assertFalse(temp.isValidRound()) + } +} \ No newline at end of file