diff --git a/tests/StringTest.cpp b/tests/StringTest.cpp index e20732a186..0a7d375a07 100644 --- a/tests/StringTest.cpp +++ b/tests/StringTest.cpp @@ -162,33 +162,35 @@ DEF_TEST(String, reporter) { } REPORTER_ASSERT(reporter, SkStringPrintf("%i", 0).equals("0")); +} +static void assert_2000_spaces(skiatest::Reporter* reporter, const SkString& str) { + REPORTER_ASSERT(reporter, str.size() == 2000); + for (size_t i = 0; i < str.size(); ++i) { + REPORTER_ASSERT(reporter, str[i] == ' '); + } +} + +DEF_TEST(String_overflow, reporter) { // 2000 is larger than the static buffer size inside SkString.cpp - a = SkStringPrintf("%2000s", " "); - REPORTER_ASSERT(reporter, a.size() == 2000); - for (size_t i = 0; i < a.size(); ++i) { - if (a[i] != ' ') { - ERRORF(reporter, "SkStringPrintf fail: a[%zu] = '%c'", i, a[i]); - break; - } - } - a.reset(); + SkString a = SkStringPrintf("%2000s", " "); + assert_2000_spaces(reporter, a); + + a = "X"; a.printf("%2000s", " "); - REPORTER_ASSERT(reporter, a.size() == 2000); - for (size_t i = 0; i < a.size(); ++i) { - if (a[i] != ' ') { - ERRORF(reporter, "SkString::printf fail: a[%zu] = '%c'", i, a[i]); - break; - } - } - a.appendf("%2000s", " "); - REPORTER_ASSERT(reporter, a.size() == 4000); - for (size_t i = 0; i < a.size(); ++i) { - if (a[i] != ' ') { - ERRORF(reporter, "SkString::appendf fail: a[%zu] = '%c'", i, a[i]); - break; - } - } + assert_2000_spaces(reporter, a); + + a = "X"; + a.appendf("%1999s", " "); + REPORTER_ASSERT(reporter, a[0] == 'X'); + a[0] = ' '; + assert_2000_spaces(reporter, a); + + a = "X"; + a.prependf("%1999s", " "); + REPORTER_ASSERT(reporter, a[1999] == 'X'); + a[1999] = ' '; + assert_2000_spaces(reporter, a); } DEF_TEST(String_SkStrSplit, r) { @@ -320,3 +322,84 @@ DEF_TEST(String_fromUTF16, r) { REPORTER_ASSERT(r, SkStringFromUTF16(test3, SK_ARRAY_COUNT(test3)).equals("αβγδε ζηθικ")); } +static void test_va_list_print(skiatest::Reporter* r, const char format[], ...) { + va_list args; + va_start(args, format); + + SkString str("123"); + str.printVAList(format, args); + REPORTER_ASSERT(r, str.equals("hello world")); + + va_end(args); +} + +static void test_va_list_append(skiatest::Reporter* r, const char format[], ...) { + va_list args; + va_start(args, format); + + SkString str("123"); + str.appendVAList(format, args); + REPORTER_ASSERT(r, str.equals("123hello world")); + + va_end(args); +} + +static void test_va_list_prepend(skiatest::Reporter* r, const char format[], ...) { + va_list args; + va_start(args, format); + + SkString str("123"); + str.prependVAList(format, args); + REPORTER_ASSERT(r, str.equals("hello world123")); + + va_end(args); +} + +DEF_TEST(String_VAList, r) { + test_va_list_print(r, "%s %c%c%c%c%c", "hello", 'w', 'o', 'r', 'l', 'd'); + test_va_list_append(r, "%s %c%c%c%c%c", "hello", 'w', 'o', 'r', 'l', 'd'); + test_va_list_prepend(r, "%s %c%c%c%c%c", "hello", 'w', 'o', 'r', 'l', 'd'); +} + +static void test_va_list_overflow_print(skiatest::Reporter* r, const char format[], ...) { + va_list args; + va_start(args, format); + + SkString str("X"); + str.printVAList(format, args); + assert_2000_spaces(r, str); + + va_end(args); +} + +static void test_va_list_overflow_append(skiatest::Reporter* r, const char format[], ...) { + va_list args; + va_start(args, format); + + SkString str("X"); + str.appendVAList(format, args); + REPORTER_ASSERT(r, str[0] == 'X'); + str[0] = ' '; + assert_2000_spaces(r, str); + + va_end(args); +} + +static void test_va_list_overflow_prepend(skiatest::Reporter* r, const char format[], ...) { + va_list args; + va_start(args, format); + + SkString str("X"); + str.prependVAList(format, args); + REPORTER_ASSERT(r, str[1999] == 'X'); + str[1999] = ' '; + assert_2000_spaces(r, str); + + va_end(args); +} + +DEF_TEST(String_VAList_overflow, r) { + test_va_list_overflow_print(r, "%2000s", " "); + test_va_list_overflow_append(r, "%1999s", " "); + test_va_list_overflow_prepend(r, "%1999s", " "); +}