Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/xpcom/tests/gtest/TestCRT.cpp
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
 * License, v. 2.0. If a copy of the MPL was not distributed with this
5
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#include "nsCRT.h"
8
#include "nsString.h"
9
#include "plstr.h"
10
#include <stdlib.h>
11
#include "gtest/gtest.h"
12
13
namespace TestCRT {
14
15
// The return from strcmp etc is only defined to be postive, zero or
16
// negative. The magnitude of a non-zero return is irrelevant.
17
0
int sign(int val) {
18
0
  if (val == 0) {
19
0
    return 0;
20
0
  } else {
21
0
    if (val > 0) {
22
0
      return 1;
23
0
    } else {
24
0
      return -1;
25
0
    }
26
0
  }
27
0
}
28
29
30
// Verify that nsCRT versions of string comparison routines get the
31
// same answers as the native non-unicode versions. We only pass in
32
// iso-latin-1 strings, so the comparison must be valid.
33
static void Check(const char* s1, const char* s2, size_t n)
34
0
{
35
0
  bool longerThanN = strlen(s1) > n || strlen(s2) > n;
36
0
37
0
  int clib = PL_strcmp(s1, s2);
38
0
  int clib_n = PL_strncmp(s1, s2, n);
39
0
40
0
  if (!longerThanN) {
41
0
    EXPECT_EQ(sign(clib), sign(clib_n));
42
0
  }
43
0
44
0
  nsAutoString t1,t2;
45
0
  CopyASCIItoUTF16(mozilla::MakeStringSpan(s1), t1);
46
0
  CopyASCIItoUTF16(mozilla::MakeStringSpan(s2), t2);
47
0
  const char16_t* us1 = t1.get();
48
0
  const char16_t* us2 = t2.get();
49
0
50
0
  int u2, u2_n;
51
0
  u2 = nsCRT::strcmp(us1, us2);
52
0
53
0
  EXPECT_EQ(sign(clib), sign(u2));
54
0
55
0
  u2 = NS_strcmp(us1, us2);
56
0
  u2_n = NS_strncmp(us1, us2, n);
57
0
58
0
  EXPECT_EQ(sign(clib), sign(u2));
59
0
  EXPECT_EQ(sign(clib_n), sign(u2_n));
60
0
}
61
62
struct Test {
63
  const char* s1;
64
  const char* s2;
65
  size_t n;
66
};
67
68
static Test tests[] = {
69
  { "foo", "foo", 3 },
70
  { "foo", "fo", 3 },
71
72
  { "foo", "bar", 3 },
73
  { "foo", "ba", 3 },
74
75
  { "foo", "zap", 3 },
76
  { "foo", "za", 3 },
77
78
  { "bar", "foo", 3 },
79
  { "bar", "fo", 3 },
80
81
  { "bar", "foo", 3 },
82
  { "bar", "fo", 3 },
83
84
  { "foo", "foobar", 3 },
85
  { "foobar", "foo", 3 },
86
  { "foobar", "foozap", 3 },
87
  { "foozap", "foobar", 3 },
88
};
89
0
#define NUM_TESTS int((sizeof(tests) / sizeof(tests[0])))
90
91
TEST(CRT, main)
92
0
{
93
0
  TestCRT::Test* tp = tests;
94
0
  for (int i = 0; i < NUM_TESTS; i++, tp++) {
95
0
    Check(tp->s1, tp->s2, tp->n);
96
0
  }
97
0
}
98
99
} // namespace TestCRT