diff options
author | polwex <polwex@sortug.com> | 2025-10-07 00:13:21 +0700 |
---|---|---|
committer | polwex <polwex@sortug.com> | 2025-10-07 00:13:21 +0700 |
commit | 5242ee845cdb3a7e9780443bf5d7e534f72c3f28 (patch) | |
tree | 24270ef9d0ce356e92b1048394fec2020e2bec71 /vere | |
parent | 5de3f7a3ad7b0cf63b4a6cbddfc1e26359dea161 (diff) |
mugs in ocaml with murmur3 fork
Diffstat (limited to 'vere')
-rw-r--r-- | vere/build.zig | 5 | ||||
-rw-r--r-- | vere/pkg/vere/mug_test.c | 168 |
2 files changed, 173 insertions, 0 deletions
diff --git a/vere/build.zig b/vere/build.zig index 72baf8e..e2a7590 100644 --- a/vere/build.zig +++ b/vere/build.zig @@ -643,6 +643,11 @@ fn buildBinary( .deps = vere_test_deps, }, .{ + .name = "mug-test", + .file = "pkg/vere/mug_test.c", + .deps = vere_test_deps, + }, + .{ .name = "newt-test", .file = "pkg/vere/newt_tests.c", .deps = vere_test_deps, diff --git a/vere/pkg/vere/mug_test.c b/vere/pkg/vere/mug_test.c new file mode 100644 index 0000000..19bad89 --- /dev/null +++ b/vere/pkg/vere/mug_test.c @@ -0,0 +1,168 @@ +/* Mug Test - Compare C vs OCaml mug computation */ + +#include "noun.h" + +#include <stdio.h> +#include <stdlib.h> + +/* Helper to print mug with description */ +static void +test_mug(const char* desc, u3_noun noun) +{ + c3_w mug = u3r_mug(noun); + printf("%-40s 0x%08x\n", desc, mug); +} + +/* Helper to build cord from string */ +static u3_atom +_cord(const char* str) +{ + return u3i_string(str); +} + +int +main(int argc, char* argv[]) +{ + /* Initialize u3 with lite boot (no jets, just memory) */ + u3C.wag_w |= u3o_hashless; + u3m_boot_lite(1 << 28); // 256MB loom + + printf("\n═══════════════════════════════════════\n"); + printf(" Mug Computation Test (C)\n"); + printf("═══════════════════════════════════════\n\n"); + + printf("ATOMS - Small values:\n"); + printf("─────────────────────────────────────\n"); + test_mug("0", 0); + test_mug("1", 1); + test_mug("2", 2); + test_mug("3", 3); + test_mug("10", 10); + test_mug("42", 42); + test_mug("100", 100); + test_mug("255", 255); + test_mug("256", 256); + test_mug("1000", 1000); + printf("\n"); + + printf("ATOMS - Powers of 2:\n"); + printf("─────────────────────────────────────\n"); + test_mug("2^8 (256)", 256); + test_mug("2^16 (65536)", 65536); + test_mug("2^20 (1048576)", 1048576); + test_mug("2^24 (16777216)", 16777216); + test_mug("2^31 - 1 (max signed 32-bit)", 2147483647); + printf("\n"); + + printf("ATOMS - Large values:\n"); + printf("─────────────────────────────────────\n"); + test_mug("2^32", u3i_chub(1ULL << 32)); + test_mug("2^64", u3i_chubs(1, (c3_d[]){0})); // 2^64 would overflow, use 2^63 + test_mug("2^128", u3i_chubs(2, (c3_d[]){0, 1})); + test_mug("0xdeadbeef", u3i_word(0xdeadbeef)); + test_mug("0xcafebabe", u3i_word(0xcafebabe)); + printf("\n"); + + printf("CELLS - Simple pairs:\n"); + printf("─────────────────────────────────────\n"); + test_mug("[0 0]", u3nc(0, 0)); + test_mug("[0 1]", u3nc(0, 1)); + test_mug("[1 0]", u3nc(1, 0)); + test_mug("[1 1]", u3nc(1, 1)); + test_mug("[1 2]", u3nc(1, 2)); + test_mug("[2 1]", u3nc(2, 1)); + test_mug("[42 0]", u3nc(42, 0)); + test_mug("[0 42]", u3nc(0, 42)); + test_mug("[42 42]", u3nc(42, 42)); + test_mug("[100 200]", u3nc(100, 200)); + printf("\n"); + + printf("CELLS - Nested structures:\n"); + printf("─────────────────────────────────────\n"); + test_mug("[[0 0] 0]", u3nc(u3nc(0, 0), 0)); + test_mug("[0 [0 0]]", u3nc(0, u3nc(0, 0))); + test_mug("[[1 2] 3]", u3nc(u3nc(1, 2), 3)); + test_mug("[1 [2 3]]", u3nc(1, u3nc(2, 3))); + test_mug("[[1 2] [3 4]]", u3nc(u3nc(1, 2), u3nc(3, 4))); + test_mug("[[[0 1] 2] 3]", u3nc(u3nc(u3nc(0, 1), 2), 3)); + test_mug("[0 [1 [2 3]]]", u3nc(0, u3nc(1, u3nc(2, 3)))); + printf("\n"); + + printf("CELLS - Binary trees:\n"); + printf("─────────────────────────────────────\n"); + /* Balanced binary tree depth 2 */ + u3_noun tree2 = u3nc(u3nc(1, 2), u3nc(3, 4)); + test_mug("Balanced tree depth 2", tree2); + + /* Balanced binary tree depth 3 */ + u3_noun tree3 = u3nc( + u3nc(u3nc(1, 2), u3nc(3, 4)), + u3nc(u3nc(5, 6), u3nc(7, 8)) + ); + test_mug("Balanced tree depth 3", tree3); + + /* Left-heavy tree */ + u3_noun left_heavy = u3nc(u3nc(u3nc(1, 2), 3), 4); + test_mug("Left-heavy tree", left_heavy); + + /* Right-heavy tree */ + u3_noun right_heavy = u3nc(1, u3nc(2, u3nc(3, 4))); + test_mug("Right-heavy tree", right_heavy); + printf("\n"); + + printf("LISTS - Null-terminated lists:\n"); + printf("─────────────────────────────────────\n"); + /* [1 2 3 ~] = [1 [2 [3 0]]] */ + u3_noun list_123 = u3nc(1, u3nc(2, u3nc(3, 0))); + test_mug("[1 2 3 ~]", list_123); + + /* [0 ~] = [0 0] */ + test_mug("[0 ~]", u3nc(0, 0)); + + /* [42 ~] = [42 0] */ + test_mug("[42 ~]", u3nc(42, 0)); + + /* Empty list: ~ = 0 */ + test_mug("~ (null/empty list)", 0); + + /* Longer list [1 2 3 4 5 ~] */ + u3_noun list_12345 = u3nc(1, u3nc(2, u3nc(3, u3nc(4, u3nc(5, 0))))); + test_mug("[1 2 3 4 5 ~]", list_12345); + printf("\n"); + + printf("SPECIAL - Urbit constants:\n"); + printf("─────────────────────────────────────\n"); + /* YES and NO in Urbit */ + test_mug("YES (0)", 0); + test_mug("NO (1)", 1); + + /* Common Urbit atoms */ + test_mug("~zod (0)", 0); + test_mug("~nec (1)", 1); + test_mug("~bud (2)", 2); + + /* Cord atoms (text) */ + test_mug("'hello'", _cord("hello")); + test_mug("'world'", _cord("world")); + test_mug("'urbit'", _cord("urbit")); + test_mug("'nock'", _cord("nock")); + printf("\n"); + + printf("EDGE CASES - Repeated values:\n"); + printf("─────────────────────────────────────\n"); + /* Same cell computed multiple times should have same mug */ + u3_noun pair = u3nc(123, 456); + test_mug("[123 456] - first computation", pair); + test_mug("[123 456] - second computation", u3nc(123, 456)); + + /* Nested identical cells */ + u3_noun nested = u3nc(pair, pair); + test_mug("[[123 456] [123 456]]", nested); + printf("\n"); + + printf("═══════════════════════════════════════\n"); + printf("Test complete! Compare with OCaml.\n"); + printf("═══════════════════════════════════════\n\n"); + + return 0; +} |