nx_qc_spc_test.nx source
↩ module page · 44 lines · 1958 B
1// nx_qc_spc_test.nx -- smoke for nx_qc_spc. Proves Shewhart X-bar/R control
2// limits, in/out-of-control detection, Cp/Cpk capability, proximate-analysis
3// conservation, and Kjeldahl protein. Exit code = failed assertion #.
4
5import "nx_syscalls.nx"
6import "nx_qc_spc.nx"
7
8func main() -> i64 {
9 // --- Shewhart constants ---
10 if qc_d2(5) != 2326 { return 1 }
11 if qc_a2(5) != 577 { return 2 }
12 if qc_d4(5) != 2114 { return 3 }
13
14 // --- X-bar control limits (Xbarbar=100, Rbar=5, n=5) ---
15 let ucl: i64 = qc_xbar_ucl_milli(100, 577, 5) // 100000 + 2885
16 let lcl: i64 = qc_xbar_lcl_milli(100, 577, 5) // 100000 - 2885
17 if ucl != 102885 { return 4 }
18 if lcl != 97115 { return 5 }
19
20 // --- in / out of control ---
21 if qc_in_control(101, ucl, lcl) != 1 { return 6 } // 101.000 inside
22 if qc_in_control(103, ucl, lcl) != 0 { return 7 } // 103.000 above UCL
23 if qc_in_control(97, ucl, lcl) != 0 { return 8 } // 97.000 below LCL
24
25 // --- R chart upper limit ---
26 if qc_r_ucl_milli(2114, 5) != 10570 { return 9 } // 10.570
27
28 // --- process capability (USL 110, LSL 90, Rbar 5, n=5) ---
29 if qc_cp_milli(110, 90, 2326, 5) != 1550 { return 10 } // Cp ~ 1.55
30 if qc_cpk_milli(110, 90, 100, 2326, 5) != 1550 { return 11 } // centered: Cpk == Cp
31 if qc_cpk_milli(110, 90, 104, 2326, 5) != 930 { return 12 } // off-center: Cpk ~ 0.93
32 if qc_is_capable(1550) != 1 { return 13 }
33 if qc_is_capable(930) != 0 { return 14 } // not capable (< 1.33)
34
35 // --- proximate analysis (must conserve to 100%) ---
36 if qc_carb_by_difference(70, 10, 5, 2) != 13 { return 15 }
37 if qc_proximate_conserves(70, 10, 5, 2, 13) != 1 { return 16 }
38 if qc_proximate_conserves(70, 10, 5, 2, 10) != 0 { return 17 } // sums to 97, rejected
39
40 // --- Kjeldahl protein (nitrogen 2.0% -> 12.5% crude protein) ---
41 if qc_protein_from_nitrogen(20) != 125 { return 18 }
42
43 return 0
44}