prftest.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. /*
  6. * File: prftest.c
  7. * Description:
  8. * This is a simple test of the PR_snprintf() function defined
  9. * in prprf.c.
  10. */
  11. #include "prlong.h"
  12. #include "prprf.h"
  13. #include <string.h>
  14. #define BUF_SIZE 128
  15. int main(int argc, char **argv)
  16. {
  17. PRInt16 i16;
  18. PRIntn n;
  19. PRInt32 i32;
  20. PRInt64 i64;
  21. char buf[BUF_SIZE];
  22. char answer[BUF_SIZE];
  23. int i, rv = 0;
  24. i16 = -1;
  25. n = -1;
  26. i32 = -1;
  27. LL_I2L(i64, i32);
  28. PR_snprintf(buf, BUF_SIZE, "%hx %x %lx %llx", i16, n, i32, i64);
  29. strcpy(answer, "ffff ");
  30. for (i = PR_BYTES_PER_INT * 2; i; i--) {
  31. strcat(answer, "f");
  32. }
  33. strcat(answer, " ffffffff ffffffffffffffff");
  34. if (!strcmp(buf, answer)) {
  35. printf("PR_snprintf test 1 passed\n");
  36. } else {
  37. printf("PR_snprintf test 1 failed\n");
  38. printf("Converted string is %s\n", buf);
  39. printf("Should be %s\n", answer);
  40. rv = 1;
  41. }
  42. i16 = -32;
  43. n = 30;
  44. i32 = 64;
  45. LL_I2L(i64, 333);
  46. PR_snprintf(buf, BUF_SIZE, "%d %hd %lld %ld", n, i16, i64, i32);
  47. if (!strcmp(buf, "30 -32 333 64")) {
  48. printf("PR_snprintf test 2 passed\n");
  49. } else {
  50. printf("PR_snprintf test 2 failed\n");
  51. printf("Converted string is %s\n", buf);
  52. printf("Should be 30 -32 333 64\n");
  53. rv = 1;
  54. }
  55. return rv;
  56. }