File: testSystemTools.cxx

package info (click to toggle)
cmake 4.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 158,704 kB
  • sloc: ansic: 406,077; cpp: 309,512; sh: 4,233; python: 3,696; yacc: 3,109; lex: 1,279; f90: 538; asm: 471; lisp: 375; java: 310; cs: 270; fortran: 239; objc: 215; perl: 213; xml: 198; makefile: 110; javascript: 83; pascal: 63; tcl: 55; php: 25; ruby: 22; sed: 2
file content (174 lines) | stat: -rw-r--r-- 6,391 bytes parent folder | download | duplicates (3)
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
   file LICENSE.rst or https://cmake.org/licensing for details.  */

#include <cmConfigure.h> // IWYU pragma: keep

#include <string>
#include <vector>

#include <stddef.h>

#include "cmSystemTools.h"

#include "testCommon.h"

static bool testUpperCase()
{
  std::cout << "testUpperCase()\n";
  ASSERT_EQUAL(cmSystemTools::UpperCase("abc"), "ABC");
  return true;
}

static bool testVersionCompare()
{
  std::cout << "testVersionCompare()\n";
  ASSERT_TRUE(cmSystemTools::VersionCompareEqual("", ""));
  ASSERT_TRUE(!cmSystemTools::VersionCompareGreater("", ""));
  ASSERT_TRUE(cmSystemTools::VersionCompareEqual("1", "1a"));
  ASSERT_TRUE(!cmSystemTools::VersionCompareGreater("1", "1a"));
  ASSERT_TRUE(cmSystemTools::VersionCompareEqual("001", "1"));
  ASSERT_TRUE(!cmSystemTools::VersionCompareGreater("001", "1"));
  ASSERT_TRUE(!cmSystemTools::VersionCompareEqual("002", "1"));
  ASSERT_TRUE(cmSystemTools::VersionCompareGreater("002", "1"));
  ASSERT_TRUE(!cmSystemTools::VersionCompareEqual("6.2.1", "6.3.1"));
  ASSERT_TRUE(!cmSystemTools::VersionCompareGreater("6.2.1", "6.3.1"));
  ASSERT_TRUE(!cmSystemTools::VersionCompareEqual("6.2.1", "6.2"));
  ASSERT_TRUE(cmSystemTools::VersionCompareGreater("6.2.1", "6.2"));
  ASSERT_TRUE(!cmSystemTools::VersionCompareEqual(
    "3.14159265358979323846264338327950288419716939937510582097494459230",
    "3.14159265358979323846264338327950288419716939937510582097494459231"));
  ASSERT_TRUE(!cmSystemTools::VersionCompareGreater(
    "3.14159265358979323846264338327950288419716939937510582097494459230",
    "3.14159265358979323846264338327950288419716939937510582097494459231"));
  ASSERT_TRUE(!cmSystemTools::VersionCompareEqual(
    "3.141592653589793238462643383279502884197169399375105820974944592307",
    "3.14159265358979323846264338327950288419716939937510582097494459231"));
  ASSERT_TRUE(cmSystemTools::VersionCompareGreater(
    "3.141592653589793238462643383279502884197169399375105820974944592307",
    "3.14159265358979323846264338327950288419716939937510582097494459231"));
  return true;
}

static bool testStrVersCmp()
{
  std::cout << "testStrVersCmp()\n";
  ASSERT_TRUE(cmSystemTools::strverscmp("", "") == 0);
  ASSERT_TRUE(cmSystemTools::strverscmp("abc", "") > 0);
  ASSERT_TRUE(cmSystemTools::strverscmp("abc", "abc") == 0);
  ASSERT_TRUE(cmSystemTools::strverscmp("abd", "abc") > 0);
  ASSERT_TRUE(cmSystemTools::strverscmp("abc", "abd") < 0);
  ASSERT_TRUE(cmSystemTools::strverscmp("12345", "12344") > 0);
  ASSERT_TRUE(cmSystemTools::strverscmp("100", "99") > 0);
  ASSERT_TRUE(cmSystemTools::strverscmp("12345", "00345") > 0);
  ASSERT_TRUE(cmSystemTools::strverscmp("99999999999999", "99999999999991") >
              0);
  ASSERT_TRUE(cmSystemTools::strverscmp("00000000000009", "00000000000001") >
              0);
  ASSERT_TRUE(cmSystemTools::strverscmp("a.b.c.0", "a.b.c.000") > 0);
  ASSERT_TRUE(cmSystemTools::strverscmp("lib_1.2_10", "lib_1.2_2") > 0);
  ASSERT_TRUE(cmSystemTools::strverscmp("12lib", "2lib") > 0);
  ASSERT_TRUE(cmSystemTools::strverscmp("02lib", "002lib") > 0);
  ASSERT_TRUE(cmSystemTools::strverscmp("10", "9a") > 0);
  ASSERT_TRUE(cmSystemTools::strverscmp("000", "0001") > 0);

  // test sorting using standard strvercmp input
  std::vector<std::string> testString;
  testString.push_back("000");
  testString.push_back("00");
  testString.push_back("01");
  testString.push_back("010");
  testString.push_back("09");
  testString.push_back("0");
  testString.push_back("1");
  testString.push_back("9");
  testString.push_back("10");

  // test global ordering of input strings
  for (size_t i = 0; i < testString.size() - 1; i++) {
    for (size_t j = i + 1; j < testString.size(); j++) {
      if (cmSystemTools::strverscmp(testString[i], testString[j]) >= 0) {
        std::cout << "cmSystemTools::strverscmp error in comparing strings "
                  << testString[i] << ' ' << testString[j] << '\n';
        return false;
      }
    }
  }
  return true;
}

static bool testRelativeIfUnder()
{
  std::cout << "testRelativeIfUnder()\n";

  std::string result = cmSystemTools::RelativeIfUnder("/a/b", "/a/b");
  if (result != ".") {
    std::cout << "cmSystemTools::RelativeIfUnder failed on /a/b, /a/b: "
              << result << "\n";
    return false;
  }
  result = cmSystemTools::RelativeIfUnder("/a/b", "/a/b/c/d");
  if (result != "c/d") {
    std::cout << "cmSystemTools::RelativeIfUnder failed on /a/b, /a/b/c/d: "
              << result << "\n";
    return false;
  }
  result = cmSystemTools::RelativeIfUnder("/d/f/", "/a/b/c/d");
  if (result != "/a/b/c/d") {
    std::cout << "cmSystemTools::RelativeIfUnder failed on /d/f/, /a/b/c/d: "
              << result << "\n";
    return false;
  }
  result = cmSystemTools::RelativeIfUnder("/", "/a/b");
  if (result != "a/b") {
    std::cout << "cmSystemTools::RelativeIfUnder failed on /, /a/b: " << result
              << "\n";
    return false;
  }
  result = cmSystemTools::RelativeIfUnder("I:/", "I:/CMakeLists.txt");
  if (result != "CMakeLists.txt") {
    std::cout
      << "cmSystemTools::RelativeIfUnder failed on I:/, I:/CMakeLists.txt: "
      << result << "\n";
    return false;
  }
  result = cmSystemTools::RelativeIfUnder("", "/a/b");
  if (result != "/a/b") {
    std::cout << "cmSystemTools::RelativeIfUnder failed on \"\", /a/b: "
              << result << "\n";
    return false;
  }
  return true;
}

static bool testMakeTempDirectory()
{
  std::cout << "testMakeTempDirectory()\n";

  static std::string const kTemplate = "testMakeTempDirectory-XXXXXX";
  std::string tempDir = kTemplate;
  cmsys::Status status = cmSystemTools::MakeTempDirectory(tempDir);
  if (!status) {
    std::cout << "cmSystemTools::MakeTempDirectory failed on \"" << tempDir
              << "\": " << status.GetString() << '\n';
    return false;
  }
  if (!cmSystemTools::FileIsDirectory(tempDir)) {
    std::cout << "cmSystemTools::MakeTempDirectory did not create \""
              << tempDir << '\n';
    return false;
  }
  cmSystemTools::RemoveADirectory(tempDir);
  ASSERT_TRUE(tempDir != kTemplate);
  return true;
}

int testSystemTools(int /*unused*/, char* /*unused*/[])
{
  return runTests({
    testUpperCase,
    testVersionCompare,
    testStrVersCmp,
    testRelativeIfUnder,
    testMakeTempDirectory,
  });
}