package nextflow.k8s

import spock.lang.Specification

class K8sRuntimeEstimatorTest extends Specification {

    def 'does extract a function' () {
        given:
        def observations = [a: [new Tuple2(0, 0), new Tuple2(100, 100)]]
        def estimator = new K8sRuntimeEstimator(observations)

        when:
        def estimation = estimator.estimate("a", 1234)

        then:
        estimation != Double.POSITIVE_INFINITY
        estimation == 1234.0
    }

    def 'can extract a function from single measurement' () {
        given:
        def observations = [a: [new Tuple2(100, 100)]]
        def estimator = new K8sRuntimeEstimator(observations)

        when:
        def estimation = estimator.estimate("a", 1234)

        then:
        estimation == 100.0 /* Single measurement implies constant runtime */
    }

    def 'unknown function returns positive infinity' () {
        given:
        def observations = [] as HashMap<String, ArrayList>
        def estimator = new K8sRuntimeEstimator(observations)

        when:
        def estimation = estimator.estimate("b", 1234)

        then:
        estimation == Double.POSITIVE_INFINITY
    }

    def 'extracts function from multiple measurements' () {
        given:
        def observations = [a: [new Tuple2(10, 15), new Tuple2(20, 34), new Tuple2(30, 46)]]
        def estimator = new K8sRuntimeEstimator(observations)

        when:
        def estimation = estimator.estimate("a", 40)

        then:
        estimation != Double.POSITIVE_INFINITY
    }
}
