Itβs good that you included the test structure, but your test structure does not have rigor and abuses the principle of DRY (Do not Repeat Yourself).
static const struct test_case { int inval; int rcode; int rtemp; } test[] = { { 317, 1, 95 }, { 595, 1, -10 }, { 900, 0, 0 },
Now you can write test code for one test in a function:
static int test_one(int testnum, const struct test_case *test) { int result = 0; int temp; int code = lookup_temp(test->inval, temp_ADC, TEMP_ADC_TABLE_SIZE, &temp); if (temp == test->rtemp && code == test->rcode) printf("PASS %d: reading %d, code %d, temperature %d\n", testnum, test->inval, code, temp); else { printf("FAIL %d: reading %d, code (got %d, wanted %d), " "temperature (got %d, wanted %d)\n", testnum, test->inval, code, test->rcode, temp, test->rtemp); result = 1; } }
And then the main program can have a loop that controls the test function:
#define DIM(x) (sizeof(x)/sizeof(*(x))) int failures = 0; int i; for (i = 0; i < DIM(test); i++) failures += test_one(i + 1, &test[i]); if (failures != 0) printf("!! FAIL !! (%d of %d tests failed)\n", failures, (int)DIM(test)); else printf("== PASS == (%d tests passed)\n", (int)DIM(test));
Now, if there is a problem with any of the tests, it will be difficult to excuse if you do not identify the problem. With your source code, someone might ignore the error.
Obviously, if you want to get comments about the tests, you can add const char *tag to the array and provide and print those tags. If you really want to get fancy, you can even encode null pointer tests (for example, to enable these) into mode by including the corresponding initialized pointers in the array - you can add a couple of bit flags for "table is null" and "the temperature pointer is zero "and conditional code in function.