unit1398.c 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 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. #define CURL_NO_FMT_CHECKS
  25. #include "curlcheck.h"
  26. #include "curl/mprintf.h"
  27. static CURLcode unit_setup(void) {return CURLE_OK;}
  28. static void unit_stop(void) {}
  29. UNITTEST_START
  30. int rc;
  31. char buf[3] = {'b', 'u', 'g'};
  32. const char *str = "bug";
  33. int width = 3;
  34. char output[24];
  35. /*#define curl_msnprintf snprintf */
  36. /* without a trailing zero */
  37. rc = curl_msnprintf(output, 4, "%.*s", width, buf);
  38. fail_unless(rc == 3, "return code should be 3");
  39. fail_unless(!strcmp(output, "bug"), "wrong output");
  40. /* with a trailing zero */
  41. rc = curl_msnprintf(output, 4, "%.*s", width, str);
  42. fail_unless(rc == 3, "return code should be 3");
  43. fail_unless(!strcmp(output, "bug"), "wrong output");
  44. width = 2;
  45. /* one byte less */
  46. rc = curl_msnprintf(output, 4, "%.*s", width, buf);
  47. fail_unless(rc == 2, "return code should be 2");
  48. fail_unless(!strcmp(output, "bu"), "wrong output");
  49. /* string with larger precision */
  50. rc = curl_msnprintf(output, 8, "%.8s", str);
  51. fail_unless(rc == 3, "return code should be 3");
  52. fail_unless(!strcmp(output, "bug"), "wrong output");
  53. /* longer string with precision */
  54. rc = curl_msnprintf(output, 8, "%.3s", "0123456789");
  55. fail_unless(rc == 3, "return code should be 3");
  56. fail_unless(!strcmp(output, "012"), "wrong output");
  57. /* negative width */
  58. rc = curl_msnprintf(output, 8, "%-8s", str);
  59. fail_unless(rc == 7, "return code should be 7");
  60. fail_unless(!strcmp(output, "bug "), "wrong output");
  61. /* larger width that string length */
  62. rc = curl_msnprintf(output, 8, "%8s", str);
  63. fail_unless(rc == 7, "return code should be 7");
  64. fail_unless(!strcmp(output, " bu"), "wrong output");
  65. /* output a number in a limited output */
  66. rc = curl_msnprintf(output, 4, "%d", 10240);
  67. fail_unless(rc == 3, "return code should be 3");
  68. fail_unless(!strcmp(output, "102"), "wrong output");
  69. /* padded strings */
  70. rc = curl_msnprintf(output, 16, "%8s%8s", str, str);
  71. fail_unless(rc == 15, "return code should be 15");
  72. fail_unless(!strcmp(output, " bug bu"), "wrong output");
  73. /* padded numbers */
  74. rc = curl_msnprintf(output, 16, "%8d%8d", 1234, 5678);
  75. fail_unless(rc == 15, "return code should be 15");
  76. fail_unless(!strcmp(output, " 1234 567"), "wrong output");
  77. /* double precision */
  78. rc = curl_msnprintf(output, 24, "%2$.*1$.99d", 3, 5678);
  79. fail_unless(rc == 0, "return code should be 0");
  80. UNITTEST_STOP