xpcom/tests/TestCRT.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/xpcom/tests/TestCRT.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,98 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include "nsCRT.h"
    1.10 +#include "nsString.h"
    1.11 +#include "plstr.h"
    1.12 +#include <stdlib.h>
    1.13 +
    1.14 +namespace TestCRT {
    1.15 +
    1.16 +// The return from strcmp etc is only defined to be postive, zero or
    1.17 +// negative. The magnitude of a non-zero return is irrelevant.
    1.18 +int sign(int val) {
    1.19 +    if (val == 0)
    1.20 +	return 0;
    1.21 +    else {
    1.22 +	if (val > 0)
    1.23 +	    return 1;
    1.24 +	else
    1.25 +	    return -1;
    1.26 +    }
    1.27 +}
    1.28 +
    1.29 +
    1.30 +// Verify that nsCRT versions of string comparison routines get the
    1.31 +// same answers as the native non-unicode versions. We only pass in
    1.32 +// iso-latin-1 strings, so the comparison must be valid.
    1.33 +static void Check(const char* s1, const char* s2, int n)
    1.34 +{
    1.35 +#ifdef DEBUG
    1.36 +  int clib =
    1.37 +#endif
    1.38 +    PL_strcmp(s1, s2);
    1.39 +
    1.40 +#ifdef DEBUG
    1.41 +  int clib_n =
    1.42 +#endif
    1.43 +    PL_strncmp(s1, s2, n);
    1.44 +
    1.45 +  nsAutoString t1,t2; 
    1.46 +  t1.AssignWithConversion(s1);
    1.47 +  t2.AssignWithConversion(s2);
    1.48 +  const char16_t* us1 = t1.get();
    1.49 +  const char16_t* us2 = t2.get();
    1.50 +
    1.51 +#ifdef DEBUG
    1.52 +  int u2 =
    1.53 +#endif
    1.54 +    nsCRT::strcmp(us1, us2);
    1.55 +
    1.56 +#ifdef DEBUG
    1.57 +  int u2_n =
    1.58 +#endif
    1.59 +    nsCRT::strncmp(us1, us2, n);
    1.60 +
    1.61 +  NS_ASSERTION(sign(clib) == sign(u2), "strcmp");
    1.62 +  NS_ASSERTION(sign(clib_n) == sign(u2_n), "strncmp");
    1.63 +}
    1.64 +
    1.65 +struct Test {
    1.66 +  const char* s1;
    1.67 +  const char* s2;
    1.68 +  int n;
    1.69 +};
    1.70 +
    1.71 +static Test tests[] = {
    1.72 +  { "foo", "foo", 3 },
    1.73 +  { "foo", "fo", 3 },
    1.74 +
    1.75 +  { "foo", "bar", 3 },
    1.76 +  { "foo", "ba", 3 },
    1.77 +
    1.78 +  { "foo", "zap", 3 },
    1.79 +  { "foo", "za", 3 },
    1.80 +
    1.81 +  { "bar", "foo", 3 },
    1.82 +  { "bar", "fo", 3 },
    1.83 +
    1.84 +  { "bar", "foo", 3 },
    1.85 +  { "bar", "fo", 3 },
    1.86 +};
    1.87 +#define NUM_TESTS int((sizeof(tests) / sizeof(tests[0])))
    1.88 +
    1.89 +}
    1.90 +
    1.91 +using namespace TestCRT;
    1.92 +
    1.93 +int main()
    1.94 +{
    1.95 +  Test* tp = tests;
    1.96 +  for (int i = 0; i < NUM_TESTS; i++, tp++) {
    1.97 +    Check(tp->s1, tp->s2, tp->n);
    1.98 +  }
    1.99 +
   1.100 +  return 0;
   1.101 +}

mercurial