michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include michael@0: #include "nsStringAPI.h" michael@0: michael@0: #define CHECK(x) \ michael@0: _doCheck(x, #x, __LINE__) michael@0: michael@0: int _doCheck(bool cond, const char* msg, int line) { michael@0: if (cond) return 0; michael@0: fprintf(stderr, "FAIL: line %d: %s\n", line, msg); michael@0: return 1; michael@0: } michael@0: michael@0: int testEmpty() { michael@0: nsString s; michael@0: return CHECK(0 == s.Length()) + michael@0: CHECK(s.IsEmpty()); michael@0: } michael@0: michael@0: int testAccess() { michael@0: nsString s; michael@0: s.Assign(NS_LITERAL_STRING("hello")); michael@0: int res = CHECK(5 == s.Length()) + michael@0: CHECK(s.EqualsLiteral("hello")); michael@0: const char16_t *it, *end; michael@0: int len = s.BeginReading(&it, &end); michael@0: res += CHECK(5 == len); michael@0: res += CHECK(char16_t('h') == it[0]) + michael@0: CHECK(char16_t('e') == it[1]) + michael@0: CHECK(char16_t('l') == it[2]) + michael@0: CHECK(char16_t('l') == it[3]) + michael@0: CHECK(char16_t('o') == it[4]) + michael@0: CHECK(it + len == end); michael@0: res += CHECK(s[0] == s.First()); michael@0: for (int i = 0; i < len; ++i) { michael@0: res += CHECK(s[i] == it[i]); michael@0: res += CHECK(s[i] == s.CharAt(i)); michael@0: } michael@0: res += CHECK(it == s.BeginReading()); michael@0: res += CHECK(end == s.EndReading()); michael@0: return res; michael@0: } michael@0: michael@0: int testWrite() { michael@0: nsString s(NS_LITERAL_STRING("xyzz")); michael@0: char16_t *begin, *end; michael@0: int res = CHECK(4 == s.Length()); michael@0: uint32_t len = s.BeginWriting(&begin, &end, 5); michael@0: res += CHECK(5 == s.Length()) + michael@0: CHECK(5 == len) + michael@0: CHECK(end == begin + 5) + michael@0: CHECK(begin == s.BeginWriting()) + michael@0: CHECK(end == s.EndWriting()); michael@0: begin[4] = char16_t('y'); michael@0: res += CHECK(s.Equals(NS_LITERAL_STRING("xyzzy"))); michael@0: s.SetLength(4); michael@0: res += CHECK(4 == s.Length()) + michael@0: CHECK(s.Equals(NS_LITERAL_STRING("xyzz"))) + michael@0: CHECK(!s.Equals(NS_LITERAL_STRING("xyzzy"))) + michael@0: CHECK(!s.IsEmpty()); michael@0: s.Truncate(); michael@0: res += CHECK(0 == s.Length()) + michael@0: CHECK(s.IsEmpty()); michael@0: const char16_t sample[] = { 's', 'a', 'm', 'p', 'l', 'e', '\0' }; michael@0: s.Assign(sample); michael@0: res += CHECK(s.EqualsLiteral("sample")); michael@0: s.Assign(sample, 4); michael@0: res += CHECK(s.EqualsLiteral("samp")); michael@0: s.Assign(char16_t('q')); michael@0: res += CHECK(s.EqualsLiteral("q")); michael@0: return res; michael@0: } michael@0: michael@0: int testFind() { michael@0: nsString str_haystack; michael@0: nsString str_needle; michael@0: str_needle.AssignLiteral("world"); michael@0: michael@0: int32_t ret = 0; michael@0: ret += CHECK(-1 == str_haystack.Find("world")); michael@0: ret += CHECK(-1 == str_haystack.Find(str_needle)); michael@0: michael@0: str_haystack.AssignLiteral("hello world hello world hello"); michael@0: ret += CHECK( 6 == str_haystack.Find("world")); michael@0: ret += CHECK( 6 == str_haystack.Find(str_needle)); michael@0: ret += CHECK(-1 == str_haystack.Find("world", 20, false)); michael@0: ret += CHECK(-1 == str_haystack.Find(str_needle, 20)); michael@0: ret += CHECK(18 == str_haystack.Find("world", 12, false)); michael@0: ret += CHECK(18 == str_haystack.Find(str_needle, 12)); michael@0: michael@0: nsCString cstr_haystack; michael@0: nsCString cstr_needle; michael@0: cstr_needle.AssignLiteral("world"); michael@0: michael@0: ret += CHECK(-1 == cstr_haystack.Find("world")); michael@0: ret += CHECK(-1 == cstr_haystack.Find(cstr_needle)); michael@0: michael@0: cstr_haystack.AssignLiteral("hello world hello world hello"); michael@0: ret += CHECK( 6 == cstr_haystack.Find("world")); michael@0: ret += CHECK( 6 == cstr_haystack.Find(cstr_needle)); michael@0: ret += CHECK(-1 == cstr_haystack.Find(cstr_needle, 20)); michael@0: ret += CHECK(18 == cstr_haystack.Find(cstr_needle, 12)); michael@0: ret += CHECK( 6 == cstr_haystack.Find("world", 5)); michael@0: michael@0: return ret; michael@0: } michael@0: michael@0: int testVoid() { michael@0: nsString s; michael@0: int ret = CHECK(!s.IsVoid()); michael@0: s.SetIsVoid(false); michael@0: ret += CHECK(!s.IsVoid()); michael@0: s.SetIsVoid(true); michael@0: ret += CHECK(s.IsVoid()); michael@0: s.SetIsVoid(false); michael@0: ret += CHECK(!s.IsVoid()); michael@0: s.SetIsVoid(true); michael@0: s.AssignLiteral("hello"); michael@0: ret += CHECK(!s.IsVoid()); michael@0: return ret; michael@0: } michael@0: michael@0: int testRFind() { michael@0: int32_t ret = 0; michael@0: michael@0: // nsString.RFind michael@0: nsString str_haystack; michael@0: nsString str_needle; michael@0: str_needle.AssignLiteral("world"); michael@0: michael@0: ret += CHECK(-1 == str_haystack.RFind(str_needle)); michael@0: ret += CHECK(-1 == str_haystack.RFind("world")); michael@0: michael@0: str_haystack.AssignLiteral("hello world hElLo woRlD"); michael@0: michael@0: ret += CHECK( 6 == str_haystack.RFind(str_needle)); michael@0: ret += CHECK( 6 == str_haystack.RFind(str_needle, -1)); michael@0: ret += CHECK( 6 == str_haystack.RFind(str_needle, 17)); michael@0: ret += CHECK( 6 == str_haystack.RFind("world", false)); michael@0: ret += CHECK(18 == str_haystack.RFind("world", true)); michael@0: ret += CHECK( 6 == str_haystack.RFind("world", -1, false)); michael@0: ret += CHECK(18 == str_haystack.RFind("world", -1, true)); michael@0: ret += CHECK( 6 == str_haystack.RFind("world", 17, false)); michael@0: ret += CHECK( 0 == str_haystack.RFind("hello", 0, false)); michael@0: ret += CHECK(18 == str_haystack.RFind("world", 19, true)); michael@0: ret += CHECK(18 == str_haystack.RFind("world", 22, true)); michael@0: ret += CHECK(18 == str_haystack.RFind("world", 23, true)); michael@0: michael@0: // nsCString.RFind michael@0: nsCString cstr_haystack; michael@0: nsCString cstr_needle; michael@0: cstr_needle.AssignLiteral("world"); michael@0: michael@0: ret += CHECK(-1 == cstr_haystack.RFind(cstr_needle)); michael@0: ret += CHECK(-1 == cstr_haystack.RFind("world")); michael@0: michael@0: cstr_haystack.AssignLiteral("hello world hElLo woRlD"); michael@0: michael@0: ret += CHECK( 6 == cstr_haystack.RFind(cstr_needle)); michael@0: ret += CHECK( 6 == cstr_haystack.RFind(cstr_needle, -1)); michael@0: ret += CHECK( 6 == cstr_haystack.RFind(cstr_needle, 17)); michael@0: ret += CHECK( 6 == cstr_haystack.RFind("world", 5)); michael@0: ret += CHECK( 0 == cstr_haystack.RFind(nsDependentCString("hello"), 0)); michael@0: michael@0: return ret; michael@0: } michael@0: michael@0: int testCompressWhitespace() { michael@0: int32_t ret = 0; michael@0: michael@0: // CompressWhitespace utility function michael@0: nsString s; michael@0: michael@0: s.AssignLiteral(" "); michael@0: CompressWhitespace(s); michael@0: ret += CHECK(s.EqualsLiteral("")); michael@0: michael@0: s.AssignLiteral(" no more leading spaces"); michael@0: CompressWhitespace(s); michael@0: ret += CHECK(s.EqualsLiteral("no more leading spaces")); michael@0: michael@0: s.AssignLiteral("no more trailing spaces "); michael@0: CompressWhitespace(s); michael@0: ret += CHECK(s.EqualsLiteral("no more trailing spaces")); michael@0: michael@0: s.AssignLiteral(" hello one 2 three 45 "); michael@0: CompressWhitespace(s); michael@0: ret += CHECK(s.EqualsLiteral("hello one 2 three 45")); michael@0: michael@0: return ret; michael@0: } michael@0: michael@0: int main() { michael@0: int rv = 0; michael@0: rv += testEmpty(); michael@0: rv += testAccess(); michael@0: rv += testWrite(); michael@0: rv += testFind(); michael@0: rv += testVoid(); michael@0: rv += testRFind(); michael@0: rv += testCompressWhitespace(); michael@0: if (0 == rv) { michael@0: fprintf(stderr, "PASS: StringAPI tests\n"); michael@0: } michael@0: return rv; michael@0: }