// a sample test function to call test_str_to_long_int // void test_str_to_long_int() { char *endptr; // this is for strtol int base; // test for a range of base, including an invalid base 37 // here are some suggestions for additional test, exceeding LONG_MAX, or // going below LONG_MIN, and many others for (base = 37; base < 38; base++) { // before you call the function str_to_long_int // set errno to zero errno = 0; long int test = str_to_long_int(" -234agh", base); // print errno // typically, you want to check errno when the returned value // is LONG_MAX or LONG_MIN in a more useful program printf("%ld %d\n", test, errno); // call strtol to cross check the results // note that the results should not match when base == 16. // before you call the function strtol // set errno to zero errno = 0; test = strtol(" -234agh", &endptr, base); printf("%ld :%s %d\n", test, endptr, errno); } }
// a sample test function to call test_str_to_long_int // void test_str_to_long_int() { char *endptr; // this is for strtol int base; // test for a range of base, including an invalid base 37 // here are some suggestions for additional test, exceeding LONG_MAX, or // going below LONG_MIN, and many others for (base = 2; base < 38; base++) { // before you call the function str_to_long_int // set errno to zero errno = 0; int test_bin; long int test = str_to_long_int(" 8000000000000", base); // print errno // typically, you want to check errno when the returned value // is LONG_MAX or LONG_MIN in a more useful program long int test_temp = test; //printf("Program:\n"); //printf("Test: %ld\nErrno: %d\nBase: %d\n\n", test, errno, base); // call strtol to cross check the results // note that the results should not match when base == 16. // before you call the function strtol // set errno to zero errno = 0; test = strtol(" 8000000000000", &endptr, base); //printf("Expected:\n"); //printf("Answer: Endptr: %ld :%s\nErrno: %d\nBase: %d\n\n", test, endptr, errno, base); if (test_temp == test) { test_bin = 1; } else { test_bin = 0; } printf("Base: %d\nSuccess? %d\n\n", base, test_bin); } }