Back to index

lightning-sunbird  0.9+nobinonly
TestCRT.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "nsCRT.h"
00039 #include "nsString.h"
00040 #include "plstr.h"
00041 #include <stdlib.h>
00042 
00043 // The return from strcmp etc is only defined to be postive, zero or
00044 // negative. The magnitude of a non-zero return is irrelevant.
00045 PRIntn sign(PRIntn val) {
00046     if (val == 0)
00047        return 0;
00048     else {
00049        if (val > 0)
00050            return 1;
00051        else
00052            return -1;
00053     }
00054 }
00055 
00056 
00057 // Verify that nsCRT versions of string comparison routines get the
00058 // same answers as the native non-unicode versions. We only pass in
00059 // iso-latin-1 strings, so the comparison must be valid.
00060 static void Check(const char* s1, const char* s2, PRIntn n)
00061 {
00062   PRIntn clib = PL_strcmp(s1, s2);
00063   PRIntn clib_n = PL_strncmp(s1, s2, n);
00064   PRIntn clib_case = PL_strcasecmp(s1, s2);
00065   PRIntn clib_case_n = PL_strncasecmp(s1, s2, n);
00066 
00067   nsAutoString t1,t2; 
00068   t1.AssignWithConversion(s1);
00069   t2.AssignWithConversion(s2);
00070   const PRUnichar* us1 = t1.get();
00071   const PRUnichar* us2 = t2.get();
00072 
00073   PRIntn u2 = nsCRT::strcmp(us1, us2);
00074   PRIntn u2_n = nsCRT::strncmp(us1, us2, n);
00075 
00076   NS_ASSERTION(sign(clib) == sign(u2), "strcmp");
00077   NS_ASSERTION(sign(clib_n) == sign(u2_n), "strncmp");
00078 }
00079 
00080 struct Test {
00081   const char* s1;
00082   const char* s2;
00083   PRIntn n;
00084 };
00085 
00086 static Test tests[] = {
00087   { "foo", "foo", 3 },
00088   { "foo", "fo", 3 },
00089 
00090   { "foo", "bar", 3 },
00091   { "foo", "ba", 3 },
00092 
00093   { "foo", "zap", 3 },
00094   { "foo", "za", 3 },
00095 
00096   { "bar", "foo", 3 },
00097   { "bar", "fo", 3 },
00098 
00099   { "bar", "foo", 3 },
00100   { "bar", "fo", 3 },
00101 };
00102 #define NUM_TESTS int((sizeof(tests) / sizeof(tests[0])))
00103 
00104 int main()
00105 {
00106   Test* tp = tests;
00107   for (int i = 0; i < NUM_TESTS; i++, tp++) {
00108     Check(tp->s1, tp->s2, tp->n);
00109   }
00110 
00111   return 0;
00112 }