blob: ceb4df5308df0559796ee6217a6b9b059e666249 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +00007
reed@android.comd8730ea2009-02-27 22:06:06 +00008#include "Test.h"
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +00009#include "TestClassDef.h"
reed@android.comd8730ea2009-02-27 22:06:06 +000010#include "SkString.h"
tomhudson@google.com47e0a092011-07-08 17:49:22 +000011#include <stdarg.h>
bungeman@google.comfab44db2013-10-11 18:50:45 +000012#include <stdio.h>
tomhudson@google.com47e0a092011-07-08 17:49:22 +000013
14// Windows vsnprintf doesn't 0-terminate safely), but is so far
15// encapsulated in SkString that we can't test it directly.
16
17#ifdef SK_BUILD_FOR_WIN
18 #define VSNPRINTF(buffer, size, format, args) \
19 vsnprintf_s(buffer, size, _TRUNCATE, format, args)
20#else
21 #define VSNPRINTF vsnprintf
22#endif
23
24#define ARGS_TO_BUFFER(format, buffer, size) \
25 do { \
26 va_list args; \
27 va_start(args, format); \
28 VSNPRINTF(buffer, size, format, args); \
29 va_end(args); \
30 } while (0)
31
caryclark@google.com42639cd2012-06-06 12:03:39 +000032static void printfAnalog(char* buffer, int size, const char format[], ...) {
tomhudson@google.com47e0a092011-07-08 17:49:22 +000033 ARGS_TO_BUFFER(format, buffer, size);
34}
35
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +000036DEF_TEST(String, reporter) {
reed@android.comd8730ea2009-02-27 22:06:06 +000037 SkString a;
38 SkString b((size_t)0);
39 SkString c("");
40 SkString d(NULL, 0);
41
42 REPORTER_ASSERT(reporter, a.isEmpty());
43 REPORTER_ASSERT(reporter, a == b && a == c && a == d);
44
45 a.set("hello");
46 b.set("hellox", 5);
47 c.set(a);
48 d.resize(5);
49 memcpy(d.writable_str(), "helloz", 5);
50
51 REPORTER_ASSERT(reporter, !a.isEmpty());
52 REPORTER_ASSERT(reporter, a.size() == 5);
53 REPORTER_ASSERT(reporter, a == b && a == c && a == d);
54 REPORTER_ASSERT(reporter, a.equals("hello", 5));
55 REPORTER_ASSERT(reporter, a.equals("hello"));
56 REPORTER_ASSERT(reporter, !a.equals("help"));
57
epoger@google.comc4ae9742012-04-27 17:11:31 +000058 REPORTER_ASSERT(reporter, a.startsWith("hell"));
epoger@google.come8ebeb12012-10-29 16:42:11 +000059 REPORTER_ASSERT(reporter, a.startsWith('h'));
epoger@google.comc4ae9742012-04-27 17:11:31 +000060 REPORTER_ASSERT(reporter, !a.startsWith( "ell"));
epoger@google.come8ebeb12012-10-29 16:42:11 +000061 REPORTER_ASSERT(reporter, !a.startsWith( 'e'));
epoger@google.comc4ae9742012-04-27 17:11:31 +000062 REPORTER_ASSERT(reporter, a.startsWith(""));
63 REPORTER_ASSERT(reporter, a.endsWith("llo"));
epoger@google.come8ebeb12012-10-29 16:42:11 +000064 REPORTER_ASSERT(reporter, a.endsWith('o'));
epoger@google.comc4ae9742012-04-27 17:11:31 +000065 REPORTER_ASSERT(reporter, !a.endsWith("ll" ));
epoger@google.come8ebeb12012-10-29 16:42:11 +000066 REPORTER_ASSERT(reporter, !a.endsWith('l'));
epoger@google.comc4ae9742012-04-27 17:11:31 +000067 REPORTER_ASSERT(reporter, a.endsWith(""));
68 REPORTER_ASSERT(reporter, a.contains("he"));
69 REPORTER_ASSERT(reporter, a.contains("ll"));
70 REPORTER_ASSERT(reporter, a.contains("lo"));
71 REPORTER_ASSERT(reporter, a.contains("hello"));
72 REPORTER_ASSERT(reporter, !a.contains("hellohello"));
73 REPORTER_ASSERT(reporter, a.contains(""));
epoger@google.come8ebeb12012-10-29 16:42:11 +000074 REPORTER_ASSERT(reporter, a.contains('e'));
75 REPORTER_ASSERT(reporter, !a.contains('z'));
rmistry@google.comd6176b02012-08-23 18:14:13 +000076
reed@android.comd8730ea2009-02-27 22:06:06 +000077 SkString e(a);
78 SkString f("hello");
79 SkString g("helloz", 5);
80
81 REPORTER_ASSERT(reporter, a == e && a == f && a == g);
82
83 b.set("world");
84 c = b;
85 REPORTER_ASSERT(reporter, a != b && a != c && b == c);
86
87 a.append(" world");
88 e.append("worldz", 5);
89 e.insert(5, " ");
90 f.set("world");
91 f.prepend("hello ");
92 REPORTER_ASSERT(reporter, a.equals("hello world") && a == e && a == f);
93
94 a.reset();
95 b.resize(0);
96 REPORTER_ASSERT(reporter, a.isEmpty() && b.isEmpty() && a == b);
97
98 a.set("a");
99 a.set("ab");
100 a.set("abc");
101 a.set("abcd");
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +0000102
103 a.set("");
epoger@google.comd88a3d82013-06-19 18:27:20 +0000104 a.appendS32(0x7FFFFFFFL);
105 REPORTER_ASSERT(reporter, a.equals("2147483647"));
106 a.set("");
107 a.appendS32(0x80000001L);
108 REPORTER_ASSERT(reporter, a.equals("-2147483647"));
109 a.set("");
110 a.appendS32(0x80000000L);
111 REPORTER_ASSERT(reporter, a.equals("-2147483648"));
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +0000112
113 a.set("");
epoger@google.comd88a3d82013-06-19 18:27:20 +0000114 a.appendU32(0x7FFFFFFFUL);
115 REPORTER_ASSERT(reporter, a.equals("2147483647"));
116 a.set("");
117 a.appendU32(0x80000001UL);
118 REPORTER_ASSERT(reporter, a.equals("2147483649"));
119 a.set("");
120 a.appendU32(0xFFFFFFFFUL);
121 REPORTER_ASSERT(reporter, a.equals("4294967295"));
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +0000122
123 a.set("");
epoger@google.comd88a3d82013-06-19 18:27:20 +0000124 a.appendS64(0x7FFFFFFFFFFFFFFFLL, 0);
125 REPORTER_ASSERT(reporter, a.equals("9223372036854775807"));
126 a.set("");
127 a.appendS64(0x8000000000000001LL, 0);
128 REPORTER_ASSERT(reporter, a.equals("-9223372036854775807"));
129 a.set("");
130 a.appendS64(0x8000000000000000LL, 0);
131 REPORTER_ASSERT(reporter, a.equals("-9223372036854775808"));
132 a.set("");
133 a.appendS64(0x0000000001000000LL, 15);
134 REPORTER_ASSERT(reporter, a.equals("000000016777216"));
135 a.set("");
136 a.appendS64(0xFFFFFFFFFF000000LL, 15);
137 REPORTER_ASSERT(reporter, a.equals("-000000016777216"));
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +0000138
139 a.set("");
epoger@google.comd88a3d82013-06-19 18:27:20 +0000140 a.appendU64(0x7FFFFFFFFFFFFFFFULL, 0);
141 REPORTER_ASSERT(reporter, a.equals("9223372036854775807"));
142 a.set("");
143 a.appendU64(0x8000000000000001ULL, 0);
144 REPORTER_ASSERT(reporter, a.equals("9223372036854775809"));
145 a.set("");
146 a.appendU64(0xFFFFFFFFFFFFFFFFULL, 0);
147 REPORTER_ASSERT(reporter, a.equals("18446744073709551615"));
148 a.set("");
149 a.appendU64(0x0000000001000000ULL, 15);
150 REPORTER_ASSERT(reporter, a.equals("000000016777216"));
reed@google.comfa06e522011-02-28 21:29:58 +0000151
152 static const struct {
153 SkScalar fValue;
154 const char* fString;
155 } gRec[] = {
156 { 0, "0" },
157 { SK_Scalar1, "1" },
158 { -SK_Scalar1, "-1" },
159 { SK_Scalar1/2, "0.5" },
tomhudson@google.com47e0a092011-07-08 17:49:22 +0000160 #ifdef SK_BUILD_FOR_WIN
161 { 3.4028234e38f, "3.4028235e+038" },
162 { -3.4028234e38f, "-3.4028235e+038" },
163 #else
reed@google.com8072e4f2011-03-01 15:44:08 +0000164 { 3.4028234e38f, "3.4028235e+38" },
165 { -3.4028234e38f, "-3.4028235e+38" },
tomhudson@google.com47e0a092011-07-08 17:49:22 +0000166 #endif
reed@google.comfa06e522011-02-28 21:29:58 +0000167 };
168 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) {
169 a.reset();
170 a.appendScalar(gRec[i].fValue);
reed@google.com8072e4f2011-03-01 15:44:08 +0000171 REPORTER_ASSERT(reporter, a.size() <= SkStrAppendScalar_MaxSize);
172// SkDebugf(" received <%s> expected <%s>\n", a.c_str(), gRec[i].fString);
reed@google.comfa06e522011-02-28 21:29:58 +0000173 REPORTER_ASSERT(reporter, a.equals(gRec[i].fString));
174 }
tomhudson@google.com3a1f6a02011-06-30 14:39:52 +0000175
176 REPORTER_ASSERT(reporter, SkStringPrintf("%i", 0).equals("0"));
tomhudson@google.com47e0a092011-07-08 17:49:22 +0000177
178 char buffer [40];
179 memset(buffer, 'a', 40);
180 REPORTER_ASSERT(reporter, buffer[18] == 'a');
181 REPORTER_ASSERT(reporter, buffer[19] == 'a');
182 REPORTER_ASSERT(reporter, buffer[20] == 'a');
183 printfAnalog(buffer, 20, "%30d", 0);
184 REPORTER_ASSERT(reporter, buffer[18] == ' ');
185 REPORTER_ASSERT(reporter, buffer[19] == 0);
186 REPORTER_ASSERT(reporter, buffer[20] == 'a');
rmistry@google.comd6176b02012-08-23 18:14:13 +0000187
reed@android.comd8730ea2009-02-27 22:06:06 +0000188}
189
rmistry@google.comd6bab022013-12-02 13:50:38 +0000190DEF_TEST(String_SkStrSplit, r) {
191 SkTArray<SkString> results;
192
193 SkStrSplit("a-_b_c-dee--f-_-_-g-", "-_", &results);
194 REPORTER_ASSERT(r, results.count() == 6);
195 REPORTER_ASSERT(r, results[0].equals("a"));
196 REPORTER_ASSERT(r, results[1].equals("b"));
197 REPORTER_ASSERT(r, results[2].equals("c"));
198 REPORTER_ASSERT(r, results[3].equals("dee"));
199 REPORTER_ASSERT(r, results[4].equals("f"));
200 REPORTER_ASSERT(r, results[5].equals("g"));
201}