code wiki / (root) / nx_caplab_market_test.nx

nx_caplab_market_test.nx source

↩ module page · 83 lines · 3843 B

1// nx_caplab_market_test.nx -- gate for Capitalism Lab R0 (the market atom). 2// 3// Proves, bit-exact: (1) the linear demand curve, (2) revenue/profit, 4// (3) NEG-CONTROL that pricing below cost loses money, (4) NEG-CONTROL 5// that no units sell past the choke price, (5) the optimal-pricing solver 6// finds the TRUE monopoly peak (matches the closed form AND is a strict 7// local max, not an endpoint artifact). 8// 9// Market under test: a=100 (max demand), b=2 (slope), unit_cost=10. 10// Closed-form monopoly price p* = (a + b*c)/(2b) = 120/4 = 30, profit 800. 11 12import "nx_syscalls.nx" 13import "nx_runtime.nx" 14import "nx_tier.nx" 15import "nx_caplab_market.nx" 16 17func nx_assert_eq(label: *u8, got: nx_int, want: nx_int, pass_n: *nx_int, fail_n: *nx_int) { 18 print(label); print(": got=" as *u8); print_i64(got); print(" want=" as *u8); print_i64(want) 19 if got == want { 20 println(" PASS" as *u8) 21 pass_n[0] = pass_n[0] + 1 22 return 23 } 24 println(" FAIL" as *u8) 25 fail_n[0] = fail_n[0] + 1 26} 27 28// Strict greater-than, for proving the profit peak beats its neighbours. 29func nx_assert_gt(label: *u8, got: nx_int, floor: nx_int, pass_n: *nx_int, fail_n: *nx_int) { 30 print(label); print(": got=" as *u8); print_i64(got); print(" > " as *u8); print_i64(floor) 31 if got > floor { 32 println(" PASS" as *u8) 33 pass_n[0] = pass_n[0] + 1 34 return 35 } 36 println(" FAIL" as *u8) 37 fail_n[0] = fail_n[0] + 1 38} 39 40func main() -> nx_exit { 41 let pass_n: *nx_int = (sys_mmap(8)) as *nx_int 42 let fail_n: *nx_int = (sys_mmap(8)) as *nx_int 43 pass_n[0] = 0 44 fail_n[0] = 0 45 46 let a: nx_int = 100 47 let b: nx_int = 2 48 let c: nx_int = 10 49 50 println("=== CAPLAB R0: single-firm linear market ===" as *u8) 51 52 // --- Demand curve (known-answer) --- 53 nx_assert_eq("demand(p=0) " as *u8, nx_clab_demand(a, b, 0), 100, pass_n, fail_n) 54 nx_assert_eq("demand(p=20) " as *u8, nx_clab_demand(a, b, 20), 60, pass_n, fail_n) 55 nx_assert_eq("demand(p=50) at choke " as *u8, nx_clab_demand(a, b, 50), 0, pass_n, fail_n) 56 nx_assert_eq("demand(p=60) clamp>=0 " as *u8, nx_clab_demand(a, b, 60), 0, pass_n, fail_n) 57 58 // --- Revenue / profit (known-answer) --- 59 nx_assert_eq("revenue(p=20) " as *u8, nx_clab_revenue(a, b, 20), 1200, pass_n, fail_n) 60 nx_assert_eq("profit(p=20) " as *u8, nx_clab_profit(a, b, 20, c), 600, pass_n, fail_n) 61 nx_assert_eq("profit(p=30) peak " as *u8, nx_clab_profit(a, b, 30, c), 800, pass_n, fail_n) 62 63 // --- NEG-CONTROL: pricing below unit cost loses money --- 64 nx_assert_eq("profit(p=5) below cost " as *u8, nx_clab_profit(a, b, 5, c), -450, pass_n, fail_n) 65 nx_assert_eq("profit(p=0) giving away" as *u8, nx_clab_profit(a, b, 0, c), -1000, pass_n, fail_n) 66 67 // --- NEG-CONTROL: no units sell past the choke price --- 68 nx_assert_eq("revenue(p=60) choked " as *u8, nx_clab_revenue(a, b, 60), 0, pass_n, fail_n) 69 nx_assert_eq("profit(p=60) choked " as *u8, nx_clab_profit(a, b, 60, c), 0, pass_n, fail_n) 70 71 // --- Optimal-pricing solver finds the true monopoly peak --- 72 let opt: nx_int = nx_clab_optimal_price(a, b, c) 73 nx_assert_eq("optimal_price " as *u8, opt, 30, pass_n, fail_n) 74 nx_assert_eq("opt == closed-form " as *u8, opt, (a + b * c) / (2 * b), pass_n, fail_n) 75 nx_assert_gt("peak > left (30 vs 29)" as *u8, nx_clab_profit(a, b, 30, c), nx_clab_profit(a, b, 29, c), pass_n, fail_n) 76 nx_assert_gt("peak > right (30 vs 31)" as *u8, nx_clab_profit(a, b, 30, c), nx_clab_profit(a, b, 31, c), pass_n, fail_n) 77 78 println("" as *u8) 79 print("PASS=" as *u8); print_i64(pass_n[0]) 80 print(" FAIL=" as *u8); print_i64(fail_n[0]); println("" as *u8) 81 if fail_n[0] > 0 { return 1 } 82 return 0 83}