[flang-commits] [flang] 263a89c - [flang] Change capitalization for Adjustl/r

Diana Picus via flang-commits flang-commits at lists.llvm.org
Fri Jun 4 01:35:29 PDT 2021


Author: Diana Picus
Date: 2021-06-04T08:34:53Z
New Revision: 263a89c9b788b4c6bc174dc169dca2a3b729ea02

URL: https://github.com/llvm/llvm-project/commit/263a89c9b788b4c6bc174dc169dca2a3b729ea02
DIFF: https://github.com/llvm/llvm-project/commit/263a89c9b788b4c6bc174dc169dca2a3b729ea02.diff

LOG: [flang] Change capitalization for Adjustl/r

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

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/flang/runtime/character.cpp b/flang/runtime/character.cpp
index e1e529d988b60..826cb478770be 100644
--- a/flang/runtime/character.cpp
+++ b/flang/runtime/character.cpp
@@ -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);
 }

diff  --git a/flang/unittests/RuntimeGTest/CharacterTest.cpp b/flang/unittests/RuntimeGTest/CharacterTest.cpp
index 8b1f9a19250c4..89109b1989cfc 100644
--- a/flang/unittests/RuntimeGTest/CharacterTest.cpp
+++ b/flang/unittests/RuntimeGTest/CharacterTest.cpp
@@ -19,6 +19,35 @@
 
 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)>


        


More information about the flang-commits mailing list