TestFormat.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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(is_integral_works_properly)
  30. {
  31. EXPECT(!IsIntegral<const char*>::value);
  32. EXPECT(IsIntegral<unsigned long>::value);
  33. }
  34. TEST_CASE(format_string_literals)
  35. {
  36. EXPECT_EQ(String::formatted("prefix-{}-suffix", "abc"), "prefix-abc-suffix");
  37. EXPECT_EQ(String::formatted("{}{}{}", "a", "b", "c"), "abc");
  38. }
  39. TEST_CASE(format_integers)
  40. {
  41. EXPECT_EQ(String::formatted("{}", 42u), "42");
  42. EXPECT_EQ(String::formatted("{:4}", 42u), " 42");
  43. EXPECT_EQ(String::formatted("{:08}", 42u), "00000042");
  44. EXPECT_EQ(String::formatted("{:7}", -17), " -17");
  45. EXPECT_EQ(String::formatted("{}", -17), "-17");
  46. EXPECT_EQ(String::formatted("{:04}", 13), "0013");
  47. EXPECT_EQ(String::formatted("{:08x}", 4096), "00001000");
  48. EXPECT_EQ(String::formatted("{:x}", 0x1111222233334444ull), "1111222233334444");
  49. EXPECT_EQ(String::formatted("{:4}", 12345678), "12345678");
  50. }
  51. TEST_CASE(reorder_format_arguments)
  52. {
  53. EXPECT_EQ(String::formatted("{1}{0}", "a", "b"), "ba");
  54. EXPECT_EQ(String::formatted("{0}{1}", "a", "b"), "ab");
  55. EXPECT_EQ(String::formatted("{0}{0}{0}", "a", "b"), "aaa");
  56. EXPECT_EQ(String::formatted("{1}{}{0}", "a", "b", "c"), "baa");
  57. }
  58. TEST_CASE(escape_braces)
  59. {
  60. EXPECT_EQ(String::formatted("{{{}", "foo"), "{foo");
  61. EXPECT_EQ(String::formatted("{}}}", "bar"), "bar}");
  62. }
  63. TEST_CASE(everything)
  64. {
  65. EXPECT_EQ(String::formatted("{{{:04}/{}/{0:8}/{1}", 42u, "foo"), "{0042/foo/ 42/foo");
  66. }
  67. TEST_CASE(string_builder)
  68. {
  69. StringBuilder builder;
  70. builder.appendff(" {} ", 42);
  71. builder.appendff("{1}{0} ", 1, 2);
  72. EXPECT_EQ(builder.to_string(), " 42 21 ");
  73. }
  74. TEST_CASE(format_without_arguments)
  75. {
  76. EXPECT_EQ(String::formatted("foo"), "foo");
  77. }
  78. TEST_CASE(format_upper_case_integer)
  79. {
  80. EXPECT_EQ(String::formatted("{:4X}", 0xff), " FF");
  81. EXPECT_EQ(String::formatted("{:#4X}", 0xff), "0XFF");
  82. EXPECT_EQ(String::formatted("{:b}", 0xff), "11111111");
  83. EXPECT_EQ(String::formatted("{:B}", 0xff), "11111111");
  84. EXPECT_EQ(String::formatted("{:#b}", 0xff), "0b11111111");
  85. }
  86. TEST_CASE(format_aligned)
  87. {
  88. EXPECT_EQ(String::formatted("{:*<8}", 13), "13******");
  89. EXPECT_EQ(String::formatted("{:*^8}", 13), "***13***");
  90. EXPECT_EQ(String::formatted("{:*>8}", 13), "******13");
  91. EXPECT_EQ(String::formatted("{:*>+8}", 13), "*****+13");
  92. EXPECT_EQ(String::formatted("{:*^ 8}", 13), "** 13***");
  93. }
  94. TEST_CASE(format_octal)
  95. {
  96. EXPECT_EQ(String::formatted("{:o}", 0744), "744");
  97. EXPECT_EQ(String::formatted("{:#o}", 0744), "0744");
  98. }
  99. TEST_CASE(zero_pad)
  100. {
  101. EXPECT_EQ(String::formatted("{: <010}", 42), "42 ");
  102. EXPECT_EQ(String::formatted("{:010}", 42), "0000000042");
  103. EXPECT_EQ(String::formatted("{:/^010}", 42), "////42////");
  104. EXPECT_EQ(String::formatted("{:04x}", -32), "-0020");
  105. EXPECT_EQ(String::formatted("{:#06x}", -64), "-0x000040");
  106. }
  107. TEST_CASE(replacement_field)
  108. {
  109. EXPECT_EQ(String::formatted("{:*>{1}}", 13, static_cast<size_t>(10)), "********13");
  110. EXPECT_EQ(String::formatted("{:*<{1}}", 7, 4), "7***");
  111. EXPECT_EQ(String::formatted("{:{2}}", -5, 8, 16), " -5");
  112. EXPECT_EQ(String::formatted("{{{:*^{1}}}}", 1, 3), "{*1*}");
  113. EXPECT_EQ(String::formatted("{:0{}}", 1, 3), "001");
  114. }
  115. TEST_CASE(replacement_field_regression)
  116. {
  117. EXPECT_EQ(String::formatted("{:{}}", "", static_cast<unsigned long>(6)), " ");
  118. }
  119. TEST_CASE(complex_string_specifiers)
  120. {
  121. EXPECT_EQ(String::formatted("{:.8}", "123456789"), "12345678");
  122. EXPECT_EQ(String::formatted("{:9}", "abcd"), "abcd ");
  123. EXPECT_EQ(String::formatted("{:>9}", "abcd"), " abcd");
  124. EXPECT_EQ(String::formatted("{:^9}", "abcd"), " abcd ");
  125. }
  126. TEST_CASE(cast_integer_to_character)
  127. {
  128. EXPECT_EQ(String::formatted("{:c}", static_cast<int>('a')), "a");
  129. EXPECT_EQ(String::formatted("{:c}", static_cast<unsigned int>('f')), "f");
  130. }
  131. TEST_CASE(boolean_values)
  132. {
  133. EXPECT_EQ(String::formatted("{}", true), "true");
  134. EXPECT_EQ(String::formatted("{}", false), "false");
  135. EXPECT_EQ(String::formatted("{:6}", true), "true ");
  136. EXPECT_EQ(String::formatted("{:>4}", false), "false");
  137. EXPECT_EQ(String::formatted("{:d}", false), "0");
  138. EXPECT_EQ(String::formatted("{:d}", true), "1");
  139. EXPECT_EQ(String::formatted("{:#08x}", true), "0x00000001");
  140. }
  141. TEST_CASE(pointers)
  142. {
  143. void* ptr = reinterpret_cast<void*>(0x4000);
  144. if (sizeof(void*) == 4) {
  145. EXPECT_EQ(String::formatted("{:p}", 32), "0x00000020");
  146. EXPECT_EQ(String::formatted("{:p}", ptr), "0x00004000");
  147. EXPECT_EQ(String::formatted("{}", ptr), "0x00004000");
  148. } else if (sizeof(void*) == 8) {
  149. EXPECT_EQ(String::formatted("{:p}", 32), "0x0000000000000020");
  150. EXPECT_EQ(String::formatted("{:p}", ptr), "0x0000000000004000");
  151. EXPECT_EQ(String::formatted("{}", ptr), "0x0000000000004000");
  152. } else {
  153. ASSERT_NOT_REACHED();
  154. }
  155. }
  156. // If the format implementation did absolutely nothing, all tests would pass. This
  157. // is because when a test fails we only write "FAIL" to stdout using format.
  158. //
  159. // This is a bit scary, thus this test. At least this test should fail in this case.
  160. TEST_CASE(ensure_that_format_works)
  161. {
  162. if (String::formatted("FAIL") != "FAIL") {
  163. fprintf(stderr, "FAIL\n");
  164. exit(1);
  165. }
  166. if (String::formatted("{} FAIL {}", 1, 2) != "1 FAIL 2") {
  167. fprintf(stderr, "FAIL\n");
  168. exit(1);
  169. }
  170. }
  171. TEST_CASE(format_string_literal_as_pointer)
  172. {
  173. const char* literal = "abc";
  174. EXPECT_EQ(String::formatted("{:p}", literal), String::formatted("{:p}", reinterpret_cast<FlatPtr>(literal)));
  175. }
  176. TEST_CASE(format_character)
  177. {
  178. char a = 'a';
  179. EXPECT_EQ(String::formatted("{}", true ? a : 'b'), "a");
  180. }
  181. struct A {
  182. };
  183. struct B {
  184. };
  185. template<>
  186. struct AK::Formatter<B> : Formatter<StringView> {
  187. void format(FormatBuilder& builder, B)
  188. {
  189. Formatter<StringView>::format(builder, "B");
  190. }
  191. };
  192. TEST_CASE(format_if_supported)
  193. {
  194. EXPECT_EQ(String::formatted("{}", FormatIfSupported { A {} }), "?");
  195. EXPECT_EQ(String::formatted("{}", FormatIfSupported { B {} }), "B");
  196. }
  197. TEST_CASE(file_descriptor)
  198. {
  199. char filename[] = "/tmp/test-file-descriptor-XXXXXX";
  200. int fd = mkstemp(filename);
  201. FILE* file = fdopen(fd, "w+");
  202. outln(file, "{}", "Hello, World!");
  203. out(file, "foo");
  204. outln(file, "bar");
  205. rewind(file);
  206. Array<u8, 256> buffer;
  207. const auto nread = fread(buffer.data(), 1, buffer.size(), file);
  208. EXPECT_EQ(StringView { "Hello, World!\nfoobar\n" }, StringView { buffer.span().trim(nread) });
  209. fclose(file);
  210. }
  211. TEST_CASE(floating_point_numbers)
  212. {
  213. EXPECT_EQ(String::formatted("{}", 1.12), "1.120000");
  214. EXPECT_EQ(String::formatted("{}", 1.), "1.000000");
  215. EXPECT_EQ(String::formatted("{:.3}", 1.12), "1.120");
  216. EXPECT_EQ(String::formatted("{:.1}", 1.12), "1.1");
  217. EXPECT_EQ(String::formatted("{}", -1.12), "-1.120000");
  218. // FIXME: There is always the question what we mean with the width field. Do we mean significant digits?
  219. // Do we mean the whole width? This is what was the simplest to implement:
  220. EXPECT_EQ(String::formatted("{:x>5.1}", 1.12), "xx1.1");
  221. }
  222. TEST_CASE(no_precision_no_trailing_number)
  223. {
  224. EXPECT_EQ(String::formatted("{:.0}", 0.1), "0.");
  225. }
  226. TEST_CASE(yay_this_implementation_sucks)
  227. {
  228. EXPECT_EQ(String::formatted("{:.0}", .99999999999), "0.");
  229. }
  230. TEST_MAIN(Format)