cheatah
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 fixed
5// list, compiles it with purrc, runs it under the cheatah runtime, and asserts
6// the exact stdout. Complements the in-process unit tests
7// (stdlib/tests/statistics_test.cpp) and the per-module system-level test
8// (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.
15TEST(StatisticsCompileRun, Sum) {
16 e2e::expect_e2e("statistics_sum", R"PURR(import io
17import statistics
18let xs = [2.0, 4.0, 4.0, 4.0, 5.0, 5.0, 7.0, 9.0]
19io.print(statistics.sum(xs))
20)PURR", "40\n");
23TEST(StatisticsCompileRun, Count) {
24 e2e::expect_e2e("statistics_count", R"PURR(import io
25import statistics
26let xs = [2.0, 4.0, 4.0, 4.0, 5.0, 5.0, 7.0, 9.0]
27io.print(statistics.count(xs))
28)PURR", "8\n");
31TEST(StatisticsCompileRun, Mean) {
32 e2e::expect_e2e("statistics_mean", R"PURR(import io
33import statistics
34let xs = [2.0, 4.0, 4.0, 4.0, 5.0, 5.0, 7.0, 9.0]
35io.print(statistics.mean(xs))
36)PURR", "5\n");
39TEST(StatisticsCompileRun, Pvariance) {
40 e2e::expect_e2e("statistics_pvariance", R"PURR(import io
41import statistics
42let xs = [2.0, 4.0, 4.0, 4.0, 5.0, 5.0, 7.0, 9.0]
43io.print(statistics.pvariance(xs))
44)PURR", "4\n");
47TEST(StatisticsCompileRun, Pstdev) {
48 e2e::expect_e2e("statistics_pstdev", R"PURR(import io
49import statistics
50let xs = [2.0, 4.0, 4.0, 4.0, 5.0, 5.0, 7.0, 9.0]
51io.print(statistics.pstdev(xs))
52)PURR", "2\n");
55TEST(StatisticsCompileRun, Variance) {
56 e2e::expect_e2e("statistics_variance", R"PURR(import io
57import statistics
58let xs = [1.0, 2.0, 3.0, 4.0, 5.0]
59io.print(statistics.variance(xs))
60)PURR", "2.5\n");
63TEST(StatisticsCompileRun, Stdev) {
64 e2e::expect_e2e("statistics_stdev", R"PURR(import io
65import statistics
66let xs = [1.0, 2.0, 3.0, 4.0, 5.0]
67io.print(statistics.stdev(xs))
68)PURR", "1.58114\n");
71TEST(StatisticsCompileRun, Median) {
72 e2e::expect_e2e("statistics_median", R"PURR(import io
73import statistics
74let xs = [1.0, 2.0, 3.0, 4.0, 5.0]
75io.print(statistics.median(xs))
76)PURR", "3\n");