scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | README | LICENSE

0080-atoi.c (702B)


      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 	int n;
     17 	char buf[64];
     18 
     19 	n = atoi("abc");
     20 	assert(n == 0);
     21 
     22 	n = atoi(" abc");
     23 	assert(n == 0);
     24 
     25 	n = atoi("1234 ");
     26 	assert(n == 1234);
     27 
     28 	n = atoi(" 1234 0");
     29 	assert(n == 1234);
     30 
     31 	n = atoi("+");
     32 	assert(n == 0);
     33 
     34 	n = atoi(" +1");
     35 	assert(n == 1);
     36 
     37 	n = atoi("+ 1");
     38 	assert(n == 0);
     39 
     40 	n = atoi(" -1");
     41 	assert(n == -1);
     42 
     43 	n = atoi("- 1");
     44 	assert(n == 0);
     45 
     46 	sprintf(buf, "%d", INT_MAX);
     47 	n = atoi(buf);
     48 	assert(n == INT_MAX);
     49 
     50 	sprintf(buf, "%d", INT_MIN);
     51 	n = atoi(buf);
     52 	assert(n == INT_MIN);
     53 }
     54 
     55 int
     56 main()
     57 {
     58 	puts("testing");
     59 	test();
     60 	puts("done");
     61 
     62 	return 0;
     63 }