Add new class files with Unit tests for tracking the history.

This commit is contained in:
Fabian Zobrist
2020-08-19 16:27:55 +02:00
parent ca3d0b962a
commit e8044a7c70
5 changed files with 236 additions and 29 deletions

View File

@@ -0,0 +1,55 @@
package me.zobrist.tichucounter
class History {
private var scores: ArrayList<Round> = 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()
}
}

View File

@@ -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)
}
}

View File

@@ -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
}
}
}

View File

@@ -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())
}
}

View File

@@ -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())
}
}