unit1398.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curlcheck.h"
  25. #include "curl/mprintf.h"
  26. static CURLcode unit_setup(void) {return CURLE_OK;}
  27. static void unit_stop(void) {}
  28. UNITTEST_START
  29. int rc;
  30. char buf[3] = {'b', 'u', 'g'};
  31. const char *str = "bug";
  32. int width = 3;
  33. char output[24];
  34. /*#define curl_msnprintf snprintf */
  35. /* without a trailing zero */
  36. rc = curl_msnprintf(output, 4, "%.*s", width, buf);
  37. fail_unless(rc == 3, "return code should be 3");
  38. fail_unless(!strcmp(output, "bug"), "wrong output");
  39. /* with a trailing zero */
  40. rc = curl_msnprintf(output, 4, "%.*s", width, str);
  41. fail_unless(rc == 3, "return code should be 3");
  42. fail_unless(!strcmp(output, "bug"), "wrong output");
  43. width = 2;
  44. /* one byte less */
  45. rc = curl_msnprintf(output, 4, "%.*s", width, buf);
  46. fail_unless(rc == 2, "return code should be 2");
  47. fail_unless(!strcmp(output, "bu"), "wrong output");
  48. /* string with larger precision */
  49. rc = curl_msnprintf(output, 8, "%.8s", str);
  50. fail_unless(rc == 3, "return code should be 3");
  51. fail_unless(!strcmp(output, "bug"), "wrong output");
  52. /* longer string with precision */
  53. rc = curl_msnprintf(output, 8, "%.3s", "0123456789");
  54. fail_unless(rc == 3, "return code should be 3");
  55. fail_unless(!strcmp(output, "012"), "wrong output");
  56. /* negative width */
  57. rc = curl_msnprintf(output, 8, "%-8s", str);
  58. fail_unless(rc == 7, "return code should be 7");
  59. fail_unless(!strcmp(output, "bug "), "wrong output");
  60. /* larger width that string length */
  61. rc = curl_msnprintf(output, 8, "%8s", str);
  62. fail_unless(rc == 7, "return code should be 7");
  63. fail_unless(!strcmp(output, " bu"), "wrong output");
  64. /* output a number in a limited output */
  65. rc = curl_msnprintf(output, 4, "%d", 10240);
  66. fail_unless(rc == 3, "return code should be 3");
  67. fail_unless(!strcmp(output, "102"), "wrong output");
  68. /* padded strings */
  69. rc = curl_msnprintf(output, 16, "%8s%8s", str, str);
  70. fail_unless(rc == 15, "return code should be 15");
  71. fail_unless(!strcmp(output, " bug bu"), "wrong output");
  72. /* padded numbers */
  73. rc = curl_msnprintf(output, 16, "%8d%8d", 1234, 5678);
  74. fail_unless(rc == 15, "return code should be 15");
  75. fail_unless(!strcmp(output, " 1234 567"), "wrong output");
  76. /* double precision */
  77. rc = curl_msnprintf(output, 24, "%.*1$.99d", 3, 5678);
  78. fail_unless(rc == 0, "return code should be 0");
  79. UNITTEST_STOP