unit1398.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. #include "curlcheck.h"
  25. #if defined(__GNUC__) || defined(__clang__)
  26. #pragma GCC diagnostic push
  27. #pragma GCC diagnostic ignored "-Wformat"
  28. #endif
  29. static CURLcode unit_setup(void) {return CURLE_OK;}
  30. static void unit_stop(void) {}
  31. UNITTEST_START
  32. int rc;
  33. char buf[3] = {'b', 'u', 'g'};
  34. const char *str = "bug";
  35. int width = 3;
  36. char output[130];
  37. /*#define curl_msnprintf snprintf */
  38. /* without a trailing zero */
  39. rc = curl_msnprintf(output, 4, "%.*s", width, buf);
  40. fail_unless(rc == 3, "return code should be 3");
  41. fail_unless(!strcmp(output, "bug"), "wrong output");
  42. /* with a trailing zero */
  43. rc = curl_msnprintf(output, 4, "%.*s", width, str);
  44. fail_unless(rc == 3, "return code should be 3");
  45. fail_unless(!strcmp(output, "bug"), "wrong output");
  46. width = 2;
  47. /* one byte less */
  48. rc = curl_msnprintf(output, 4, "%.*s", width, buf);
  49. fail_unless(rc == 2, "return code should be 2");
  50. fail_unless(!strcmp(output, "bu"), "wrong output");
  51. /* string with larger precision */
  52. rc = curl_msnprintf(output, 8, "%.8s", str);
  53. fail_unless(rc == 3, "return code should be 3");
  54. fail_unless(!strcmp(output, "bug"), "wrong output");
  55. /* longer string with precision */
  56. rc = curl_msnprintf(output, 8, "%.3s", "0123456789");
  57. fail_unless(rc == 3, "return code should be 3");
  58. fail_unless(!strcmp(output, "012"), "wrong output");
  59. /* negative width */
  60. rc = curl_msnprintf(output, 8, "%-8s", str);
  61. fail_unless(rc == 7, "return code should be 7");
  62. fail_unless(!strcmp(output, "bug "), "wrong output");
  63. /* larger width that string length */
  64. rc = curl_msnprintf(output, 8, "%8s", str);
  65. fail_unless(rc == 7, "return code should be 7");
  66. fail_unless(!strcmp(output, " bu"), "wrong output");
  67. /* output a number in a limited output */
  68. rc = curl_msnprintf(output, 4, "%d", 10240);
  69. fail_unless(rc == 3, "return code should be 3");
  70. fail_unless(!strcmp(output, "102"), "wrong output");
  71. /* padded strings */
  72. rc = curl_msnprintf(output, 16, "%8s%8s", str, str);
  73. fail_unless(rc == 15, "return code should be 15");
  74. fail_unless(!strcmp(output, " bug bu"), "wrong output");
  75. /* padded numbers */
  76. rc = curl_msnprintf(output, 16, "%8d%8d", 1234, 5678);
  77. fail_unless(rc == 15, "return code should be 15");
  78. fail_unless(!strcmp(output, " 1234 567"), "wrong output");
  79. /* double precision */
  80. rc = curl_msnprintf(output, 24, "%2$.*1$.99d", 3, 5678);
  81. fail_unless(rc == 0, "return code should be 0");
  82. /* 129 input % flags */
  83. rc = curl_msnprintf(output, 130,
  84. "%s%s%s%s%s%s%s%s%s%s" /* 10 */
  85. "%s%s%s%s%s%s%s%s%s%s" /* 20 */
  86. "%s%s%s%s%s%s%s%s%s%s" /* 30 */
  87. "%s%s%s%s%s%s%s%s%s%s" /* 40 */
  88. "%s%s%s%s%s%s%s%s%s%s" /* 50 */
  89. "%s%s%s%s%s%s%s%s%s%s" /* 60 */
  90. "%s%s%s%s%s%s%s%s%s%s" /* 70 */
  91. "%s%s%s%s%s%s%s%s%s%s" /* 80 */
  92. "%s%s%s%s%s%s%s%s%s%s" /* 90 */
  93. "%s%s%s%s%s%s%s%s%s%s" /* 100 */
  94. "%s%s%s%s%s%s%s%s%s%s" /* 110 */
  95. "%s%s%s%s%s%s%s%s%s%s" /* 120 */
  96. "%s%s%s%s%s%s%s%s%s", /* 129 */
  97. "a", "", "", "", "", "", "", "", "", "", /* 10 */
  98. "b", "", "", "", "", "", "", "", "", "", /* 20 */
  99. "c", "", "", "", "", "", "", "", "", "", /* 30 */
  100. "d", "", "", "", "", "", "", "", "", "", /* 40 */
  101. "e", "", "", "", "", "", "", "", "", "", /* 50 */
  102. "f", "", "", "", "", "", "", "", "", "", /* 60 */
  103. "g", "", "", "", "", "", "", "", "", "", /* 70 */
  104. "h", "", "", "", "", "", "", "", "", "", /* 80 */
  105. "i", "", "", "", "", "", "", "", "", "", /* 90 */
  106. "j", "", "", "", "", "", "", "", "", "", /* 100 */
  107. "k", "", "", "", "", "", "", "", "", "", /* 110 */
  108. "l", "", "", "", "", "", "", "", "", "", /* 120 */
  109. "m", "", "", "", "", "", "", "", "" /* 129 */
  110. );
  111. fail_unless(rc == 0, "return code should be 0");
  112. /* 128 input % flags */
  113. rc = curl_msnprintf(output, 130,
  114. "%s%s%s%s%s%s%s%s%s%s" /* 10 */
  115. "%s%s%s%s%s%s%s%s%s%s" /* 20 */
  116. "%s%s%s%s%s%s%s%s%s%s" /* 30 */
  117. "%s%s%s%s%s%s%s%s%s%s" /* 40 */
  118. "%s%s%s%s%s%s%s%s%s%s" /* 50 */
  119. "%s%s%s%s%s%s%s%s%s%s" /* 60 */
  120. "%s%s%s%s%s%s%s%s%s%s" /* 70 */
  121. "%s%s%s%s%s%s%s%s%s%s" /* 80 */
  122. "%s%s%s%s%s%s%s%s%s%s" /* 90 */
  123. "%s%s%s%s%s%s%s%s%s%s" /* 100 */
  124. "%s%s%s%s%s%s%s%s%s%s" /* 110 */
  125. "%s%s%s%s%s%s%s%s%s%s" /* 120 */
  126. "%s%s%s%s%s%s%s%s", /* 128 */
  127. "a", "", "", "", "", "", "", "", "", "", /* 10 */
  128. "b", "", "", "", "", "", "", "", "", "", /* 20 */
  129. "c", "", "", "", "", "", "", "", "", "", /* 30 */
  130. "d", "", "", "", "", "", "", "", "", "", /* 40 */
  131. "e", "", "", "", "", "", "", "", "", "", /* 50 */
  132. "f", "", "", "", "", "", "", "", "", "", /* 60 */
  133. "g", "", "", "", "", "", "", "", "", "", /* 70 */
  134. "h", "", "", "", "", "", "", "", "", "", /* 80 */
  135. "i", "", "", "", "", "", "", "", "", "", /* 90 */
  136. "j", "", "", "", "", "", "", "", "", "", /* 100 */
  137. "k", "", "", "", "", "", "", "", "", "", /* 110 */
  138. "l", "", "", "", "", "", "", "", "", "", /* 120 */
  139. "m", "", "", "", "", "", "", "" /* 128 */
  140. );
  141. fail_unless(rc == 13, "return code should be 13");
  142. /* 129 output segments */
  143. rc = curl_msnprintf(output, 130,
  144. "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" /* 20 */
  145. "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" /* 40 */
  146. "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" /* 60 */
  147. "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" /* 80 */
  148. "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" /* 100 */
  149. "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" /* 120 */
  150. "%%%%%%%%%%%%%%%%%%" /* 129 */
  151. );
  152. fail_unless(rc == 0, "return code should be 0");
  153. /* 128 output segments */
  154. rc = curl_msnprintf(output, 129,
  155. "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" /* 20 */
  156. "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" /* 40 */
  157. "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" /* 60 */
  158. "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" /* 80 */
  159. "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" /* 100 */
  160. "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" /* 120 */
  161. "%%%%%%%%%%%%%%%%" /* 128 */
  162. );
  163. fail_unless(rc == 128, "return code should be 128");
  164. UNITTEST_STOP
  165. #if defined(__GNUC__) || defined(__clang__)
  166. #pragma GCC diagnostic pop
  167. #endif