package icurves

import icurves.graph.cycles.ElementaryCyclesSearch
import org.hamcrest.CoreMatchers.`is`
import org.hamcrest.CoreMatchers.hasItems
import org.hamcrest.MatcherAssert.assertThat
import org.junit.jupiter.api.Test

/**
 *
 *
 * @author Almas Baimagambetov (almaslvl@gmail.com)
 */
class CycleFinderTest {

    @Test
    fun `Strong connected components`() {
        val nodes = arrayOfNulls<String>(10)
        val adjMatrix = Array(10) { BooleanArray(10) }

        for (i in 0..9) {
            nodes[i] = "Node " + i
        }

        adjMatrix[0][1] = true
        adjMatrix[1][2] = true
        adjMatrix[2][0] = true
        adjMatrix[2][6] = true
        adjMatrix[3][4] = true
        adjMatrix[4][5] = true
        adjMatrix[4][6] = true
        adjMatrix[5][3] = true
        adjMatrix[6][7] = true
        adjMatrix[7][8] = true
        adjMatrix[8][6] = true
        adjMatrix[6][1] = true

        val ecs = ElementaryCyclesSearch(adjMatrix, nodes)
        val cycles = ecs.elementaryCycles

        val results = arrayListOf<String>()

        for (i in cycles.indices) {
            var result = ""
            val cycle = cycles[i] as List<*>
            for (j in cycle.indices) {
                val node = cycle[j] as String
                if (j < cycle.size - 1) {
                    result += (node + " -> ")
                } else {
                    result += (node)
                }
            }

            results.add(result)
        }

        assertThat(results.size, `is`(4))
        assertThat(results, hasItems(
                "Node 0 -> Node 1 -> Node 2",
                "Node 1 -> Node 2 -> Node 6",
                "Node 3 -> Node 4 -> Node 5",
                "Node 6 -> Node 7 -> Node 8"
        ))

//        Node 0 -> Node 1 -> Node 2
//        Node 1 -> Node 2 -> Node 6
//        Node 3 -> Node 4 -> Node 5
//        Node 6 -> Node 7 -> Node 8
    }
}