Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | #include <stdio.h> |
michael@0 | 6 | #include "nsStringAPI.h" |
michael@0 | 7 | |
michael@0 | 8 | #define CHECK(x) \ |
michael@0 | 9 | _doCheck(x, #x, __LINE__) |
michael@0 | 10 | |
michael@0 | 11 | int _doCheck(bool cond, const char* msg, int line) { |
michael@0 | 12 | if (cond) return 0; |
michael@0 | 13 | fprintf(stderr, "FAIL: line %d: %s\n", line, msg); |
michael@0 | 14 | return 1; |
michael@0 | 15 | } |
michael@0 | 16 | |
michael@0 | 17 | int testEmpty() { |
michael@0 | 18 | nsString s; |
michael@0 | 19 | return CHECK(0 == s.Length()) + |
michael@0 | 20 | CHECK(s.IsEmpty()); |
michael@0 | 21 | } |
michael@0 | 22 | |
michael@0 | 23 | int testAccess() { |
michael@0 | 24 | nsString s; |
michael@0 | 25 | s.Assign(NS_LITERAL_STRING("hello")); |
michael@0 | 26 | int res = CHECK(5 == s.Length()) + |
michael@0 | 27 | CHECK(s.EqualsLiteral("hello")); |
michael@0 | 28 | const char16_t *it, *end; |
michael@0 | 29 | int len = s.BeginReading(&it, &end); |
michael@0 | 30 | res += CHECK(5 == len); |
michael@0 | 31 | res += CHECK(char16_t('h') == it[0]) + |
michael@0 | 32 | CHECK(char16_t('e') == it[1]) + |
michael@0 | 33 | CHECK(char16_t('l') == it[2]) + |
michael@0 | 34 | CHECK(char16_t('l') == it[3]) + |
michael@0 | 35 | CHECK(char16_t('o') == it[4]) + |
michael@0 | 36 | CHECK(it + len == end); |
michael@0 | 37 | res += CHECK(s[0] == s.First()); |
michael@0 | 38 | for (int i = 0; i < len; ++i) { |
michael@0 | 39 | res += CHECK(s[i] == it[i]); |
michael@0 | 40 | res += CHECK(s[i] == s.CharAt(i)); |
michael@0 | 41 | } |
michael@0 | 42 | res += CHECK(it == s.BeginReading()); |
michael@0 | 43 | res += CHECK(end == s.EndReading()); |
michael@0 | 44 | return res; |
michael@0 | 45 | } |
michael@0 | 46 | |
michael@0 | 47 | int testWrite() { |
michael@0 | 48 | nsString s(NS_LITERAL_STRING("xyzz")); |
michael@0 | 49 | char16_t *begin, *end; |
michael@0 | 50 | int res = CHECK(4 == s.Length()); |
michael@0 | 51 | uint32_t len = s.BeginWriting(&begin, &end, 5); |
michael@0 | 52 | res += CHECK(5 == s.Length()) + |
michael@0 | 53 | CHECK(5 == len) + |
michael@0 | 54 | CHECK(end == begin + 5) + |
michael@0 | 55 | CHECK(begin == s.BeginWriting()) + |
michael@0 | 56 | CHECK(end == s.EndWriting()); |
michael@0 | 57 | begin[4] = char16_t('y'); |
michael@0 | 58 | res += CHECK(s.Equals(NS_LITERAL_STRING("xyzzy"))); |
michael@0 | 59 | s.SetLength(4); |
michael@0 | 60 | res += CHECK(4 == s.Length()) + |
michael@0 | 61 | CHECK(s.Equals(NS_LITERAL_STRING("xyzz"))) + |
michael@0 | 62 | CHECK(!s.Equals(NS_LITERAL_STRING("xyzzy"))) + |
michael@0 | 63 | CHECK(!s.IsEmpty()); |
michael@0 | 64 | s.Truncate(); |
michael@0 | 65 | res += CHECK(0 == s.Length()) + |
michael@0 | 66 | CHECK(s.IsEmpty()); |
michael@0 | 67 | const char16_t sample[] = { 's', 'a', 'm', 'p', 'l', 'e', '\0' }; |
michael@0 | 68 | s.Assign(sample); |
michael@0 | 69 | res += CHECK(s.EqualsLiteral("sample")); |
michael@0 | 70 | s.Assign(sample, 4); |
michael@0 | 71 | res += CHECK(s.EqualsLiteral("samp")); |
michael@0 | 72 | s.Assign(char16_t('q')); |
michael@0 | 73 | res += CHECK(s.EqualsLiteral("q")); |
michael@0 | 74 | return res; |
michael@0 | 75 | } |
michael@0 | 76 | |
michael@0 | 77 | int testFind() { |
michael@0 | 78 | nsString str_haystack; |
michael@0 | 79 | nsString str_needle; |
michael@0 | 80 | str_needle.AssignLiteral("world"); |
michael@0 | 81 | |
michael@0 | 82 | int32_t ret = 0; |
michael@0 | 83 | ret += CHECK(-1 == str_haystack.Find("world")); |
michael@0 | 84 | ret += CHECK(-1 == str_haystack.Find(str_needle)); |
michael@0 | 85 | |
michael@0 | 86 | str_haystack.AssignLiteral("hello world hello world hello"); |
michael@0 | 87 | ret += CHECK( 6 == str_haystack.Find("world")); |
michael@0 | 88 | ret += CHECK( 6 == str_haystack.Find(str_needle)); |
michael@0 | 89 | ret += CHECK(-1 == str_haystack.Find("world", 20, false)); |
michael@0 | 90 | ret += CHECK(-1 == str_haystack.Find(str_needle, 20)); |
michael@0 | 91 | ret += CHECK(18 == str_haystack.Find("world", 12, false)); |
michael@0 | 92 | ret += CHECK(18 == str_haystack.Find(str_needle, 12)); |
michael@0 | 93 | |
michael@0 | 94 | nsCString cstr_haystack; |
michael@0 | 95 | nsCString cstr_needle; |
michael@0 | 96 | cstr_needle.AssignLiteral("world"); |
michael@0 | 97 | |
michael@0 | 98 | ret += CHECK(-1 == cstr_haystack.Find("world")); |
michael@0 | 99 | ret += CHECK(-1 == cstr_haystack.Find(cstr_needle)); |
michael@0 | 100 | |
michael@0 | 101 | cstr_haystack.AssignLiteral("hello world hello world hello"); |
michael@0 | 102 | ret += CHECK( 6 == cstr_haystack.Find("world")); |
michael@0 | 103 | ret += CHECK( 6 == cstr_haystack.Find(cstr_needle)); |
michael@0 | 104 | ret += CHECK(-1 == cstr_haystack.Find(cstr_needle, 20)); |
michael@0 | 105 | ret += CHECK(18 == cstr_haystack.Find(cstr_needle, 12)); |
michael@0 | 106 | ret += CHECK( 6 == cstr_haystack.Find("world", 5)); |
michael@0 | 107 | |
michael@0 | 108 | return ret; |
michael@0 | 109 | } |
michael@0 | 110 | |
michael@0 | 111 | int testVoid() { |
michael@0 | 112 | nsString s; |
michael@0 | 113 | int ret = CHECK(!s.IsVoid()); |
michael@0 | 114 | s.SetIsVoid(false); |
michael@0 | 115 | ret += CHECK(!s.IsVoid()); |
michael@0 | 116 | s.SetIsVoid(true); |
michael@0 | 117 | ret += CHECK(s.IsVoid()); |
michael@0 | 118 | s.SetIsVoid(false); |
michael@0 | 119 | ret += CHECK(!s.IsVoid()); |
michael@0 | 120 | s.SetIsVoid(true); |
michael@0 | 121 | s.AssignLiteral("hello"); |
michael@0 | 122 | ret += CHECK(!s.IsVoid()); |
michael@0 | 123 | return ret; |
michael@0 | 124 | } |
michael@0 | 125 | |
michael@0 | 126 | int testRFind() { |
michael@0 | 127 | int32_t ret = 0; |
michael@0 | 128 | |
michael@0 | 129 | // nsString.RFind |
michael@0 | 130 | nsString str_haystack; |
michael@0 | 131 | nsString str_needle; |
michael@0 | 132 | str_needle.AssignLiteral("world"); |
michael@0 | 133 | |
michael@0 | 134 | ret += CHECK(-1 == str_haystack.RFind(str_needle)); |
michael@0 | 135 | ret += CHECK(-1 == str_haystack.RFind("world")); |
michael@0 | 136 | |
michael@0 | 137 | str_haystack.AssignLiteral("hello world hElLo woRlD"); |
michael@0 | 138 | |
michael@0 | 139 | ret += CHECK( 6 == str_haystack.RFind(str_needle)); |
michael@0 | 140 | ret += CHECK( 6 == str_haystack.RFind(str_needle, -1)); |
michael@0 | 141 | ret += CHECK( 6 == str_haystack.RFind(str_needle, 17)); |
michael@0 | 142 | ret += CHECK( 6 == str_haystack.RFind("world", false)); |
michael@0 | 143 | ret += CHECK(18 == str_haystack.RFind("world", true)); |
michael@0 | 144 | ret += CHECK( 6 == str_haystack.RFind("world", -1, false)); |
michael@0 | 145 | ret += CHECK(18 == str_haystack.RFind("world", -1, true)); |
michael@0 | 146 | ret += CHECK( 6 == str_haystack.RFind("world", 17, false)); |
michael@0 | 147 | ret += CHECK( 0 == str_haystack.RFind("hello", 0, false)); |
michael@0 | 148 | ret += CHECK(18 == str_haystack.RFind("world", 19, true)); |
michael@0 | 149 | ret += CHECK(18 == str_haystack.RFind("world", 22, true)); |
michael@0 | 150 | ret += CHECK(18 == str_haystack.RFind("world", 23, true)); |
michael@0 | 151 | |
michael@0 | 152 | // nsCString.RFind |
michael@0 | 153 | nsCString cstr_haystack; |
michael@0 | 154 | nsCString cstr_needle; |
michael@0 | 155 | cstr_needle.AssignLiteral("world"); |
michael@0 | 156 | |
michael@0 | 157 | ret += CHECK(-1 == cstr_haystack.RFind(cstr_needle)); |
michael@0 | 158 | ret += CHECK(-1 == cstr_haystack.RFind("world")); |
michael@0 | 159 | |
michael@0 | 160 | cstr_haystack.AssignLiteral("hello world hElLo woRlD"); |
michael@0 | 161 | |
michael@0 | 162 | ret += CHECK( 6 == cstr_haystack.RFind(cstr_needle)); |
michael@0 | 163 | ret += CHECK( 6 == cstr_haystack.RFind(cstr_needle, -1)); |
michael@0 | 164 | ret += CHECK( 6 == cstr_haystack.RFind(cstr_needle, 17)); |
michael@0 | 165 | ret += CHECK( 6 == cstr_haystack.RFind("world", 5)); |
michael@0 | 166 | ret += CHECK( 0 == cstr_haystack.RFind(nsDependentCString("hello"), 0)); |
michael@0 | 167 | |
michael@0 | 168 | return ret; |
michael@0 | 169 | } |
michael@0 | 170 | |
michael@0 | 171 | int testCompressWhitespace() { |
michael@0 | 172 | int32_t ret = 0; |
michael@0 | 173 | |
michael@0 | 174 | // CompressWhitespace utility function |
michael@0 | 175 | nsString s; |
michael@0 | 176 | |
michael@0 | 177 | s.AssignLiteral(" "); |
michael@0 | 178 | CompressWhitespace(s); |
michael@0 | 179 | ret += CHECK(s.EqualsLiteral("")); |
michael@0 | 180 | |
michael@0 | 181 | s.AssignLiteral(" no more leading spaces"); |
michael@0 | 182 | CompressWhitespace(s); |
michael@0 | 183 | ret += CHECK(s.EqualsLiteral("no more leading spaces")); |
michael@0 | 184 | |
michael@0 | 185 | s.AssignLiteral("no more trailing spaces "); |
michael@0 | 186 | CompressWhitespace(s); |
michael@0 | 187 | ret += CHECK(s.EqualsLiteral("no more trailing spaces")); |
michael@0 | 188 | |
michael@0 | 189 | s.AssignLiteral(" hello one 2 three 45 "); |
michael@0 | 190 | CompressWhitespace(s); |
michael@0 | 191 | ret += CHECK(s.EqualsLiteral("hello one 2 three 45")); |
michael@0 | 192 | |
michael@0 | 193 | return ret; |
michael@0 | 194 | } |
michael@0 | 195 | |
michael@0 | 196 | int main() { |
michael@0 | 197 | int rv = 0; |
michael@0 | 198 | rv += testEmpty(); |
michael@0 | 199 | rv += testAccess(); |
michael@0 | 200 | rv += testWrite(); |
michael@0 | 201 | rv += testFind(); |
michael@0 | 202 | rv += testVoid(); |
michael@0 | 203 | rv += testRFind(); |
michael@0 | 204 | rv += testCompressWhitespace(); |
michael@0 | 205 | if (0 == rv) { |
michael@0 | 206 | fprintf(stderr, "PASS: StringAPI tests\n"); |
michael@0 | 207 | } |
michael@0 | 208 | return rv; |
michael@0 | 209 | } |