character.cpp
1.68 KB
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
// Basic sanity tests of CHARACTER API; exhaustive testing will be done
// in Fortran.
#include "../../runtime/character.h"
#include "testing.h"
#include <cstring>
using namespace Fortran::runtime;
static void AppendAndPad(std::size_t limit) {
char x[8];
std::size_t xLen{0};
std::memset(x, 0, sizeof x);
xLen = RTNAME(CharacterAppend1)(x, limit, xLen, "abc", 3);
xLen = RTNAME(CharacterAppend1)(x, limit, xLen, "DE", 2);
RTNAME(CharacterPad1)(x, limit, xLen);
if (xLen > limit) {
Fail() << "xLen " << xLen << ">" << limit << '\n';
}
if (x[limit]) {
Fail() << "x[" << limit << "]='" << x[limit] << "'\n";
x[limit] = '\0';
}
if (std::memcmp(x, "abcDE ", limit)) {
Fail() << "x = '" << x << "'\n";
}
}
static void TestCharCompare(const char *x, const char *y, std::size_t xBytes,
std::size_t yBytes, int expect) {
int cmp{RTNAME(CharacterCompareScalar1)(x, y, xBytes, yBytes)};
if (cmp != expect) {
char buf[2][8];
std::memset(buf, 0, sizeof buf);
std::memcpy(buf[0], x, xBytes);
std::memcpy(buf[1], y, yBytes);
Fail() << "compare '" << buf[0] << "'(" << xBytes << ") to '" << buf[1]
<< "'(" << yBytes << "), got " << cmp << ", should be " << expect
<< '\n';
}
}
static void Compare(const char *x, const char *y, std::size_t xBytes,
std::size_t yBytes, int expect) {
TestCharCompare(x, y, xBytes, yBytes, expect);
TestCharCompare(y, x, yBytes, xBytes, -expect);
}
int main() {
StartTests();
for (std::size_t j{0}; j < 8; ++j) {
AppendAndPad(j);
}
Compare("abc", "abc", 3, 3, 0);
Compare("abc", "def", 3, 3, -1);
Compare("ab ", "abc", 3, 2, 0);
Compare("abc", "abc", 2, 3, -1);
return EndTests();
}