teststrtoq.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. #if defined(__GLIBC__) || defined(__UCLIBC__)
  57. int i;
  58. quad_t n;
  59. char *endptr;
  60. for(i=0;i<n_tests;i++){
  61. n=strtoq(strings[i],&endptr,base);
  62. printf("strtoq(\"%s\",%d) len=%lu res=%qd\n",
  63. strings[i],base,(unsigned long)(endptr-strings[i]),n);
  64. }
  65. #endif
  66. }
  67. int main(int argc,char *argv[])
  68. {
  69. #if defined(__GLIBC__) || defined(__UCLIBC__)
  70. do_test(0);
  71. do_test(8);
  72. do_test(10);
  73. do_test(16);
  74. do_test(36);
  75. return 0;
  76. #else
  77. return 23;
  78. #endif
  79. }