1 2 /* 3 * Copyright 2011 Google Inc. 4 * 5 * Use of this source code is governed by a BSD-style license that can be 6 * found in the LICENSE file. 7 */ 8 #include "Test.h" 9 #include "SkString.h" 10 #include <stdarg.h> 11 12 13 // Windows vsnprintf doesn't 0-terminate safely), but is so far 14 // encapsulated in SkString that we can't test it directly. 15 16 #ifdef SK_BUILD_FOR_WIN 17 #define VSNPRINTF(buffer, size, format, args) \ 18 vsnprintf_s(buffer, size, _TRUNCATE, format, args) 19 #else 20 #define VSNPRINTF vsnprintf 21 #endif 22 23 #define ARGS_TO_BUFFER(format, buffer, size) \ 24 do { \ 25 va_list args; \ 26 va_start(args, format); \ 27 VSNPRINTF(buffer, size, format, args); \ 28 va_end(args); \ 29 } while (0) 30 31 static void printfAnalog(char* buffer, int size, const char format[], ...) { 32 ARGS_TO_BUFFER(format, buffer, size); 33 } 34 35 36 37 static void TestString(skiatest::Reporter* reporter) { 38 SkString a; 39 SkString b((size_t)0); 40 SkString c(""); 41 SkString d(NULL, 0); 42 43 REPORTER_ASSERT(reporter, a.isEmpty()); 44 REPORTER_ASSERT(reporter, a == b && a == c && a == d); 45 46 a.set("hello"); 47 b.set("hellox", 5); 48 c.set(a); 49 d.resize(5); 50 memcpy(d.writable_str(), "helloz", 5); 51 52 REPORTER_ASSERT(reporter, !a.isEmpty()); 53 REPORTER_ASSERT(reporter, a.size() == 5); 54 REPORTER_ASSERT(reporter, a == b && a == c && a == d); 55 REPORTER_ASSERT(reporter, a.equals("hello", 5)); 56 REPORTER_ASSERT(reporter, a.equals("hello")); 57 REPORTER_ASSERT(reporter, !a.equals("help")); 58 59 REPORTER_ASSERT(reporter, a.startsWith("hell")); 60 REPORTER_ASSERT(reporter, a.startsWith('h')); 61 REPORTER_ASSERT(reporter, !a.startsWith( "ell")); 62 REPORTER_ASSERT(reporter, !a.startsWith( 'e')); 63 REPORTER_ASSERT(reporter, a.startsWith("")); 64 REPORTER_ASSERT(reporter, a.endsWith("llo")); 65 REPORTER_ASSERT(reporter, a.endsWith('o')); 66 REPORTER_ASSERT(reporter, !a.endsWith("ll" )); 67 REPORTER_ASSERT(reporter, !a.endsWith('l')); 68 REPORTER_ASSERT(reporter, a.endsWith("")); 69 REPORTER_ASSERT(reporter, a.contains("he")); 70 REPORTER_ASSERT(reporter, a.contains("ll")); 71 REPORTER_ASSERT(reporter, a.contains("lo")); 72 REPORTER_ASSERT(reporter, a.contains("hello")); 73 REPORTER_ASSERT(reporter, !a.contains("hellohello")); 74 REPORTER_ASSERT(reporter, a.contains("")); 75 REPORTER_ASSERT(reporter, a.contains('e')); 76 REPORTER_ASSERT(reporter, !a.contains('z')); 77 78 SkString e(a); 79 SkString f("hello"); 80 SkString g("helloz", 5); 81 82 REPORTER_ASSERT(reporter, a == e && a == f && a == g); 83 84 b.set("world"); 85 c = b; 86 REPORTER_ASSERT(reporter, a != b && a != c && b == c); 87 88 a.append(" world"); 89 e.append("worldz", 5); 90 e.insert(5, " "); 91 f.set("world"); 92 f.prepend("hello "); 93 REPORTER_ASSERT(reporter, a.equals("hello world") && a == e && a == f); 94 95 a.reset(); 96 b.resize(0); 97 REPORTER_ASSERT(reporter, a.isEmpty() && b.isEmpty() && a == b); 98 99 a.set("a"); 100 a.set("ab"); 101 a.set("abc"); 102 a.set("abcd"); 103 104 a.set(""); 105 a.appendS64(72036854775808LL, 0); 106 REPORTER_ASSERT(reporter, a.equals("72036854775808")); 107 108 a.set(""); 109 a.appendS64(-1844674407370LL, 0); 110 REPORTER_ASSERT(reporter, a.equals("-1844674407370")); 111 112 a.set(""); 113 a.appendS64(73709551616LL, 15); 114 REPORTER_ASSERT(reporter, a.equals("000073709551616")); 115 116 a.set(""); 117 a.appendS64(-429496729612LL, 15); 118 REPORTER_ASSERT(reporter, a.equals("-000429496729612")); 119 120 static const struct { 121 SkScalar fValue; 122 const char* fString; 123 } gRec[] = { 124 { 0, "0" }, 125 { SK_Scalar1, "1" }, 126 { -SK_Scalar1, "-1" }, 127 { SK_Scalar1/2, "0.5" }, 128 #ifdef SK_SCALAR_IS_FLOAT 129 #ifdef SK_BUILD_FOR_WIN 130 { 3.4028234e38f, "3.4028235e+038" }, 131 { -3.4028234e38f, "-3.4028235e+038" }, 132 #else 133 { 3.4028234e38f, "3.4028235e+38" }, 134 { -3.4028234e38f, "-3.4028235e+38" }, 135 #endif 136 #endif 137 }; 138 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) { 139 a.reset(); 140 a.appendScalar(gRec[i].fValue); 141 REPORTER_ASSERT(reporter, a.size() <= SkStrAppendScalar_MaxSize); 142 // SkDebugf(" received <%s> expected <%s>\n", a.c_str(), gRec[i].fString); 143 REPORTER_ASSERT(reporter, a.equals(gRec[i].fString)); 144 } 145 146 REPORTER_ASSERT(reporter, SkStringPrintf("%i", 0).equals("0")); 147 148 char buffer [40]; 149 memset(buffer, 'a', 40); 150 REPORTER_ASSERT(reporter, buffer[18] == 'a'); 151 REPORTER_ASSERT(reporter, buffer[19] == 'a'); 152 REPORTER_ASSERT(reporter, buffer[20] == 'a'); 153 printfAnalog(buffer, 20, "%30d", 0); 154 REPORTER_ASSERT(reporter, buffer[18] == ' '); 155 REPORTER_ASSERT(reporter, buffer[19] == 0); 156 REPORTER_ASSERT(reporter, buffer[20] == 'a'); 157 158 } 159 160 #include "TestClassDef.h" 161 DEFINE_TESTCLASS("String", StringTestClass, TestString) 162