[flang] Move buffer runtime test to GTest
authorAsher Mancinelli <ashermancinelli@gmail.com>
Mon, 14 Jun 2021 16:16:46 +0000 (09:16 -0700)
committerAsher Mancinelli <ashermancinelli@gmail.com>
Mon, 14 Jun 2021 17:13:32 +0000 (10:13 -0700)
Move buffer unit test from Runtime directory to RuntimeGtest
directory and use GTest. Test coverage is only maintained.

Differential Revision: https://reviews.llvm.org/D102335
Reviewed By: awarzynski, klausler

flang/unittests/Runtime/CMakeLists.txt
flang/unittests/RuntimeGTest/BufferTest.cpp [moved from flang/unittests/Runtime/buffer.cpp with 73% similarity]
flang/unittests/RuntimeGTest/CMakeLists.txt

index 2459aed..f4dba0d 100644 (file)
@@ -31,8 +31,3 @@ add_flang_nongtest_unittest(external-io
   RuntimeTesting
   FortranRuntime
 )
-
-add_flang_nongtest_unittest(buffer
-  RuntimeTesting
-  FortranRuntime
-)
similarity index 73%
rename from flang/unittests/Runtime/buffer.cpp
rename to flang/unittests/RuntimeGTest/BufferTest.cpp
index f5eca03..e409688 100644 (file)
@@ -1,16 +1,25 @@
+//===-- flang/unittests/RuntimeGTest/BufferTest.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
+//
+//===----------------------------------------------------------------------===//
+
 #include "../../runtime/buffer.h"
-#include "testing.h"
+#include "CrashHandlerFixture.h"
+#include "gtest/gtest.h"
 #include <algorithm>
 #include <cstdint>
 #include <cstring>
 #include <memory>
 
-static constexpr std::size_t tinyBuffer{32};
+static constexpr std::size_t tinyBufferSize{32};
 using FileOffset = std::int64_t;
 using namespace Fortran::runtime;
 using namespace Fortran::runtime::io;
 
-class Store : public FileFrame<Store, tinyBuffer> {
+class Store : public FileFrame<Store, tinyBufferSize> {
 public:
   explicit Store(std::size_t bytes = 65536) : bytes_{bytes} {
     data_.reset(new char[bytes]);
@@ -60,16 +69,17 @@ private:
 
 inline int ChunkSize(int j, int most) {
   // 31, 1, 29, 3, 27, ...
-  j %= tinyBuffer;
-  auto chunk{
-      static_cast<int>(((j % 2) ? j : (tinyBuffer - 1 - j)) % tinyBuffer)};
+  j %= tinyBufferSize;
+  auto chunk{static_cast<int>(
+      ((j % 2) ? j : (tinyBufferSize - 1 - j)) % tinyBufferSize)};
   return std::min(chunk, most);
 }
 
 inline int ValueFor(int at) { return (at ^ (at >> 8)) & 0xff; }
 
-int main() {
-  StartTests();
+struct BufferTests : CrashHandlerFixture {};
+
+TEST(BufferTests, TestFrameBufferReadAndWrite) {
   Terminator terminator{__FILE__, __LINE__};
   IoErrorHandler handler{terminator};
   Store store;
@@ -94,22 +104,19 @@ int main() {
   while (at < bytes) {
     auto chunk{ChunkSize(j, static_cast<int>(bytes - at))};
     std::size_t frame{store.ReadFrame(at, chunk, handler)};
-    if (frame < static_cast<std::size_t>(chunk)) {
-      Fail() << "Badly-sized ReadFrame at " << at << ", chunk=" << chunk
-             << ", got " << frame << '\n';
-      break;
-    }
+    ASSERT_GE(frame, static_cast<std::size_t>(chunk))
+        << "Badly-sized ReadFrame at " << at << ", chunk=" << chunk << ", got "
+        << frame << '\n';
+
     const char *from{store.Frame()};
     for (int k{0}; k < chunk; ++k) {
       auto expect{static_cast<char>(ValueFor(at + k))};
-      if (from[k] != expect) {
-        Fail() << "At " << at << '+' << k << '(' << (at + k) << "), read "
-               << (from[k] & 0xff) << ", expected " << static_cast<int>(expect)
-               << '\n';
-      }
+      ASSERT_EQ(from[k], expect)
+          << "At " << at << '+' << k << '(' << (at + k) << "), read "
+          << (from[k] & 0xff) << ", expected " << static_cast<int>(expect)
+          << '\n';
     }
     at += chunk;
     ++j;
   }
-  return EndTests();
 }
index a1e5348..fb842c7 100644 (file)
@@ -1,4 +1,5 @@
 add_flang_unittest(FlangRuntimeTests
+  BufferTest.cpp
   CharacterTest.cpp
   CrashHandlerFixture.cpp
   Format.cpp