1 | // |
---|
2 | // Lol Engine |
---|
3 | // |
---|
4 | // Copyright: (c) 2010-2012 Sam Hocevar <sam@hocevar.net> |
---|
5 | // This program is free software; you can redistribute it and/or |
---|
6 | // modify it under the terms of the Do What The Fuck You Want To |
---|
7 | // Public License, Version 2, as published by Sam Hocevar. See |
---|
8 | // http://sam.zoy.org/projects/COPYING.WTFPL for more details. |
---|
9 | // |
---|
10 | |
---|
11 | #if defined HAVE_CONFIG_H |
---|
12 | # include "config.h" |
---|
13 | #endif |
---|
14 | |
---|
15 | #include "core.h" |
---|
16 | #include "lol/unit.h" |
---|
17 | |
---|
18 | namespace lol |
---|
19 | { |
---|
20 | |
---|
21 | LOLUNIT_FIXTURE(StringTest) |
---|
22 | { |
---|
23 | void SetUp() {} |
---|
24 | |
---|
25 | void TearDown() {} |
---|
26 | |
---|
27 | LOLUNIT_TEST(StringBuild) |
---|
28 | { |
---|
29 | String s1; |
---|
30 | LOLUNIT_ASSERT_EQUAL(s1.Count(), 0); |
---|
31 | LOLUNIT_ASSERT_EQUAL(s1[0], '\0'); |
---|
32 | |
---|
33 | String s2(""); |
---|
34 | LOLUNIT_ASSERT_EQUAL(s2.Count(), 0); |
---|
35 | LOLUNIT_ASSERT_EQUAL(s2[0], '\0'); |
---|
36 | |
---|
37 | String s3("a"); |
---|
38 | LOLUNIT_ASSERT_EQUAL(s3.Count(), 1); |
---|
39 | LOLUNIT_ASSERT_EQUAL(s3[0], 'a'); |
---|
40 | LOLUNIT_ASSERT_EQUAL(s3[1], '\0'); |
---|
41 | |
---|
42 | String s4(s3); |
---|
43 | LOLUNIT_ASSERT_EQUAL(s4.Count(), 1); |
---|
44 | LOLUNIT_ASSERT_EQUAL(s4[0], 'a'); |
---|
45 | LOLUNIT_ASSERT_EQUAL(s4[1], '\0'); |
---|
46 | } |
---|
47 | |
---|
48 | LOLUNIT_TEST(StringAppendChar) |
---|
49 | { |
---|
50 | String s; |
---|
51 | s += 'a'; |
---|
52 | s += 'b'; |
---|
53 | s += 'c'; |
---|
54 | |
---|
55 | LOLUNIT_ASSERT_EQUAL(s[0], 'a'); |
---|
56 | LOLUNIT_ASSERT_EQUAL(s[1], 'b'); |
---|
57 | LOLUNIT_ASSERT_EQUAL(s[2], 'c'); |
---|
58 | LOLUNIT_ASSERT_EQUAL(s[3], '\0'); |
---|
59 | } |
---|
60 | |
---|
61 | LOLUNIT_TEST(StringCopy) |
---|
62 | { |
---|
63 | String s1 = "abc"; |
---|
64 | |
---|
65 | String s2 = s1; |
---|
66 | |
---|
67 | LOLUNIT_ASSERT_EQUAL(s1[0], s2[0]); |
---|
68 | LOLUNIT_ASSERT_EQUAL(s1[1], s2[1]); |
---|
69 | LOLUNIT_ASSERT_EQUAL(s1[2], s2[2]); |
---|
70 | LOLUNIT_ASSERT_EQUAL(s1[3], s2[3]); |
---|
71 | } |
---|
72 | |
---|
73 | LOLUNIT_TEST(StringConcat) |
---|
74 | { |
---|
75 | String s1("ab"), s2("cd"); |
---|
76 | |
---|
77 | String s3 = s1 + s2; |
---|
78 | LOLUNIT_ASSERT_EQUAL(s3[0], 'a'); |
---|
79 | LOLUNIT_ASSERT_EQUAL(s3[1], 'b'); |
---|
80 | LOLUNIT_ASSERT_EQUAL(s3[2], 'c'); |
---|
81 | LOLUNIT_ASSERT_EQUAL(s3[3], 'd'); |
---|
82 | LOLUNIT_ASSERT_EQUAL(s3[4], '\0'); |
---|
83 | } |
---|
84 | |
---|
85 | LOLUNIT_TEST(StringAppendString) |
---|
86 | { |
---|
87 | String s1("ab"), s2("cd"); |
---|
88 | |
---|
89 | s1 += s2; |
---|
90 | LOLUNIT_ASSERT_EQUAL(s1[0], 'a'); |
---|
91 | LOLUNIT_ASSERT_EQUAL(s1[1], 'b'); |
---|
92 | LOLUNIT_ASSERT_EQUAL(s1[2], 'c'); |
---|
93 | LOLUNIT_ASSERT_EQUAL(s1[3], 'd'); |
---|
94 | LOLUNIT_ASSERT_EQUAL(s1[4], '\0'); |
---|
95 | |
---|
96 | s2 += s2; |
---|
97 | LOLUNIT_ASSERT_EQUAL(s2[0], 'c'); |
---|
98 | LOLUNIT_ASSERT_EQUAL(s2[1], 'd'); |
---|
99 | LOLUNIT_ASSERT_EQUAL(s2[2], 'c'); |
---|
100 | LOLUNIT_ASSERT_EQUAL(s2[3], 'd'); |
---|
101 | LOLUNIT_ASSERT_EQUAL(s2[4], '\0'); |
---|
102 | } |
---|
103 | |
---|
104 | LOLUNIT_TEST(StringEqual) |
---|
105 | { |
---|
106 | String s1("abc"); |
---|
107 | String s2("abc"); |
---|
108 | String s3("ab"); |
---|
109 | |
---|
110 | LOLUNIT_ASSERT(s1 == s2); |
---|
111 | LOLUNIT_ASSERT(!(s1 == s3)); |
---|
112 | } |
---|
113 | |
---|
114 | LOLUNIT_TEST(StringDifferent) |
---|
115 | { |
---|
116 | String s1("abc"); |
---|
117 | String s2("ab"); |
---|
118 | String s3("abc"); |
---|
119 | |
---|
120 | LOLUNIT_ASSERT(s1 != s2); |
---|
121 | LOLUNIT_ASSERT(!(s1 != s3)); |
---|
122 | } |
---|
123 | |
---|
124 | LOLUNIT_TEST(StringPrintf) |
---|
125 | { |
---|
126 | String s1 = "3a"; |
---|
127 | String s2 = String::Printf("%d%x", 3, 10); |
---|
128 | |
---|
129 | LOLUNIT_ASSERT(s1 == s2); |
---|
130 | |
---|
131 | String s3 = "abc 3"; |
---|
132 | String s4 = String::Printf("abc %d", 3); |
---|
133 | |
---|
134 | LOLUNIT_ASSERT(s3 == s4); |
---|
135 | } |
---|
136 | }; |
---|
137 | |
---|
138 | } /* namespace lol */ |
---|
139 | |
---|