prfz.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. /*
  5. * This is a simple test of the PR_fprintf() function for size_t formats.
  6. */
  7. #include "prprf.h"
  8. #include <sys/types.h>
  9. #include <limits.h>
  10. #include <string.h>
  11. #include <stdint.h>
  12. int
  13. main(int argc, char **argv)
  14. {
  15. char buffer[128];
  16. size_t unsigned_small = 266;
  17. #ifdef XP_UNIX
  18. ssize_t signed_small_p = 943;
  19. ssize_t signed_small_n = -1;
  20. #endif
  21. size_t unsigned_max = SIZE_MAX;
  22. size_t unsigned_min = 0;
  23. #ifdef XP_UNIX
  24. ssize_t signed_max = SSIZE_MAX;
  25. #endif
  26. printf("Test: unsigned small '%%zu' : ");
  27. PR_snprintf(buffer, sizeof(buffer), "%zu", unsigned_small);
  28. if (strncmp(buffer, "266", sizeof(buffer)) != 0) {
  29. printf("Failed, got '%s'\n", buffer);
  30. return -1;
  31. }
  32. printf("OK\n");
  33. #ifdef XP_UNIX
  34. printf("Test: signed small positive '%%zd' : ");
  35. PR_snprintf(buffer, sizeof(buffer), "%zd", signed_small_p);
  36. if (strncmp(buffer, "943", sizeof(buffer)) != 0) {
  37. printf("Failed, got '%s'\n", buffer);
  38. return -1;
  39. }
  40. printf("OK\n");
  41. printf("Test: signed small negative '%%zd' : ");
  42. PR_snprintf(buffer, sizeof(buffer), "%zd", signed_small_n);
  43. if (strncmp(buffer, "-1", sizeof(buffer)) != 0) {
  44. printf("Failed, got '%s'\n", buffer);
  45. return -1;
  46. }
  47. printf("OK\n");
  48. #endif
  49. printf("Test: 0 '%%zu' : ");
  50. PR_snprintf(buffer, sizeof(buffer), "%zu", unsigned_min);
  51. if (strncmp(buffer, "0", sizeof(buffer)) != 0) {
  52. printf("Failed, got '%s'\n", buffer);
  53. return -1;
  54. }
  55. printf("OK\n");
  56. printf("Test: SIZE_MAX '%%zx' : ");
  57. PR_snprintf(buffer, sizeof(buffer), "%zx", unsigned_max);
  58. if (strspn(buffer, "f") != sizeof(size_t) * 2) {
  59. printf("Failed, got '%s'\n", buffer);
  60. return -1;
  61. }
  62. printf("OK\n");
  63. #ifdef XP_UNIX
  64. printf("Test: SSIZE_MAX '%%zx' : ");
  65. PR_snprintf(buffer, sizeof(buffer), "%zx", signed_max);
  66. if (*buffer != '7' ||
  67. strspn(buffer + 1, "f") != sizeof(ssize_t) * 2 - 1) {
  68. printf("Failed, got '%s'\n", buffer);
  69. return -1;
  70. }
  71. printf("OK\n");
  72. #endif
  73. return 0;
  74. }