TestCRT.cpp revision 677833bc953b6cb418c701facbdcf4aa18d6c44e
5090N/A/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
5090N/A/* ***** BEGIN LICENSE BLOCK *****
5090N/A * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5090N/A *
5090N/A * The contents of this file are subject to the Mozilla Public License Version
5090N/A * 1.1 (the "License"); you may not use this file except in compliance with
5090N/A * the License. You may obtain a copy of the License at
5090N/A * http://www.mozilla.org/MPL/
5090N/A *
5090N/A * Software distributed under the License is distributed on an "AS IS" basis,
5090N/A * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
5090N/A * for the specific language governing rights and limitations under the
5090N/A * License.
5090N/A *
5090N/A * The Original Code is mozilla.org code.
5090N/A *
5090N/A * The Initial Developer of the Original Code is
5090N/A * Netscape Communications Corporation.
5090N/A * Portions created by the Initial Developer are Copyright (C) 1998
5090N/A * the Initial Developer. All Rights Reserved.
5090N/A *
5090N/A * Contributor(s):
5090N/A *
5090N/A * Alternatively, the contents of this file may be used under the terms of
5090N/A * either of the GNU General Public License Version 2 or later (the "GPL"),
5090N/A * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
5090N/A * in which case the provisions of the GPL or the LGPL are applicable instead
5090N/A * of those above. If you wish to allow use of your version of this file only
5090N/A * under the terms of either the GPL or the LGPL, and not to allow others to
5090N/A * use your version of this file under the terms of the MPL, indicate your
5090N/A * decision by deleting the provisions above and replace them with the notice
5090N/A * and other provisions required by the GPL or the LGPL. If you do not delete
5090N/A * the provisions above, a recipient may use your version of this file under
5090N/A * the terms of any one of the MPL, the GPL or the LGPL.
5090N/A *
5090N/A * ***** END LICENSE BLOCK ***** */
5090N/A
5090N/A#include "nsCRT.h"
5090N/A#include "nsString.h"
5090N/A#include "plstr.h"
5090N/A#include <stdlib.h>
5090N/A
5090N/A// The return from strcmp etc is only defined to be postive, zero or
5090N/A// negative. The magnitude of a non-zero return is irrelevant.
5090N/APRIntn sign(PRIntn val) {
5090N/A if (val == 0)
5090N/A return 0;
5090N/A else {
5090N/A if (val > 0)
5090N/A return 1;
5090N/A else
5090N/A return -1;
5090N/A }
5090N/A}
5090N/A
5090N/A
5090N/A// Verify that nsCRT versions of string comparison routines get the
5090N/A// same answers as the native non-unicode versions. We only pass in
5090N/A// iso-latin-1 strings, so the comparison must be valid.
5090N/Astatic void Check(const char* s1, const char* s2, PRIntn n)
5090N/A{
5090N/A PRIntn clib = PL_strcmp(s1, s2);
5090N/A PRIntn clib_n = PL_strncmp(s1, s2, n);
5090N/A PRIntn clib_case = PL_strcasecmp(s1, s2);
5090N/A PRIntn clib_case_n = PL_strncasecmp(s1, s2, n);
5090N/A
5090N/A nsAutoString t1,t2;
5090N/A t1.AssignWithConversion(s1);
5090N/A t2.AssignWithConversion(s2);
5090N/A const PRUnichar* us1 = t1.get();
5090N/A const PRUnichar* us2 = t2.get();
5090N/A
5090N/A PRIntn u2 = nsCRT::strcmp(us1, us2);
5090N/A PRIntn u2_n = nsCRT::strncmp(us1, us2, n);
5090N/A
5090N/A NS_ASSERTION(sign(clib) == sign(u2), "strcmp");
5090N/A NS_ASSERTION(sign(clib_n) == sign(u2_n), "strncmp");
5090N/A}
5090N/A
5090N/Astruct Test {
5090N/A const char* s1;
5090N/A const char* s2;
5090N/A PRIntn n;
5090N/A};
5090N/A
5090N/Astatic Test tests[] = {
5090N/A { "foo", "foo", 3 },
5090N/A { "foo", "fo", 3 },
5090N/A
5090N/A { "foo", "bar", 3 },
5090N/A { "foo", "ba", 3 },
5090N/A
5090N/A { "foo", "zap", 3 },
5090N/A { "foo", "za", 3 },
5090N/A
5090N/A { "bar", "foo", 3 },
5090N/A { "bar", "fo", 3 },
5090N/A
5090N/A { "bar", "foo", 3 },
{ "bar", "fo", 3 },
};
#define NUM_TESTS int((sizeof(tests) / sizeof(tests[0])))
int main()
{
Test* tp = tests;
for (int i = 0; i < NUM_TESTS; i++, tp++) {
Check(tp->s1, tp->s2, tp->n);
}
return 0;
}