blob: 52a038db44c34507e857fa60c979995f285b3e75 [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
31void printfAnalog(char* buffer, int size, const char format[], ...) {
32 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
59 SkString e(a);
60 SkString f("hello");
61 SkString g("helloz", 5);
62
63 REPORTER_ASSERT(reporter, a == e && a == f && a == g);
64
65 b.set("world");
66 c = b;
67 REPORTER_ASSERT(reporter, a != b && a != c && b == c);
68
69 a.append(" world");
70 e.append("worldz", 5);
71 e.insert(5, " ");
72 f.set("world");
73 f.prepend("hello ");
74 REPORTER_ASSERT(reporter, a.equals("hello world") && a == e && a == f);
75
76 a.reset();
77 b.resize(0);
78 REPORTER_ASSERT(reporter, a.isEmpty() && b.isEmpty() && a == b);
79
80 a.set("a");
81 a.set("ab");
82 a.set("abc");
83 a.set("abcd");
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000084
85 a.set("");
86 a.appendS64(72036854775808LL, 0);
87 REPORTER_ASSERT(reporter, a.equals("72036854775808"));
88
89 a.set("");
90 a.appendS64(-1844674407370LL, 0);
91 REPORTER_ASSERT(reporter, a.equals("-1844674407370"));
92
93 a.set("");
94 a.appendS64(73709551616LL, 15);
95 REPORTER_ASSERT(reporter, a.equals("000073709551616"));
96
97 a.set("");
98 a.appendS64(-429496729612LL, 15);
99 REPORTER_ASSERT(reporter, a.equals("-000429496729612"));
reed@google.comfa06e522011-02-28 21:29:58 +0000100
101 static const struct {
102 SkScalar fValue;
103 const char* fString;
104 } gRec[] = {
105 { 0, "0" },
106 { SK_Scalar1, "1" },
107 { -SK_Scalar1, "-1" },
108 { SK_Scalar1/2, "0.5" },
reed@google.com8072e4f2011-03-01 15:44:08 +0000109#ifdef SK_SCALAR_IS_FLOAT
tomhudson@google.com47e0a092011-07-08 17:49:22 +0000110 #ifdef SK_BUILD_FOR_WIN
111 { 3.4028234e38f, "3.4028235e+038" },
112 { -3.4028234e38f, "-3.4028235e+038" },
113 #else
reed@google.com8072e4f2011-03-01 15:44:08 +0000114 { 3.4028234e38f, "3.4028235e+38" },
115 { -3.4028234e38f, "-3.4028235e+38" },
tomhudson@google.com47e0a092011-07-08 17:49:22 +0000116 #endif
reed@google.com8072e4f2011-03-01 15:44:08 +0000117#endif
reed@google.comfa06e522011-02-28 21:29:58 +0000118 };
119 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) {
120 a.reset();
121 a.appendScalar(gRec[i].fValue);
reed@google.com8072e4f2011-03-01 15:44:08 +0000122 REPORTER_ASSERT(reporter, a.size() <= SkStrAppendScalar_MaxSize);
123// SkDebugf(" received <%s> expected <%s>\n", a.c_str(), gRec[i].fString);
reed@google.comfa06e522011-02-28 21:29:58 +0000124 REPORTER_ASSERT(reporter, a.equals(gRec[i].fString));
125 }
tomhudson@google.com3a1f6a02011-06-30 14:39:52 +0000126
127 REPORTER_ASSERT(reporter, SkStringPrintf("%i", 0).equals("0"));
tomhudson@google.com47e0a092011-07-08 17:49:22 +0000128
129 char buffer [40];
130 memset(buffer, 'a', 40);
131 REPORTER_ASSERT(reporter, buffer[18] == 'a');
132 REPORTER_ASSERT(reporter, buffer[19] == 'a');
133 REPORTER_ASSERT(reporter, buffer[20] == 'a');
134 printfAnalog(buffer, 20, "%30d", 0);
135 REPORTER_ASSERT(reporter, buffer[18] == ' ');
136 REPORTER_ASSERT(reporter, buffer[19] == 0);
137 REPORTER_ASSERT(reporter, buffer[20] == 'a');
138
reed@android.comd8730ea2009-02-27 22:06:06 +0000139}
140
141#include "TestClassDef.h"
142DEFINE_TESTCLASS("String", StringTestClass, TestString)