My main function in post #7 is a very simple example of how you might write unit tests to test your function. You don't get much information out of running the tests other than whether they passed or failed. Here's a slightly more sophisticated version:
Code:
void test_htoi(char input[], int expected_output) {
int actual_output = htoi(input);
if (actual_output == expected_output) {
printf("htoi(\"%s\") passed\n", input);
} else {
printf("htoi(\"%s\") failed: expected=%d; actual=%d \n",
input, expected_output,
actual_output);
}
}
int main(void) {
test_htoi("0", 0);
test_htoi("1", 1);
test_htoi("9", 9);
test_htoi("a", 10);
test_htoi("f", 15);
test_htoi("A", 10);
test_htoi("F", 15);
test_htoi("10", 16);
test_htoi("1d", 29);
test_htoi("e1", 225);
test_htoi("abc", 2748);
test_htoi("a2b", 2603);
test_htoi("5C7", 1479);
test_htoi("a1b2", 41394);
return 0;
}
If we run this with your code from post #1, we get:
Code:
htoi("0") passed
htoi("1") failed: expected=1; actual=0
htoi("9") failed: expected=9; actual=0
htoi("a") failed: expected=10; actual=0
htoi("f") failed: expected=15; actual=0
htoi("A") failed: expected=10; actual=0
htoi("F") failed: expected=15; actual=0
htoi("10") failed: expected=16; actual=0
htoi("1d") failed: expected=29; actual=0
htoi("e1") failed: expected=225; actual=0
htoi("abc") failed: expected=2748; actual=0
htoi("a2b") failed: expected=2603; actual=0
htoi("5C7") failed: expected=1479; actual=0
htoi("a1b2") failed: expected=41394; actual=0
and now you can see clearly that the failures are due to the actual value always being 0, which gives you a hint that you are always returning 0.
With your code from post #5:
Code:
htoi("0") passed
htoi("1") passed
htoi("9") passed
htoi("a") passed
htoi("f") passed
htoi("A") passed
htoi("F") passed
htoi("10") failed: expected=16; actual=1
htoi("1d") failed: expected=29; actual=1
htoi("e1") failed: expected=225; actual=14
htoi("abc") failed: expected=2748; actual=10
htoi("a2b") failed: expected=2603; actual=10
htoi("5C7") failed: expected=1479; actual=5
htoi("a1b2") failed: expected=41394; actual=10
You can see that the actual values are fairly low compared to the expected values, so it is a hint that you are not handling enough of the input string. This is confirmed when you look a bit more closely and see that the actual values correspond to the first hex digit of each of the input strings.