code wiki / (root) / nx_caplab_compete_test.nx

nx_caplab_compete_test.nx source

↩ module page · 112 lines · 5043 B

1// nx_caplab_compete_test.nx -- gate for Capitalism Lab R1 (competition). 2// 3// Proves: (1) symmetric firms split the market evenly, (2) the cheaper firm 4// wins a larger share, (3) CONSERVATION -- firm quantities always sum to the 5// market size (largest-remainder), even when the split is uneven, (4) 6// NEG-CONTROL: a firm priced at/above the choke sells zero, (5) NEG-CONTROL: 7// if every firm is priced out, nobody buys, (6) margin x volume -- the 8// higher-priced firm can still out-earn the cheaper one. 9 10import "nx_syscalls.nx" 11import "nx_runtime.nx" 12import "nx_tier.nx" 13import "nx_caplab_compete.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 let choke: nx_int = 100 44 45 println("=== CAPLAB R1: multi-firm price competition ===" as *u8) 46 47 // --- T1: symmetric duopoly splits evenly (M=120) --- 48 let pa: *i64 = (sys_mmap(2 * NX_SIZEOF_NX_INT)) as *i64 49 pa[0] = 50 50 pa[1] = 50 51 let qa: *i64 = (sys_mmap(2 * NX_SIZEOF_NX_INT)) as *i64 52 nx_clab_split(2, pa, choke, 120, qa) 53 nx_assert_eq("sym: q0 " as *u8, qa[0], 60, pass_n, fail_n) 54 nx_assert_eq("sym: q1 " as *u8, qa[1], 60, pass_n, fail_n) 55 nx_assert_eq("sym: sum == M " as *u8, qa[0] + qa[1], 120, pass_n, fail_n) 56 57 // --- T2: cheaper firm wins more share (40 vs 60) --- 58 let pb: *i64 = (sys_mmap(2 * NX_SIZEOF_NX_INT)) as *i64 59 pb[0] = 40 60 pb[1] = 60 61 let qb: *i64 = (sys_mmap(2 * NX_SIZEOF_NX_INT)) as *i64 62 nx_clab_split(2, pb, choke, 120, qb) 63 nx_assert_eq("cheap: q0 " as *u8, qb[0], 72, pass_n, fail_n) 64 nx_assert_eq("cheap: q1 " as *u8, qb[1], 48, pass_n, fail_n) 65 nx_assert_gt("cheap wins more share " as *u8, qb[0], qb[1], pass_n, fail_n) 66 nx_assert_eq("cheap: sum == M " as *u8, qb[0] + qb[1], 120, pass_n, fail_n) 67 68 // --- T3: NEG-CONTROL one firm priced at choke sells zero --- 69 let pc: *i64 = (sys_mmap(2 * NX_SIZEOF_NX_INT)) as *i64 70 pc[0] = 40 71 pc[1] = 100 72 let qc: *i64 = (sys_mmap(2 * NX_SIZEOF_NX_INT)) as *i64 73 nx_clab_split(2, pc, choke, 120, qc) 74 nx_assert_eq("pricedout: rival q1=0 " as *u8, qc[1], 0, pass_n, fail_n) 75 nx_assert_eq("pricedout: q0 gets all " as *u8, qc[0], 120, pass_n, fail_n) 76 77 // --- T4: NEG-CONTROL everyone priced out -> nobody buys --- 78 let pd: *i64 = (sys_mmap(2 * NX_SIZEOF_NX_INT)) as *i64 79 pd[0] = 100 80 pd[1] = 100 81 let qd: *i64 = (sys_mmap(2 * NX_SIZEOF_NX_INT)) as *i64 82 nx_clab_split(2, pd, choke, 120, qd) 83 nx_assert_eq("all out: q0 " as *u8, qd[0], 0, pass_n, fail_n) 84 nx_assert_eq("all out: q1 " as *u8, qd[1], 0, pass_n, fail_n) 85 86 // --- T5: 3-firm uneven split conserves total (M=100) --- 87 // appeals [60,50,40] sum 150 -> floor [40,33,26]+leftover1 -> [40,33,27] 88 let pe: *i64 = (sys_mmap(3 * NX_SIZEOF_NX_INT)) as *i64 89 pe[0] = 40 90 pe[1] = 50 91 pe[2] = 60 92 let qe: *i64 = (sys_mmap(3 * NX_SIZEOF_NX_INT)) as *i64 93 nx_clab_split(3, pe, choke, 100, qe) 94 nx_assert_eq("trio: q0 " as *u8, qe[0], 40, pass_n, fail_n) 95 nx_assert_eq("trio: q1 " as *u8, qe[1], 33, pass_n, fail_n) 96 nx_assert_eq("trio: q2 " as *u8, qe[2], 27, pass_n, fail_n) 97 nx_assert_gt("trio mono: q0>q1 " as *u8, qe[0], qe[1], pass_n, fail_n) 98 nx_assert_gt("trio mono: q1>q2 " as *u8, qe[1], qe[2], pass_n, fail_n) 99 nx_assert_eq("trio: sum == M (conserv)" as *u8, qe[0] + qe[1] + qe[2], 100, pass_n, fail_n) 100 101 // --- T6: margin x volume -- pricier firm out-earns the cheaper one --- 102 // T2 quantities [72,48], unit_cost 20: profit0=20*72=1440 profit1=40*48=1920 103 nx_assert_eq("profit cheap (40) " as *u8, nx_clab_firm_profit(40, 20, qb[0]), 1440, pass_n, fail_n) 104 nx_assert_eq("profit dear (60) " as *u8, nx_clab_firm_profit(60, 20, qb[1]), 1920, pass_n, fail_n) 105 nx_assert_gt("dear out-earns cheap " as *u8, nx_clab_firm_profit(60, 20, qb[1]), nx_clab_firm_profit(40, 20, qb[0]), pass_n, fail_n) 106 107 println("" as *u8) 108 print("PASS=" as *u8); print_i64(pass_n[0]) 109 print(" FAIL=" as *u8); print_i64(fail_n[0]); println("" as *u8) 110 if fail_n[0] > 0 { return 1 } 111 return 0 112}