summaryrefslogtreecommitdiff
path: root/vere/pkg/vere/mug_test.c
blob: 19bad898ea086dc28f108ed70ad1c4548c397587 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
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;
}