TestFormat.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/TestSuite.h>
  27. #include <AK/String.h>
  28. #include <AK/StringBuilder.h>
  29. TEST_CASE(format_string_literals)
  30. {
  31. EXPECT_EQ(String::formatted("prefix-{}-suffix", "abc"), "prefix-abc-suffix");
  32. EXPECT_EQ(String::formatted("{}{}{}", "a", "b", "c"), "abc");
  33. }
  34. TEST_CASE(format_integers)
  35. {
  36. EXPECT_EQ(String::formatted("{}", 42u), "42");
  37. EXPECT_EQ(String::formatted("{:4}", 42u), " 42");
  38. EXPECT_EQ(String::formatted("{:08}", 42u), "00000042");
  39. EXPECT_EQ(String::formatted("{:7}", -17), " -17");
  40. EXPECT_EQ(String::formatted("{}", -17), "-17");
  41. EXPECT_EQ(String::formatted("{:04}", 13), "0013");
  42. EXPECT_EQ(String::formatted("{:08x}", 4096), "00001000");
  43. EXPECT_EQ(String::formatted("{:x}", 0x1111222233334444ull), "1111222233334444");
  44. EXPECT_EQ(String::formatted("{:4}", 12345678), "12345678");
  45. }
  46. TEST_CASE(reorder_format_arguments)
  47. {
  48. EXPECT_EQ(String::formatted("{1}{0}", "a", "b"), "ba");
  49. EXPECT_EQ(String::formatted("{0}{1}", "a", "b"), "ab");
  50. EXPECT_EQ(String::formatted("{0}{0}{0}", "a", "b"), "aaa");
  51. EXPECT_EQ(String::formatted("{1}{}{0}", "a", "b", "c"), "baa");
  52. }
  53. TEST_CASE(escape_braces)
  54. {
  55. EXPECT_EQ(String::formatted("{{{}", "foo"), "{foo");
  56. EXPECT_EQ(String::formatted("{}}}", "bar"), "bar}");
  57. }
  58. TEST_CASE(everything)
  59. {
  60. EXPECT_EQ(String::formatted("{{{:04}/{}/{0:8}/{1}", 42u, "foo"), "{0042/foo/ 42/foo");
  61. }
  62. TEST_CASE(string_builder)
  63. {
  64. StringBuilder builder;
  65. builder.appendff(" {} ", 42);
  66. builder.appendff("{1}{0} ", 1, 2);
  67. EXPECT_EQ(builder.to_string(), " 42 21 ");
  68. }
  69. TEST_CASE(format_without_arguments)
  70. {
  71. EXPECT_EQ(String::formatted("foo"), "foo");
  72. }
  73. TEST_CASE(format_upper_case_integer)
  74. {
  75. EXPECT_EQ(String::formatted("{:4X}", 0xff), " FF");
  76. EXPECT_EQ(String::formatted("{:#4X}", 0xff), "0XFF");
  77. EXPECT_EQ(String::formatted("{:b}", 0xff), "11111111");
  78. EXPECT_EQ(String::formatted("{:B}", 0xff), "11111111");
  79. EXPECT_EQ(String::formatted("{:#b}", 0xff), "0b11111111");
  80. }
  81. TEST_CASE(format_aligned)
  82. {
  83. EXPECT_EQ(String::formatted("{:*<8}", 13), "13******");
  84. EXPECT_EQ(String::formatted("{:*^8}", 13), "***13***");
  85. EXPECT_EQ(String::formatted("{:*>8}", 13), "******13");
  86. EXPECT_EQ(String::formatted("{:*>+8}", 13), "*****+13");
  87. EXPECT_EQ(String::formatted("{:*^ 8}", 13), "** 13***");
  88. }
  89. TEST_CASE(format_octal)
  90. {
  91. EXPECT_EQ(String::formatted("{:o}", 0744), "744");
  92. EXPECT_EQ(String::formatted("{:#o}", 0744), "0744");
  93. }
  94. TEST_CASE(zero_pad)
  95. {
  96. EXPECT_EQ(String::formatted("{: <010}", 42), "42 ");
  97. EXPECT_EQ(String::formatted("{:010}", 42), "0000000042");
  98. EXPECT_EQ(String::formatted("{:/^010}", 42), "////42////");
  99. EXPECT_EQ(String::formatted("{:04x}", -32), "-0020");
  100. EXPECT_EQ(String::formatted("{:#06x}", -64), "-0x000040");
  101. }
  102. TEST_CASE(replacement_field)
  103. {
  104. EXPECT_EQ(String::formatted("{:*>{1}}", 13, static_cast<size_t>(10)), "********13");
  105. EXPECT_EQ(String::formatted("{:*<{1}}", 7, 4), "7***");
  106. EXPECT_EQ(String::formatted("{:{2}}", -5, 8, 16), " -5");
  107. EXPECT_EQ(String::formatted("{{{:*^{1}}}}", 1, 3), "{*1*}");
  108. EXPECT_EQ(String::formatted("{:0{}}", 1, 3), "001");
  109. }
  110. TEST_CASE(complex_string_specifiers)
  111. {
  112. EXPECT_EQ(String::formatted("{:.8}", "123456789"), "12345678");
  113. EXPECT_EQ(String::formatted("{:9}", "abcd"), "abcd ");
  114. EXPECT_EQ(String::formatted("{:>9}", "abcd"), " abcd");
  115. EXPECT_EQ(String::formatted("{:^9}", "abcd"), " abcd ");
  116. }
  117. TEST_CASE(cast_integer_to_character)
  118. {
  119. EXPECT_EQ(String::formatted("{:c}", static_cast<int>('a')), "a");
  120. EXPECT_EQ(String::formatted("{:c}", static_cast<unsigned int>('f')), "f");
  121. }
  122. TEST_CASE(boolean_values)
  123. {
  124. EXPECT_EQ(String::formatted("{}", true), "true");
  125. EXPECT_EQ(String::formatted("{}", false), "false");
  126. EXPECT_EQ(String::formatted("{:6}", true), "true ");
  127. EXPECT_EQ(String::formatted("{:>4}", false), "false");
  128. EXPECT_EQ(String::formatted("{:d}", false), "0");
  129. EXPECT_EQ(String::formatted("{:d}", true), "1");
  130. EXPECT_EQ(String::formatted("{:#08x}", true), "0x00000001");
  131. }
  132. TEST_CASE(pointers)
  133. {
  134. void* ptr = reinterpret_cast<void*>(0x4000);
  135. if (sizeof(void*) == 4) {
  136. EXPECT_EQ(String::formatted("{:p}", 32), "0x00000020");
  137. EXPECT_EQ(String::formatted("{:p}", ptr), "0x00004000");
  138. EXPECT_EQ(String::formatted("{}", ptr), "0x00004000");
  139. } else if (sizeof(void*) == 8) {
  140. EXPECT_EQ(String::formatted("{:p}", 32), "0x0000000000000020");
  141. EXPECT_EQ(String::formatted("{:p}", ptr), "0x0000000000004000");
  142. EXPECT_EQ(String::formatted("{}", ptr), "0x0000000000004000");
  143. } else {
  144. ASSERT_NOT_REACHED();
  145. }
  146. }
  147. // If the format implementation did absolutely nothing, all tests would pass. This
  148. // is because when a test fails we only write "FAIL" to stdout using format.
  149. //
  150. // This is a bit scary, thus this test. At least this test should fail in this case.
  151. TEST_CASE(ensure_that_format_works)
  152. {
  153. if (String::formatted("FAIL") != "FAIL") {
  154. fprintf(stderr, "FAIL\n");
  155. exit(1);
  156. }
  157. if (String::formatted("{} FAIL {}", 1, 2) != "1 FAIL 2") {
  158. fprintf(stderr, "FAIL\n");
  159. exit(1);
  160. }
  161. }
  162. TEST_CASE(format_string_literal_as_pointer)
  163. {
  164. const char* literal = "abc";
  165. EXPECT_EQ(String::formatted("{:p}", literal), String::formatted("{:p}", reinterpret_cast<FlatPtr>(literal)));
  166. }
  167. TEST_CASE(format_character)
  168. {
  169. char a = 'a';
  170. EXPECT_EQ(String::formatted("{}", true ? a : 'b'), "a");
  171. }
  172. struct A {
  173. };
  174. struct B {
  175. };
  176. template<>
  177. struct AK::Formatter<B> : Formatter<StringView> {
  178. void format(TypeErasedFormatParams& params, FormatBuilder& builder, B)
  179. {
  180. Formatter<StringView>::format(params, builder, "B");
  181. }
  182. };
  183. TEST_CASE(format_if_supported)
  184. {
  185. EXPECT_EQ(String::formatted("{}", FormatIfSupported { A {} }), "?");
  186. EXPECT_EQ(String::formatted("{}", FormatIfSupported { B {} }), "B");
  187. }
  188. TEST_CASE(file_descriptor)
  189. {
  190. char filename[] = "/tmp/test-file-descriptor-XXXXXX";
  191. int fd = mkstemp(filename);
  192. FILE* file = fdopen(fd, "w+");
  193. outln(file, "{}", "Hello, World!");
  194. out(file, "foo");
  195. outln(file, "bar");
  196. rewind(file);
  197. Array<u8, 256> buffer;
  198. const auto nread = fread(buffer.data(), 1, buffer.size(), file);
  199. EXPECT_EQ(StringView { "Hello, World!\nfoobar\n" }, StringView { buffer.span().trim(nread) });
  200. fclose(file);
  201. }
  202. TEST_CASE(floating_point_numbers)
  203. {
  204. EXPECT_EQ(String::formatted("{}", 1.12), "1.120000");
  205. EXPECT_EQ(String::formatted("{}", 1.), "1.000000");
  206. EXPECT_EQ(String::formatted("{:.3}", 1.12), "1.120");
  207. EXPECT_EQ(String::formatted("{:.1}", 1.12), "1.1");
  208. EXPECT_EQ(String::formatted("{}", -1.12), "-1.120000");
  209. // FIXME: There is always the question what we mean with the width field. Do we mean significant digits?
  210. // Do we mean the whole width? This is what was the simplest to implement:
  211. EXPECT_EQ(String::formatted("{:x>5.1}", 1.12), "xx1.1");
  212. }
  213. TEST_CASE(no_precision_no_trailing_number)
  214. {
  215. EXPECT_EQ(String::formatted("{:.0}", 0.1), "0.");
  216. }
  217. TEST_CASE(yay_this_implementation_sucks)
  218. {
  219. EXPECT_EQ(String::formatted("{:.0}", .99999999999), "0.");
  220. }
  221. TEST_MAIN(Format)