[flang] Change capitalization for Adjustl/r
authorDiana Picus <diana.picus@linaro.org>
Mon, 31 May 2021 08:51:11 +0000 (08:51 +0000)
committerDiana Picus <diana.picus@linaro.org>
Fri, 4 Jun 2021 08:34:53 +0000 (08:34 +0000)
Rename the definitions of the character runtime functions Adjustl and
Adjustr (used to be AdjustL and AdjustR respectively).

Also add unit tests (and move some of the helpers to the top of the
file, since they're now used in more than one place).

Differential Revision: https://reviews.llvm.org/D103392

flang/runtime/character.cpp
flang/unittests/RuntimeGTest/CharacterTest.cpp

index e1e529d..826cb47 100644 (file)
@@ -824,12 +824,12 @@ void RTNAME(CharacterPad1)(char *lhs, std::size_t bytes, std::size_t offset) {
 
 // Intrinsic function entry points
 
-void RTNAME(AdjustL)(Descriptor &result, const Descriptor &string,
+void RTNAME(Adjustl)(Descriptor &result, const Descriptor &string,
     const char *sourceFile, int sourceLine) {
   AdjustLR<false>(result, string, sourceFile, sourceLine);
 }
 
-void RTNAME(AdjustR)(Descriptor &result, const Descriptor &string,
+void RTNAME(Adjustr)(Descriptor &result, const Descriptor &string,
     const char *sourceFile, int sourceLine) {
   AdjustLR<true>(result, string, sourceFile, sourceLine);
 }
index 8b1f9a1..89109b1 100644 (file)
 
 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)};
+  if ((shape.empty() ? descriptor->Allocate()
+                     : descriptor->Allocate(
+                           std::vector<SubscriptValue>(shape.size(), 1).data(),
+                           shape.data())) != 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];
@@ -53,6 +82,60 @@ TEST(CharacterTests, CharacterAppend1Overrun) {
                            << 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
 //------------------------------------------------------------------------------
@@ -110,7 +193,6 @@ struct CharacterComparisonTests : public ::testing::Test {
   ComparisonFuncTy<CHAR> characterComparisonFunc;
 };
 
-using CharacterTypes = ::testing::Types<char, char16_t, char32_t>;
 TYPED_TEST_SUITE(CharacterComparisonTests, CharacterTypes, );
 
 TYPED_TEST(CharacterComparisonTests, CompareCharacters) {
@@ -146,33 +228,6 @@ struct ExtremumTestCase {
   std::vector<const char *> x, y, expect;
 };
 
-// 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)};
-  if ((shape.empty() ? descriptor->Allocate()
-                     : descriptor->Allocate(
-                           std::vector<SubscriptValue>(shape.size(), 1).data(),
-                           shape.data())) != 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;
-}
-
 template <typename CHAR>
 void RunExtremumTests(const char *which,
     std::function<void(Descriptor &, const Descriptor &, const char *, int)>