aboutsummaryrefslogtreecommitdiff
path: root/tests/strcase/driver.cxx
blob: e9b779c2e5a5e2de9fb3698cadf4feb03dcb28cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// file      : tests/strcase/driver.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2019 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#include <cassert>

#ifndef __cpp_lib_modules_ts
#include <string>
#endif

// Other includes.

#ifdef __cpp_modules_ts
#ifdef __cpp_lib_modules_ts
import std.core;
#endif
import butl.utility;
#else
#include <libbutl/utility.mxx>
#endif

using namespace std;
using namespace butl;

int
main ()
{
  const string upper ("+/0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ");
  const string lower ("+/0123456789abcdefghijklmnopqrstuvwxyz");

  assert (casecmp (upper, lower) == 0);
  assert (casecmp (upper, lower, upper.size ()) == 0);
  assert (casecmp (upper, lower, 100) == 0);
  assert (casecmp ("a", "A1") < 0);
  assert (casecmp ("A1", "a") > 0);
  assert (casecmp ("a", "A1", 1) == 0);
  assert (casecmp ("A1", "a", 1) == 0);
  assert (casecmp ("a", "b", 0) == 0);

  for (size_t i (0); i < upper.size (); ++i)
  {
    assert (casecmp (upper[i], lower[i]) == 0);

    if (i > 0)
    {
      assert (casecmp (upper[i], lower[i - 1]) > 0);
      assert (casecmp (lower[i - 1], upper[i]) < 0);
    }
  }

  // As casecmp() compares strings as if they have been converted to the
  // lower case the characters [\]^_` (located between 'Z' and 'a' in the ASCII
  // table) evaluates as less than any alphabetic character.
  //
  string ascii_91_96 ("[\\]^_`");
  for (const auto& c: ascii_91_96)
  {
    assert (casecmp (&c, "A", 1) < 0);
    assert (casecmp (&c, "a", 1) < 0);
  }

  assert (ucase (lower) == upper);
  assert (lcase (upper) == lower);

  assert (ucase (lower.c_str (), 20) == string (upper, 0, 20));
  assert (lcase (upper.c_str (), 20) == string (lower, 0, 20));

  assert (ucase (lower.c_str (), 0) == string ());
  assert (lcase (upper.c_str (), 0) == string ());

  assert (ucase ("") == string ());
  assert (lcase ("") == string ());

  string s (upper);
  assert (lcase (s) == lower);

  s = lower;
  ucase (const_cast<char*> (s.data ()), s.size ());
  assert (s == upper);
}