[flang-commits] [flang] [llvm] [flang][flang-rt] Implement F202X leading-zero control edit descriptors LZ, LZS, and LZP for formatted output (F, E, D, and G editing) (PR #183500)

via flang-commits flang-commits at lists.llvm.org
Fri Mar 20 16:57:47 PDT 2026


================
@@ -0,0 +1,379 @@
+//===-- unittests/Runtime/LeadingZeroTest.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
+//
+//===----------------------------------------------------------------------===//
+//
+// Tests for F202X leading-zero control edit descriptors: LZ, LZP, LZS.
+// LZ  - processor-dependent (flang prints leading zero)
+// LZP - print the optional leading zero
+// LZS - suppress the optional leading zero
+//
+//===----------------------------------------------------------------------===//
+
+#include "CrashHandlerFixture.h"
+#include "flang-rt/runtime/descriptor.h"
+#include "flang/Runtime/io-api.h"
+#include <algorithm>
+#include <cstring>
+#include <gtest/gtest.h>
+#include <string>
+#include <tuple>
+#include <vector>
+
+using namespace Fortran::runtime;
+using namespace Fortran::runtime::io;
+
+static bool CompareFormattedStrings(
+    const std::string &expect, const std::string &got) {
+  std::string want{expect};
+  want.resize(got.size(), ' ');
+  return want == got;
+}
+
+// Perform format on a double and return the trimmed result
+static std::string FormatReal(const char *format, double x) {
+  char buffer[800];
+  auto cookie{IONAME(BeginInternalFormattedOutput)(
+      buffer, sizeof buffer, format, std::strlen(format))};
+  EXPECT_TRUE(IONAME(OutputReal64)(cookie, x));
+  auto status{IONAME(EndIoStatement)(cookie)};
+  EXPECT_EQ(status, 0);
+  std::string got{buffer, sizeof buffer};
+  auto lastNonBlank{got.find_last_not_of(" ")};
+  if (lastNonBlank != std::string::npos) {
+    got.resize(lastNonBlank + 1);
+  }
+  return got;
+}
+
+static bool CompareFormatReal(
+    const char *format, double x, const char *expect, std::string &got) {
+  got = FormatReal(format, x);
+  return CompareFormattedStrings(expect, got);
+}
+
+struct LeadingZeroTests : CrashHandlerFixture {};
+
+// LZP with F editing: value < 1 should print "0." before decimal digits
+TEST(LeadingZeroTests, LZP_F_editing) {
----------------
laoshd wrote:

Thank you for pointing this out. struct LeadingZeroTests simply inherits from CrashHandlerFixture. So TEST_F is the correct function to use. It will get the handler SetUp() to be called. TEST() will not. TEST() has been changed to TEST_F().

https://github.com/llvm/llvm-project/pull/183500


More information about the flang-commits mailing list