cheatah
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. Each
4// 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 the
6// exact stdout. Complements the in-process unit tests
7// (stdlib/tests/hashlib_test.cpp) and the per-module system-level test
8// (StdlibE2E.Hashlib).
9#include "e2e_harness.hpp"
11// Standard NIST/FIPS-180 SHA-256 test vector for "abc".
12TEST(HashlibCompileRun, Sha256) {
13 e2e::expect_e2e("hashlib_sha256", R"PURR(import io
14import hashlib
15io.print(hashlib.sha256("abc"))
16)PURR", "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad\n");
19// Standard NIST/FIPS-180 SHA-384 test vector for "abc".
20TEST(HashlibCompileRun, Sha384) {
21 e2e::expect_e2e("hashlib_sha384", R"PURR(import io
22import hashlib
23io.print(hashlib.sha384("abc"))
24)PURR", "cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed"
25 "8086072ba1e7cc2358baeca134c825a7\n");
28// Standard NIST/FIPS-180 SHA-512 test vector for "abc".
29TEST(HashlibCompileRun, Sha512) {
30 e2e::expect_e2e("hashlib_sha512", R"PURR(import io
31import hashlib
32io.print(hashlib.sha512("abc"))
33)PURR", "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a"
34 "2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f\n");
37// The raw digest forms return the bytes (32 / 48 / 64), not hex — assert their length so the
38// program output stays deterministic.
39TEST(HashlibCompileRun, Sha256Digest) {
40 e2e::expect_e2e("hashlib_sha256_digest", R"PURR(import io
41import hashlib
42io.print(len(hashlib.sha256_digest("abc")))
43)PURR", "32\n");
46TEST(HashlibCompileRun, Sha384Digest) {
47 e2e::expect_e2e("hashlib_sha384_digest", R"PURR(import io
48import hashlib
49io.print(len(hashlib.sha384_digest("abc")))
50)PURR", "48\n");
53TEST(HashlibCompileRun, Sha512Digest) {
54 e2e::expect_e2e("hashlib_sha512_digest", R"PURR(import io
55import hashlib
56io.print(len(hashlib.sha512_digest("abc")))
57)PURR", "64\n");