xpcom/tests/TestStringAPI.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/xpcom/tests/TestStringAPI.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,209 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +#include <stdio.h>
     1.9 +#include "nsStringAPI.h"
    1.10 +
    1.11 +#define CHECK(x) \
    1.12 +  _doCheck(x, #x, __LINE__)
    1.13 +
    1.14 +int _doCheck(bool cond, const char* msg, int line) {
    1.15 +  if (cond) return 0;
    1.16 +  fprintf(stderr, "FAIL: line %d: %s\n", line, msg);
    1.17 +  return 1;
    1.18 +}
    1.19 +
    1.20 +int testEmpty() {
    1.21 +  nsString s;
    1.22 +  return CHECK(0 == s.Length()) +
    1.23 +         CHECK(s.IsEmpty());
    1.24 +}
    1.25 +
    1.26 +int testAccess() {
    1.27 +  nsString s;
    1.28 +  s.Assign(NS_LITERAL_STRING("hello"));
    1.29 +  int res = CHECK(5 == s.Length()) +
    1.30 +            CHECK(s.EqualsLiteral("hello"));
    1.31 +  const char16_t *it, *end;
    1.32 +  int len = s.BeginReading(&it, &end);
    1.33 +  res += CHECK(5 == len);
    1.34 +  res += CHECK(char16_t('h') == it[0]) +
    1.35 +         CHECK(char16_t('e') == it[1]) +
    1.36 +         CHECK(char16_t('l') == it[2]) +
    1.37 +         CHECK(char16_t('l') == it[3]) +
    1.38 +         CHECK(char16_t('o') == it[4]) +
    1.39 +         CHECK(it + len == end);
    1.40 +  res += CHECK(s[0] == s.First());
    1.41 +  for (int i = 0; i < len; ++i) {
    1.42 +    res += CHECK(s[i] == it[i]);
    1.43 +    res += CHECK(s[i] == s.CharAt(i));
    1.44 +  }
    1.45 +  res += CHECK(it == s.BeginReading());
    1.46 +  res += CHECK(end == s.EndReading());
    1.47 +  return res;
    1.48 +}
    1.49 +
    1.50 +int testWrite() {
    1.51 +  nsString s(NS_LITERAL_STRING("xyzz"));
    1.52 +  char16_t *begin, *end;
    1.53 +  int res = CHECK(4 == s.Length());
    1.54 +  uint32_t len = s.BeginWriting(&begin, &end, 5);
    1.55 +  res += CHECK(5 == s.Length()) +
    1.56 +         CHECK(5 == len) +
    1.57 +         CHECK(end == begin + 5) +
    1.58 +         CHECK(begin == s.BeginWriting()) +
    1.59 +         CHECK(end == s.EndWriting());
    1.60 +  begin[4] = char16_t('y');
    1.61 +  res += CHECK(s.Equals(NS_LITERAL_STRING("xyzzy")));
    1.62 +  s.SetLength(4);
    1.63 +  res += CHECK(4 == s.Length()) +
    1.64 +         CHECK(s.Equals(NS_LITERAL_STRING("xyzz"))) +
    1.65 +         CHECK(!s.Equals(NS_LITERAL_STRING("xyzzy"))) +
    1.66 +         CHECK(!s.IsEmpty());
    1.67 +  s.Truncate();
    1.68 +  res += CHECK(0 == s.Length()) +
    1.69 +         CHECK(s.IsEmpty());
    1.70 +  const char16_t sample[] = { 's', 'a', 'm', 'p', 'l', 'e', '\0' };
    1.71 +  s.Assign(sample);
    1.72 +  res += CHECK(s.EqualsLiteral("sample"));
    1.73 +  s.Assign(sample, 4);
    1.74 +  res += CHECK(s.EqualsLiteral("samp"));
    1.75 +  s.Assign(char16_t('q'));
    1.76 +  res += CHECK(s.EqualsLiteral("q"));
    1.77 +  return res;
    1.78 +}
    1.79 +
    1.80 +int testFind() {
    1.81 +  nsString str_haystack;
    1.82 +  nsString str_needle;
    1.83 +  str_needle.AssignLiteral("world");
    1.84 +
    1.85 +  int32_t ret = 0;
    1.86 +  ret += CHECK(-1 == str_haystack.Find("world"));
    1.87 +  ret += CHECK(-1 == str_haystack.Find(str_needle));
    1.88 +
    1.89 +  str_haystack.AssignLiteral("hello world hello world hello");
    1.90 +  ret += CHECK( 6 == str_haystack.Find("world"));
    1.91 +  ret += CHECK( 6 == str_haystack.Find(str_needle));
    1.92 +  ret += CHECK(-1 == str_haystack.Find("world", 20, false));
    1.93 +  ret += CHECK(-1 == str_haystack.Find(str_needle, 20));
    1.94 +  ret += CHECK(18 == str_haystack.Find("world", 12, false));
    1.95 +  ret += CHECK(18 == str_haystack.Find(str_needle, 12));
    1.96 +
    1.97 +  nsCString cstr_haystack;
    1.98 +  nsCString cstr_needle;
    1.99 +  cstr_needle.AssignLiteral("world");
   1.100 +  
   1.101 +  ret += CHECK(-1 == cstr_haystack.Find("world"));
   1.102 +  ret += CHECK(-1 == cstr_haystack.Find(cstr_needle));
   1.103 +
   1.104 +  cstr_haystack.AssignLiteral("hello world hello world hello");
   1.105 +  ret += CHECK( 6 == cstr_haystack.Find("world"));
   1.106 +  ret += CHECK( 6 == cstr_haystack.Find(cstr_needle));
   1.107 +  ret += CHECK(-1 == cstr_haystack.Find(cstr_needle, 20));
   1.108 +  ret += CHECK(18 == cstr_haystack.Find(cstr_needle, 12));
   1.109 +  ret += CHECK( 6 == cstr_haystack.Find("world", 5));
   1.110 +
   1.111 +  return ret;
   1.112 +}
   1.113 +
   1.114 +int testVoid() {
   1.115 +  nsString s;
   1.116 +  int ret = CHECK(!s.IsVoid());
   1.117 +  s.SetIsVoid(false);
   1.118 +  ret += CHECK(!s.IsVoid());
   1.119 +  s.SetIsVoid(true);
   1.120 +  ret += CHECK(s.IsVoid());
   1.121 +  s.SetIsVoid(false);
   1.122 +  ret += CHECK(!s.IsVoid());
   1.123 +  s.SetIsVoid(true);
   1.124 +  s.AssignLiteral("hello");
   1.125 +  ret += CHECK(!s.IsVoid());
   1.126 +  return ret;
   1.127 +}
   1.128 +
   1.129 +int testRFind() {
   1.130 +  int32_t ret = 0;
   1.131 +
   1.132 +  // nsString.RFind
   1.133 +  nsString str_haystack;
   1.134 +  nsString str_needle;
   1.135 +  str_needle.AssignLiteral("world");
   1.136 +
   1.137 +  ret += CHECK(-1 == str_haystack.RFind(str_needle));
   1.138 +  ret += CHECK(-1 == str_haystack.RFind("world"));
   1.139 +
   1.140 +  str_haystack.AssignLiteral("hello world hElLo woRlD");
   1.141 +
   1.142 +  ret += CHECK( 6 == str_haystack.RFind(str_needle));
   1.143 +  ret += CHECK( 6 == str_haystack.RFind(str_needle, -1));
   1.144 +  ret += CHECK( 6 == str_haystack.RFind(str_needle, 17));
   1.145 +  ret += CHECK( 6 == str_haystack.RFind("world", false));
   1.146 +  ret += CHECK(18 == str_haystack.RFind("world", true));
   1.147 +  ret += CHECK( 6 == str_haystack.RFind("world", -1, false));
   1.148 +  ret += CHECK(18 == str_haystack.RFind("world", -1, true));
   1.149 +  ret += CHECK( 6 == str_haystack.RFind("world", 17, false));
   1.150 +  ret += CHECK( 0 == str_haystack.RFind("hello", 0, false));
   1.151 +  ret += CHECK(18 == str_haystack.RFind("world", 19, true));
   1.152 +  ret += CHECK(18 == str_haystack.RFind("world", 22, true));
   1.153 +  ret += CHECK(18 == str_haystack.RFind("world", 23, true));
   1.154 +
   1.155 +  // nsCString.RFind
   1.156 +  nsCString cstr_haystack;
   1.157 +  nsCString cstr_needle;
   1.158 +  cstr_needle.AssignLiteral("world");
   1.159 +
   1.160 +  ret += CHECK(-1 == cstr_haystack.RFind(cstr_needle));
   1.161 +  ret += CHECK(-1 == cstr_haystack.RFind("world"));
   1.162 +  
   1.163 +  cstr_haystack.AssignLiteral("hello world hElLo woRlD");
   1.164 +
   1.165 +  ret += CHECK( 6 == cstr_haystack.RFind(cstr_needle));
   1.166 +  ret += CHECK( 6 == cstr_haystack.RFind(cstr_needle, -1));
   1.167 +  ret += CHECK( 6 == cstr_haystack.RFind(cstr_needle, 17));
   1.168 +  ret += CHECK( 6 == cstr_haystack.RFind("world", 5));
   1.169 +  ret += CHECK( 0 == cstr_haystack.RFind(nsDependentCString("hello"), 0));
   1.170 +
   1.171 +  return ret;
   1.172 +}
   1.173 +
   1.174 +int testCompressWhitespace() {
   1.175 +  int32_t ret = 0;
   1.176 +
   1.177 +  // CompressWhitespace utility function
   1.178 +  nsString s;
   1.179 +
   1.180 +  s.AssignLiteral("     ");
   1.181 +  CompressWhitespace(s);
   1.182 +  ret += CHECK(s.EqualsLiteral(""));
   1.183 +
   1.184 +  s.AssignLiteral("  no more  leading spaces");
   1.185 +  CompressWhitespace(s);
   1.186 +  ret += CHECK(s.EqualsLiteral("no more leading spaces"));
   1.187 +
   1.188 +  s.AssignLiteral("no    more trailing spaces ");
   1.189 +  CompressWhitespace(s);
   1.190 +  ret += CHECK(s.EqualsLiteral("no more trailing spaces"));
   1.191 +
   1.192 +  s.AssignLiteral("   hello one    2         three    45        ");
   1.193 +  CompressWhitespace(s);
   1.194 +  ret += CHECK(s.EqualsLiteral("hello one 2 three 45"));
   1.195 +
   1.196 +  return ret;
   1.197 +}
   1.198 +
   1.199 +int main() {
   1.200 +  int rv = 0;
   1.201 +  rv += testEmpty();
   1.202 +  rv += testAccess();
   1.203 +  rv += testWrite();
   1.204 +  rv += testFind();
   1.205 +  rv += testVoid();
   1.206 +  rv += testRFind();
   1.207 +  rv += testCompressWhitespace();
   1.208 +  if (0 == rv) {
   1.209 +    fprintf(stderr, "PASS: StringAPI tests\n");
   1.210 +  }
   1.211 +  return rv;
   1.212 +}

mercurial