Source
tests/purrc/statistics_cr_test.cpp
1
// Copyright (c) 2026 BigBrain LLC. MIT-licensed (see LICENSE).2
// Original work; see ACKNOWLEDGMENTS.md for the open-source ideas we build upon.3
// Compile-run unit tests for the `statistics` module: one test per function.4
// Each writes a tiny .purr that calls a single statistics function over a fixed5
// list, compiles it with purrc, runs it under the cheatah runtime, and asserts6
// the exact stdout. Complements the in-process unit tests7
// (stdlib/tests/statistics_test.cpp) and the per-module system-level test8
// (StdlibE2E.Statistics).9
#include "e2e_harness.hpp"11
// Fixed dataset {2,4,4,4,5,5,7,9}: sum 40, count 8, mean 5, pvariance 4,12
// pstdev 2. Sample stats use {1,2,3,4,5}: variance 2.5, stdev sqrt(2.5),13
// median 3.15
TEST(StatisticsCompileRun, Sum) {16
e2e::expect_e2e("statistics_sum", R"PURR(import io17
import statistics18
let xs = [2.0, 4.0, 4.0, 4.0, 5.0, 5.0, 7.0, 9.0]19
io.print(statistics.sum(xs))20
)PURR", "40\n");21
}23
TEST(StatisticsCompileRun, Count) {24
e2e::expect_e2e("statistics_count", R"PURR(import io25
import statistics26
let xs = [2.0, 4.0, 4.0, 4.0, 5.0, 5.0, 7.0, 9.0]27
io.print(statistics.count(xs))28
)PURR", "8\n");29
}31
TEST(StatisticsCompileRun, Mean) {32
e2e::expect_e2e("statistics_mean", R"PURR(import io33
import statistics34
let xs = [2.0, 4.0, 4.0, 4.0, 5.0, 5.0, 7.0, 9.0]35
io.print(statistics.mean(xs))36
)PURR", "5\n");37
}39
TEST(StatisticsCompileRun, Pvariance) {40
e2e::expect_e2e("statistics_pvariance", R"PURR(import io41
import statistics42
let xs = [2.0, 4.0, 4.0, 4.0, 5.0, 5.0, 7.0, 9.0]43
io.print(statistics.pvariance(xs))44
)PURR", "4\n");45
}47
TEST(StatisticsCompileRun, Pstdev) {48
e2e::expect_e2e("statistics_pstdev", R"PURR(import io49
import statistics50
let xs = [2.0, 4.0, 4.0, 4.0, 5.0, 5.0, 7.0, 9.0]51
io.print(statistics.pstdev(xs))52
)PURR", "2\n");53
}55
TEST(StatisticsCompileRun, Variance) {56
e2e::expect_e2e("statistics_variance", R"PURR(import io57
import statistics58
let xs = [1.0, 2.0, 3.0, 4.0, 5.0]59
io.print(statistics.variance(xs))60
)PURR", "2.5\n");61
}63
TEST(StatisticsCompileRun, Stdev) {64
e2e::expect_e2e("statistics_stdev", R"PURR(import io65
import statistics66
let xs = [1.0, 2.0, 3.0, 4.0, 5.0]67
io.print(statistics.stdev(xs))68
)PURR", "1.58114\n");69
}71
TEST(StatisticsCompileRun, Median) {72
e2e::expect_e2e("statistics_median", R"PURR(import io73
import statistics74
let xs = [1.0, 2.0, 3.0, 4.0, 5.0]75
io.print(statistics.median(xs))76
)PURR", "3\n");77
}