unit1398.c 3.1 KB

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