blob: 02510d56dc77a5dd0429afb51fef42d731b78bf4 [file] [log] [blame]
reed@android.comd8730ea2009-02-27 22:06:06 +00001#include "Test.h"
2#include "SkString.h"
3
4static void TestString(skiatest::Reporter* reporter) {
5 SkString a;
6 SkString b((size_t)0);
7 SkString c("");
8 SkString d(NULL, 0);
9
10 REPORTER_ASSERT(reporter, a.isEmpty());
11 REPORTER_ASSERT(reporter, a == b && a == c && a == d);
12
13 a.set("hello");
14 b.set("hellox", 5);
15 c.set(a);
16 d.resize(5);
17 memcpy(d.writable_str(), "helloz", 5);
18
19 REPORTER_ASSERT(reporter, !a.isEmpty());
20 REPORTER_ASSERT(reporter, a.size() == 5);
21 REPORTER_ASSERT(reporter, a == b && a == c && a == d);
22 REPORTER_ASSERT(reporter, a.equals("hello", 5));
23 REPORTER_ASSERT(reporter, a.equals("hello"));
24 REPORTER_ASSERT(reporter, !a.equals("help"));
25
26 SkString e(a);
27 SkString f("hello");
28 SkString g("helloz", 5);
29
30 REPORTER_ASSERT(reporter, a == e && a == f && a == g);
31
32 b.set("world");
33 c = b;
34 REPORTER_ASSERT(reporter, a != b && a != c && b == c);
35
36 a.append(" world");
37 e.append("worldz", 5);
38 e.insert(5, " ");
39 f.set("world");
40 f.prepend("hello ");
41 REPORTER_ASSERT(reporter, a.equals("hello world") && a == e && a == f);
42
43 a.reset();
44 b.resize(0);
45 REPORTER_ASSERT(reporter, a.isEmpty() && b.isEmpty() && a == b);
46
47 a.set("a");
48 a.set("ab");
49 a.set("abc");
50 a.set("abcd");
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000051
52 a.set("");
53 a.appendS64(72036854775808LL, 0);
54 REPORTER_ASSERT(reporter, a.equals("72036854775808"));
55
56 a.set("");
57 a.appendS64(-1844674407370LL, 0);
58 REPORTER_ASSERT(reporter, a.equals("-1844674407370"));
59
60 a.set("");
61 a.appendS64(73709551616LL, 15);
62 REPORTER_ASSERT(reporter, a.equals("000073709551616"));
63
64 a.set("");
65 a.appendS64(-429496729612LL, 15);
66 REPORTER_ASSERT(reporter, a.equals("-000429496729612"));
reed@google.comfa06e522011-02-28 21:29:58 +000067
68 static const struct {
69 SkScalar fValue;
70 const char* fString;
71 } gRec[] = {
72 { 0, "0" },
73 { SK_Scalar1, "1" },
74 { -SK_Scalar1, "-1" },
75 { SK_Scalar1/2, "0.5" },
reed@google.com8072e4f2011-03-01 15:44:08 +000076#ifdef SK_SCALAR_IS_FLOAT
77 { 3.4028234e38f, "3.4028235e+38" },
78 { -3.4028234e38f, "-3.4028235e+38" },
79#endif
reed@google.comfa06e522011-02-28 21:29:58 +000080 };
81 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) {
82 a.reset();
83 a.appendScalar(gRec[i].fValue);
reed@google.com8072e4f2011-03-01 15:44:08 +000084 REPORTER_ASSERT(reporter, a.size() <= SkStrAppendScalar_MaxSize);
85// SkDebugf(" received <%s> expected <%s>\n", a.c_str(), gRec[i].fString);
reed@google.comfa06e522011-02-28 21:29:58 +000086 REPORTER_ASSERT(reporter, a.equals(gRec[i].fString));
87 }
tomhudson@google.com3a1f6a02011-06-30 14:39:52 +000088
89 REPORTER_ASSERT(reporter, SkStringPrintf("%i", 0).equals("0"));
reed@android.comd8730ea2009-02-27 22:06:06 +000090}
91
92#include "TestClassDef.h"
93DEFINE_TESTCLASS("String", StringTestClass, TestString)