nx_caplab_supply_test.nx source
↩ module page · 102 lines · 3986 B
1// nx_caplab_supply_test.nx -- gate for Capitalism Lab R2 (production/supply).
2//
3// Proves: (1) unit-cost rollup for 1 and 2 inputs, (2) multi-stage chain
4// rollup (raw -> intermediate -> finished), (3) throughput from stock,
5// (4) the bottleneck input caps throughput, (5) NEG-CONTROL: zero stock of
6// any input -> 0 output, (6) INTEGRATION: a supply-derived unit cost feeds
7// the R0 market profit, and pricier inputs earn less (no magic numbers).
8
9import "nx_syscalls.nx"
10import "nx_runtime.nx"
11import "nx_tier.nx"
12import "nx_caplab_supply.nx"
13import "nx_caplab_market.nx"
14
15func nx_assert_eq(label: *u8, got: nx_int, want: nx_int, pass_n: *nx_int, fail_n: *nx_int) {
16 print(label); print(": got=" as *u8); print_i64(got); print(" want=" as *u8); print_i64(want)
17 if got == want {
18 println(" PASS" as *u8)
19 pass_n[0] = pass_n[0] + 1
20 return
21 }
22 println(" FAIL" as *u8)
23 fail_n[0] = fail_n[0] + 1
24}
25
26func nx_assert_gt(label: *u8, got: nx_int, floor: nx_int, pass_n: *nx_int, fail_n: *nx_int) {
27 print(label); print(": got=" as *u8); print_i64(got); print(" > " as *u8); print_i64(floor)
28 if got > floor {
29 println(" PASS" as *u8)
30 pass_n[0] = pass_n[0] + 1
31 return
32 }
33 println(" FAIL" as *u8)
34 fail_n[0] = fail_n[0] + 1
35}
36
37func main() -> nx_exit {
38 let pass_n: *nx_int = (sys_mmap(8)) as *nx_int
39 let fail_n: *nx_int = (sys_mmap(8)) as *nx_int
40 pass_n[0] = 0
41 fail_n[0] = 0
42
43 println("=== CAPLAB R2: production / supply chains ===" as *u8)
44
45 // --- T1: one input. 1 widget = 2 raw @5 + processing 3 = 13 ---
46 let qp1: *i64 = (sys_mmap(8)) as *i64
47 qp1[0] = 2
48 let pr1: *i64 = (sys_mmap(8)) as *i64
49 pr1[0] = 5
50 let c1: nx_int = nx_clab_unit_cost(1, qp1, pr1, 3)
51 nx_assert_eq("unit_cost 1-input " as *u8, c1, 13, pass_n, fail_n)
52
53 // --- T2: two inputs. 2 raw @5 + 1 labor @3 + processing 3 = 16 ---
54 let qp2: *i64 = (sys_mmap(16)) as *i64
55 qp2[0] = 2
56 qp2[1] = 1
57 let pr2: *i64 = (sys_mmap(16)) as *i64
58 pr2[0] = 5
59 pr2[1] = 3
60 nx_assert_eq("unit_cost 2-input " as *u8, nx_clab_unit_cost(2, qp2, pr2, 3), 16, pass_n, fail_n)
61
62 // --- T3: chain rollup. finished = 3 intermediate @ c1(13) + processing 4 = 43 ---
63 let qpf: *i64 = (sys_mmap(8)) as *i64
64 qpf[0] = 3
65 let prf: *i64 = (sys_mmap(8)) as *i64
66 prf[0] = c1
67 nx_assert_eq("chain rollup raw->fin " as *u8, nx_clab_unit_cost(1, qpf, prf, 4), 43, pass_n, fail_n)
68
69 // --- T4: throughput. 100 raw / 2-per-unit = 50 ---
70 let qt: *i64 = (sys_mmap(8)) as *i64
71 qt[0] = 2
72 let st1: *i64 = (sys_mmap(8)) as *i64
73 st1[0] = 100
74 nx_assert_eq("throughput single " as *u8, nx_clab_throughput(1, qt, st1), 50, pass_n, fail_n)
75
76 // --- T5: bottleneck. raw 100@2 ->50, labor 30@1 ->30, min = 30 ---
77 let qt2: *i64 = (sys_mmap(16)) as *i64
78 qt2[0] = 2
79 qt2[1] = 1
80 let st2: *i64 = (sys_mmap(16)) as *i64
81 st2[0] = 100
82 st2[1] = 30
83 nx_assert_eq("throughput bottleneck " as *u8, nx_clab_throughput(2, qt2, st2), 30, pass_n, fail_n)
84
85 // --- T6: NEG-CONTROL. zero stock of an input -> cannot produce ---
86 let st3: *i64 = (sys_mmap(16)) as *i64
87 st3[0] = 100
88 st3[1] = 0
89 nx_assert_eq("zero stock -> 0 (neg) " as *u8, nx_clab_throughput(2, qt2, st3), 0, pass_n, fail_n)
90
91 // --- T7: INTEGRATION. supply-derived cost (13) feeds R0 profit. ---
92 // a=100,b=2,price=30: profit = (30-13)*demand(30) = 17*40 = 680
93 nx_assert_eq("supply cost -> profit " as *u8, nx_clab_profit(100, 2, 30, c1), 680, pass_n, fail_n)
94 // cheaper inputs (cost 10) out-earn pricier ones (cost 13): 800 > 680
95 nx_assert_gt("cheaper inputs win " as *u8, nx_clab_profit(100, 2, 30, 10), nx_clab_profit(100, 2, 30, 13), pass_n, fail_n)
96
97 println("" as *u8)
98 print("PASS=" as *u8); print_i64(pass_n[0])
99 print(" FAIL=" as *u8); print_i64(fail_n[0]); println("" as *u8)
100 if fail_n[0] > 0 { return 1 }
101 return 0
102}