a64l.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include "test_helpers.h"
  4. int main(void) {
  5. char * s = "azAZ9."; // test boundaries
  6. long l = a64l(s);
  7. if (l != 194301926) {
  8. printf("Invalid result: a64l(%s) = %ld\n", s, l);
  9. exit(EXIT_FAILURE);
  10. }
  11. printf("Correct a64l: %s = %ld\n", s, l);
  12. s = "azA"; // test null terminated string
  13. l = a64l(s);
  14. if (l != 53222) {
  15. printf("Invalid result: a64l(%s) = %ld\n", s, l);
  16. exit(EXIT_FAILURE);
  17. }
  18. printf("Correct a64l: %s = %ld\n", s, l);
  19. /* Test near boundaries of digit character mapping, and near
  20. * boundaries for number of digits */
  21. long l64a_test_values[] = {0, 1, 2, 11, 12, 37, \
  22. 38, 63, \
  23. 64, 65, 4095, \
  24. 4096, 262143, \
  25. 262144, 16777215, \
  26. 16777216, 1073741823, \
  27. 1073741824, 2147483647};
  28. // l64a tests
  29. for (size_t i = 0; i < sizeof(l64a_test_values)/sizeof(long); i++) {
  30. printf("l64a(%ld): %s\n", l64a_test_values[i], l64a(l64a_test_values[i]));
  31. }
  32. // a64l(l64a(x)) round-trip tests
  33. for (size_t i = 0; i < sizeof(l64a_test_values)/sizeof(long); i++) {
  34. printf("a64l(l64a(%ld)): %ld\n", l64a_test_values[i], a64l(l64a(l64a_test_values[i])));
  35. }
  36. /* For testing 32-bit truncation behavior (for platforms where long
  37. * is larger than 32 bits). Note that the behavior for a64l() and
  38. * l64a() is unspecified for negative values. */
  39. int64_t test_value_64bit = 0x7edcba9876543210;
  40. printf("l64a(x) (lower 32 bits of x are %ld): %s\n", ((long)test_value_64bit) & 0xffffffff, l64a((long)test_value_64bit));
  41. /* Test for trunctation in l64a(a64(x)) round trip (POSIX says the
  42. * result of that is "x in the low-order 32-bits". */
  43. printf("a64l(l64a(x)) (lower 32 bits of x are %ld): %ld\n", ((long)test_value_64bit) & 0xffffffff, a64l(l64a((long)test_value_64bit)));
  44. }