teststrtoq.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. const char *strings[]={
  4. /* some simple stuff */
  5. "0", "1", "10",
  6. "100", "1000", "10000", "100000", "1000000",
  7. "10000000", "100000000", "1000000000",
  8. /* negative */
  9. "-0", "-1", "-10",
  10. "-100", "-1000", "-10000", "-100000", "-1000000",
  11. "-10000000", "-100000000", "-1000000000",
  12. /* test base>10 */
  13. "a", "b", "f", "g", "z",
  14. /* test hex */
  15. "0x0", "0x1", "0xa", "0xf", "0x10",
  16. /* test octal */
  17. "00", "01", "07", "08", "0a", "010",
  18. /* other */
  19. "0x8000000",
  20. /* check overflow cases: (for 32 bit) */
  21. "2147483645",
  22. "2147483646",
  23. "2147483647",
  24. "2147483648",
  25. "2147483649",
  26. "-2147483645",
  27. "-2147483646",
  28. "-2147483647",
  29. "-2147483648",
  30. "-2147483649",
  31. "4294967293",
  32. "4294967294",
  33. "4294967295",
  34. "4294967296",
  35. "4294967297",
  36. "-4294967293",
  37. "-4294967294",
  38. "-4294967295",
  39. "-4294967296",
  40. "-4294967297",
  41. /* bad input tests */
  42. "",
  43. "00",
  44. "0x",
  45. "0x0",
  46. "-",
  47. "+",
  48. " ",
  49. " -",
  50. " - 0",
  51. };
  52. int n_tests=sizeof(strings)/sizeof(strings[0]);
  53. void do_test(int base);
  54. void do_test(int base)
  55. {
  56. int i;
  57. quad_t n;
  58. char *endptr;
  59. for(i=0;i<n_tests;i++){
  60. n=strtoq(strings[i],&endptr,base);
  61. printf("strtoq(\"%s\",%d) len=%lu res=%qd\n",
  62. strings[i],base,(unsigned long)(endptr-strings[i]),n);
  63. }
  64. }
  65. int main(int argc,char *argv[])
  66. {
  67. do_test(0);
  68. do_test(8);
  69. do_test(10);
  70. do_test(16);
  71. do_test(36);
  72. return 0;
  73. }