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