import "../../runtime/eltest.el"
import "../../runtime/elbench.el"

// test_elbench.el — proves the growth-curve classifier against KNOWN curves.
//
// Every series below is real measured data from lang/tests/bench/fitprobe.el
// on a geometric sweep n = 200/400/800/1600. The classifier must be provable
// without depending on a live defect existing, which is the whole point of
// keeping controlled specimens.

fn _s4(a: Int, b: Int, c: Int, d: Int) -> [Int] {
    let l: [Int] = native_list_empty()
    let l = native_list_append(l, a)
    let l = native_list_append(l, b)
    let l = native_list_append(l, c)
    let l = native_list_append(l, d)
    return l
}

test "classifies a linear allocation series as O(n)" {
    // fitprobe `linear`, allocation count
    let v = _s4(208, 409, 810, 1611)
    assert elb_measured_curve(v, 10) == 2, "linear allocs should classify O(n)"
}

test "classifies a linear byte series as O(n)" {
    // fitprobe `linear`, allocation bytes
    let v = _s4(4786, 9682, 19474, 39658)
    assert elb_measured_curve(v, 10) == 2, "linear bytes should classify O(n)"
}

test "classifies a quadratic byte series as O(n^2)" {
    // fitprobe `accum`, allocation bytes -- the accumulator-rebuild shape
    let v = _s4(20300, 80600, 321200, 1282400)
    assert elb_measured_curve(v, 10) == 4, "accum bytes should classify O(n^2)"
}

test "accumulator count is linear -- proves count alone misses it" {
    // Same run as above. The COUNT is exactly linear while bytes are
    // quadratic. A count-only gate passes this defect clean.
    let v = _s4(200, 400, 800, 1600)
    assert elb_measured_curve(v, 10) == 2, "accum count classifies O(n)"
    assert elb_gate(v, 2, 10) == 0, "count-only gate PASSES the quadratic"
}

test "classifies a quadratic time series as O(n^2)" {
    // fitprobe `compute` -- el #132's shape: n scans over n characters
    let v = _s4(67, 205, 818, 3268)
    assert elb_measured_curve(v, 10) == 4, "compute time should classify O(n^2)"
}

test "REFUSES an all-zero series instead of calling it O(1)" {
    // fitprobe `compute` allocation count. Pure CPU, allocates nothing.
    // Reporting O(1) here would be a confident answer with nothing behind it.
    let v = _s4(0, 0, 0, 0)
    assert elb_gate(v, 2, 10) == 3, "all-zero series must be REFUSED"
    assert elb_measured_curve(v, 10) < 0, "unclassifiable returns -1"
}

test "REFUSES an implausibly flat series" {
    // The shape produced when clang closes a loop to a multiply: a real
    // answer, no work done, no movement across an 8x input range.
    let v = _s4(1000, 1001, 1002, 1003)
    assert elb_gate(v, 2, 10) == 3, "hard-flat series must be REFUSED"
}

test "gate FAILS a quadratic declared as linear" {
    let v = _s4(20300, 80600, 321200, 1282400)
    assert elb_gate(v, 2, 10) == 1, "O(n^2) measured vs O(n) declared must FAIL"
}

test "gate PASSES a linear series declared as linear" {
    let v = _s4(208, 409, 810, 1611)
    assert elb_gate(v, 2, 10) == 0, "O(n) measured vs O(n) declared must PASS"
}

test "gate reports BETTER when measured beats the declared bound" {
    let v = _s4(208, 409, 810, 1611)
    assert elb_gate(v, 4, 10) == 4, "O(n) measured vs O(n^2) declared is BETTER"
}

test "gate reports INDETERMINATE on disagreeing ratios" {
    // fitprobe `linear` WALL TIME at these sizes: 26/19/43/78 microseconds.
    // Ratios 0.73, 2.26, 1.81 disagree well past the noise threshold. The
    // honest answer is "cannot tell", not a classification -- this is exactly
    // why benchmarks need auto-scaled iteration counts rather than one shot.
    let v = _s4(26, 19, 43, 78)
    assert elb_gate(v, 2, 10) == 2, "disagreeing ratios must be INDETERMINATE"
}

test "black_box is a real barrier and returns its input" {
    assert el_black_box(42) == 42, "black_box is value-preserving"
    let s: Int = 0
    let i: Int = 0
    while i < 100 {
        // Bind the call before using it in arithmetic: `x + call(...)`
        // lowers to el_str_concat() on integers. Same inference defect
        // as `call(...) == y` lowering to str_eq().
        let bx: Int = el_black_box(1)
        let s = s + bx
        let i = i + 1
    }
    assert s == 100, "black_box does not disturb the computation"
}

test "curve names round-trip" {
    assert elb_curve_from_name("O(n)") == 2, "O(n) parses"
    assert elb_curve_from_name("O(n^2)") == 4, "O(n^2) parses"
    assert str_eq(elb_curve_name(4), "O(n^2)"), "O(n^2) renders"
    assert elb_curve_from_name("O(nonsense)") < 0, "unknown curve is -1"
}
