Nantes Université

Skip to content
Extraits de code Groupes Projets
TestIte6Remove.kt 1,69 ko
Newer Older
Arnaud LANOIX's avatar
Arnaud LANOIX a validé
import but3.collections.EmptyBSTException
import but3.collections.IterativeBinarySearchTree
import but3.collections.KeyNotFoundException
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.CsvSource

class TestIte6Remove {

    lateinit var bst: IterativeBinarySearchTree<Int>

    @BeforeEach
    fun initialise() {
        bst = IterativeBinarySearchTree.buildFrom(10, 3, 5, 7, 100, 30, 20, 45, 50, 48, 60, 10000)
    }

    @Test
    fun empty() {
        bst = IterativeBinarySearchTree()
        assertThrows<EmptyBSTException> { bst.removeB(42) }
    }


    @ParameterizedTest(name = "removed value {0} : impossible")
    @CsvSource(
        "42,4",
        "2,2",
        "0,2",
        "1000,3",
        "-3,2",
        "666,3"
    )
    fun removeImpossible(contained: Int, expectedCalls: Int) {
        assertThrows<KeyNotFoundException> { bst.removeB(contained) }
    }

    @ParameterizedTest(name = "element removed : {0}")
    @CsvSource(
        "5,3,6",
        "7,4,7",
        "10,5,9",
        "20,4,7",
        "30,5,9",
        "45,4,9",
        "48,6,11",
        "50,7,13",
        "60,6,11",
        "100,4,10",
        "10000,3,5"
    )
    fun remove(removed: Int, expectedCalls: Int, expectedCalls2: Int) {
        bst.removeB(removed)
        assertFalse(bst.containsB(removed))
    }

    @Test
    fun removeAll() {
        val elements = listOf(10, 3, 5, 7, 100, 30, 20, 45, 50, 48, 60, 10000)
        elements.forEach {
            bst.removeB(it)
        }
        assertTrue(bst.isEmpty())
    }

}