1 // Basic sanity tests of CHARACTER API; exhaustive testing will be done
2 // in Fortran.
3
4 #include "../../runtime/character.h"
5 #include "testing.h"
6 #include <cstring>
7
8 using namespace Fortran::runtime;
9
AppendAndPad(std::size_t limit)10 static void AppendAndPad(std::size_t limit) {
11 char x[8];
12 std::size_t xLen{0};
13 std::memset(x, 0, sizeof x);
14 xLen = RTNAME(CharacterAppend1)(x, limit, xLen, "abc", 3);
15 xLen = RTNAME(CharacterAppend1)(x, limit, xLen, "DE", 2);
16 RTNAME(CharacterPad1)(x, limit, xLen);
17 if (xLen > limit) {
18 Fail() << "xLen " << xLen << ">" << limit << '\n';
19 }
20 if (x[limit]) {
21 Fail() << "x[" << limit << "]='" << x[limit] << "'\n";
22 x[limit] = '\0';
23 }
24 if (std::memcmp(x, "abcDE ", limit)) {
25 Fail() << "x = '" << x << "'\n";
26 }
27 }
28
TestCharCompare(const char * x,const char * y,std::size_t xBytes,std::size_t yBytes,int expect)29 static void TestCharCompare(const char *x, const char *y, std::size_t xBytes,
30 std::size_t yBytes, int expect) {
31 int cmp{RTNAME(CharacterCompareScalar1)(x, y, xBytes, yBytes)};
32 if (cmp != expect) {
33 char buf[2][8];
34 std::memset(buf, 0, sizeof buf);
35 std::memcpy(buf[0], x, xBytes);
36 std::memcpy(buf[1], y, yBytes);
37 Fail() << "compare '" << buf[0] << "'(" << xBytes << ") to '" << buf[1]
38 << "'(" << yBytes << "), got " << cmp << ", should be " << expect
39 << '\n';
40 }
41 }
42
Compare(const char * x,const char * y,std::size_t xBytes,std::size_t yBytes,int expect)43 static void Compare(const char *x, const char *y, std::size_t xBytes,
44 std::size_t yBytes, int expect) {
45 TestCharCompare(x, y, xBytes, yBytes, expect);
46 TestCharCompare(y, x, yBytes, xBytes, -expect);
47 }
48
main()49 int main() {
50 StartTests();
51 for (std::size_t j{0}; j < 8; ++j) {
52 AppendAndPad(j);
53 }
54 Compare("abc", "abc", 3, 3, 0);
55 Compare("abc", "def", 3, 3, -1);
56 Compare("ab ", "abc", 3, 2, 0);
57 Compare("abc", "abc", 2, 3, -1);
58 return EndTests();
59 }
60