package com.pixelized.desktop.lwa.business

import com.pixelized.shared.lwa.usecase.RollUseCase
import org.junit.Test

class RollUseCaseTest {

    companion object {
        private const val ROLL_COUNT = 100000000 // 100*10^6
    }

    @Test
    fun testRoll1D100() {
        val result = build1D100ResultSet()

        println("Testing if with $ROLL_COUNT rolls we have at least 1 result of each value.")
        assert(result.all { it > 0 }) {
            "Maybe a false negative, we expected that all values should have a result.\nroll amount: $ROLL_COUNT - result:$result"
        }
    }

    @Test
    fun testRoll1D100_() {
        val results = build1D100ResultSet()

        val delta = 0.005f
        val median = (ROLL_COUNT / 100).let {
            (it * (1f - delta)).toInt()..(it * (1f + delta)).toInt()
        }

        println("Testing if with $ROLL_COUNT rolls we have at least all results in $median.")
        results.map { it in median }.forEachIndexed { index, result ->
            assert(result) {
                "Maybe a false negative, we expected that all values should be in $median a result.\nroll amount: $ROLL_COUNT - index:$index result:${results[index]}"
            }
        }
    }

    private fun build1D100ResultSet(count: Int = ROLL_COUNT): List<Int> {
        val useCase = RollUseCase()
        val result = MutableList(100) { 0 }
        repeat(count) {
            val roll = useCase.roll(faces = 100)
            result[roll - 1] += 1
        }
        return result
    }
}