LCOV - code coverage report
Current view: directory - xpcom/tests - TestStringAPI.cpp (source / functions) Found Hit Coverage
Test: app.info Lines: 154 152 98.7 %
Date: 2012-06-02 Functions: 9 9 100.0 %

       1                 : /* ***** BEGIN LICENSE BLOCK *****
       2                 :  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
       3                 :  *
       4                 :  * The contents of this file are subject to the Mozilla Public License Version
       5                 :  * 1.1 (the "License"); you may not use this file except in compliance with
       6                 :  * the License. You may obtain a copy of the License at
       7                 :  * http://www.mozilla.org/MPL/
       8                 :  *
       9                 :  * Software distributed under the License is distributed on an "AS IS" basis,
      10                 :  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
      11                 :  * for the specific language governing rights and limitations under the
      12                 :  * License.
      13                 :  *
      14                 :  * The Original Code is XPCOM external strings test.
      15                 :  *
      16                 :  * The Initial Developer of the Original Code is
      17                 :  * Mook <mook.moz@gmail.com>.
      18                 :  * Portions created by the Initial Developer are Copyright (C) 2007
      19                 :  * the Initial Developer. All Rights Reserved.
      20                 :  *
      21                 :  * Contributor(s):
      22                 :  *    Prasad Sunkari <prasad@medhas.org>
      23                 :  *
      24                 :  * Alternatively, the contents of this file may be used under the terms of
      25                 :  * either the GNU General Public License Version 2 or later (the "GPL"), or
      26                 :  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
      27                 :  * in which case the provisions of the GPL or the LGPL are applicable instead
      28                 :  * of those above. If you wish to allow use of your version of this file only
      29                 :  * under the terms of either the GPL or the LGPL, and not to allow others to
      30                 :  * use your version of this file under the terms of the MPL, indicate your
      31                 :  * decision by deleting the provisions above and replace them with the notice
      32                 :  * and other provisions required by the GPL or the LGPL. If you do not delete
      33                 :  * the provisions above, a recipient may use your version of this file under
      34                 :  * the terms of any one of the MPL, the GPL or the LGPL.
      35                 :  *
      36                 :  * ***** END LICENSE BLOCK ***** */
      37                 : 
      38                 : #include <stdio.h>
      39                 : #include "nsStringAPI.h"
      40                 : 
      41                 : #define CHECK(x) \
      42                 :   _doCheck(x, #x, __LINE__)
      43                 : 
      44              85 : int _doCheck(bool cond, const char* msg, int line) {
      45              85 :   if (cond) return 0;
      46               0 :   fprintf(stderr, "FAIL: line %d: %s\n", line, msg);
      47               0 :   return 1;
      48                 : }
      49                 : 
      50               1 : int testEmpty() {
      51               2 :   nsString s;
      52               1 :   return CHECK(0 == s.Length()) +
      53               1 :          CHECK(s.IsEmpty());
      54                 : }
      55                 : 
      56               1 : int testAccess() {
      57               2 :   nsString s;
      58               1 :   s.Assign(NS_LITERAL_STRING("hello"));
      59               1 :   int res = CHECK(5 == s.Length()) +
      60               1 :             CHECK(s.EqualsLiteral("hello"));
      61                 :   const PRUnichar *it, *end;
      62               1 :   int len = s.BeginReading(&it, &end);
      63               1 :   res += CHECK(5 == len);
      64               1 :   res += CHECK(PRUnichar('h') == it[0]) +
      65               1 :          CHECK(PRUnichar('e') == it[1]) +
      66               1 :          CHECK(PRUnichar('l') == it[2]) +
      67               1 :          CHECK(PRUnichar('l') == it[3]) +
      68               1 :          CHECK(PRUnichar('o') == it[4]) +
      69               4 :          CHECK(it + len == end);
      70               1 :   res += CHECK(s[0] == s.First());
      71               6 :   for (int i = 0; i < len; ++i) {
      72               5 :     res += CHECK(s[i] == it[i]);
      73               5 :     res += CHECK(s[i] == s.CharAt(i));
      74                 :   }
      75               1 :   res += CHECK(it == s.BeginReading());
      76               1 :   res += CHECK(end == s.EndReading());
      77               1 :   return res;
      78                 : }
      79                 : 
      80               1 : int testWrite() {
      81               2 :   nsString s(NS_LITERAL_STRING("xyzz"));
      82                 :   PRUnichar *begin, *end;
      83               1 :   int res = CHECK(4 == s.Length());
      84               1 :   PRUint32 len = s.BeginWriting(&begin, &end, 5);
      85               1 :   res += CHECK(5 == s.Length()) +
      86               1 :          CHECK(5 == len) +
      87               1 :          CHECK(end == begin + 5) +
      88               1 :          CHECK(begin == s.BeginWriting()) +
      89               3 :          CHECK(end == s.EndWriting());
      90               1 :   begin[4] = PRUnichar('y');
      91               1 :   res += CHECK(s.Equals(NS_LITERAL_STRING("xyzzy")));
      92               1 :   s.SetLength(4);
      93               1 :   res += CHECK(4 == s.Length()) +
      94               2 :          CHECK(s.Equals(NS_LITERAL_STRING("xyzz"))) +
      95               2 :          CHECK(!s.Equals(NS_LITERAL_STRING("xyzzy"))) +
      96               2 :          CHECK(!s.IsEmpty());
      97               1 :   s.Truncate();
      98               1 :   res += CHECK(0 == s.Length()) +
      99               1 :          CHECK(s.IsEmpty());
     100               1 :   const PRUnichar sample[] = { 's', 'a', 'm', 'p', 'l', 'e', '\0' };
     101               1 :   s.Assign(sample);
     102               1 :   res += CHECK(s.EqualsLiteral("sample"));
     103               1 :   s.Assign(sample, 4);
     104               1 :   res += CHECK(s.EqualsLiteral("samp"));
     105               1 :   s.Assign(PRUnichar('q'));
     106               1 :   res += CHECK(s.EqualsLiteral("q"));
     107               1 :   return res;
     108                 : }
     109                 : 
     110               1 : int testFind() {
     111               2 :   nsString str_haystack;
     112               2 :   nsString str_needle;
     113               1 :   str_needle.AssignLiteral("world");
     114                 : 
     115               1 :   PRInt32 ret = 0;
     116               1 :   ret += CHECK(-1 == str_haystack.Find("world"));
     117               1 :   ret += CHECK(-1 == str_haystack.Find(str_needle));
     118                 : 
     119               1 :   str_haystack.AssignLiteral("hello world hello world hello");
     120               1 :   ret += CHECK( 6 == str_haystack.Find("world"));
     121               1 :   ret += CHECK( 6 == str_haystack.Find(str_needle));
     122               1 :   ret += CHECK(-1 == str_haystack.Find("world", 20, false));
     123               1 :   ret += CHECK(-1 == str_haystack.Find(str_needle, 20));
     124               1 :   ret += CHECK(18 == str_haystack.Find("world", 12, false));
     125               1 :   ret += CHECK(18 == str_haystack.Find(str_needle, 12));
     126                 : 
     127               2 :   nsCString cstr_haystack;
     128               2 :   nsCString cstr_needle;
     129               1 :   cstr_needle.AssignLiteral("world");
     130                 :   
     131               1 :   ret += CHECK(-1 == cstr_haystack.Find("world"));
     132               1 :   ret += CHECK(-1 == cstr_haystack.Find(cstr_needle));
     133                 : 
     134               1 :   cstr_haystack.AssignLiteral("hello world hello world hello");
     135               1 :   ret += CHECK( 6 == cstr_haystack.Find("world"));
     136               1 :   ret += CHECK( 6 == cstr_haystack.Find(cstr_needle));
     137               1 :   ret += CHECK(-1 == cstr_haystack.Find(cstr_needle, 20));
     138               1 :   ret += CHECK(18 == cstr_haystack.Find(cstr_needle, 12));
     139               1 :   ret += CHECK( 6 == cstr_haystack.Find("world", 5));
     140                 : 
     141               1 :   return ret;
     142                 : }
     143                 : 
     144               1 : int testVoid() {
     145               2 :   nsString s;
     146               1 :   int ret = CHECK(!s.IsVoid());
     147               1 :   s.SetIsVoid(false);
     148               1 :   ret += CHECK(!s.IsVoid());
     149               1 :   s.SetIsVoid(true);
     150               1 :   ret += CHECK(s.IsVoid());
     151               1 :   s.SetIsVoid(false);
     152               1 :   ret += CHECK(!s.IsVoid());
     153               1 :   s.SetIsVoid(true);
     154               1 :   s.AssignLiteral("hello");
     155               1 :   ret += CHECK(!s.IsVoid());
     156               1 :   return ret;
     157                 : }
     158                 : 
     159               1 : int testRFind() {
     160               1 :   PRInt32 ret = 0;
     161                 : 
     162                 :   // nsString.RFind
     163               2 :   nsString str_haystack;
     164               2 :   nsString str_needle;
     165               1 :   str_needle.AssignLiteral("world");
     166                 : 
     167               1 :   ret += CHECK(-1 == str_haystack.RFind(str_needle));
     168               1 :   ret += CHECK(-1 == str_haystack.RFind("world"));
     169                 : 
     170               1 :   str_haystack.AssignLiteral("hello world hElLo woRlD");
     171                 : 
     172               1 :   ret += CHECK( 6 == str_haystack.RFind(str_needle));
     173               1 :   ret += CHECK( 6 == str_haystack.RFind(str_needle, -1));
     174               1 :   ret += CHECK( 6 == str_haystack.RFind(str_needle, 17));
     175               1 :   ret += CHECK( 6 == str_haystack.RFind("world", false));
     176               1 :   ret += CHECK(18 == str_haystack.RFind("world", true));
     177               1 :   ret += CHECK( 6 == str_haystack.RFind("world", -1, false));
     178               1 :   ret += CHECK(18 == str_haystack.RFind("world", -1, true));
     179               1 :   ret += CHECK( 6 == str_haystack.RFind("world", 17, false));
     180               1 :   ret += CHECK( 0 == str_haystack.RFind("hello", 0, false));
     181               1 :   ret += CHECK(18 == str_haystack.RFind("world", 19, true));
     182               1 :   ret += CHECK(18 == str_haystack.RFind("world", 22, true));
     183               1 :   ret += CHECK(18 == str_haystack.RFind("world", 23, true));
     184                 : 
     185                 :   // nsCString.RFind
     186               2 :   nsCString cstr_haystack;
     187               2 :   nsCString cstr_needle;
     188               1 :   cstr_needle.AssignLiteral("world");
     189                 : 
     190               1 :   ret += CHECK(-1 == cstr_haystack.RFind(cstr_needle));
     191               1 :   ret += CHECK(-1 == cstr_haystack.RFind("world"));
     192                 :   
     193               1 :   cstr_haystack.AssignLiteral("hello world hElLo woRlD");
     194                 : 
     195               1 :   ret += CHECK( 6 == cstr_haystack.RFind(cstr_needle));
     196               1 :   ret += CHECK( 6 == cstr_haystack.RFind(cstr_needle, -1));
     197               1 :   ret += CHECK( 6 == cstr_haystack.RFind(cstr_needle, 17));
     198               1 :   ret += CHECK( 6 == cstr_haystack.RFind("world", 5));
     199               1 :   ret += CHECK( 0 == cstr_haystack.RFind(nsDependentCString("hello"), 0));
     200                 : 
     201               1 :   return ret;
     202                 : }
     203                 : 
     204               1 : int testCompressWhitespace() {
     205               1 :   PRInt32 ret = 0;
     206                 : 
     207                 :   // CompressWhitespace utility function
     208               2 :   nsString s;
     209                 : 
     210               1 :   s.AssignLiteral("     ");
     211               1 :   CompressWhitespace(s);
     212               1 :   ret += CHECK(s.EqualsLiteral(""));
     213                 : 
     214               1 :   s.AssignLiteral("  no more  leading spaces");
     215               1 :   CompressWhitespace(s);
     216               1 :   ret += CHECK(s.EqualsLiteral("no more leading spaces"));
     217                 : 
     218               1 :   s.AssignLiteral("no    more trailing spaces ");
     219               1 :   CompressWhitespace(s);
     220               1 :   ret += CHECK(s.EqualsLiteral("no more trailing spaces"));
     221                 : 
     222               1 :   s.AssignLiteral("   hello one    2         three    45        ");
     223               1 :   CompressWhitespace(s);
     224               1 :   ret += CHECK(s.EqualsLiteral("hello one 2 three 45"));
     225                 : 
     226               1 :   return ret;
     227                 : }
     228                 : 
     229               1 : int main() {
     230               1 :   int rv = 0;
     231               1 :   rv += testEmpty();
     232               1 :   rv += testAccess();
     233               1 :   rv += testWrite();
     234               1 :   rv += testFind();
     235               1 :   rv += testVoid();
     236               1 :   rv += testRFind();
     237               1 :   rv += testCompressWhitespace();
     238               1 :   if (0 == rv) {
     239               1 :     fprintf(stderr, "PASS: StringAPI tests\n");
     240                 :   }
     241               1 :   return rv;
     242                 : }

Generated by: LCOV version 1.7