michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nsCRT.h" michael@0: #include "nsString.h" michael@0: #include "plstr.h" michael@0: #include michael@0: michael@0: namespace TestCRT { michael@0: michael@0: // The return from strcmp etc is only defined to be postive, zero or michael@0: // negative. The magnitude of a non-zero return is irrelevant. michael@0: int sign(int val) { michael@0: if (val == 0) michael@0: return 0; michael@0: else { michael@0: if (val > 0) michael@0: return 1; michael@0: else michael@0: return -1; michael@0: } michael@0: } michael@0: michael@0: michael@0: // Verify that nsCRT versions of string comparison routines get the michael@0: // same answers as the native non-unicode versions. We only pass in michael@0: // iso-latin-1 strings, so the comparison must be valid. michael@0: static void Check(const char* s1, const char* s2, int n) michael@0: { michael@0: #ifdef DEBUG michael@0: int clib = michael@0: #endif michael@0: PL_strcmp(s1, s2); michael@0: michael@0: #ifdef DEBUG michael@0: int clib_n = michael@0: #endif michael@0: PL_strncmp(s1, s2, n); michael@0: michael@0: nsAutoString t1,t2; michael@0: t1.AssignWithConversion(s1); michael@0: t2.AssignWithConversion(s2); michael@0: const char16_t* us1 = t1.get(); michael@0: const char16_t* us2 = t2.get(); michael@0: michael@0: #ifdef DEBUG michael@0: int u2 = michael@0: #endif michael@0: nsCRT::strcmp(us1, us2); michael@0: michael@0: #ifdef DEBUG michael@0: int u2_n = michael@0: #endif michael@0: nsCRT::strncmp(us1, us2, n); michael@0: michael@0: NS_ASSERTION(sign(clib) == sign(u2), "strcmp"); michael@0: NS_ASSERTION(sign(clib_n) == sign(u2_n), "strncmp"); michael@0: } michael@0: michael@0: struct Test { michael@0: const char* s1; michael@0: const char* s2; michael@0: int n; michael@0: }; michael@0: michael@0: static Test tests[] = { michael@0: { "foo", "foo", 3 }, michael@0: { "foo", "fo", 3 }, michael@0: michael@0: { "foo", "bar", 3 }, michael@0: { "foo", "ba", 3 }, michael@0: michael@0: { "foo", "zap", 3 }, michael@0: { "foo", "za", 3 }, michael@0: michael@0: { "bar", "foo", 3 }, michael@0: { "bar", "fo", 3 }, michael@0: michael@0: { "bar", "foo", 3 }, michael@0: { "bar", "fo", 3 }, michael@0: }; michael@0: #define NUM_TESTS int((sizeof(tests) / sizeof(tests[0]))) michael@0: michael@0: } michael@0: michael@0: using namespace TestCRT; michael@0: michael@0: int main() michael@0: { michael@0: Test* tp = tests; michael@0: for (int i = 0; i < NUM_TESTS; i++, tp++) { michael@0: Check(tp->s1, tp->s2, tp->n); michael@0: } michael@0: michael@0: return 0; michael@0: }