Hash :
d30d81d0
        
        Author :
  
        
        Date :
2024-11-06T16:08:28
        
      
[code health] Remove underscores from test names in ANGLE (1/N) In the GoogleTest framework, underscores are not allowed. [1] ANGLE has underscores in many test names, and those should be changed. At the same time, add a comment to a test case according to the requirements of the Presubmit check. ```` *************** ANGLE requires a comment describing what a test does. *************** ```` [1] https://github.com/google/googletest/blob/main/docs/faq.md#why-should-test-suite-names-and-test-names-not-contain-underscore Bug: angleproject:377543127 Change-Id: I90e634bd985ca518eca43e2c919a3a8bca91a55b Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/5997835 Reviewed-by: Yuly Novikov <ynovikov@chromium.org> Commit-Queue: Yuly Novikov <ynovikov@chromium.org> Auto-Submit: Ho Cheung <hocheung@chromium.org>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
//
// Copyright 2015 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// string_utils_unittests:
//   Unit tests for the string utils.
//
#include "string_utils.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
using namespace angle;
namespace
{
// Basic SplitString tests
TEST(StringUtilsTest, SplitStringBasics)
{
    std::vector<std::string> r;
    r = SplitString(std::string(), ",:;", KEEP_WHITESPACE, SPLIT_WANT_ALL);
    EXPECT_TRUE(r.empty());
    // Empty separator list
    r = SplitString("hello, world", "", KEEP_WHITESPACE, SPLIT_WANT_ALL);
    ASSERT_EQ(1u, r.size());
    EXPECT_EQ("hello, world", r[0]);
    // Should split on any of the separators.
    r = SplitString("::,,;;", ",:;", KEEP_WHITESPACE, SPLIT_WANT_ALL);
    ASSERT_EQ(7u, r.size());
    for (auto str : r)
        ASSERT_TRUE(str.empty());
    r = SplitString("red, green; blue:", ",:;", TRIM_WHITESPACE, SPLIT_WANT_NONEMPTY);
    ASSERT_EQ(3u, r.size());
    EXPECT_EQ("red", r[0]);
    EXPECT_EQ("green", r[1]);
    EXPECT_EQ("blue", r[2]);
    // Want to split a string along whitespace sequences.
    r = SplitString("  red green   \tblue\n", " \t\n", TRIM_WHITESPACE, SPLIT_WANT_NONEMPTY);
    ASSERT_EQ(3u, r.size());
    EXPECT_EQ("red", r[0]);
    EXPECT_EQ("green", r[1]);
    EXPECT_EQ("blue", r[2]);
    // Weird case of splitting on spaces but not trimming.
    r = SplitString(" red ", " ", TRIM_WHITESPACE, SPLIT_WANT_ALL);
    ASSERT_EQ(3u, r.size());
    EXPECT_EQ("", r[0]);  // Before the first space.
    EXPECT_EQ("red", r[1]);
    EXPECT_EQ("", r[2]);  // After the last space.
}
// Check different whitespace and result types for SplitString
TEST(StringUtilsTest, SplitStringWhitespaceAndResultType)
{
    std::vector<std::string> r;
    // Empty input handling.
    r = SplitString(std::string(), ",", KEEP_WHITESPACE, SPLIT_WANT_ALL);
    EXPECT_TRUE(r.empty());
    r = SplitString(std::string(), ",", KEEP_WHITESPACE, SPLIT_WANT_NONEMPTY);
    EXPECT_TRUE(r.empty());
    // Input string is space and we're trimming.
    r = SplitString(" ", ",", TRIM_WHITESPACE, SPLIT_WANT_ALL);
    ASSERT_EQ(1u, r.size());
    EXPECT_EQ("", r[0]);
    r = SplitString(" ", ",", TRIM_WHITESPACE, SPLIT_WANT_NONEMPTY);
    EXPECT_TRUE(r.empty());
    // Test all 4 combinations of flags on ", ,".
    r = SplitString(", ,", ",", KEEP_WHITESPACE, SPLIT_WANT_ALL);
    ASSERT_EQ(3u, r.size());
    EXPECT_EQ("", r[0]);
    EXPECT_EQ(" ", r[1]);
    EXPECT_EQ("", r[2]);
    r = SplitString(", ,", ",", KEEP_WHITESPACE, SPLIT_WANT_NONEMPTY);
    ASSERT_EQ(1u, r.size());
    ASSERT_EQ(" ", r[0]);
    r = SplitString(", ,", ",", TRIM_WHITESPACE, SPLIT_WANT_ALL);
    ASSERT_EQ(3u, r.size());
    EXPECT_EQ("", r[0]);
    EXPECT_EQ("", r[1]);
    EXPECT_EQ("", r[2]);
    r = SplitString(", ,", ",", TRIM_WHITESPACE, SPLIT_WANT_NONEMPTY);
    ASSERT_TRUE(r.empty());
}
// Tests for SplitStringAlongWhitespace
TEST(StringUtilsTest, SplitStringAlongWhitespace)
{
    {
        // No whitespace.
        std::vector<std::string> r;
        SplitStringAlongWhitespace("abcd", &r);
        ASSERT_THAT(r, testing::ElementsAre("abcd"));
    }
    {
        // Just whitespace.
        std::vector<std::string> r;
        SplitStringAlongWhitespace(" \t", &r);
        ASSERT_THAT(r, testing::ElementsAre());
    }
    {
        // Consecutive whitespace of same type.
        std::vector<std::string> r;
        SplitStringAlongWhitespace("a  b", &r);
        ASSERT_THAT(r, testing::ElementsAre("a", "b"));
    }
    {
        // Consecutive whitespace of different types.
        std::vector<std::string> r;
        SplitStringAlongWhitespace("ab \tcd", &r);
        ASSERT_THAT(r, testing::ElementsAre("ab", "cd"));
    }
    {
        // Non-empty output std::vector.
        std::vector<std::string> r;
        r.push_back("z");
        SplitStringAlongWhitespace("abc", &r);
        ASSERT_THAT(r, testing::ElementsAre("z", "abc"));
    }
}
// Tests for TrimString
TEST(StringUtilsTest, TrimString)
{
    // Basic tests
    EXPECT_EQ("a", TrimString("a", kWhitespaceASCII));
    EXPECT_EQ("a", TrimString(" a", kWhitespaceASCII));
    EXPECT_EQ("a", TrimString("a ", kWhitespaceASCII));
    EXPECT_EQ("a", TrimString(" a ", kWhitespaceASCII));
    // Tests with empty strings
    EXPECT_EQ("", TrimString("", kWhitespaceASCII));
    EXPECT_EQ("", TrimString(" \n\r\t", kWhitespaceASCII));
    EXPECT_EQ(" foo ", TrimString(" foo ", ""));
    // Tests it doesn't removes characters in the middle
    EXPECT_EQ("foo bar", TrimString(" foo bar ", kWhitespaceASCII));
    // Test with non-whitespace trimChars
    EXPECT_EQ(" ", TrimString("foo bar", "abcdefghijklmnopqrstuvwxyz"));
}
// Basic functionality tests for HexStringToUInt
TEST(StringUtilsTest, HexStringToUIntBasic)
{
    unsigned int uintValue;
    std::string emptyString;
    ASSERT_FALSE(HexStringToUInt(emptyString, &uintValue));
    std::string testStringA("0xBADF00D");
    ASSERT_TRUE(HexStringToUInt(testStringA, &uintValue));
    EXPECT_EQ(0xBADF00Du, uintValue);
    std::string testStringB("0xBADFOOD");
    EXPECT_FALSE(HexStringToUInt(testStringB, &uintValue));
    std::string testStringC("BADF00D");
    EXPECT_TRUE(HexStringToUInt(testStringC, &uintValue));
    EXPECT_EQ(0xBADF00Du, uintValue);
    std::string testStringD("0x BADF00D");
    EXPECT_FALSE(HexStringToUInt(testStringD, &uintValue));
}
// Tests for ToCamelCase
TEST(StringUtilsTest, ToCamelCase)
{
    // No underscore in input; expect identical output
    EXPECT_EQ("", ToCamelCase(""));
    EXPECT_EQ("a", ToCamelCase("a"));
    EXPECT_EQ("AbCdEfG", ToCamelCase("AbCdEfG"));
    EXPECT_EQ("aBcDeFg", ToCamelCase("aBcDeFg"));
    // Underscore should be removed and the next character turned upper case
    EXPECT_EQ("", ToCamelCase("_"));
    EXPECT_EQ("aB", ToCamelCase("a_b"));
    EXPECT_EQ("aB", ToCamelCase("a_b"));
    EXPECT_EQ("camelCase", ToCamelCase("camel_case"));
    EXPECT_EQ("abCDeBEfG", ToCamelCase("abCDe_bEfG"));
    // Multiple underscores
    EXPECT_EQ("aBCDEFG", ToCamelCase("a_b_c_d_e_f_g"));
    EXPECT_EQ("abCdEfGh", ToCamelCase("ab_cd_ef_gh"));
    EXPECT_EQ("aShortName", ToCamelCase("a_short_name"));
    EXPECT_EQ("someShortWords", ToCamelCase("some_short_words"));
    EXPECT_EQ("bunchOLetters", ToCamelCase("bunch_o_letters"));
    EXPECT_EQ("whatEndsInE", ToCamelCase("what_ends_in_e"));
    EXPECT_EQ("adjustSrcDstRegionForBlitFramebuffer",
              ToCamelCase("adjust_src_dst_region_for_BlitFramebuffer"));
    // Uppercase after underscore
    EXPECT_EQ("abCDEFGh", ToCamelCase("ab_CD_eF_Gh"));
    EXPECT_EQ("IWasThere", ToCamelCase("I_was_there"));
    EXPECT_EQ("whereDidTHATComeFrom", ToCamelCase("where_did_THAT_come_from"));
    // Digits
    EXPECT_EQ("ab123c4deF5gHi6J", ToCamelCase("ab1_2_3c_4de_f5g_hi6_j"));
    EXPECT_EQ("maxSize16KB", ToCamelCase("max_size_16KB"));
    EXPECT_EQ("supportRGBA8", ToCamelCase("support_RGBA8"));
}
// Basic functionality for NamesMatchWithWildcard.
TEST(StringUtilsTest, NamesMatchWithWildcard)
{
    EXPECT_TRUE(NamesMatchWithWildcard("ASDF", "ASDF"));
    EXPECT_TRUE(NamesMatchWithWildcard("A*", "ASDF"));
    EXPECT_TRUE(NamesMatchWithWildcard("AS*", "ASDF"));
    EXPECT_TRUE(NamesMatchWithWildcard("ASD*", "ASDF"));
    EXPECT_TRUE(NamesMatchWithWildcard("ASDF*", "ASDF"));
    EXPECT_TRUE(NamesMatchWithWildcard("*F", "ASDF"));
    EXPECT_TRUE(NamesMatchWithWildcard("*DF", "ASDF"));
    EXPECT_TRUE(NamesMatchWithWildcard("*SDF", "ASDF"));
    EXPECT_TRUE(NamesMatchWithWildcard("*ASDF", "ASDF"));
    EXPECT_TRUE(NamesMatchWithWildcard("AS**", "ASDF"));
    EXPECT_TRUE(NamesMatchWithWildcard("AS***", "ASDF"));
    EXPECT_TRUE(NamesMatchWithWildcard("**DF", "ASDF"));
    EXPECT_TRUE(NamesMatchWithWildcard("***DF", "ASDF"));
    EXPECT_TRUE(NamesMatchWithWildcard("A*F", "ASDF"));
    EXPECT_TRUE(NamesMatchWithWildcard("A**F", "ASDF"));
    EXPECT_TRUE(NamesMatchWithWildcard("*SD*", "ASDF"));
    EXPECT_TRUE(NamesMatchWithWildcard("*S*D*", "ASDF"));
    EXPECT_TRUE(NamesMatchWithWildcard("ASD*", "ASDF*"));
}
// Note: ReadFileToString is harder to test
class BeginsWithTest : public testing::Test
{
  public:
    BeginsWithTest() : mMode(TestMode::CHAR_ARRAY) {}
    enum class TestMode
    {
        CHAR_ARRAY,
        STRING_AND_CHAR_ARRAY,
        STRING
    };
    void setMode(TestMode mode) { mMode = mode; }
    bool runBeginsWith(const char *str, const char *prefix)
    {
        if (mMode == TestMode::CHAR_ARRAY)
        {
            return BeginsWith(str, prefix);
        }
        if (mMode == TestMode::STRING_AND_CHAR_ARRAY)
        {
            return BeginsWith(std::string(str), prefix);
        }
        return BeginsWith(std::string(str), std::string(prefix));
    }
    void runTest()
    {
        ASSERT_FALSE(runBeginsWith("foo", "bar"));
        ASSERT_FALSE(runBeginsWith("", "foo"));
        ASSERT_FALSE(runBeginsWith("foo", "foobar"));
        ASSERT_TRUE(runBeginsWith("foobar", "foo"));
        ASSERT_TRUE(runBeginsWith("foobar", ""));
        ASSERT_TRUE(runBeginsWith("foo", "foo"));
        ASSERT_TRUE(runBeginsWith("", ""));
    }
  private:
    TestMode mMode;
};
// Test that BeginsWith works correctly for const char * arguments.
TEST_F(BeginsWithTest, CharArrays)
{
    setMode(TestMode::CHAR_ARRAY);
    runTest();
}
// Test that BeginsWith works correctly for std::string and const char * arguments.
TEST_F(BeginsWithTest, StringAndCharArray)
{
    setMode(TestMode::STRING_AND_CHAR_ARRAY);
    runTest();
}
// Test that BeginsWith works correctly for std::string arguments.
TEST_F(BeginsWithTest, Strings)
{
    setMode(TestMode::STRING);
    runTest();
}
class EndsWithTest : public testing::Test
{
  public:
    EndsWithTest() : mMode(TestMode::CHAR_ARRAY) {}
    enum class TestMode
    {
        CHAR_ARRAY,
        STRING_AND_CHAR_ARRAY,
        STRING
    };
    void setMode(TestMode mode) { mMode = mode; }
    bool runEndsWith(const char *str, const char *suffix)
    {
        if (mMode == TestMode::CHAR_ARRAY)
        {
            return EndsWith(str, suffix);
        }
        if (mMode == TestMode::STRING_AND_CHAR_ARRAY)
        {
            return EndsWith(std::string(str), suffix);
        }
        return EndsWith(std::string(str), std::string(suffix));
    }
    void runTest()
    {
        ASSERT_FALSE(EndsWith("foo", "bar"));
        ASSERT_FALSE(EndsWith("", "bar"));
        ASSERT_FALSE(EndsWith("foo", "foobar"));
        ASSERT_TRUE(EndsWith("foobar", "bar"));
        ASSERT_TRUE(EndsWith("foobar", ""));
        ASSERT_TRUE(EndsWith("bar", "bar"));
        ASSERT_TRUE(EndsWith("", ""));
    }
  private:
    TestMode mMode;
};
// Test that EndsWith works correctly for const char * arguments.
TEST_F(EndsWithTest, CharArrays)
{
    setMode(TestMode::CHAR_ARRAY);
    runTest();
}
// Test that EndsWith works correctly for std::string and const char * arguments.
TEST_F(EndsWithTest, StringAndCharArray)
{
    setMode(TestMode::STRING_AND_CHAR_ARRAY);
    runTest();
}
// Test that EndsWith works correctly for std::string arguments.
TEST_F(EndsWithTest, Strings)
{
    setMode(TestMode::STRING);
    runTest();
}
}  // anonymous namespace