Mirror of :pserver:cvs@cvs.fefe.de:/cvs libowfat https://www.fefe.de/libowfat/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

34 lines
1.0 KiB

  1. #include "scan.h"
  2. #ifdef UNITTEST
  3. #undef UNITTEST
  4. #include "scan_longn.c"
  5. #define UNITTEST
  6. #endif
  7. size_t scan_long(const char *src,long *dest) {
  8. return scan_longn(src,(size_t)(-1),dest);
  9. }
  10. #ifdef UNITTEST
  11. #include <assert.h>
  12. int main() {
  13. long i;
  14. assert(scan_long("1234",&i)==4 && i==1234);
  15. assert(scan_long("-1234",&i)==5 && i==-1234);
  16. assert(scan_long("+1234",&i)==5 && i==1234);
  17. assert(scan_long("2147483647",&i)==10 && i==2147483647); // INT_MAX
  18. assert(scan_long("-2147483648",&i)==11 && i==-2147483648); // INT_MIN
  19. if (sizeof(int) == sizeof(long)) {
  20. assert(scan_long("4294967295",&i)==9 && i==429496729); // overflow
  21. assert(scan_long("2147483648",&i)==9 && i==214748364); // overflow
  22. assert(scan_long("-2147483649",&i)==10 && i==-214748364); // underflow
  23. } else {
  24. assert(scan_long("9223372036854775807",&i)==19 && i==0x7fffffffffffffffull); // LONG_MAX
  25. assert(scan_long("-9223372036854775808",&i)==20 && i==(long long)0x8000000000000000ull); // LONG_MIN
  26. }
  27. return 0;
  28. }
  29. #endif