blob: d8814224c0842f34778b80d537f2938db0e74e22 [file] [log] [blame]
reed@android.comd8730ea2009-02-27 22:06:06 +00001#include "Test.h"
2#include "SkString.h"
tomhudson@google.com47e0a092011-07-08 17:49:22 +00003#include <stdarg.h>
4
5
6// Windows vsnprintf doesn't 0-terminate safely), but is so far
7// encapsulated in SkString that we can't test it directly.
8
9#ifdef SK_BUILD_FOR_WIN
10 #define VSNPRINTF(buffer, size, format, args) \
11 vsnprintf_s(buffer, size, _TRUNCATE, format, args)
12#else
13 #define VSNPRINTF vsnprintf
14#endif
15
16#define ARGS_TO_BUFFER(format, buffer, size) \
17 do { \
18 va_list args; \
19 va_start(args, format); \
20 VSNPRINTF(buffer, size, format, args); \
21 va_end(args); \
22 } while (0)
23
24void printfAnalog(char* buffer, int size, const char format[], ...) {
25 ARGS_TO_BUFFER(format, buffer, size);
26}
27
28
reed@android.comd8730ea2009-02-27 22:06:06 +000029
30static void TestString(skiatest::Reporter* reporter) {
31 SkString a;
32 SkString b((size_t)0);
33 SkString c("");
34 SkString d(NULL, 0);
35
36 REPORTER_ASSERT(reporter, a.isEmpty());
37 REPORTER_ASSERT(reporter, a == b && a == c && a == d);
38
39 a.set("hello");
40 b.set("hellox", 5);
41 c.set(a);
42 d.resize(5);
43 memcpy(d.writable_str(), "helloz", 5);
44
45 REPORTER_ASSERT(reporter, !a.isEmpty());
46 REPORTER_ASSERT(reporter, a.size() == 5);
47 REPORTER_ASSERT(reporter, a == b && a == c && a == d);
48 REPORTER_ASSERT(reporter, a.equals("hello", 5));
49 REPORTER_ASSERT(reporter, a.equals("hello"));
50 REPORTER_ASSERT(reporter, !a.equals("help"));
51
52 SkString e(a);
53 SkString f("hello");
54 SkString g("helloz", 5);
55
56 REPORTER_ASSERT(reporter, a == e && a == f && a == g);
57
58 b.set("world");
59 c = b;
60 REPORTER_ASSERT(reporter, a != b && a != c && b == c);
61
62 a.append(" world");
63 e.append("worldz", 5);
64 e.insert(5, " ");
65 f.set("world");
66 f.prepend("hello ");
67 REPORTER_ASSERT(reporter, a.equals("hello world") && a == e && a == f);
68
69 a.reset();
70 b.resize(0);
71 REPORTER_ASSERT(reporter, a.isEmpty() && b.isEmpty() && a == b);
72
73 a.set("a");
74 a.set("ab");
75 a.set("abc");
76 a.set("abcd");
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000077
78 a.set("");
79 a.appendS64(72036854775808LL, 0);
80 REPORTER_ASSERT(reporter, a.equals("72036854775808"));
81
82 a.set("");
83 a.appendS64(-1844674407370LL, 0);
84 REPORTER_ASSERT(reporter, a.equals("-1844674407370"));
85
86 a.set("");
87 a.appendS64(73709551616LL, 15);
88 REPORTER_ASSERT(reporter, a.equals("000073709551616"));
89
90 a.set("");
91 a.appendS64(-429496729612LL, 15);
92 REPORTER_ASSERT(reporter, a.equals("-000429496729612"));
reed@google.comfa06e522011-02-28 21:29:58 +000093
94 static const struct {
95 SkScalar fValue;
96 const char* fString;
97 } gRec[] = {
98 { 0, "0" },
99 { SK_Scalar1, "1" },
100 { -SK_Scalar1, "-1" },
101 { SK_Scalar1/2, "0.5" },
reed@google.com8072e4f2011-03-01 15:44:08 +0000102#ifdef SK_SCALAR_IS_FLOAT
tomhudson@google.com47e0a092011-07-08 17:49:22 +0000103 #ifdef SK_BUILD_FOR_WIN
104 { 3.4028234e38f, "3.4028235e+038" },
105 { -3.4028234e38f, "-3.4028235e+038" },
106 #else
reed@google.com8072e4f2011-03-01 15:44:08 +0000107 { 3.4028234e38f, "3.4028235e+38" },
108 { -3.4028234e38f, "-3.4028235e+38" },
tomhudson@google.com47e0a092011-07-08 17:49:22 +0000109 #endif
reed@google.com8072e4f2011-03-01 15:44:08 +0000110#endif
reed@google.comfa06e522011-02-28 21:29:58 +0000111 };
112 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) {
113 a.reset();
114 a.appendScalar(gRec[i].fValue);
reed@google.com8072e4f2011-03-01 15:44:08 +0000115 REPORTER_ASSERT(reporter, a.size() <= SkStrAppendScalar_MaxSize);
116// SkDebugf(" received <%s> expected <%s>\n", a.c_str(), gRec[i].fString);
reed@google.comfa06e522011-02-28 21:29:58 +0000117 REPORTER_ASSERT(reporter, a.equals(gRec[i].fString));
118 }
tomhudson@google.com3a1f6a02011-06-30 14:39:52 +0000119
120 REPORTER_ASSERT(reporter, SkStringPrintf("%i", 0).equals("0"));
tomhudson@google.com47e0a092011-07-08 17:49:22 +0000121
122 char buffer [40];
123 memset(buffer, 'a', 40);
124 REPORTER_ASSERT(reporter, buffer[18] == 'a');
125 REPORTER_ASSERT(reporter, buffer[19] == 'a');
126 REPORTER_ASSERT(reporter, buffer[20] == 'a');
127 printfAnalog(buffer, 20, "%30d", 0);
128 REPORTER_ASSERT(reporter, buffer[18] == ' ');
129 REPORTER_ASSERT(reporter, buffer[19] == 0);
130 REPORTER_ASSERT(reporter, buffer[20] == 'a');
131
reed@android.comd8730ea2009-02-27 22:06:06 +0000132}
133
134#include "TestClassDef.h"
135DEFINE_TESTCLASS("String", StringTestClass, TestString)