TestFormat.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibTest/TestCase.h>
  7. #include <AK/String.h>
  8. #include <AK/StringBuilder.h>
  9. #include <AK/Vector.h>
  10. #include <math.h>
  11. #include <unistd.h>
  12. TEST_CASE(is_integral_works_properly)
  13. {
  14. EXPECT(!IsIntegral<const char*>);
  15. EXPECT(IsIntegral<unsigned long>);
  16. }
  17. TEST_CASE(format_string_literals)
  18. {
  19. EXPECT_EQ(String::formatted("prefix-{}-suffix", "abc"), "prefix-abc-suffix");
  20. EXPECT_EQ(String::formatted("{}{}{}", "a", "b", "c"), "abc");
  21. }
  22. TEST_CASE(format_integers)
  23. {
  24. EXPECT_EQ(String::formatted("{}", 42u), "42");
  25. EXPECT_EQ(String::formatted("{:4}", 42u), " 42");
  26. EXPECT_EQ(String::formatted("{:08}", 42u), "00000042");
  27. EXPECT_EQ(String::formatted("{:7}", -17), " -17");
  28. EXPECT_EQ(String::formatted("{}", -17), "-17");
  29. EXPECT_EQ(String::formatted("{:04}", 13), "0013");
  30. EXPECT_EQ(String::formatted("{:08x}", 4096), "00001000");
  31. EXPECT_EQ(String::formatted("{:x}", 0x1111222233334444ull), "1111222233334444");
  32. EXPECT_EQ(String::formatted("{:4}", 12345678), "12345678");
  33. }
  34. TEST_CASE(reorder_format_arguments)
  35. {
  36. EXPECT_EQ(String::formatted("{1}{0}", "a", "b"), "ba");
  37. EXPECT_EQ(String::formatted("{0}{1}", "a", "b"), "ab");
  38. // Compiletime check bypass: ignoring a passed argument.
  39. EXPECT_EQ(String::formatted("{0}{0}{0}"sv, "a", "b"), "aaa");
  40. // Compiletime check bypass: ignoring a passed argument.
  41. EXPECT_EQ(String::formatted("{1}{}{0}"sv, "a", "b", "c"), "baa");
  42. }
  43. TEST_CASE(escape_braces)
  44. {
  45. EXPECT_EQ(String::formatted("{{{}", "foo"), "{foo");
  46. EXPECT_EQ(String::formatted("{}}}", "bar"), "bar}");
  47. }
  48. TEST_CASE(everything)
  49. {
  50. EXPECT_EQ(String::formatted("{{{:04}/{}/{0:8}/{1}", 42u, "foo"), "{0042/foo/ 42/foo");
  51. }
  52. TEST_CASE(string_builder)
  53. {
  54. StringBuilder builder;
  55. builder.appendff(" {} ", 42);
  56. builder.appendff("{1}{0} ", 1, 2);
  57. EXPECT_EQ(builder.to_string(), " 42 21 ");
  58. }
  59. TEST_CASE(format_without_arguments)
  60. {
  61. EXPECT_EQ(String::formatted("foo"), "foo");
  62. }
  63. TEST_CASE(format_upper_case_integer)
  64. {
  65. EXPECT_EQ(String::formatted("{:4X}", 0xff), " FF");
  66. EXPECT_EQ(String::formatted("{:#4X}", 0xff), "0XFF");
  67. EXPECT_EQ(String::formatted("{:b}", 0xff), "11111111");
  68. EXPECT_EQ(String::formatted("{:B}", 0xff), "11111111");
  69. EXPECT_EQ(String::formatted("{:#b}", 0xff), "0b11111111");
  70. }
  71. TEST_CASE(format_aligned)
  72. {
  73. EXPECT_EQ(String::formatted("{:*<8}", 13), "13******");
  74. EXPECT_EQ(String::formatted("{:*^8}", 13), "***13***");
  75. EXPECT_EQ(String::formatted("{:*>8}", 13), "******13");
  76. EXPECT_EQ(String::formatted("{:*>+8}", 13), "*****+13");
  77. EXPECT_EQ(String::formatted("{:*^ 8}", 13), "** 13***");
  78. }
  79. TEST_CASE(format_octal)
  80. {
  81. EXPECT_EQ(String::formatted("{:o}", 0744), "744");
  82. EXPECT_EQ(String::formatted("{:#o}", 0744), "0744");
  83. }
  84. TEST_CASE(zero_pad)
  85. {
  86. EXPECT_EQ(String::formatted("{: <010}", 42), "42 ");
  87. EXPECT_EQ(String::formatted("{:010}", 42), "0000000042");
  88. EXPECT_EQ(String::formatted("{:/^010}", 42), "////42////");
  89. EXPECT_EQ(String::formatted("{:04x}", -32), "-0020");
  90. EXPECT_EQ(String::formatted("{:#06x}", -64), "-0x000040");
  91. }
  92. TEST_CASE(replacement_field)
  93. {
  94. EXPECT_EQ(String::formatted("{:*>{1}}", 13, static_cast<size_t>(10)), "********13");
  95. EXPECT_EQ(String::formatted("{:*<{1}}", 7, 4), "7***");
  96. // Compiletime check bypass: intentionally ignoring extra arguments
  97. EXPECT_EQ(String::formatted("{:{2}}"sv, -5, 8, 16), " -5");
  98. EXPECT_EQ(String::formatted("{{{:*^{1}}}}", 1, 3), "{*1*}");
  99. EXPECT_EQ(String::formatted("{:0{}}", 1, 3), "001");
  100. }
  101. TEST_CASE(replacement_field_regression)
  102. {
  103. // FIXME: Compiletime check bypass: cannot parse '}}' correctly.
  104. EXPECT_EQ(String::formatted("{:{}}"sv, "", static_cast<unsigned long>(6)), " ");
  105. }
  106. TEST_CASE(complex_string_specifiers)
  107. {
  108. EXPECT_EQ(String::formatted("{:.8}", "123456789"), "12345678");
  109. EXPECT_EQ(String::formatted("{:9}", "abcd"), "abcd ");
  110. EXPECT_EQ(String::formatted("{:>9}", "abcd"), " abcd");
  111. EXPECT_EQ(String::formatted("{:^9}", "abcd"), " abcd ");
  112. EXPECT_EQ(String::formatted("{:4.6}", "a"), "a ");
  113. EXPECT_EQ(String::formatted("{:4.6}", "abcdef"), "abcdef");
  114. EXPECT_EQ(String::formatted("{:4.6}", "abcdefghi"), "abcdef");
  115. }
  116. TEST_CASE(cast_integer_to_character)
  117. {
  118. EXPECT_EQ(String::formatted("{:c}", static_cast<int>('a')), "a");
  119. EXPECT_EQ(String::formatted("{:c}", static_cast<unsigned int>('f')), "f");
  120. }
  121. TEST_CASE(boolean_values)
  122. {
  123. EXPECT_EQ(String::formatted("{}", true), "true");
  124. EXPECT_EQ(String::formatted("{}", false), "false");
  125. EXPECT_EQ(String::formatted("{:6}", true), "true ");
  126. EXPECT_EQ(String::formatted("{:>4}", false), "false");
  127. EXPECT_EQ(String::formatted("{:d}", false), "0");
  128. EXPECT_EQ(String::formatted("{:d}", true), "1");
  129. EXPECT_EQ(String::formatted("{:#08x}", true), "0x00000001");
  130. }
  131. TEST_CASE(pointers)
  132. {
  133. void* ptr = reinterpret_cast<void*>(0x4000);
  134. if (sizeof(void*) == 4) {
  135. EXPECT_EQ(String::formatted("{:p}", 32), "0x00000020");
  136. EXPECT_EQ(String::formatted("{:p}", ptr), "0x00004000");
  137. EXPECT_EQ(String::formatted("{}", ptr), "0x00004000");
  138. } else if (sizeof(void*) == 8) {
  139. EXPECT_EQ(String::formatted("{:p}", 32), "0x0000000000000020");
  140. EXPECT_EQ(String::formatted("{:p}", ptr), "0x0000000000004000");
  141. EXPECT_EQ(String::formatted("{}", ptr), "0x0000000000004000");
  142. } else {
  143. VERIFY_NOT_REACHED();
  144. }
  145. }
  146. // If the format implementation did absolutely nothing, all tests would pass. This
  147. // is because when a test fails we only write "FAIL" to stdout using format.
  148. //
  149. // This is a bit scary, thus this test. At least this test should fail in this case.
  150. TEST_CASE(ensure_that_format_works)
  151. {
  152. if (String::formatted("FAIL") != "FAIL") {
  153. fprintf(stderr, "FAIL\n");
  154. exit(1);
  155. }
  156. if (String::formatted("{} FAIL {}", 1, 2) != "1 FAIL 2") {
  157. fprintf(stderr, "FAIL\n");
  158. exit(1);
  159. }
  160. }
  161. TEST_CASE(format_string_literal_as_pointer)
  162. {
  163. const char* literal = "abc";
  164. EXPECT_EQ(String::formatted("{:p}", literal), String::formatted("{:p}", reinterpret_cast<FlatPtr>(literal)));
  165. }
  166. TEST_CASE(format_character)
  167. {
  168. char a = 'a';
  169. EXPECT_EQ(String::formatted("{}", true ? a : 'b'), "a");
  170. }
  171. struct A {
  172. };
  173. struct B {
  174. };
  175. template<>
  176. struct AK::Formatter<B> : Formatter<StringView> {
  177. ErrorOr<void> format(FormatBuilder& builder, B)
  178. {
  179. return Formatter<StringView>::format(builder, "B");
  180. }
  181. };
  182. TEST_CASE(format_if_supported)
  183. {
  184. EXPECT_EQ(String::formatted("{}", FormatIfSupported { A {} }), "?");
  185. EXPECT_EQ(String::formatted("{}", FormatIfSupported { B {} }), "B");
  186. }
  187. TEST_CASE(file_descriptor)
  188. {
  189. char filename[] = "/tmp/test-file-descriptor-XXXXXX";
  190. int fd = mkstemp(filename);
  191. FILE* file = fdopen(fd, "w+");
  192. outln(file, "{}", "Hello, World!");
  193. out(file, "foo");
  194. outln(file, "bar");
  195. rewind(file);
  196. Array<u8, 256> buffer;
  197. const auto nread = fread(buffer.data(), 1, buffer.size(), file);
  198. EXPECT_EQ("Hello, World!\nfoobar\n"sv, StringView { buffer.span().trim(nread) });
  199. fclose(file);
  200. unlink(filename);
  201. }
  202. TEST_CASE(floating_point_numbers)
  203. {
  204. EXPECT_EQ(String::formatted("{}", 1.12), "1.12");
  205. EXPECT_EQ(String::formatted("{}", 1.), "1");
  206. EXPECT_EQ(String::formatted("{:.3}", 1.12), "1.12");
  207. EXPECT_EQ(String::formatted("{:.1}", 1.12), "1.1");
  208. EXPECT_EQ(String::formatted("{}", -1.12), "-1.12");
  209. EXPECT_EQ(String::formatted("{}", NAN), "nan");
  210. EXPECT_EQ(String::formatted("{}", INFINITY), "inf");
  211. EXPECT_EQ(String::formatted("{}", -INFINITY), "-inf");
  212. // FIXME: There is always the question what we mean with the width field. Do we mean significant digits?
  213. // Do we mean the whole width? This is what was the simplest to implement:
  214. EXPECT_EQ(String::formatted("{:x>5.1}", 1.12), "xx1.1");
  215. }
  216. TEST_CASE(no_precision_no_trailing_number)
  217. {
  218. EXPECT_EQ(String::formatted("{:.0}", 0.1), "0");
  219. }
  220. TEST_CASE(yay_this_implementation_sucks)
  221. {
  222. EXPECT_EQ(String::formatted("{:.0}", .99999999999), "0");
  223. }
  224. TEST_CASE(precision_with_trailing_zeros)
  225. {
  226. EXPECT_EQ(String::formatted("{:0.3}", 1.12), "1.120");
  227. EXPECT_EQ(String::formatted("{:0.1}", 1.12), "1.1");
  228. }
  229. TEST_CASE(magnitude_less_than_zero)
  230. {
  231. EXPECT_EQ(String::formatted("{}", -0.654), "-0.654");
  232. EXPECT_EQ(String::formatted("{}", 0.654), "0.654");
  233. }
  234. TEST_CASE(format_nullptr)
  235. {
  236. EXPECT_EQ(String::formatted("{}", nullptr), String::formatted("{:p}", static_cast<FlatPtr>(0)));
  237. }
  238. struct C {
  239. int i;
  240. };
  241. template<>
  242. struct AK::Formatter<C> : AK::Formatter<FormatString> {
  243. ErrorOr<void> format(FormatBuilder& builder, C c)
  244. {
  245. return AK::Formatter<FormatString>::format(builder, "C(i={})", c.i);
  246. }
  247. };
  248. TEST_CASE(use_format_string_formatter)
  249. {
  250. EXPECT_EQ(String::formatted("{:*<10}", C { 42 }), "C(i=42)***");
  251. }
  252. TEST_CASE(long_long_regression)
  253. {
  254. EXPECT_EQ(String::formatted("{}", 0x0123456789abcdefLL), "81985529216486895");
  255. StringBuilder builder;
  256. AK::FormatBuilder fmtbuilder { builder };
  257. MUST(fmtbuilder.put_i64(0x0123456789abcdefLL));
  258. EXPECT_EQ(builder.string_view(), "81985529216486895");
  259. }
  260. TEST_CASE(hex_dump)
  261. {
  262. EXPECT_EQ(String::formatted("{:hex-dump}", "0000"), "30303030");
  263. EXPECT_EQ(String::formatted("{:>4hex-dump}", "0000"), "30303030 0000");
  264. EXPECT_EQ(String::formatted("{:>2hex-dump}", "0000"), "3030 00\n3030 00");
  265. EXPECT_EQ(String::formatted("{:*>4hex-dump}", "0000"), "30303030****0000");
  266. }
  267. TEST_CASE(vector_format)
  268. {
  269. {
  270. Vector<int> v { 1, 2, 3, 4 };
  271. EXPECT_EQ(String::formatted("{}", v), "[ 1, 2, 3, 4 ]");
  272. }
  273. {
  274. Vector<StringView> v { "1"sv, "2"sv, "3"sv, "4"sv };
  275. EXPECT_EQ(String::formatted("{}", v), "[ 1, 2, 3, 4 ]");
  276. }
  277. {
  278. Vector<Vector<String>> v { { "1"sv, "2"sv }, { "3"sv, "4"sv } };
  279. EXPECT_EQ(String::formatted("{}", v), "[ [ 1, 2 ], [ 3, 4 ] ]");
  280. }
  281. }
  282. TEST_CASE(format_wchar)
  283. {
  284. EXPECT_EQ(String::formatted("{}", L'a'), "a");
  285. EXPECT_EQ(String::formatted("{}", L'\U0001F41E'), "\xF0\x9F\x90\x9E");
  286. EXPECT_EQ(String::formatted("{:x}", L'a'), "61");
  287. EXPECT_EQ(String::formatted("{:x}", L'\U0001F41E'), "1f41e");
  288. EXPECT_EQ(String::formatted("{:d}", L'a'), "97");
  289. EXPECT_EQ(String::formatted("{:d}", L'\U0001F41E'), "128030");
  290. EXPECT_EQ(String::formatted("{:6}", L'a'), "a ");
  291. EXPECT_EQ(String::formatted("{:6d}", L'a'), " 97");
  292. EXPECT_EQ(String::formatted("{:#x}", L'\U0001F41E'), "0x1f41e");
  293. }