Source
tests/purrc/hashlib_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 `hashlib` module: one test per function. Each4
// writes a tiny .purr that calls a single hashlib function on a known input,5
// compiles it with purrc, runs it under the cheatah runtime, and asserts the6
// exact stdout. Complements the in-process unit tests7
// (stdlib/tests/hashlib_test.cpp) and the per-module system-level test8
// (StdlibE2E.Hashlib).9
#include "e2e_harness.hpp"11
// Standard NIST/FIPS-180 SHA-256 test vector for "abc".12
TEST(HashlibCompileRun, Sha256) {13
e2e::expect_e2e("hashlib_sha256", R"PURR(import io14
import hashlib15
io.print(hashlib.sha256("abc"))16
)PURR", "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad\n");17
}19
// Standard NIST/FIPS-180 SHA-384 test vector for "abc".20
TEST(HashlibCompileRun, Sha384) {21
e2e::expect_e2e("hashlib_sha384", R"PURR(import io22
import hashlib23
io.print(hashlib.sha384("abc"))24
)PURR", "cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed"25
"8086072ba1e7cc2358baeca134c825a7\n");26
}28
// Standard NIST/FIPS-180 SHA-512 test vector for "abc".29
TEST(HashlibCompileRun, Sha512) {30
e2e::expect_e2e("hashlib_sha512", R"PURR(import io31
import hashlib32
io.print(hashlib.sha512("abc"))33
)PURR", "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a"34
"2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f\n");35
}37
// The raw digest forms return the bytes (32 / 48 / 64), not hex — assert their length so the38
// program output stays deterministic.39
TEST(HashlibCompileRun, Sha256Digest) {40
e2e::expect_e2e("hashlib_sha256_digest", R"PURR(import io41
import hashlib42
io.print(len(hashlib.sha256_digest("abc")))43
)PURR", "32\n");44
}46
TEST(HashlibCompileRun, Sha384Digest) {47
e2e::expect_e2e("hashlib_sha384_digest", R"PURR(import io48
import hashlib49
io.print(len(hashlib.sha384_digest("abc")))50
)PURR", "48\n");51
}53
TEST(HashlibCompileRun, Sha512Digest) {54
e2e::expect_e2e("hashlib_sha512_digest", R"PURR(import io55
import hashlib56
io.print(len(hashlib.sha512_digest("abc")))57
)PURR", "64\n");58
}