2 * Copyright 2011 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
13 // Windows vsnprintf doesn't 0-terminate safely), but is so far
14 // encapsulated in SkString that we can't test it directly.
16 #ifdef SK_BUILD_FOR_WIN
17 #define VSNPRINTF(buffer, size, format, args) \
18 vsnprintf_s(buffer, size, _TRUNCATE, format, args)
20 #define VSNPRINTF vsnprintf
23 #define ARGS_TO_BUFFER(format, buffer, size) \
26 va_start(args, format); \
27 VSNPRINTF(buffer, size, format, args); \
31 static void printfAnalog(char* buffer, int size, const char format[], ...) {
32 ARGS_TO_BUFFER(format, buffer, size);
35 DEF_TEST(String, reporter) {
37 SkString b((size_t)0);
41 REPORTER_ASSERT(reporter, a.isEmpty());
42 REPORTER_ASSERT(reporter, a == b && a == c && a == d);
48 memcpy(d.writable_str(), "helloz", 5);
50 REPORTER_ASSERT(reporter, !a.isEmpty());
51 REPORTER_ASSERT(reporter, a.size() == 5);
52 REPORTER_ASSERT(reporter, a == b && a == c && a == d);
53 REPORTER_ASSERT(reporter, a.equals("hello", 5));
54 REPORTER_ASSERT(reporter, a.equals("hello"));
55 REPORTER_ASSERT(reporter, !a.equals("help"));
57 REPORTER_ASSERT(reporter, a.startsWith("hell"));
58 REPORTER_ASSERT(reporter, a.startsWith('h'));
59 REPORTER_ASSERT(reporter, !a.startsWith( "ell"));
60 REPORTER_ASSERT(reporter, !a.startsWith( 'e'));
61 REPORTER_ASSERT(reporter, a.startsWith(""));
62 REPORTER_ASSERT(reporter, a.endsWith("llo"));
63 REPORTER_ASSERT(reporter, a.endsWith('o'));
64 REPORTER_ASSERT(reporter, !a.endsWith("ll" ));
65 REPORTER_ASSERT(reporter, !a.endsWith('l'));
66 REPORTER_ASSERT(reporter, a.endsWith(""));
67 REPORTER_ASSERT(reporter, a.contains("he"));
68 REPORTER_ASSERT(reporter, a.contains("ll"));
69 REPORTER_ASSERT(reporter, a.contains("lo"));
70 REPORTER_ASSERT(reporter, a.contains("hello"));
71 REPORTER_ASSERT(reporter, !a.contains("hellohello"));
72 REPORTER_ASSERT(reporter, a.contains(""));
73 REPORTER_ASSERT(reporter, a.contains('e'));
74 REPORTER_ASSERT(reporter, !a.contains('z'));
78 SkString g("helloz", 5);
80 REPORTER_ASSERT(reporter, a == e && a == f && a == g);
84 REPORTER_ASSERT(reporter, a != b && a != c && b == c);
87 e.append("worldz", 5);
91 REPORTER_ASSERT(reporter, a.equals("hello world") && a == e && a == f);
95 REPORTER_ASSERT(reporter, a.isEmpty() && b.isEmpty() && a == b);
103 a.appendS32(0x7FFFFFFFL);
104 REPORTER_ASSERT(reporter, a.equals("2147483647"));
106 a.appendS32(0x80000001L);
107 REPORTER_ASSERT(reporter, a.equals("-2147483647"));
109 a.appendS32(0x80000000L);
110 REPORTER_ASSERT(reporter, a.equals("-2147483648"));
113 a.appendU32(0x7FFFFFFFUL);
114 REPORTER_ASSERT(reporter, a.equals("2147483647"));
116 a.appendU32(0x80000001UL);
117 REPORTER_ASSERT(reporter, a.equals("2147483649"));
119 a.appendU32(0xFFFFFFFFUL);
120 REPORTER_ASSERT(reporter, a.equals("4294967295"));
123 a.appendS64(0x7FFFFFFFFFFFFFFFLL, 0);
124 REPORTER_ASSERT(reporter, a.equals("9223372036854775807"));
126 a.appendS64(0x8000000000000001LL, 0);
127 REPORTER_ASSERT(reporter, a.equals("-9223372036854775807"));
129 a.appendS64(0x8000000000000000LL, 0);
130 REPORTER_ASSERT(reporter, a.equals("-9223372036854775808"));
132 a.appendS64(0x0000000001000000LL, 15);
133 REPORTER_ASSERT(reporter, a.equals("000000016777216"));
135 a.appendS64(0xFFFFFFFFFF000000LL, 15);
136 REPORTER_ASSERT(reporter, a.equals("-000000016777216"));
139 a.appendU64(0x7FFFFFFFFFFFFFFFULL, 0);
140 REPORTER_ASSERT(reporter, a.equals("9223372036854775807"));
142 a.appendU64(0x8000000000000001ULL, 0);
143 REPORTER_ASSERT(reporter, a.equals("9223372036854775809"));
145 a.appendU64(0xFFFFFFFFFFFFFFFFULL, 0);
146 REPORTER_ASSERT(reporter, a.equals("18446744073709551615"));
148 a.appendU64(0x0000000001000000ULL, 15);
149 REPORTER_ASSERT(reporter, a.equals("000000016777216"));
151 static const struct {
157 { -SK_Scalar1, "-1" },
158 { SK_Scalar1/2, "0.5" },
159 #ifdef SK_BUILD_FOR_WIN
160 { 3.4028234e38f, "3.4028235e+038" },
161 { -3.4028234e38f, "-3.4028235e+038" },
163 { 3.4028234e38f, "3.4028235e+38" },
164 { -3.4028234e38f, "-3.4028235e+38" },
167 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) {
169 a.appendScalar(gRec[i].fValue);
170 REPORTER_ASSERT(reporter, a.size() <= SkStrAppendScalar_MaxSize);
171 // SkDebugf(" received <%s> expected <%s>\n", a.c_str(), gRec[i].fString);
172 REPORTER_ASSERT(reporter, a.equals(gRec[i].fString));
175 REPORTER_ASSERT(reporter, SkStringPrintf("%i", 0).equals("0"));
178 memset(buffer, 'a', 40);
179 REPORTER_ASSERT(reporter, buffer[18] == 'a');
180 REPORTER_ASSERT(reporter, buffer[19] == 'a');
181 REPORTER_ASSERT(reporter, buffer[20] == 'a');
182 printfAnalog(buffer, 20, "%30d", 0);
183 REPORTER_ASSERT(reporter, buffer[18] == ' ');
184 REPORTER_ASSERT(reporter, buffer[19] == 0);
185 REPORTER_ASSERT(reporter, buffer[20] == 'a');
189 DEF_TEST(String_SkStrSplit, r) {
190 SkTArray<SkString> results;
192 SkStrSplit("a-_b_c-dee--f-_-_-g-", "-_", &results);
193 REPORTER_ASSERT(r, results.count() == 6);
194 REPORTER_ASSERT(r, results[0].equals("a"));
195 REPORTER_ASSERT(r, results[1].equals("b"));
196 REPORTER_ASSERT(r, results[2].equals("c"));
197 REPORTER_ASSERT(r, results[3].equals("dee"));
198 REPORTER_ASSERT(r, results[4].equals("f"));
199 REPORTER_ASSERT(r, results[5].equals("g"));