blob: 3076719040459e98059449e9761115d445c8c7fa [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
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 */
reed@android.comd8730ea2009-02-27 22:06:06 +00008#include "Test.h"
9#include "SkString.h"
tomhudson@google.com47e0a092011-07-08 17:49:22 +000010#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
caryclark@google.com42639cd2012-06-06 12:03:39 +000031static void printfAnalog(char* buffer, int size, const char format[], ...) {
tomhudson@google.com47e0a092011-07-08 17:49:22 +000032 ARGS_TO_BUFFER(format, buffer, size);
33}
34
35
reed@android.comd8730ea2009-02-27 22:06:06 +000036
37static 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
epoger@google.comc4ae9742012-04-27 17:11:31 +000059 REPORTER_ASSERT(reporter, a.startsWith("hell"));
epoger@google.come8ebeb12012-10-29 16:42:11 +000060 REPORTER_ASSERT(reporter, a.startsWith('h'));
epoger@google.comc4ae9742012-04-27 17:11:31 +000061 REPORTER_ASSERT(reporter, !a.startsWith( "ell"));
epoger@google.come8ebeb12012-10-29 16:42:11 +000062 REPORTER_ASSERT(reporter, !a.startsWith( 'e'));
epoger@google.comc4ae9742012-04-27 17:11:31 +000063 REPORTER_ASSERT(reporter, a.startsWith(""));
64 REPORTER_ASSERT(reporter, a.endsWith("llo"));
epoger@google.come8ebeb12012-10-29 16:42:11 +000065 REPORTER_ASSERT(reporter, a.endsWith('o'));
epoger@google.comc4ae9742012-04-27 17:11:31 +000066 REPORTER_ASSERT(reporter, !a.endsWith("ll" ));
epoger@google.come8ebeb12012-10-29 16:42:11 +000067 REPORTER_ASSERT(reporter, !a.endsWith('l'));
epoger@google.comc4ae9742012-04-27 17:11:31 +000068 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(""));
epoger@google.come8ebeb12012-10-29 16:42:11 +000075 REPORTER_ASSERT(reporter, a.contains('e'));
76 REPORTER_ASSERT(reporter, !a.contains('z'));
rmistry@google.comd6176b02012-08-23 18:14:13 +000077
reed@android.comd8730ea2009-02-27 22:06:06 +000078 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");
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +0000103
104 a.set("");
epoger@google.comd88a3d82013-06-19 18:27:20 +0000105 a.appendS32(0x7FFFFFFFL);
106 REPORTER_ASSERT(reporter, a.equals("2147483647"));
107 a.set("");
108 a.appendS32(0x80000001L);
109 REPORTER_ASSERT(reporter, a.equals("-2147483647"));
110 a.set("");
111 a.appendS32(0x80000000L);
112 REPORTER_ASSERT(reporter, a.equals("-2147483648"));
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +0000113
114 a.set("");
epoger@google.comd88a3d82013-06-19 18:27:20 +0000115 a.appendU32(0x7FFFFFFFUL);
116 REPORTER_ASSERT(reporter, a.equals("2147483647"));
117 a.set("");
118 a.appendU32(0x80000001UL);
119 REPORTER_ASSERT(reporter, a.equals("2147483649"));
120 a.set("");
121 a.appendU32(0xFFFFFFFFUL);
122 REPORTER_ASSERT(reporter, a.equals("4294967295"));
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +0000123
124 a.set("");
epoger@google.comd88a3d82013-06-19 18:27:20 +0000125 a.appendS64(0x7FFFFFFFFFFFFFFFLL, 0);
126 REPORTER_ASSERT(reporter, a.equals("9223372036854775807"));
127 a.set("");
128 a.appendS64(0x8000000000000001LL, 0);
129 REPORTER_ASSERT(reporter, a.equals("-9223372036854775807"));
130 a.set("");
131 a.appendS64(0x8000000000000000LL, 0);
132 REPORTER_ASSERT(reporter, a.equals("-9223372036854775808"));
133 a.set("");
134 a.appendS64(0x0000000001000000LL, 15);
135 REPORTER_ASSERT(reporter, a.equals("000000016777216"));
136 a.set("");
137 a.appendS64(0xFFFFFFFFFF000000LL, 15);
138 REPORTER_ASSERT(reporter, a.equals("-000000016777216"));
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +0000139
140 a.set("");
epoger@google.comd88a3d82013-06-19 18:27:20 +0000141 a.appendU64(0x7FFFFFFFFFFFFFFFULL, 0);
142 REPORTER_ASSERT(reporter, a.equals("9223372036854775807"));
143 a.set("");
144 a.appendU64(0x8000000000000001ULL, 0);
145 REPORTER_ASSERT(reporter, a.equals("9223372036854775809"));
146 a.set("");
147 a.appendU64(0xFFFFFFFFFFFFFFFFULL, 0);
148 REPORTER_ASSERT(reporter, a.equals("18446744073709551615"));
149 a.set("");
150 a.appendU64(0x0000000001000000ULL, 15);
151 REPORTER_ASSERT(reporter, a.equals("000000016777216"));
reed@google.comfa06e522011-02-28 21:29:58 +0000152
153 static const struct {
154 SkScalar fValue;
155 const char* fString;
156 } gRec[] = {
157 { 0, "0" },
158 { SK_Scalar1, "1" },
159 { -SK_Scalar1, "-1" },
160 { SK_Scalar1/2, "0.5" },
reed@google.com8072e4f2011-03-01 15:44:08 +0000161#ifdef SK_SCALAR_IS_FLOAT
tomhudson@google.com47e0a092011-07-08 17:49:22 +0000162 #ifdef SK_BUILD_FOR_WIN
163 { 3.4028234e38f, "3.4028235e+038" },
164 { -3.4028234e38f, "-3.4028235e+038" },
165 #else
reed@google.com8072e4f2011-03-01 15:44:08 +0000166 { 3.4028234e38f, "3.4028235e+38" },
167 { -3.4028234e38f, "-3.4028235e+38" },
tomhudson@google.com47e0a092011-07-08 17:49:22 +0000168 #endif
reed@google.com8072e4f2011-03-01 15:44:08 +0000169#endif
reed@google.comfa06e522011-02-28 21:29:58 +0000170 };
171 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) {
172 a.reset();
173 a.appendScalar(gRec[i].fValue);
reed@google.com8072e4f2011-03-01 15:44:08 +0000174 REPORTER_ASSERT(reporter, a.size() <= SkStrAppendScalar_MaxSize);
175// SkDebugf(" received <%s> expected <%s>\n", a.c_str(), gRec[i].fString);
reed@google.comfa06e522011-02-28 21:29:58 +0000176 REPORTER_ASSERT(reporter, a.equals(gRec[i].fString));
177 }
tomhudson@google.com3a1f6a02011-06-30 14:39:52 +0000178
179 REPORTER_ASSERT(reporter, SkStringPrintf("%i", 0).equals("0"));
tomhudson@google.com47e0a092011-07-08 17:49:22 +0000180
181 char buffer [40];
182 memset(buffer, 'a', 40);
183 REPORTER_ASSERT(reporter, buffer[18] == 'a');
184 REPORTER_ASSERT(reporter, buffer[19] == 'a');
185 REPORTER_ASSERT(reporter, buffer[20] == 'a');
186 printfAnalog(buffer, 20, "%30d", 0);
187 REPORTER_ASSERT(reporter, buffer[18] == ' ');
188 REPORTER_ASSERT(reporter, buffer[19] == 0);
189 REPORTER_ASSERT(reporter, buffer[20] == 'a');
rmistry@google.comd6176b02012-08-23 18:14:13 +0000190
reed@android.comd8730ea2009-02-27 22:06:06 +0000191}
192
193#include "TestClassDef.h"
194DEFINE_TESTCLASS("String", StringTestClass, TestString)