0081-atol.c (709B)
1 #include <assert.h> 2 #include <limits.h> 3 #include <stdio.h> 4 #include <stdlib.h> 5 6 /* 7 output: 8 testing 9 done 10 end: 11 */ 12 13 void 14 test(void) 15 { 16 long n; 17 char buf[64]; 18 19 n = atol("abc"); 20 assert(n == 0); 21 22 n = atol(" abc"); 23 assert(n == 0); 24 25 n = atol("1234 "); 26 assert(n == 1234); 27 28 n = atol(" 1234 0"); 29 assert(n == 1234); 30 31 n = atol("+"); 32 assert(n == 0); 33 34 n = atol(" +1"); 35 assert(n == 1); 36 37 n = atol("+ 1"); 38 assert(n == 0); 39 40 n = atol(" -1"); 41 assert(n == -1); 42 43 n = atol("- 1"); 44 assert(n == 0); 45 46 sprintf(buf, "%ld", LONG_MAX); 47 n = atol(buf); 48 assert(n == LONG_MAX); 49 50 sprintf(buf, "%ld", LONG_MIN); 51 n = atol(buf); 52 assert(n == LONG_MIN); 53 } 54 55 int 56 main() 57 { 58 puts("testing"); 59 test(); 60 puts("done"); 61 62 return 0; 63 }