File: CharacterTest.cpp

package info (click to toggle)
llvm-toolchain-17 1%3A17.0.6-22
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,799,624 kB
  • sloc: cpp: 6,428,607; ansic: 1,383,196; asm: 793,408; python: 223,504; objc: 75,364; f90: 60,502; lisp: 33,869; pascal: 15,282; sh: 9,684; perl: 7,453; ml: 4,937; awk: 3,523; makefile: 2,889; javascript: 2,149; xml: 888; fortran: 619; cs: 573
file content (428 lines) | stat: -rw-r--r-- 15,491 bytes parent folder | download | duplicates (8)
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
//===-- flang/unittests/Runtime/CharacterTest.cpp ---------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

// Basic sanity tests of CHARACTER API; exhaustive testing will be done
// in Fortran.

#include "flang/Runtime/character.h"
#include "gtest/gtest.h"
#include "flang/Runtime/descriptor.h"
#include <cstring>
#include <functional>
#include <tuple>
#include <vector>

using namespace Fortran::runtime;

using CharacterTypes = ::testing::Types<char, char16_t, char32_t>;

// Helper for creating, allocating and filling up a descriptor with data from
// raw character literals, converted to the CHAR type used by the test.
template <typename CHAR>
OwningPtr<Descriptor> CreateDescriptor(const std::vector<SubscriptValue> &shape,
    const std::vector<const char *> &raw_strings) {
  std::size_t length{std::strlen(raw_strings[0])};

  OwningPtr<Descriptor> descriptor{Descriptor::Create(sizeof(CHAR), length,
      nullptr, shape.size(), nullptr, CFI_attribute_allocatable)};
  int rank{static_cast<int>(shape.size())};
  // Use a weird lower bound of 2 to flush out subscripting bugs
  for (int j{0}; j < rank; ++j) {
    descriptor->GetDimension(j).SetBounds(2, shape[j] + 1);
  }
  if (descriptor->Allocate() != 0) {
    return nullptr;
  }

  std::size_t offset = 0;
  for (const char *raw : raw_strings) {
    std::basic_string<CHAR> converted{raw, raw + length};
    std::copy(converted.begin(), converted.end(),
        descriptor->OffsetElement<CHAR>(offset * length * sizeof(CHAR)));
    ++offset;
  }

  return descriptor;
}

TEST(CharacterTests, AppendAndPad) {
  static constexpr int limitMax{8};
  static char buffer[limitMax];
  static std::size_t offset{0};
  for (std::size_t limit{0}; limit < limitMax; ++limit, offset = 0) {
    std::memset(buffer, 0, sizeof buffer);

    // Ensure appending characters does not overrun the limit
    offset = RTNAME(CharacterAppend1)(buffer, limit, offset, "abc", 3);
    offset = RTNAME(CharacterAppend1)(buffer, limit, offset, "DE", 2);
    ASSERT_LE(offset, limit) << "offset " << offset << ">" << limit;

    // Ensure whitespace padding does not overrun limit, the string is still
    // null-terminated, and string matches the expected value up to the limit.
    RTNAME(CharacterPad1)(buffer, limit, offset);
    EXPECT_EQ(buffer[limit], '\0')
        << "buffer[" << limit << "]='" << buffer[limit] << "'";
    buffer[limit] = buffer[limit] ? '\0' : buffer[limit];
    ASSERT_EQ(std::memcmp(buffer, "abcDE   ", limit), 0)
        << "buffer = '" << buffer << "'";
  }
}

TEST(CharacterTests, CharacterAppend1Overrun) {
  static constexpr int bufferSize{4};
  static constexpr std::size_t limit{2};
  static char buffer[bufferSize];
  static std::size_t offset{0};
  std::memset(buffer, 0, sizeof buffer);
  offset = RTNAME(CharacterAppend1)(buffer, limit, offset, "1234", bufferSize);
  ASSERT_EQ(offset, limit) << "CharacterAppend1 did not halt at limit = "
                           << limit << ", but at offset = " << offset;
}

// Test ADJUSTL() and ADJUSTR()
template <typename CHAR> struct AdjustLRTests : public ::testing::Test {};
TYPED_TEST_SUITE(AdjustLRTests, CharacterTypes, );

struct AdjustLRTestCase {
  const char *input, *output;
};

template <typename CHAR>
void RunAdjustLRTest(const char *which,
    const std::function<void(
        Descriptor &, const Descriptor &, const char *, int)> &adjust,
    const char *inputRaw, const char *outputRaw) {
  OwningPtr<Descriptor> input{CreateDescriptor<CHAR>({}, {inputRaw})};
  ASSERT_NE(input, nullptr);
  ASSERT_TRUE(input->IsAllocated());

  StaticDescriptor<1> outputStaticDescriptor;
  Descriptor &output{outputStaticDescriptor.descriptor()};

  adjust(output, *input, /*sourceFile=*/nullptr, /*sourceLine=*/0);
  std::basic_string<CHAR> got{
      output.OffsetElement<CHAR>(), std::strlen(inputRaw)};
  std::basic_string<CHAR> expect{outputRaw, outputRaw + std::strlen(outputRaw)};
  ASSERT_EQ(got, expect) << which << "('" << inputRaw
                         << "') for CHARACTER(kind=" << sizeof(CHAR) << ")";
}

TYPED_TEST(AdjustLRTests, AdjustL) {
  static std::vector<AdjustLRTestCase> testcases{
      {"     where should the spaces be?", "where should the spaces be?     "},
      {"   leading and trailing whitespaces   ",
          "leading and trailing whitespaces      "},
      {"shouldn't change", "shouldn't change"},
  };

  for (const auto &t : testcases) {
    RunAdjustLRTest<TypeParam>("Adjustl", RTNAME(Adjustl), t.input, t.output);
  }
}

TYPED_TEST(AdjustLRTests, AdjustR) {
  static std::vector<AdjustLRTestCase> testcases{
      {"where should the spaces be?   ", "   where should the spaces be?"},
      {" leading and trailing whitespaces ",
          "  leading and trailing whitespaces"},
      {"shouldn't change", "shouldn't change"},
  };

  for (const auto &t : testcases) {
    RunAdjustLRTest<TypeParam>("Adjustr", RTNAME(Adjustr), t.input, t.output);
  }
}

//------------------------------------------------------------------------------
/// Tests and infrastructure for character comparison functions
//------------------------------------------------------------------------------

template <typename CHAR>
using ComparisonFuncTy =
    std::function<int(const CHAR *, const CHAR *, std::size_t, std::size_t)>;

using ComparisonFuncsTy = std::tuple<ComparisonFuncTy<char>,
    ComparisonFuncTy<char16_t>, ComparisonFuncTy<char32_t>>;

// These comparison functions are the systems under test in the
// CharacterComparisonTests test cases.
static ComparisonFuncsTy comparisonFuncs{
    RTNAME(CharacterCompareScalar1),
    RTNAME(CharacterCompareScalar2),
    RTNAME(CharacterCompareScalar4),
};

// Types of _values_ over which comparison tests are parameterized
template <typename CHAR>
using ComparisonParametersTy =
    std::vector<std::tuple<const CHAR *, const CHAR *, int, int, int>>;

using ComparisonTestCasesTy = std::tuple<ComparisonParametersTy<char>,
    ComparisonParametersTy<char16_t>, ComparisonParametersTy<char32_t>>;

static ComparisonTestCasesTy comparisonTestCases{
    {
        std::make_tuple("abc", "abc", 3, 3, 0),
        std::make_tuple("abc", "def", 3, 3, -1),
        std::make_tuple("ab ", "abc", 3, 2, 0),
        std::make_tuple("abc", "abc", 2, 3, -1),
        std::make_tuple("ab\xff", "ab ", 3, 2, 1),
        std::make_tuple("ab ", "ab\xff", 2, 3, -1),
    },
    {
        std::make_tuple(u"abc", u"abc", 3, 3, 0),
        std::make_tuple(u"abc", u"def", 3, 3, -1),
        std::make_tuple(u"ab ", u"abc", 3, 2, 0),
        std::make_tuple(u"abc", u"abc", 2, 3, -1),
    },
    {
        std::make_tuple(U"abc", U"abc", 3, 3, 0),
        std::make_tuple(U"abc", U"def", 3, 3, -1),
        std::make_tuple(U"ab ", U"abc", 3, 2, 0),
        std::make_tuple(U"abc", U"abc", 2, 3, -1),
    }};

template <typename CHAR>
struct CharacterComparisonTests : public ::testing::Test {
  CharacterComparisonTests()
      : parameters{std::get<ComparisonParametersTy<CHAR>>(comparisonTestCases)},
        characterComparisonFunc{
            std::get<ComparisonFuncTy<CHAR>>(comparisonFuncs)} {}
  ComparisonParametersTy<CHAR> parameters;
  ComparisonFuncTy<CHAR> characterComparisonFunc;
};

TYPED_TEST_SUITE(CharacterComparisonTests, CharacterTypes, );

TYPED_TEST(CharacterComparisonTests, CompareCharacters) {
  for (auto &[x, y, xBytes, yBytes, expect] : this->parameters) {
    int cmp{this->characterComparisonFunc(x, y, xBytes, yBytes)};
    TypeParam buf[2][8];
    std::memset(buf, 0, sizeof buf);
    std::memcpy(buf[0], x, xBytes);
    std::memcpy(buf[1], y, yBytes);
    ASSERT_EQ(cmp, expect) << "compare '" << x << "'(" << xBytes << ") to '"
                           << y << "'(" << yBytes << "), got " << cmp
                           << ", should be " << expect << '\n';

    // Perform the same test with the parameters reversed and the difference
    // negated
    std::swap(x, y);
    std::swap(xBytes, yBytes);
    expect = -expect;

    cmp = this->characterComparisonFunc(x, y, xBytes, yBytes);
    std::memset(buf, 0, sizeof buf);
    std::memcpy(buf[0], x, xBytes);
    std::memcpy(buf[1], y, yBytes);
    ASSERT_EQ(cmp, expect) << "compare '" << x << "'(" << xBytes << ") to '"
                           << y << "'(" << yBytes << "'), got " << cmp
                           << ", should be " << expect << '\n';
  }
}

// Test MIN() and MAX()
struct ExtremumTestCase {
  std::vector<SubscriptValue> shape; // Empty = scalar, non-empty = array.
  std::vector<const char *> x, y, expect;
};

template <typename CHAR>
void RunExtremumTests(const char *which,
    std::function<void(Descriptor &, const Descriptor &, const char *, int)>
        function,
    const std::vector<ExtremumTestCase> &testCases) {
  std::stringstream traceMessage;
  traceMessage << which << " for CHARACTER(kind=" << sizeof(CHAR) << ")";
  SCOPED_TRACE(traceMessage.str());

  for (const auto &t : testCases) {
    OwningPtr<Descriptor> x = CreateDescriptor<CHAR>(t.shape, t.x);
    OwningPtr<Descriptor> y = CreateDescriptor<CHAR>(t.shape, t.y);

    ASSERT_NE(x, nullptr);
    ASSERT_TRUE(x->IsAllocated());
    ASSERT_NE(y, nullptr);
    ASSERT_TRUE(y->IsAllocated());
    function(*x, *y, __FILE__, __LINE__);

    std::size_t length = x->ElementBytes() / sizeof(CHAR);
    for (std::size_t i = 0; i < t.x.size(); ++i) {
      std::basic_string<CHAR> got{
          x->OffsetElement<CHAR>(i * x->ElementBytes()), length};
      std::basic_string<CHAR> expect{
          t.expect[i], t.expect[i] + std::strlen(t.expect[i])};
      EXPECT_EQ(expect, got) << "inputs: '" << t.x[i] << "','" << t.y[i] << "'";
    }
  }
}

template <typename CHAR> struct ExtremumTests : public ::testing::Test {};
TYPED_TEST_SUITE(ExtremumTests, CharacterTypes, );

TYPED_TEST(ExtremumTests, MinTests) {
  static std::vector<ExtremumTestCase> tests{{{}, {"a"}, {"z"}, {"a"}},
      {{1}, {"zaaa"}, {"aa"}, {"aa  "}},
      {{1, 1}, {"aaz"}, {"aaaaa"}, {"aaaaa"}},
      {{2, 3}, {"a", "b", "c", "d", "E", "f"},
          {"xa", "ya", "az", "dd", "Sz", "cc"},
          {"a ", "b ", "az", "d ", "E ", "cc"}}};
  RunExtremumTests<TypeParam>("MIN", RTNAME(CharacterMin), tests);
}

TYPED_TEST(ExtremumTests, MaxTests) {
  static std::vector<ExtremumTestCase> tests{
      {{}, {"a"}, {"z"}, {"z"}},
      {{1}, {"zaa"}, {"aaaaa"}, {"zaa  "}},
      {{1, 1, 1}, {"aaaaa"}, {"aazaa"}, {"aazaa"}},
  };
  RunExtremumTests<TypeParam>("MAX", RTNAME(CharacterMax), tests);
}

template <typename CHAR>
void RunAllocationTest(const char *xRaw, const char *yRaw) {
  OwningPtr<Descriptor> x = CreateDescriptor<CHAR>({}, {xRaw});
  OwningPtr<Descriptor> y = CreateDescriptor<CHAR>({}, {yRaw});

  ASSERT_NE(x, nullptr);
  ASSERT_TRUE(x->IsAllocated());
  ASSERT_NE(y, nullptr);
  ASSERT_TRUE(y->IsAllocated());

  void *old = x->raw().base_addr;
  RTNAME(CharacterMin)(*x, *y, __FILE__, __LINE__);
  EXPECT_EQ(old, x->raw().base_addr);
}

TYPED_TEST(ExtremumTests, NoReallocate) {
  // Test that we don't reallocate if the accumulator is already large enough.
  RunAllocationTest<TypeParam>("loooooong", "short");
}

// Test search functions INDEX(), SCAN(), and VERIFY()

template <typename CHAR>
using SearchFunction = std::function<std::size_t(
    const CHAR *, std::size_t, const CHAR *, std::size_t, bool)>;
template <template <typename> class FUNC>
using CharTypedFunctions =
    std::tuple<FUNC<char>, FUNC<char16_t>, FUNC<char32_t>>;
using SearchFunctions = CharTypedFunctions<SearchFunction>;
struct SearchTestCase {
  const char *x, *y;
  bool back;
  std::size_t expect;
};

template <typename CHAR>
void RunSearchTests(const char *which,
    const std::vector<SearchTestCase> &testCases,
    const SearchFunction<CHAR> &function) {
  for (const auto &t : testCases) {
    // Convert default character to desired kind
    std::size_t xLen{std::strlen(t.x)}, yLen{std::strlen(t.y)};
    std::basic_string<CHAR> x{t.x, t.x + xLen};
    std::basic_string<CHAR> y{t.y, t.y + yLen};
    auto got{function(x.data(), xLen, y.data(), yLen, t.back)};
    ASSERT_EQ(got, t.expect)
        << which << "('" << t.x << "','" << t.y << "',back=" << t.back
        << ") for CHARACTER(kind=" << sizeof(CHAR) << "): got " << got
        << ", expected " << t.expect;
  }
}

template <typename CHAR> struct SearchTests : public ::testing::Test {};
TYPED_TEST_SUITE(SearchTests, CharacterTypes, );

TYPED_TEST(SearchTests, IndexTests) {
  static SearchFunctions functions{
      RTNAME(Index1), RTNAME(Index2), RTNAME(Index4)};
  static std::vector<SearchTestCase> tests{
      {"", "", false, 1},
      {"", "", true, 1},
      {"a", "", false, 1},
      {"a", "", true, 2},
      {"", "a", false, 0},
      {"", "a", true, 0},
      {"aa", "a", false, 1},
      {"aa", "a", true, 2},
      {"Fortran that I ran", "that I ran", false, 9},
      {"Fortran that I ran", "that I ran", true, 9},
      {"Fortran that you ran", "that I ran", false, 0},
      {"Fortran that you ran", "that I ran", true, 0},
  };
  RunSearchTests(
      "INDEX", tests, std::get<SearchFunction<TypeParam>>(functions));
}

TYPED_TEST(SearchTests, ScanTests) {
  static SearchFunctions functions{RTNAME(Scan1), RTNAME(Scan2), RTNAME(Scan4)};
  static std::vector<SearchTestCase> tests{
      {"abc", "abc", false, 1},
      {"abc", "abc", true, 3},
      {"abc", "cde", false, 3},
      {"abc", "cde", true, 3},
      {"abc", "x", false, 0},
      {"", "x", false, 0},
  };
  RunSearchTests("SCAN", tests, std::get<SearchFunction<TypeParam>>(functions));
}

TYPED_TEST(SearchTests, VerifyTests) {
  static SearchFunctions functions{
      RTNAME(Verify1), RTNAME(Verify2), RTNAME(Verify4)};
  static std::vector<SearchTestCase> tests{
      {"abc", "abc", false, 0},
      {"abc", "abc", true, 0},
      {"abc", "cde", false, 1},
      {"abc", "cde", true, 2},
      {"abc", "x", false, 1},
      {"", "x", false, 0},
  };
  RunSearchTests(
      "VERIFY", tests, std::get<SearchFunction<TypeParam>>(functions));
}

// Test REPEAT()
template <typename CHAR> struct RepeatTests : public ::testing::Test {};
TYPED_TEST_SUITE(RepeatTests, CharacterTypes, );

struct RepeatTestCase {
  std::size_t ncopies;
  const char *input, *output;
};

template <typename CHAR>
void RunRepeatTest(
    std::size_t ncopies, const char *inputRaw, const char *outputRaw) {
  OwningPtr<Descriptor> input{CreateDescriptor<CHAR>({}, {inputRaw})};
  ASSERT_NE(input, nullptr);
  ASSERT_TRUE(input->IsAllocated());

  StaticDescriptor<1> outputStaticDescriptor;
  Descriptor &output{outputStaticDescriptor.descriptor()};

  RTNAME(Repeat)(output, *input, ncopies);
  std::basic_string<CHAR> got{
      output.OffsetElement<CHAR>(), output.ElementBytes() / sizeof(CHAR)};
  std::basic_string<CHAR> expect{outputRaw, outputRaw + std::strlen(outputRaw)};
  ASSERT_EQ(got, expect) << "'" << inputRaw << "' * " << ncopies
                         << "' for CHARACTER(kind=" << sizeof(CHAR) << ")";
}

TYPED_TEST(RepeatTests, Repeat) {
  static std::vector<RepeatTestCase> testcases{
      {1, "just one copy", "just one copy"},
      {5, "copy.", "copy.copy.copy.copy.copy."},
      {0, "no copies", ""},
  };

  for (const auto &t : testcases) {
    RunRepeatTest<TypeParam>(t.ncopies, t.input, t.output);
  }
}