unit1398.c 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2017, 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.haxx.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 == 8, "return code should be 8");
  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 == 8, "return code should be 8");
  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. /* TODO: this should return 5 to be POSIX/snprintf compliant! */
  65. fail_unless(rc == 4, "return code should be 4");
  66. fail_unless(!strcmp(output, "102"), "wrong output");
  67. /* padded strings */
  68. rc = curl_msnprintf(output, 16, "%8s%8s", str, str);
  69. fail_unless(rc == 16, "return code should be 16");
  70. fail_unless(!strcmp(output, " bug bu"), "wrong output");
  71. /* padded numbers */
  72. rc = curl_msnprintf(output, 16, "%8d%8d", 1234, 5678);
  73. fail_unless(rc == 16, "return code should be 16");
  74. fail_unless(!strcmp(output, " 1234 567"), "wrong output");
  75. UNITTEST_STOP