test_bug12_pa_cal_loop_inverted.c
1 /******************************************************************************* 2 * test_bug12_pa_cal_loop_inverted.c 3 * 4 * Bug #12 (FIXED): PA IDQ calibration loop condition was inverted. 5 * Old: while (DAC_val > 38 && abs(Idq - 1.680) < 0.2) 6 * → loop continued ONLY when CLOSE to target, exited when far away 7 * New: while (DAC_val > 38 && abs(Idq - 1.680) > 0.2) 8 * → loop continues while FAR from target, exits when converged 9 * 10 * Test strategy: 11 * Simulate the loop logic with known Idq values and verify: 12 * 1. Loop continues when Idq is far from 1.680A (e.g., 0.5A) 13 * 2. Loop exits when Idq is within 0.2A of target (e.g., 1.60A) 14 * 3. Loop exits when DAC_val reaches lower bound (38) 15 ******************************************************************************/ 16 #include <assert.h> 17 #include <stdio.h> 18 #include <stdlib.h> 19 #include <math.h> 20 21 /* Extracted calibration loop condition (post-fix) */ 22 static int should_continue_loop(int DAC_val, double Idq_reading) 23 { 24 /* This matches the FIXED condition in main.cpp lines 1854/1874 */ 25 return (DAC_val > 38 && fabs(Idq_reading - 1.680) > 0.2); 26 } 27 28 int main(void) 29 { 30 printf("=== Bug #12 (FIXED): PA calibration loop condition ===\n"); 31 32 /* Test 1: Idq far from target → loop should CONTINUE */ 33 printf(" Test 1: Idq=0.500A (far from 1.680A), DAC=100 → "); 34 assert(should_continue_loop(100, 0.500) == 1); 35 printf("CONTINUE (correct)\n"); 36 37 /* Test 2: Idq within tolerance → loop should EXIT */ 38 printf(" Test 2: Idq=1.600A (within 0.2A of 1.680A), DAC=80 → "); 39 assert(should_continue_loop(80, 1.600) == 0); 40 printf("EXIT (correct)\n"); 41 42 /* Test 3: Idq exactly at target → loop should EXIT */ 43 printf(" Test 3: Idq=1.680A (exactly at target), DAC=60 → "); 44 assert(should_continue_loop(60, 1.680) == 0); 45 printf("EXIT (correct)\n"); 46 47 /* Test 4: DAC at lower bound → loop should EXIT regardless of Idq */ 48 printf(" Test 4: Idq=0.200A (far), DAC=38 → "); 49 assert(should_continue_loop(38, 0.200) == 0); 50 printf("EXIT (DAC limit, correct)\n"); 51 52 /* Test 5: Idq just outside tolerance (0.201 from target) → CONTINUE */ 53 printf(" Test 5: Idq=1.479A (|diff|=0.201), DAC=50 → "); 54 assert(should_continue_loop(50, 1.479) == 1); 55 printf("CONTINUE (correct)\n"); 56 57 /* Test 6: Idq just inside tolerance (0.199 from target) → EXIT */ 58 printf(" Test 6: Idq=1.481A (|diff|=0.199), DAC=50 → "); 59 assert(should_continue_loop(50, 1.481) == 0); 60 printf("EXIT (correct)\n"); 61 62 /* Test 7: Simulate full loop convergence */ 63 printf(" Test 7: Full loop simulation... "); 64 { 65 int DAC_val = 126; 66 int iterations = 0; 67 /* Simulate decreasing DAC → increasing Idq */ 68 while (1) { 69 DAC_val -= 4; 70 iterations++; 71 /* Simulate: Idq = 1.680 - (DAC_val - 50) * 0.02 */ 72 double Idq = 1.680 - (DAC_val - 50) * 0.02; 73 if (!should_continue_loop(DAC_val, Idq)) { 74 printf("converged at DAC=%d Idq=%.3fA after %d iterations", 75 DAC_val, Idq, iterations); 76 /* Should converge somewhere around DAC=50-60 */ 77 assert(iterations < 50); /* safety counter limit */ 78 assert(fabs(Idq - 1.680) <= 0.2); /* should be in tolerance */ 79 break; 80 } 81 assert(iterations < 100); /* prevent infinite loop in test */ 82 } 83 printf(" → PASS\n"); 84 } 85 86 printf("\n=== Bug #12: ALL TESTS PASSED (post-fix) ===\n\n"); 87 return 0; 88 }