| 1 | /* |
| 2 | * Copyright (C) 2017 Apple Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
| 14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 15 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
| 17 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 23 | * THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #include "config.h" |
| 27 | |
| 28 | #include "WTFStringUtilities.h" |
| 29 | #include <cstddef> |
| 30 | #include <cstdint> |
| 31 | #include <unicode/uvernum.h> |
| 32 | #include <wtf/HexNumber.h> |
| 33 | #include <wtf/text/StringConcatenate.h> |
| 34 | #include <wtf/text/StringConcatenateNumbers.h> |
| 35 | |
| 36 | namespace TestWebKitAPI { |
| 37 | |
| 38 | static int arr[2]; |
| 39 | struct S { |
| 40 | char c; |
| 41 | int i; |
| 42 | }; |
| 43 | |
| 44 | TEST(WTF, StringConcatenate) |
| 45 | { |
| 46 | EXPECT_STREQ("hello world" , makeString("hello" , " " , "world" ).utf8().data()); |
| 47 | } |
| 48 | |
| 49 | TEST(WTF, StringConcatenate_Int) |
| 50 | { |
| 51 | EXPECT_EQ(5u, WTF::lengthOfNumberAsStringSigned(17890)); |
| 52 | EXPECT_STREQ("hello 17890 world" , makeString("hello " , 17890 , " world" ).utf8().data()); |
| 53 | EXPECT_STREQ("hello 17890 world" , makeString("hello " , 17890l , " world" ).utf8().data()); |
| 54 | EXPECT_STREQ("hello 17890 world" , makeString("hello " , 17890ll , " world" ).utf8().data()); |
| 55 | EXPECT_STREQ("hello 17890 world" , makeString("hello " , static_cast<int64_t>(17890) , " world" ).utf8().data()); |
| 56 | EXPECT_STREQ("hello 17890 world" , makeString("hello " , static_cast<int64_t>(17890) , " world" ).utf8().data()); |
| 57 | |
| 58 | EXPECT_EQ(6u, WTF::lengthOfNumberAsStringSigned(-17890)); |
| 59 | EXPECT_STREQ("hello -17890 world" , makeString("hello " , -17890 , " world" ).utf8().data()); |
| 60 | EXPECT_STREQ("hello -17890 world" , makeString("hello " , -17890l , " world" ).utf8().data()); |
| 61 | EXPECT_STREQ("hello -17890 world" , makeString("hello " , -17890ll , " world" ).utf8().data()); |
| 62 | EXPECT_STREQ("hello -17890 world" , makeString("hello " , static_cast<int64_t>(-17890) , " world" ).utf8().data()); |
| 63 | EXPECT_STREQ("hello -17890 world" , makeString("hello " , static_cast<int64_t>(-17890) , " world" ).utf8().data()); |
| 64 | |
| 65 | EXPECT_EQ(1u, WTF::lengthOfNumberAsStringSigned(0)); |
| 66 | EXPECT_STREQ("hello 0 world" , makeString("hello " , 0 , " world" ).utf8().data()); |
| 67 | |
| 68 | EXPECT_STREQ("hello 42 world" , makeString("hello " , static_cast<signed char>(42) , " world" ).utf8().data()); |
| 69 | EXPECT_STREQ("hello 42 world" , makeString("hello " , static_cast<short>(42) , " world" ).utf8().data()); |
| 70 | EXPECT_STREQ("hello 1 world" , makeString("hello " , &arr[1] - &arr[0] , " world" ).utf8().data()); |
| 71 | } |
| 72 | |
| 73 | TEST(WTF, StringConcatenate_Unsigned) |
| 74 | { |
| 75 | EXPECT_EQ(5u, WTF::lengthOfNumberAsStringUnsigned(17890u)); |
| 76 | EXPECT_STREQ("hello 17890 world" , makeString("hello " , 17890u , " world" ).utf8().data()); |
| 77 | EXPECT_STREQ("hello 17890 world" , makeString("hello " , 17890ul , " world" ).utf8().data()); |
| 78 | EXPECT_STREQ("hello 17890 world" , makeString("hello " , 17890ull , " world" ).utf8().data()); |
| 79 | EXPECT_STREQ("hello 17890 world" , makeString("hello " , static_cast<uint64_t>(17890) , " world" ).utf8().data()); |
| 80 | EXPECT_STREQ("hello 17890 world" , makeString("hello " , static_cast<uint64_t>(17890) , " world" ).utf8().data()); |
| 81 | |
| 82 | EXPECT_EQ(1u, WTF::lengthOfNumberAsStringSigned(0u)); |
| 83 | EXPECT_STREQ("hello 0 world" , makeString("hello " , 0u , " world" ).utf8().data()); |
| 84 | |
| 85 | EXPECT_STREQ("hello 42 world" , makeString("hello " , static_cast<unsigned char>(42) , " world" ).utf8().data()); |
| 86 | #if PLATFORM(WIN) || (U_ICU_VERSION_MAJOR_NUM >= 59 && !PLATFORM(COCOA)) |
| 87 | EXPECT_STREQ("hello 42 world" , makeString("hello " , static_cast<unsigned short>(42) , " world" ).utf8().data()); |
| 88 | #else |
| 89 | EXPECT_STREQ("hello * world" , makeString("hello " , static_cast<unsigned short>(42) , " world" ).utf8().data()); // Treated as a character. |
| 90 | #endif |
| 91 | EXPECT_STREQ("hello 4 world" , makeString("hello " , sizeof(int) , " world" ).utf8().data()); // size_t |
| 92 | EXPECT_STREQ("hello 4 world" , makeString("hello " , offsetof(S, i) , " world" ).utf8().data()); // size_t |
| 93 | EXPECT_STREQ("hello 3235839742 world" , makeString("hello " , static_cast<size_t>(0xc0defefe), " world" ).utf8().data()); |
| 94 | } |
| 95 | |
| 96 | TEST(WTF, StringConcatenate_Float) |
| 97 | { |
| 98 | EXPECT_STREQ("hello 17890 world" , makeString("hello " , 17890.0f , " world" ).utf8().data()); |
| 99 | EXPECT_STREQ("hello 17890.5 world" , makeString("hello " , 17890.5f , " world" ).utf8().data()); |
| 100 | |
| 101 | EXPECT_STREQ("hello -17890 world" , makeString("hello " , -17890.0f , " world" ).utf8().data()); |
| 102 | EXPECT_STREQ("hello -17890.5 world" , makeString("hello " , -17890.5f , " world" ).utf8().data()); |
| 103 | |
| 104 | EXPECT_STREQ("hello 0 world" , makeString("hello " , 0.0f , " world" ).utf8().data()); |
| 105 | } |
| 106 | |
| 107 | TEST(WTF, StringConcatenate_Double) |
| 108 | { |
| 109 | EXPECT_STREQ("hello 17890 world" , makeString("hello " , 17890.0 , " world" ).utf8().data()); |
| 110 | EXPECT_STREQ("hello 17890.5 world" , makeString("hello " , 17890.5 , " world" ).utf8().data()); |
| 111 | |
| 112 | EXPECT_STREQ("hello -17890 world" , makeString("hello " , -17890.0 , " world" ).utf8().data()); |
| 113 | EXPECT_STREQ("hello -17890.5 world" , makeString("hello " , -17890.5 , " world" ).utf8().data()); |
| 114 | |
| 115 | EXPECT_STREQ("hello 0 world" , makeString("hello " , 0.0 , " world" ).utf8().data()); |
| 116 | } |
| 117 | |
| 118 | TEST(WTF, StringConcatenate_FormattedDoubleFixedPrecision) |
| 119 | { |
| 120 | EXPECT_STREQ("hello 17890 world" , makeString("hello " , FormattedNumber::fixedPrecision(17890.0) , " world" ).utf8().data()); |
| 121 | EXPECT_STREQ("hello 17890.0 world" , makeString("hello " , FormattedNumber::fixedPrecision(17890.0, 6, KeepTrailingZeros) , " world" ).utf8().data()); |
| 122 | EXPECT_STREQ("hello 1.79e+4 world" , makeString("hello " , FormattedNumber::fixedPrecision(17890.0, 3, KeepTrailingZeros) , " world" ).utf8().data()); |
| 123 | EXPECT_STREQ("hello 17890.000 world" , makeString("hello " , FormattedNumber::fixedPrecision(17890.0, 8, KeepTrailingZeros) , " world" ).utf8().data()); |
| 124 | EXPECT_STREQ("hello 17890 world" , makeString("hello " , FormattedNumber::fixedPrecision(17890.0, 8) , " world" ).utf8().data()); |
| 125 | |
| 126 | EXPECT_STREQ("hello 17890.5 world" , makeString("hello " , FormattedNumber::fixedPrecision(17890.5) , " world" ).utf8().data()); |
| 127 | EXPECT_STREQ("hello 1.79e+4 world" , makeString("hello " , FormattedNumber::fixedPrecision(17890.5, 3, KeepTrailingZeros) , " world" ).utf8().data()); |
| 128 | EXPECT_STREQ("hello 17890.500 world" , makeString("hello " , FormattedNumber::fixedPrecision(17890.5, 8, KeepTrailingZeros) , " world" ).utf8().data()); |
| 129 | EXPECT_STREQ("hello 17890.5 world" , makeString("hello " , FormattedNumber::fixedPrecision(17890.5, 8) , " world" ).utf8().data()); |
| 130 | |
| 131 | EXPECT_STREQ("hello -17890 world" , makeString("hello " , FormattedNumber::fixedPrecision(-17890.0) , " world" ).utf8().data()); |
| 132 | EXPECT_STREQ("hello -17890.0 world" , makeString("hello " , FormattedNumber::fixedPrecision(-17890.0, 6, KeepTrailingZeros) , " world" ).utf8().data()); |
| 133 | EXPECT_STREQ("hello -1.79e+4 world" , makeString("hello " , FormattedNumber::fixedPrecision(-17890.0, 3, KeepTrailingZeros) , " world" ).utf8().data()); |
| 134 | EXPECT_STREQ("hello -17890.000 world" , makeString("hello " , FormattedNumber::fixedPrecision(-17890.0, 8, KeepTrailingZeros) , " world" ).utf8().data()); |
| 135 | EXPECT_STREQ("hello -17890 world" , makeString("hello " , FormattedNumber::fixedPrecision(-17890.0, 8) , " world" ).utf8().data()); |
| 136 | |
| 137 | EXPECT_STREQ("hello -17890.5 world" , makeString("hello " , FormattedNumber::fixedPrecision(-17890.5) , " world" ).utf8().data()); |
| 138 | EXPECT_STREQ("hello -1.79e+4 world" , makeString("hello " , FormattedNumber::fixedPrecision(-17890.5, 3, KeepTrailingZeros) , " world" ).utf8().data()); |
| 139 | EXPECT_STREQ("hello -17890.500 world" , makeString("hello " , FormattedNumber::fixedPrecision(-17890.5, 8, KeepTrailingZeros) , " world" ).utf8().data()); |
| 140 | EXPECT_STREQ("hello -17890.5 world" , makeString("hello " , FormattedNumber::fixedPrecision(-17890.5, 8) , " world" ).utf8().data()); |
| 141 | |
| 142 | EXPECT_STREQ("hello 0 world" , makeString("hello " , FormattedNumber::fixedPrecision(0.0) , " world" ).utf8().data()); |
| 143 | EXPECT_STREQ("hello 0.00000 world" , makeString("hello " , FormattedNumber::fixedPrecision(0.0, 6, KeepTrailingZeros) , " world" ).utf8().data()); |
| 144 | EXPECT_STREQ("hello 0.00 world" , makeString("hello " , FormattedNumber::fixedPrecision(0.0, 3, KeepTrailingZeros) , " world" ).utf8().data()); |
| 145 | EXPECT_STREQ("hello 0.0000000 world" , makeString("hello " , FormattedNumber::fixedPrecision(0.0, 8, KeepTrailingZeros) , " world" ).utf8().data()); |
| 146 | EXPECT_STREQ("hello 0 world" , makeString("hello " , FormattedNumber::fixedPrecision(0.0, 8) , " world" ).utf8().data()); |
| 147 | } |
| 148 | |
| 149 | TEST(WTF, StringConcatenate_FormattedDoubleFixedWidth) |
| 150 | { |
| 151 | EXPECT_STREQ("hello 17890.000 world" , makeString("hello " , FormattedNumber::fixedWidth(17890.0, 3) , " world" ).utf8().data()); |
| 152 | EXPECT_STREQ("hello 17890.500 world" , makeString("hello " , FormattedNumber::fixedWidth(17890.5, 3) , " world" ).utf8().data()); |
| 153 | |
| 154 | EXPECT_STREQ("hello -17890.000 world" , makeString("hello " , FormattedNumber::fixedWidth(-17890.0, 3) , " world" ).utf8().data()); |
| 155 | EXPECT_STREQ("hello -17890.500 world" , makeString("hello " , FormattedNumber::fixedWidth(-17890.5, 3) , " world" ).utf8().data()); |
| 156 | |
| 157 | EXPECT_STREQ("hello 0.000 world" , makeString("hello " , FormattedNumber::fixedWidth(0.0, 3) , " world" ).utf8().data()); |
| 158 | } |
| 159 | |
| 160 | TEST(WTF, StringConcatenate_Pad) |
| 161 | { |
| 162 | EXPECT_STREQ("" , makeString(pad('x', 0, "" )).utf8().data()); |
| 163 | EXPECT_STREQ("x" , makeString(pad('x', 1, "" )).utf8().data()); |
| 164 | EXPECT_STREQ("y" , makeString(pad('x', 1, "y" )).utf8().data()); |
| 165 | EXPECT_STREQ("xy" , makeString(pad('x', 2, "y" )).utf8().data()); |
| 166 | |
| 167 | EXPECT_STREQ("xxxxxxxxxxxxxxx1E240" , makeString(pad('x', 20, hex(123456))).utf8().data()); |
| 168 | EXPECT_STREQ("xxxxxxxxxxxxxxx1E2400.000" , makeString(pad('x', 20, hex(123456)), FormattedNumber::fixedWidth(0.f, 3)).utf8().data()); |
| 169 | EXPECT_STREQ(" B32AF0071F9 id 1231232312313231 (0.000,0.000-0.000,0.000) 0.00KB" , makeString(pad(' ', 12, hex(12312312312313)), " id " , 1231232312313231, " (" , FormattedNumber::fixedWidth(0.f, 3), ',', FormattedNumber::fixedWidth(0.f, 3), '-', FormattedNumber::fixedWidth(0.f, 3), ',', FormattedNumber::fixedWidth(0.f, 3), ") " , FormattedNumber::fixedWidth(0.f, 2), "KB" ).utf8().data()); |
| 170 | } |
| 171 | |
| 172 | } |
| 173 | |