[libc-commits] [libc] [libc] Implement strfromd() and strfroml() (PR #86113)

Vinayak Dev via libc-commits libc-commits at lists.llvm.org
Fri Mar 22 10:40:25 PDT 2024


================
@@ -0,0 +1,434 @@
+//===-- A template class for testing strfrom functions ----------*- 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 "src/__support/CPP/type_traits.h"
+#include "test/UnitTest/Test.h"
+
+#define ASSERT_STREQ_LEN(actual_written, actual_str, expected_str)             \
+  EXPECT_EQ(actual_written, static_cast<int>(sizeof(expected_str) - 1));       \
+  EXPECT_STREQ(actual_str, expected_str);
+
+template <typename InputT>
+class StrfromTest : public LIBC_NAMESPACE::testing::Test {
+
+  static const bool is_single_prec =
+      LIBC_NAMESPACE::cpp::is_same<InputT, float>::value;
+  static const bool is_double_prec =
+      LIBC_NAMESPACE::cpp::is_same<InputT, double>::value;
+
+  using FunctionT = int (*)(char *, size_t, const char *, InputT fp);
+
+public:
+  void floatDecimalFormat(FunctionT func) {
+    if (is_single_prec)
+      floatDecimalSinglePrec(func);
+    else if (is_double_prec)
+      floatDecimalDoublePrec(func);
+    else
+      floatDecimalLongDoublePrec(func);
+  }
+
+  void floatHexExpFormat(FunctionT func) {
+    if (is_single_prec)
+      floatHexExpSinglePrec(func);
+    else if (is_double_prec)
+      floatHexExpDoublePrec(func);
+    else
+      floatHexExpLongDoublePrec(func);
+  }
+
+  void floatDecimalExpFormat(FunctionT func) {
+    if (is_single_prec)
+      floatDecimalExpSinglePrec(func);
+    else if (is_double_prec)
+      floatDecimalExpDoublePrec(func);
+    else
+      floatDecimalExpLongDoublePrec(func);
+  }
+
+  void floatDecimalAutoFormat(FunctionT func) {
+    if (is_single_prec)
+      floatDecimalAutoSinglePrec(func);
+    else if (is_double_prec)
+      floatDecimalAutoDoublePrec(func);
+    else
+      floatDecimalAutoLongDoublePrec(func);
+  }
+
+  void improperFormatString(FunctionT func) {
+    char buff[100];
+    int written;
+    const bool is_long_double = !is_single_prec && !is_double_prec;
+
+    written = func(buff, 37, "A simple string with no conversions.", 1.0);
+    ASSERT_STREQ_LEN(written, buff, "A simple string with no conversions.");
+
+    written =
+        func(buff, 37,
+             "%A simple string with one conversion, should overwrite.", 1.0);
+    ASSERT_STREQ_LEN(written, buff, is_long_double ? "0X8P-3" : "0X1P+0");
+
+    written = func(buff, 74,
+                   "A simple string with one conversion in %A "
+                   "between, writes string as it is",
+                   1.0);
+    ASSERT_STREQ_LEN(written, buff,
+                     "A simple string with one conversion in %A between, "
+                     "writes string as it is");
+
+    written = func(buff, 36, "A simple string with one conversion", 1.0);
+    ASSERT_STREQ_LEN(written, buff, "A simple string with one conversion");
+
+    written = func(buff, 20, "%1f", 1234567890.0);
+    ASSERT_STREQ_LEN(written, buff, "%1f");
+  }
+
+  void insufficentBufsize(FunctionT func) {
+    char buff[20];
+    int written;
+
+    written = func(buff, 5, "%f", 1234567890.0);
+    EXPECT_EQ(written, 17);
+    ASSERT_STREQ(buff, "1234");
+
+    written = func(buff, 5, "%.5f", 1.05);
+    EXPECT_EQ(written, 7);
+    ASSERT_STREQ(buff, "1.05");
+
+    written = func(buff, 0, "%g", 1.0);
+    EXPECT_EQ(written, 1);
----------------
vinayakdsci wrote:

OK, sorry, I understand now, I will add a check for this too. Thanks!

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


More information about the libc-commits mailing list