[libc-commits] [libc] [libc][test] make `str_to_float_comparison_test` independent of C++ headers. (PR #133978)

Muhammad Bassiouni via libc-commits libc-commits at lists.llvm.org
Wed Apr 9 01:44:13 PDT 2025


https://github.com/bassiounix updated https://github.com/llvm/llvm-project/pull/133978

>From b6456185a69e136f4ff13fef8605db294c5b9f44 Mon Sep 17 00:00:00 2001
From: bassiounix <muhammad.m.bassiouni at gmail.com>
Date: Tue, 1 Apr 2025 22:12:56 +0200
Subject: [PATCH 01/22] [libc][test] make test independent of C++ headers

---
 .../str_to_float_comparison_test.cpp          | 65 +++++++++----------
 1 file changed, 32 insertions(+), 33 deletions(-)

diff --git a/libc/test/src/__support/str_to_float_comparison_test.cpp b/libc/test/src/__support/str_to_float_comparison_test.cpp
index 61bfc3cd0903a..81193f4723624 100644
--- a/libc/test/src/__support/str_to_float_comparison_test.cpp
+++ b/libc/test/src/__support/str_to_float_comparison_test.cpp
@@ -8,14 +8,11 @@
 
 // #include "src/__support/str_float_conv_utils.h"
 
-#include <stdlib.h> // For string to float functions
-
 // #include "src/__support/FPUtil/FPBits.h"
 
-#include <cstdint>
-#include <fstream>
-#include <iostream>
-#include <string>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
 
 // The intent of this test is to read in files in the format used in this test
 // dataset: https://github.com/nigeltao/parse-number-fxx-test-data
@@ -59,16 +56,17 @@ int checkFile(char *inputFileName, int *totalFails, int *totalBitDiffs,
   int32_t curFails = 0;    // Only counts actual failures, not bitdiffs.
   int32_t curBitDiffs = 0; // A bitdiff is when the expected result and actual
                            // result are off by +/- 1 bit.
-  std::string line;
-  std::string num;
+  char line[100];
+  char num[100];
 
-  std::ifstream fileStream(inputFileName, std::ifstream::in);
+  auto *fileHandle = fopen(inputFileName, "r");
 
-  if (!fileStream.is_open()) {
-    std::cout << "file '" << inputFileName << "' failed to open. Exiting.\n";
+  if (!fileHandle) {
+    printf("file '%s' failed to open. Exiting.\n", inputFileName);
     return 1;
   }
-  while (getline(fileStream, line)) {
+
+  while (fgets(line, sizeof(line), fileHandle)) {
     if (line[0] == '#') {
       continue;
     }
@@ -76,13 +74,13 @@ int checkFile(char *inputFileName, int *totalFails, int *totalBitDiffs,
     uint32_t expectedFloatRaw;
     uint64_t expectedDoubleRaw;
 
-    expectedFloatRaw = fastHexToU32(line.c_str() + 5);
-    expectedDoubleRaw = fastHexToU64(line.c_str() + 14);
-    num = line.substr(31);
+    expectedFloatRaw = fastHexToU32(line + 5);
+    expectedDoubleRaw = fastHexToU64(line + 14);
+    sscanf(line + 31, "%s", num);
 
-    float floatResult = strtof(num.c_str(), nullptr);
+    float floatResult = strtof(num, nullptr);
 
-    double doubleResult = strtod(num.c_str(), nullptr);
+    double doubleResult = strtod(num, nullptr);
 
     uint32_t floatRaw = *(uint32_t *)(&floatResult);
 
@@ -101,9 +99,8 @@ int checkFile(char *inputFileName, int *totalFails, int *totalBitDiffs,
         curFails++;
       }
       if (curFails + curBitDiffs < 10) {
-        std::cout << "Float fail for '" << num << "'. Expected " << std::hex
-                  << expectedFloatRaw << " but got " << floatRaw << "\n"
-                  << std::dec;
+        printf("Float fail for '%s'. Expected %x but got %x\n", num,
+               expectedFloatRaw, floatRaw);
       }
     }
 
@@ -120,14 +117,13 @@ int checkFile(char *inputFileName, int *totalFails, int *totalBitDiffs,
         curFails++;
       }
       if (curFails + curBitDiffs < 10) {
-        std::cout << "Double fail for '" << num << "'. Expected " << std::hex
-                  << expectedDoubleRaw << " but got " << doubleRaw << "\n"
-                  << std::dec;
+        printf("Double fail for '%s'. Expected %lx but got %lx\n", num,
+               expectedDoubleRaw, doubleRaw);
       }
     }
   }
 
-  fileStream.close();
+  fclose(fileHandle);
 
   *totalBitDiffs += curBitDiffs;
   *totalFails += curFails;
@@ -151,7 +147,7 @@ int main(int argc, char *argv[]) {
 
   int total = 0;
   for (int i = 1; i < argc; i++) {
-    std::cout << "Starting file " << argv[i] << "\n";
+    printf("Starting file %s\n", argv[i]);
     int curResult =
         checkFile(argv[i], &fails, &bitdiffs, detailedBitDiffs, &total);
     if (curResult == 1) {
@@ -161,13 +157,16 @@ int main(int argc, char *argv[]) {
       result = 2;
     }
   }
-  std::cout << "Results:\n"
-            << "Total significant failed conversions: " << fails << "\n"
-            << "Total conversions off by +/- 1 bit: " << bitdiffs << "\n"
-            << "\t" << detailedBitDiffs[0] << "\tfloat low\n"
-            << "\t" << detailedBitDiffs[1] << "\tfloat high\n"
-            << "\t" << detailedBitDiffs[2] << "\tdouble low\n"
-            << "\t" << detailedBitDiffs[3] << "\tdouble high\n"
-            << "Total lines: " << total << "\n";
+  printf("Results:\n"
+         "Total significant failed conversions: %d\n"
+         "Total conversions off by +/- 1 bit: %d\n"
+         "\t%d\tfloat low\n"
+         "\t%d\tfloat high\n"
+         "\t%d\tdouble low\n"
+         "\t%d\tdouble high\n"
+         "Total lines: %d\n",
+         fails, bitdiffs, detailedBitDiffs[0], detailedBitDiffs[1],
+         detailedBitDiffs[2], detailedBitDiffs[3], total);
+
   return result;
 }

>From 70ecf0174e2ded8184c63c856a62d73d35fc596f Mon Sep 17 00:00:00 2001
From: bassiounix <muhammad.m.bassiouni at gmail.com>
Date: Wed, 2 Apr 2025 00:18:09 +0200
Subject: [PATCH 02/22] remove executable and convert to unit test

---
 libc/test/src/__support/CMakeLists.txt        | 37 ++++-----
 .../str_to_float_comparison_test.cpp          | 81 +++++++++----------
 2 files changed, 54 insertions(+), 64 deletions(-)

diff --git a/libc/test/src/__support/CMakeLists.txt b/libc/test/src/__support/CMakeLists.txt
index d056969034d69..6513e534607b9 100644
--- a/libc/test/src/__support/CMakeLists.txt
+++ b/libc/test/src/__support/CMakeLists.txt
@@ -249,31 +249,22 @@ add_libc_test(
     libc.src.__support.memory_size
 )
 
-# FIXME: We shouldn't have regular executables created because we could be
-#        cross-compiling the tests and running through an emulator.
 if(NOT LIBC_TARGET_OS_IS_GPU)
-  add_executable(
-    libc_str_to_float_comparison_test
-    str_to_float_comparison_test.cpp
-  )
-
-  target_link_libraries(libc_str_to_float_comparison_test
-    PRIVATE
-      "${LIBC_TARGET}"
-  )
-
-  add_executable(
-    libc_system_str_to_float_comparison_test
-    str_to_float_comparison_test.cpp
+  add_libc_test(
+    str_to_float_comparison_test
+    SUITE
+      libc-support-tests
+    SRCS
+      str_to_float_comparison_test.cpp
+    DEPENDS
+      libc.src.stdio.printf
+      libc.src.stdio.fopen
+      libc.src.stdio.fclose
+      libc.src.stdio.fgets
+      libc.src.stdlib.strtof
+      libc.src.stdlib.strtod
+    NO_RUN_POSTBUILD
   )
-
-  set(float_test_file ${CMAKE_CURRENT_SOURCE_DIR}/str_to_float_comparison_data.txt)
-
-  add_custom_command(TARGET libc_str_to_float_comparison_test
-                     POST_BUILD
-                     COMMAND ${CMAKE_CROSSCOMPILING_EMULATOR} $<TARGET_FILE:libc_str_to_float_comparison_test> ${float_test_file}
-                     COMMENT "Test the strtof and strtod implementations against precomputed results."
-                     VERBATIM)
 endif()
 
 add_subdirectory(CPP)
diff --git a/libc/test/src/__support/str_to_float_comparison_test.cpp b/libc/test/src/__support/str_to_float_comparison_test.cpp
index 81193f4723624..5f3100d3b5946 100644
--- a/libc/test/src/__support/str_to_float_comparison_test.cpp
+++ b/libc/test/src/__support/str_to_float_comparison_test.cpp
@@ -6,13 +6,14 @@
 //
 //===----------------------------------------------------------------------===//
 
-// #include "src/__support/str_float_conv_utils.h"
-
-// #include "src/__support/FPUtil/FPBits.h"
-
+#include "src/stdio/fclose.h"
+#include "src/stdio/fgets.h"
+#include "src/stdio/fopen.h"
+#include "src/stdio/printf.h"
+#include "src/stdlib/strtod.h"
+#include "src/stdlib/strtof.h"
+#include "test/UnitTest/Test.h"
 #include <stdint.h>
-#include <stdio.h>
-#include <stdlib.h>
 
 // The intent of this test is to read in files in the format used in this test
 // dataset: https://github.com/nigeltao/parse-number-fxx-test-data
@@ -56,17 +57,18 @@ int checkFile(char *inputFileName, int *totalFails, int *totalBitDiffs,
   int32_t curFails = 0;    // Only counts actual failures, not bitdiffs.
   int32_t curBitDiffs = 0; // A bitdiff is when the expected result and actual
                            // result are off by +/- 1 bit.
-  char line[100];
-  char num[100];
+  char *line = nullptr;
+  char *num = nullptr;
 
-  auto *fileHandle = fopen(inputFileName, "r");
+  auto *fileHandle = LIBC_NAMESPACE::fopen(inputFileName, "r");
 
   if (!fileHandle) {
-    printf("file '%s' failed to open. Exiting.\n", inputFileName);
+    LIBC_NAMESPACE::printf("file '%s' failed to open. Exiting.\n",
+                           inputFileName);
     return 1;
   }
 
-  while (fgets(line, sizeof(line), fileHandle)) {
+  while (LIBC_NAMESPACE::fgets(line, 100, fileHandle)) {
     if (line[0] == '#') {
       continue;
     }
@@ -76,11 +78,11 @@ int checkFile(char *inputFileName, int *totalFails, int *totalBitDiffs,
 
     expectedFloatRaw = fastHexToU32(line + 5);
     expectedDoubleRaw = fastHexToU64(line + 14);
-    sscanf(line + 31, "%s", num);
+    num = line + 31;
 
-    float floatResult = strtof(num, nullptr);
+    float floatResult = LIBC_NAMESPACE::strtof(num, nullptr);
 
-    double doubleResult = strtod(num, nullptr);
+    double doubleResult = LIBC_NAMESPACE::strtod(num, nullptr);
 
     uint32_t floatRaw = *(uint32_t *)(&floatResult);
 
@@ -99,8 +101,8 @@ int checkFile(char *inputFileName, int *totalFails, int *totalBitDiffs,
         curFails++;
       }
       if (curFails + curBitDiffs < 10) {
-        printf("Float fail for '%s'. Expected %x but got %x\n", num,
-               expectedFloatRaw, floatRaw);
+        LIBC_NAMESPACE::printf("Float fail for '%s'. Expected %x but got %x\n",
+                               num, expectedFloatRaw, floatRaw);
       }
     }
 
@@ -117,13 +119,14 @@ int checkFile(char *inputFileName, int *totalFails, int *totalBitDiffs,
         curFails++;
       }
       if (curFails + curBitDiffs < 10) {
-        printf("Double fail for '%s'. Expected %lx but got %lx\n", num,
-               expectedDoubleRaw, doubleRaw);
+        LIBC_NAMESPACE::printf(
+            "Double fail for '%s'. Expected %lx but got %lx\n", num,
+            expectedDoubleRaw, doubleRaw);
       }
     }
   }
 
-  fclose(fileHandle);
+  LIBC_NAMESPACE::fclose(fileHandle);
 
   *totalBitDiffs += curBitDiffs;
   *totalFails += curFails;
@@ -134,7 +137,7 @@ int checkFile(char *inputFileName, int *totalFails, int *totalBitDiffs,
   return 0;
 }
 
-int main(int argc, char *argv[]) {
+TEST(LlvmLibcStrToFloatComparisonTest, CheckFile) {
   int result = 0;
   int fails = 0;
 
@@ -146,27 +149,23 @@ int main(int argc, char *argv[]) {
   int detailedBitDiffs[4] = {0, 0, 0, 0};
 
   int total = 0;
-  for (int i = 1; i < argc; i++) {
-    printf("Starting file %s\n", argv[i]);
-    int curResult =
-        checkFile(argv[i], &fails, &bitdiffs, detailedBitDiffs, &total);
-    if (curResult == 1) {
-      result = 1;
-      break;
-    } else if (curResult == 2) {
-      result = 2;
-    }
+
+  char filename[] = "str_to_float_comparison_data.txt";
+  LIBC_NAMESPACE::printf("Starting file %s\n", filename);
+  int curResult =
+      checkFile(filename, &fails, &bitdiffs, detailedBitDiffs, &total);
+  if (curResult == 1) {
+    result = 1;
+  } else if (curResult == 2) {
+    result = 2;
   }
-  printf("Results:\n"
-         "Total significant failed conversions: %d\n"
-         "Total conversions off by +/- 1 bit: %d\n"
-         "\t%d\tfloat low\n"
-         "\t%d\tfloat high\n"
-         "\t%d\tdouble low\n"
-         "\t%d\tdouble high\n"
-         "Total lines: %d\n",
-         fails, bitdiffs, detailedBitDiffs[0], detailedBitDiffs[1],
-         detailedBitDiffs[2], detailedBitDiffs[3], total);
 
-  return result;
+  EXPECT_EQ(result, 0);
+  EXPECT_EQ(fails, 0);
+  EXPECT_EQ(bitdiffs, 0);
+  EXPECT_EQ(detailedBitDiffs[0], 0);
+  EXPECT_EQ(detailedBitDiffs[1], 0);
+  EXPECT_EQ(detailedBitDiffs[2], 0);
+  EXPECT_EQ(detailedBitDiffs[3], 0);
+  EXPECT_EQ(total, 0);
 }

>From 6eb6fae4bbfba01cac34eac7398415ac630612e2 Mon Sep 17 00:00:00 2001
From: bassiounix <muhammad.m.bassiouni at gmail.com>
Date: Wed, 2 Apr 2025 05:04:04 +0200
Subject: [PATCH 03/22] make test support more than one test file

---
 libc/test/src/__support/CMakeLists.txt        |  6 ++-
 .../str_to_float_comparison_test.cpp          | 41 +++++++++++--------
 2 files changed, 28 insertions(+), 19 deletions(-)

diff --git a/libc/test/src/__support/CMakeLists.txt b/libc/test/src/__support/CMakeLists.txt
index 6513e534607b9..fff371bd9da50 100644
--- a/libc/test/src/__support/CMakeLists.txt
+++ b/libc/test/src/__support/CMakeLists.txt
@@ -254,6 +254,8 @@ if(NOT LIBC_TARGET_OS_IS_GPU)
     str_to_float_comparison_test
     SUITE
       libc-support-tests
+    NO_RUN_POSTBUILD
+    ENV "FILES=${CMAKE_CURRENT_SOURCE_DIR}/str_to_float_comparison_data.txt"
     SRCS
       str_to_float_comparison_test.cpp
     DEPENDS
@@ -263,7 +265,9 @@ if(NOT LIBC_TARGET_OS_IS_GPU)
       libc.src.stdio.fgets
       libc.src.stdlib.strtof
       libc.src.stdlib.strtod
-    NO_RUN_POSTBUILD
+      libc.src.stdlib.getenv
+      libc.src.string.strtok
+      libc.src.__support.CPP.bit
   )
 endif()
 
diff --git a/libc/test/src/__support/str_to_float_comparison_test.cpp b/libc/test/src/__support/str_to_float_comparison_test.cpp
index 5f3100d3b5946..f7d47db4c518c 100644
--- a/libc/test/src/__support/str_to_float_comparison_test.cpp
+++ b/libc/test/src/__support/str_to_float_comparison_test.cpp
@@ -6,12 +6,15 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "src/__support/CPP/bit.h"
 #include "src/stdio/fclose.h"
 #include "src/stdio/fgets.h"
 #include "src/stdio/fopen.h"
 #include "src/stdio/printf.h"
+#include "src/stdlib/getenv.h"
 #include "src/stdlib/strtod.h"
 #include "src/stdlib/strtof.h"
+#include "src/string/strtok.h"
 #include "test/UnitTest/Test.h"
 #include <stdint.h>
 
@@ -57,8 +60,8 @@ int checkFile(char *inputFileName, int *totalFails, int *totalBitDiffs,
   int32_t curFails = 0;    // Only counts actual failures, not bitdiffs.
   int32_t curBitDiffs = 0; // A bitdiff is when the expected result and actual
                            // result are off by +/- 1 bit.
-  char *line = nullptr;
-  char *num = nullptr;
+  char line[100];
+  char *num;
 
   auto *fileHandle = LIBC_NAMESPACE::fopen(inputFileName, "r");
 
@@ -68,7 +71,7 @@ int checkFile(char *inputFileName, int *totalFails, int *totalBitDiffs,
     return 1;
   }
 
-  while (LIBC_NAMESPACE::fgets(line, 100, fileHandle)) {
+  while (LIBC_NAMESPACE::fgets(line, sizeof(line), fileHandle)) {
     if (line[0] == '#') {
       continue;
     }
@@ -84,9 +87,9 @@ int checkFile(char *inputFileName, int *totalFails, int *totalBitDiffs,
 
     double doubleResult = LIBC_NAMESPACE::strtod(num, nullptr);
 
-    uint32_t floatRaw = *(uint32_t *)(&floatResult);
+    uint32_t floatRaw = LIBC_NAMESPACE::cpp::bit_cast<uint32_t>(floatResult);
 
-    uint64_t doubleRaw = *(uint64_t *)(&doubleResult);
+    uint64_t doubleRaw = LIBC_NAMESPACE::cpp::bit_cast<uint64_t>(doubleResult);
 
     if (!(expectedFloatRaw == floatRaw)) {
       if (expectedFloatRaw == floatRaw + 1 ||
@@ -150,22 +153,24 @@ TEST(LlvmLibcStrToFloatComparisonTest, CheckFile) {
 
   int total = 0;
 
-  char filename[] = "str_to_float_comparison_data.txt";
-  LIBC_NAMESPACE::printf("Starting file %s\n", filename);
-  int curResult =
-      checkFile(filename, &fails, &bitdiffs, detailedBitDiffs, &total);
-  if (curResult == 1) {
-    result = 1;
-  } else if (curResult == 2) {
-    result = 2;
+  char *files = LIBC_NAMESPACE::getenv("FILES");
+  for (char *file = LIBC_NAMESPACE::strtok(files, ","); file != nullptr;
+       file = LIBC_NAMESPACE::strtok(nullptr, ",")) {
+    int curResult =
+        checkFile(file, &fails, &bitdiffs, detailedBitDiffs, &total);
+    if (curResult == 1) {
+      result = 1;
+    } else if (curResult == 2) {
+      result = 2;
+    }
   }
 
   EXPECT_EQ(result, 0);
   EXPECT_EQ(fails, 0);
   EXPECT_EQ(bitdiffs, 0);
-  EXPECT_EQ(detailedBitDiffs[0], 0);
-  EXPECT_EQ(detailedBitDiffs[1], 0);
-  EXPECT_EQ(detailedBitDiffs[2], 0);
-  EXPECT_EQ(detailedBitDiffs[3], 0);
-  EXPECT_EQ(total, 0);
+  EXPECT_EQ(detailedBitDiffs[0], 0); // float low
+  EXPECT_EQ(detailedBitDiffs[1], 0); // float high
+  EXPECT_EQ(detailedBitDiffs[2], 0); // double low
+  EXPECT_EQ(detailedBitDiffs[3], 0); // double high
+  EXPECT_EQ(total, 6);
 }

>From e0e77cdc94dc43083b1dc559ca3080ec29fee21c Mon Sep 17 00:00:00 2001
From: bassiounix <muhammad.m.bassiouni at gmail.com>
Date: Wed, 2 Apr 2025 07:10:53 +0200
Subject: [PATCH 04/22] remove unnecessary condition

---
 libc/test/src/__support/CMakeLists.txt | 40 ++++++++++++--------------
 1 file changed, 19 insertions(+), 21 deletions(-)

diff --git a/libc/test/src/__support/CMakeLists.txt b/libc/test/src/__support/CMakeLists.txt
index fff371bd9da50..685ac9bd40668 100644
--- a/libc/test/src/__support/CMakeLists.txt
+++ b/libc/test/src/__support/CMakeLists.txt
@@ -249,27 +249,25 @@ add_libc_test(
     libc.src.__support.memory_size
 )
 
-if(NOT LIBC_TARGET_OS_IS_GPU)
-  add_libc_test(
-    str_to_float_comparison_test
-    SUITE
-      libc-support-tests
-    NO_RUN_POSTBUILD
-    ENV "FILES=${CMAKE_CURRENT_SOURCE_DIR}/str_to_float_comparison_data.txt"
-    SRCS
-      str_to_float_comparison_test.cpp
-    DEPENDS
-      libc.src.stdio.printf
-      libc.src.stdio.fopen
-      libc.src.stdio.fclose
-      libc.src.stdio.fgets
-      libc.src.stdlib.strtof
-      libc.src.stdlib.strtod
-      libc.src.stdlib.getenv
-      libc.src.string.strtok
-      libc.src.__support.CPP.bit
-  )
-endif()
+add_libc_test(
+  str_to_float_comparison_test
+  SUITE
+    libc-support-tests
+  NO_RUN_POSTBUILD
+  ENV "FILES=${CMAKE_CURRENT_SOURCE_DIR}/str_to_float_comparison_data.txt"
+  SRCS
+    str_to_float_comparison_test.cpp
+  DEPENDS
+    libc.src.stdio.printf
+    libc.src.stdio.fopen
+    libc.src.stdio.fclose
+    libc.src.stdio.fgets
+    libc.src.stdlib.strtof
+    libc.src.stdlib.strtod
+    libc.src.stdlib.getenv
+    libc.src.string.strtok
+    libc.src.__support.CPP.bit
+)
 
 add_subdirectory(CPP)
 add_subdirectory(File)

>From 8154f3f8840d89cc649b0c66d86ff3a455f50c98 Mon Sep 17 00:00:00 2001
From: bassiounix <muhammad.m.bassiouni at gmail.com>
Date: Wed, 2 Apr 2025 07:27:18 +0200
Subject: [PATCH 05/22] add `NO_RUN_POSTBUILD` to hermitic test

---
 libc/cmake/modules/LLVMLibCTestRules.cmake | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)

diff --git a/libc/cmake/modules/LLVMLibCTestRules.cmake b/libc/cmake/modules/LLVMLibCTestRules.cmake
index 45a36abd8ce1b..09dddb1a1f6d9 100644
--- a/libc/cmake/modules/LLVMLibCTestRules.cmake
+++ b/libc/cmake/modules/LLVMLibCTestRules.cmake
@@ -642,7 +642,7 @@ function(add_libc_hermetic test_name)
   endif()
   cmake_parse_arguments(
     "HERMETIC_TEST"
-    "IS_GPU_BENCHMARK" # Optional arguments
+    "IS_GPU_BENCHMARK;NO_RUN_POSTBUILD" # Optional arguments
     "SUITE" # Single value arguments
     "SRCS;HDRS;DEPENDS;ARGS;ENV;COMPILE_OPTIONS;LINK_LIBRARIES;LOADER_ARGS" # Multi-value arguments
     ${ARGN}
@@ -701,6 +701,7 @@ function(add_libc_hermetic test_name)
   endif()
   list(REMOVE_DUPLICATES link_object_files)
 
+
   # Make a library of all deps
   add_library(
     ${fq_target_name}.__libc__
@@ -713,7 +714,12 @@ function(add_libc_hermetic test_name)
   set_target_properties(${fq_target_name}.__libc__
       PROPERTIES ARCHIVE_OUTPUT_NAME ${fq_target_name}.libc)
 
-  set(fq_build_target_name ${fq_target_name}.__build__)
+  if(HERMETIC_TEST_NO_RUN_POSTBUILD)
+    set(fq_build_target_name ${fq_target_name})
+  else()
+    set(fq_build_target_name ${fq_target_name}.__build__)
+  endif()
+
   add_executable(
     ${fq_build_target_name}
     EXCLUDE_FROM_ALL
@@ -811,6 +817,14 @@ function(add_libc_hermetic test_name)
       SYMBOLIC "TRUE"
   )
 
+  if(NOT HERMETIC_TEST_NO_RUN_POSTBUILD)
+    add_custom_target(
+      ${fq_target_name}
+      COMMAND ${fq_build_target_name}
+      COMMENT "Running hermetic test ${fq_target_name}"
+    )
+  endif()
+
   add_dependencies(${HERMETIC_TEST_SUITE} ${fq_target_name})
   if(NOT ${HERMETIC_TEST_IS_GPU_BENCHMARK})
     # If it is a benchmark, it will already have been added to the

>From bad7c0cf6c8b488a34ee464d3f5f55075571fa76 Mon Sep 17 00:00:00 2001
From: bassiounix <muhammad.m.bassiouni at gmail.com>
Date: Wed, 2 Apr 2025 07:44:01 +0200
Subject: [PATCH 06/22] add `ENV` to unit test

---
 libc/cmake/modules/LLVMLibCTestRules.cmake | 31 +++++++++++++++++++++-
 1 file changed, 30 insertions(+), 1 deletion(-)

diff --git a/libc/cmake/modules/LLVMLibCTestRules.cmake b/libc/cmake/modules/LLVMLibCTestRules.cmake
index 09dddb1a1f6d9..86c299c9cd340 100644
--- a/libc/cmake/modules/LLVMLibCTestRules.cmake
+++ b/libc/cmake/modules/LLVMLibCTestRules.cmake
@@ -191,8 +191,11 @@ endfunction(get_object_files_for_test)
 #      SRCS  <list of .cpp files for the test>
 #      HDRS  <list of .h files for the test>
 #      DEPENDS <list of dependencies>
+#      ARGS <list of command line arguments to be passed to the test>
+#      ENV <list of environment variables to set before running the test>
 #      COMPILE_OPTIONS <list of special compile options for this target>
 #      LINK_LIBRARIES <list of linking libraries for this target>
+#      LOADER_ARGS <list of special args to loaders (like the GPU loader)>
 #    )
 function(create_libc_unittest fq_target_name)
   if(NOT LLVM_INCLUDE_TESTS)
@@ -203,7 +206,7 @@ function(create_libc_unittest fq_target_name)
     "LIBC_UNITTEST"
     "NO_RUN_POSTBUILD;C_TEST" # Optional arguments
     "SUITE;CXX_STANDARD" # Single value arguments
-    "SRCS;HDRS;DEPENDS;COMPILE_OPTIONS;LINK_LIBRARIES;FLAGS" # Multi-value arguments
+    "SRCS;HDRS;DEPENDS;ARGS;ENV;COMPILE_OPTIONS;LINK_LIBRARIES;LOADER_ARGS;FLAGS" # Multi-value arguments
     ${ARGN}
   )
   if(NOT LIBC_UNITTEST_SRCS)
@@ -316,6 +319,32 @@ function(create_libc_unittest fq_target_name)
 
   target_link_libraries(${fq_build_target_name} PRIVATE ${link_libraries})
 
+  if(TARGET libc.utils.gpu.loader)
+    add_dependencies(${fq_build_target_name} libc.utils.gpu.loader)
+    get_target_property(gpu_loader_exe libc.utils.gpu.loader "EXECUTABLE")
+  endif()
+
+  set(test_cmd ${LIBC_UNITTEST_ENV}
+      $<$<BOOL:${LIBC_TARGET_OS_IS_GPU}>:${gpu_loader_exe}> ${CMAKE_CROSSCOMPILING_EMULATOR} ${LIBC_UNITTEST_LOADER_ARGS}
+      $<TARGET_FILE:${fq_build_target_name}> ${LIBC_UNITTEST_ARGS})
+  add_custom_target(
+    ${fq_target_name}
+    DEPENDS ${fq_target_name}-cmd
+  )
+
+  add_custom_command(
+    OUTPUT ${fq_target_name}-cmd
+    COMMAND ${test_cmd}
+    COMMAND_EXPAND_LISTS
+    COMMENT "Running unit test ${fq_target_name}"
+    ${LIBC_UNIT_TEST_JOB_POOL}
+  )
+
+  set_source_files_properties(${fq_target_name}-cmd
+    PROPERTIES
+      SYMBOLIC "TRUE"
+  )
+
   if(NOT LIBC_UNITTEST_NO_RUN_POSTBUILD)
     add_custom_target(
       ${fq_target_name}

>From a0d1cff3bf96f35f813b50fbc168ec1d4dd6110d Mon Sep 17 00:00:00 2001
From: bassiounix <muhammad.m.bassiouni at gmail.com>
Date: Wed, 2 Apr 2025 23:18:12 +0200
Subject: [PATCH 07/22] fix target naming collision

---
 libc/cmake/modules/LLVMLibCTestRules.cmake | 77 +++++++++-------------
 libc/test/src/__support/CMakeLists.txt     |  4 +-
 2 files changed, 34 insertions(+), 47 deletions(-)

diff --git a/libc/cmake/modules/LLVMLibCTestRules.cmake b/libc/cmake/modules/LLVMLibCTestRules.cmake
index 86c299c9cd340..a1f6b3bdbd022 100644
--- a/libc/cmake/modules/LLVMLibCTestRules.cmake
+++ b/libc/cmake/modules/LLVMLibCTestRules.cmake
@@ -324,32 +324,26 @@ function(create_libc_unittest fq_target_name)
     get_target_property(gpu_loader_exe libc.utils.gpu.loader "EXECUTABLE")
   endif()
 
-  set(test_cmd ${LIBC_UNITTEST_ENV}
-      $<$<BOOL:${LIBC_TARGET_OS_IS_GPU}>:${gpu_loader_exe}> ${CMAKE_CROSSCOMPILING_EMULATOR} ${LIBC_UNITTEST_LOADER_ARGS}
-      $<TARGET_FILE:${fq_build_target_name}> ${LIBC_UNITTEST_ARGS})
-  add_custom_target(
-    ${fq_target_name}
-    DEPENDS ${fq_target_name}-cmd
-  )
-
-  add_custom_command(
-    OUTPUT ${fq_target_name}-cmd
-    COMMAND ${test_cmd}
-    COMMAND_EXPAND_LISTS
-    COMMENT "Running unit test ${fq_target_name}"
-    ${LIBC_UNIT_TEST_JOB_POOL}
-  )
-
-  set_source_files_properties(${fq_target_name}-cmd
-    PROPERTIES
-      SYMBOLIC "TRUE"
-  )
-
   if(NOT LIBC_UNITTEST_NO_RUN_POSTBUILD)
+    set(test_cmd ${LIBC_UNITTEST_ENV}
+        $<$<BOOL:${LIBC_TARGET_OS_IS_GPU}>:${gpu_loader_exe}> ${CMAKE_CROSSCOMPILING_EMULATOR} ${LIBC_UNITTEST_LOADER_ARGS}
+        $<TARGET_FILE:${fq_build_target_name}> ${LIBC_UNITTEST_ARGS})
     add_custom_target(
       ${fq_target_name}
-      COMMAND ${fq_build_target_name}
+      DEPENDS ${fq_target_name}-cmd
+    )
+
+    add_custom_command(
+      OUTPUT ${fq_target_name}-cmd
+      COMMAND ${test_cmd}
+      COMMAND_EXPAND_LISTS
       COMMENT "Running unit test ${fq_target_name}"
+      ${LIBC_UNIT_TEST_JOB_POOL}
+    )
+
+    set_source_files_properties(${fq_target_name}-cmd
+      PROPERTIES
+        SYMBOLIC "TRUE"
     )
   endif()
 
@@ -730,7 +724,6 @@ function(add_libc_hermetic test_name)
   endif()
   list(REMOVE_DUPLICATES link_object_files)
 
-
   # Make a library of all deps
   add_library(
     ${fq_target_name}.__libc__
@@ -825,32 +818,26 @@ function(add_libc_hermetic test_name)
     get_target_property(gpu_loader_exe libc.utils.gpu.loader "EXECUTABLE")
   endif()
 
-  set(test_cmd ${HERMETIC_TEST_ENV}
-      $<$<BOOL:${LIBC_TARGET_OS_IS_GPU}>:${gpu_loader_exe}> ${CMAKE_CROSSCOMPILING_EMULATOR} ${HERMETIC_TEST_LOADER_ARGS}
-      $<TARGET_FILE:${fq_build_target_name}> ${HERMETIC_TEST_ARGS})
-  add_custom_target(
-    ${fq_target_name}
-    DEPENDS ${fq_target_name}-cmd
-  )
-
-  add_custom_command(
-    OUTPUT ${fq_target_name}-cmd
-    COMMAND ${test_cmd}
-    COMMAND_EXPAND_LISTS
-    COMMENT "Running hermetic test ${fq_target_name}"
-    ${LIBC_HERMETIC_TEST_JOB_POOL}
-  )
-
-  set_source_files_properties(${fq_target_name}-cmd
-    PROPERTIES
-      SYMBOLIC "TRUE"
-  )
-
   if(NOT HERMETIC_TEST_NO_RUN_POSTBUILD)
+    set(test_cmd ${HERMETIC_TEST_ENV}
+        $<$<BOOL:${LIBC_TARGET_OS_IS_GPU}>:${gpu_loader_exe}> ${CMAKE_CROSSCOMPILING_EMULATOR} ${HERMETIC_TEST_LOADER_ARGS}
+        $<TARGET_FILE:${fq_build_target_name}> ${HERMETIC_TEST_ARGS})
     add_custom_target(
       ${fq_target_name}
-      COMMAND ${fq_build_target_name}
+      DEPENDS ${fq_target_name}-cmd
+    )
+
+    add_custom_command(
+      OUTPUT ${fq_target_name}-cmd
+      COMMAND ${test_cmd}
+      COMMAND_EXPAND_LISTS
       COMMENT "Running hermetic test ${fq_target_name}"
+      ${LIBC_HERMETIC_TEST_JOB_POOL}
+    )
+
+    set_source_files_properties(${fq_target_name}-cmd
+      PROPERTIES
+        SYMBOLIC "TRUE"
     )
   endif()
 
diff --git a/libc/test/src/__support/CMakeLists.txt b/libc/test/src/__support/CMakeLists.txt
index 685ac9bd40668..097c29af50f77 100644
--- a/libc/test/src/__support/CMakeLists.txt
+++ b/libc/test/src/__support/CMakeLists.txt
@@ -251,10 +251,9 @@ add_libc_test(
 
 add_libc_test(
   str_to_float_comparison_test
+  NO_RUN_POSTBUILD
   SUITE
     libc-support-tests
-  NO_RUN_POSTBUILD
-  ENV "FILES=${CMAKE_CURRENT_SOURCE_DIR}/str_to_float_comparison_data.txt"
   SRCS
     str_to_float_comparison_test.cpp
   DEPENDS
@@ -267,6 +266,7 @@ add_libc_test(
     libc.src.stdlib.getenv
     libc.src.string.strtok
     libc.src.__support.CPP.bit
+  ENV "FILES=${CMAKE_CURRENT_SOURCE_DIR}/str_to_float_comparison_data.txt"
 )
 
 add_subdirectory(CPP)

>From 81b9a9c32f69f238ef8fa791ff80b1961d166826 Mon Sep 17 00:00:00 2001
From: bassiounix <muhammad.m.bassiouni at gmail.com>
Date: Wed, 2 Apr 2025 23:45:29 +0200
Subject: [PATCH 08/22] fix argument name

---
 libc/cmake/modules/LLVMLibCTestRules.cmake | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/cmake/modules/LLVMLibCTestRules.cmake b/libc/cmake/modules/LLVMLibCTestRules.cmake
index bd7e90d1e6fac..c3a684541a000 100644
--- a/libc/cmake/modules/LLVMLibCTestRules.cmake
+++ b/libc/cmake/modules/LLVMLibCTestRules.cmake
@@ -665,7 +665,7 @@ function(add_libc_hermetic test_name)
   endif()
   cmake_parse_arguments(
     "HERMETIC_TEST"
-    "IS_GPU_BENCHMARK;NO_RUNPOSTBUILD" # Optional arguments
+    "IS_GPU_BENCHMARK;NO_RUN_POSTBUILD" # Optional arguments
     "SUITE;CXX_STANDARD" # Single value arguments
     "SRCS;HDRS;DEPENDS;ARGS;ENV;COMPILE_OPTIONS;LINK_LIBRARIES;LOADER_ARGS" # Multi-value arguments
     ${ARGN}

>From 95d43fbe222d42c23b4070848780434ce2458b9d Mon Sep 17 00:00:00 2001
From: bassiounix <muhammad.m.bassiouni at gmail.com>
Date: Wed, 2 Apr 2025 23:48:10 +0200
Subject: [PATCH 09/22] move `ENV` above `DEPENDS`

---
 libc/test/src/__support/CMakeLists.txt | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/test/src/__support/CMakeLists.txt b/libc/test/src/__support/CMakeLists.txt
index 097c29af50f77..03f6f896150a6 100644
--- a/libc/test/src/__support/CMakeLists.txt
+++ b/libc/test/src/__support/CMakeLists.txt
@@ -256,6 +256,7 @@ add_libc_test(
     libc-support-tests
   SRCS
     str_to_float_comparison_test.cpp
+  ENV "FILES=${CMAKE_CURRENT_SOURCE_DIR}/str_to_float_comparison_data.txt"
   DEPENDS
     libc.src.stdio.printf
     libc.src.stdio.fopen
@@ -266,7 +267,6 @@ add_libc_test(
     libc.src.stdlib.getenv
     libc.src.string.strtok
     libc.src.__support.CPP.bit
-  ENV "FILES=${CMAKE_CURRENT_SOURCE_DIR}/str_to_float_comparison_data.txt"
 )
 
 add_subdirectory(CPP)

>From 10ddac3ab80a4694d5df1cc50b801eb1bb9e7be9 Mon Sep 17 00:00:00 2001
From: bassiounix <muhammad.m.bassiouni at gmail.com>
Date: Wed, 2 Apr 2025 23:56:46 +0200
Subject: [PATCH 10/22] remove GPU configs from unit test

---
 libc/cmake/modules/LLVMLibCTestRules.cmake | 12 ++----------
 1 file changed, 2 insertions(+), 10 deletions(-)

diff --git a/libc/cmake/modules/LLVMLibCTestRules.cmake b/libc/cmake/modules/LLVMLibCTestRules.cmake
index c3a684541a000..4e99e048164dc 100644
--- a/libc/cmake/modules/LLVMLibCTestRules.cmake
+++ b/libc/cmake/modules/LLVMLibCTestRules.cmake
@@ -191,11 +191,9 @@ endfunction(get_object_files_for_test)
 #      SRCS  <list of .cpp files for the test>
 #      HDRS  <list of .h files for the test>
 #      DEPENDS <list of dependencies>
-#      ARGS <list of command line arguments to be passed to the test>
 #      ENV <list of environment variables to set before running the test>
 #      COMPILE_OPTIONS <list of special compile options for this target>
 #      LINK_LIBRARIES <list of linking libraries for this target>
-#      LOADER_ARGS <list of special args to loaders (like the GPU loader)>
 #    )
 function(create_libc_unittest fq_target_name)
   if(NOT LLVM_INCLUDE_TESTS)
@@ -206,7 +204,7 @@ function(create_libc_unittest fq_target_name)
     "LIBC_UNITTEST"
     "NO_RUN_POSTBUILD;C_TEST" # Optional arguments
     "SUITE;CXX_STANDARD" # Single value arguments
-    "SRCS;HDRS;DEPENDS;ARGS;ENV;COMPILE_OPTIONS;LINK_LIBRARIES;LOADER_ARGS;FLAGS" # Multi-value arguments
+    "SRCS;HDRS;DEPENDS;ENV;COMPILE_OPTIONS;LINK_LIBRARIES;FLAGS" # Multi-value arguments
     ${ARGN}
   )
   if(NOT LIBC_UNITTEST_SRCS)
@@ -319,15 +317,9 @@ function(create_libc_unittest fq_target_name)
 
   target_link_libraries(${fq_build_target_name} PRIVATE ${link_libraries})
 
-  if(TARGET libc.utils.gpu.loader)
-    add_dependencies(${fq_build_target_name} libc.utils.gpu.loader)
-    get_target_property(gpu_loader_exe libc.utils.gpu.loader "EXECUTABLE")
-  endif()
-
   if(NOT LIBC_UNITTEST_NO_RUN_POSTBUILD)
     set(test_cmd ${LIBC_UNITTEST_ENV}
-        $<$<BOOL:${LIBC_TARGET_OS_IS_GPU}>:${gpu_loader_exe}> ${CMAKE_CROSSCOMPILING_EMULATOR} ${LIBC_UNITTEST_LOADER_ARGS}
-        $<TARGET_FILE:${fq_build_target_name}> ${LIBC_UNITTEST_ARGS})
+        ${CMAKE_CROSSCOMPILING_EMULATOR} $<TARGET_FILE:${fq_build_target_name}>)
     add_custom_target(
       ${fq_target_name}
       DEPENDS ${fq_target_name}-cmd

>From 9cdff938b22c740baac7ccf764172fd18917cfa9 Mon Sep 17 00:00:00 2001
From: bassiounix <muhammad.m.bassiouni at gmail.com>
Date: Thu, 3 Apr 2025 01:25:26 +0200
Subject: [PATCH 11/22] change cmd target name

---
 libc/cmake/modules/LLVMLibCTestRules.cmake | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/libc/cmake/modules/LLVMLibCTestRules.cmake b/libc/cmake/modules/LLVMLibCTestRules.cmake
index 4e99e048164dc..d1f94aa05ce04 100644
--- a/libc/cmake/modules/LLVMLibCTestRules.cmake
+++ b/libc/cmake/modules/LLVMLibCTestRules.cmake
@@ -322,18 +322,18 @@ function(create_libc_unittest fq_target_name)
         ${CMAKE_CROSSCOMPILING_EMULATOR} $<TARGET_FILE:${fq_build_target_name}>)
     add_custom_target(
       ${fq_target_name}
-      DEPENDS ${fq_target_name}-cmd
+      DEPENDS ${fq_target_name}.__cmd__
     )
 
     add_custom_command(
-      OUTPUT ${fq_target_name}-cmd
+      OUTPUT ${fq_target_name}.__cmd__
       COMMAND ${test_cmd}
       COMMAND_EXPAND_LISTS
       COMMENT "Running unit test ${fq_target_name}"
       ${LIBC_UNIT_TEST_JOB_POOL}
     )
 
-    set_source_files_properties(${fq_target_name}-cmd
+    set_source_files_properties(${fq_target_name}.__cmd__
       PROPERTIES
         SYMBOLIC "TRUE"
     )
@@ -820,18 +820,18 @@ function(add_libc_hermetic test_name)
         $<TARGET_FILE:${fq_build_target_name}> ${HERMETIC_TEST_ARGS})
     add_custom_target(
       ${fq_target_name}
-      DEPENDS ${fq_target_name}-cmd
+      DEPENDS ${fq_target_name}.__cmd__
     )
 
     add_custom_command(
-      OUTPUT ${fq_target_name}-cmd
+      OUTPUT ${fq_target_name}.__cmd__
       COMMAND ${test_cmd}
       COMMAND_EXPAND_LISTS
       COMMENT "Running hermetic test ${fq_target_name}"
       ${LIBC_HERMETIC_TEST_JOB_POOL}
     )
 
-    set_source_files_properties(${fq_target_name}-cmd
+    set_source_files_properties(${fq_target_name}.__cmd__
       PROPERTIES
         SYMBOLIC "TRUE"
     )

>From 2ad8344170c897098f9c01db8b6963420b22c65f Mon Sep 17 00:00:00 2001
From: bassiounix <muhammad.m.bassiouni at gmail.com>
Date: Sat, 5 Apr 2025 01:53:45 +0200
Subject: [PATCH 12/22] copy string to prevent env pointer manipulation

---
 libc/test/src/__support/CMakeLists.txt                   | 2 ++
 libc/test/src/__support/str_to_float_comparison_test.cpp | 5 +++++
 2 files changed, 7 insertions(+)

diff --git a/libc/test/src/__support/CMakeLists.txt b/libc/test/src/__support/CMakeLists.txt
index 03f6f896150a6..78aa8b4c0aa0e 100644
--- a/libc/test/src/__support/CMakeLists.txt
+++ b/libc/test/src/__support/CMakeLists.txt
@@ -265,7 +265,9 @@ add_libc_test(
     libc.src.stdlib.strtof
     libc.src.stdlib.strtod
     libc.src.stdlib.getenv
+    libc.src.stdlib.free
     libc.src.string.strtok
+    libc.src.string.strdup
     libc.src.__support.CPP.bit
 )
 
diff --git a/libc/test/src/__support/str_to_float_comparison_test.cpp b/libc/test/src/__support/str_to_float_comparison_test.cpp
index f7d47db4c518c..1c562bfe4b9ea 100644
--- a/libc/test/src/__support/str_to_float_comparison_test.cpp
+++ b/libc/test/src/__support/str_to_float_comparison_test.cpp
@@ -14,7 +14,9 @@
 #include "src/stdlib/getenv.h"
 #include "src/stdlib/strtod.h"
 #include "src/stdlib/strtof.h"
+#include "src/stdlib/free.h"
 #include "src/string/strtok.h"
+#include "src/string/strdup.h"
 #include "test/UnitTest/Test.h"
 #include <stdint.h>
 
@@ -154,6 +156,7 @@ TEST(LlvmLibcStrToFloatComparisonTest, CheckFile) {
   int total = 0;
 
   char *files = LIBC_NAMESPACE::getenv("FILES");
+  files = LIBC_NAMESPACE::strdup(files);
   for (char *file = LIBC_NAMESPACE::strtok(files, ","); file != nullptr;
        file = LIBC_NAMESPACE::strtok(nullptr, ",")) {
     int curResult =
@@ -165,6 +168,8 @@ TEST(LlvmLibcStrToFloatComparisonTest, CheckFile) {
     }
   }
 
+  LIBC_NAMESPACE::free(files);
+
   EXPECT_EQ(result, 0);
   EXPECT_EQ(fails, 0);
   EXPECT_EQ(bitdiffs, 0);

>From d2c9f1b542206a18dd4c256c7334880469c9be82 Mon Sep 17 00:00:00 2001
From: bassiounix <muhammad.m.bassiouni at gmail.com>
Date: Sat, 5 Apr 2025 01:54:28 +0200
Subject: [PATCH 13/22] Update LLVMLibCTestRules.cmake

---
 libc/cmake/modules/LLVMLibCTestRules.cmake | 14 +-------------
 1 file changed, 1 insertion(+), 13 deletions(-)

diff --git a/libc/cmake/modules/LLVMLibCTestRules.cmake b/libc/cmake/modules/LLVMLibCTestRules.cmake
index d1f94aa05ce04..b1a8213364476 100644
--- a/libc/cmake/modules/LLVMLibCTestRules.cmake
+++ b/libc/cmake/modules/LLVMLibCTestRules.cmake
@@ -322,20 +322,8 @@ function(create_libc_unittest fq_target_name)
         ${CMAKE_CROSSCOMPILING_EMULATOR} $<TARGET_FILE:${fq_build_target_name}>)
     add_custom_target(
       ${fq_target_name}
-      DEPENDS ${fq_target_name}.__cmd__
-    )
-
-    add_custom_command(
-      OUTPUT ${fq_target_name}.__cmd__
-      COMMAND ${test_cmd}
-      COMMAND_EXPAND_LISTS
+      COMMAND ${LIBC_UNITTEST_ENV} ${CMAKE_CROSSCOMPILING_EMULATOR} ${fq_build_target_name}
       COMMENT "Running unit test ${fq_target_name}"
-      ${LIBC_UNIT_TEST_JOB_POOL}
-    )
-
-    set_source_files_properties(${fq_target_name}.__cmd__
-      PROPERTIES
-        SYMBOLIC "TRUE"
     )
   endif()
 

>From 34b4f1488f048dd5ee03e79b73ebbd6cb72bdab6 Mon Sep 17 00:00:00 2001
From: bassiounix <muhammad.m.bassiouni at gmail.com>
Date: Sat, 5 Apr 2025 02:04:31 +0200
Subject: [PATCH 14/22] fix formatting

---
 libc/test/src/__support/str_to_float_comparison_test.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/libc/test/src/__support/str_to_float_comparison_test.cpp b/libc/test/src/__support/str_to_float_comparison_test.cpp
index 1c562bfe4b9ea..2f31ef06a1e48 100644
--- a/libc/test/src/__support/str_to_float_comparison_test.cpp
+++ b/libc/test/src/__support/str_to_float_comparison_test.cpp
@@ -11,12 +11,12 @@
 #include "src/stdio/fgets.h"
 #include "src/stdio/fopen.h"
 #include "src/stdio/printf.h"
+#include "src/stdlib/free.h"
 #include "src/stdlib/getenv.h"
 #include "src/stdlib/strtod.h"
 #include "src/stdlib/strtof.h"
-#include "src/stdlib/free.h"
-#include "src/string/strtok.h"
 #include "src/string/strdup.h"
+#include "src/string/strtok.h"
 #include "test/UnitTest/Test.h"
 #include <stdint.h>
 

>From 7590cc7fd9bb3276af1c01c44588c9462bd69d3b Mon Sep 17 00:00:00 2001
From: bassiounix <muhammad.m.bassiouni at gmail.com>
Date: Sat, 5 Apr 2025 03:28:32 +0200
Subject: [PATCH 15/22] remove unnecessary free operation

---
 libc/test/src/__support/CMakeLists.txt                   | 1 -
 libc/test/src/__support/str_to_float_comparison_test.cpp | 3 ---
 2 files changed, 4 deletions(-)

diff --git a/libc/test/src/__support/CMakeLists.txt b/libc/test/src/__support/CMakeLists.txt
index 78aa8b4c0aa0e..971389ee6a3da 100644
--- a/libc/test/src/__support/CMakeLists.txt
+++ b/libc/test/src/__support/CMakeLists.txt
@@ -265,7 +265,6 @@ add_libc_test(
     libc.src.stdlib.strtof
     libc.src.stdlib.strtod
     libc.src.stdlib.getenv
-    libc.src.stdlib.free
     libc.src.string.strtok
     libc.src.string.strdup
     libc.src.__support.CPP.bit
diff --git a/libc/test/src/__support/str_to_float_comparison_test.cpp b/libc/test/src/__support/str_to_float_comparison_test.cpp
index 2f31ef06a1e48..b0fbf56845aa3 100644
--- a/libc/test/src/__support/str_to_float_comparison_test.cpp
+++ b/libc/test/src/__support/str_to_float_comparison_test.cpp
@@ -11,7 +11,6 @@
 #include "src/stdio/fgets.h"
 #include "src/stdio/fopen.h"
 #include "src/stdio/printf.h"
-#include "src/stdlib/free.h"
 #include "src/stdlib/getenv.h"
 #include "src/stdlib/strtod.h"
 #include "src/stdlib/strtof.h"
@@ -168,8 +167,6 @@ TEST(LlvmLibcStrToFloatComparisonTest, CheckFile) {
     }
   }
 
-  LIBC_NAMESPACE::free(files);
-
   EXPECT_EQ(result, 0);
   EXPECT_EQ(fails, 0);
   EXPECT_EQ(bitdiffs, 0);

>From 0f481ede978333b95459cc221c008f6b78e62722 Mon Sep 17 00:00:00 2001
From: bassiounix <muhammad.m.bassiouni at gmail.com>
Date: Sat, 5 Apr 2025 04:52:36 +0200
Subject: [PATCH 16/22] remove unnecessary command

---
 libc/cmake/modules/LLVMLibCTestRules.cmake | 2 --
 1 file changed, 2 deletions(-)

diff --git a/libc/cmake/modules/LLVMLibCTestRules.cmake b/libc/cmake/modules/LLVMLibCTestRules.cmake
index b1a8213364476..63a8e9ecda002 100644
--- a/libc/cmake/modules/LLVMLibCTestRules.cmake
+++ b/libc/cmake/modules/LLVMLibCTestRules.cmake
@@ -318,8 +318,6 @@ function(create_libc_unittest fq_target_name)
   target_link_libraries(${fq_build_target_name} PRIVATE ${link_libraries})
 
   if(NOT LIBC_UNITTEST_NO_RUN_POSTBUILD)
-    set(test_cmd ${LIBC_UNITTEST_ENV}
-        ${CMAKE_CROSSCOMPILING_EMULATOR} $<TARGET_FILE:${fq_build_target_name}>)
     add_custom_target(
       ${fq_target_name}
       COMMAND ${LIBC_UNITTEST_ENV} ${CMAKE_CROSSCOMPILING_EMULATOR} ${fq_build_target_name}

>From 0ab1f9d5612981842194c6ea326254676acd6844 Mon Sep 17 00:00:00 2001
From: bassiounix <muhammad.m.bassiouni at gmail.com>
Date: Sat, 5 Apr 2025 06:51:19 +0200
Subject: [PATCH 17/22] fix: make test independent from file I/O

---
 .../str_to_float_comparison_test.cpp          | 174 +++++++++++-------
 1 file changed, 108 insertions(+), 66 deletions(-)

diff --git a/libc/test/src/__support/str_to_float_comparison_test.cpp b/libc/test/src/__support/str_to_float_comparison_test.cpp
index b0fbf56845aa3..6745e21f0c8ca 100644
--- a/libc/test/src/__support/str_to_float_comparison_test.cpp
+++ b/libc/test/src/__support/str_to_float_comparison_test.cpp
@@ -56,79 +56,110 @@ static inline uint64_t fastHexToU64(const char *inStr) {
   return result;
 }
 
-int checkFile(char *inputFileName, int *totalFails, int *totalBitDiffs,
-              int *detailedBitDiffs, int *total) {
-  int32_t curFails = 0;    // Only counts actual failures, not bitdiffs.
-  int32_t curBitDiffs = 0; // A bitdiff is when the expected result and actual
-                           // result are off by +/- 1 bit.
-  char line[100];
-  char *num;
-
-  auto *fileHandle = LIBC_NAMESPACE::fopen(inputFileName, "r");
+static void parseLine(char *line, int *total, int *detailedBitDiffs,
+                      int32_t &curFails, int32_t &curBitDiffs) {
 
-  if (!fileHandle) {
-    LIBC_NAMESPACE::printf("file '%s' failed to open. Exiting.\n",
-                           inputFileName);
-    return 1;
+  if (line[0] == '#') {
+    return;
   }
+  *total = *total + 1;
+  uint32_t expectedFloatRaw;
+  uint64_t expectedDoubleRaw;
 
-  while (LIBC_NAMESPACE::fgets(line, sizeof(line), fileHandle)) {
-    if (line[0] == '#') {
-      continue;
-    }
-    *total = *total + 1;
-    uint32_t expectedFloatRaw;
-    uint64_t expectedDoubleRaw;
+  expectedFloatRaw = fastHexToU32(line + 5);
+  expectedDoubleRaw = fastHexToU64(line + 14);
 
-    expectedFloatRaw = fastHexToU32(line + 5);
-    expectedDoubleRaw = fastHexToU64(line + 14);
-    num = line + 31;
+  char *num = line + 31;
 
-    float floatResult = LIBC_NAMESPACE::strtof(num, nullptr);
+  float floatResult = LIBC_NAMESPACE::strtof(num, nullptr);
 
-    double doubleResult = LIBC_NAMESPACE::strtod(num, nullptr);
+  double doubleResult = LIBC_NAMESPACE::strtod(num, nullptr);
 
-    uint32_t floatRaw = LIBC_NAMESPACE::cpp::bit_cast<uint32_t>(floatResult);
+  uint32_t floatRaw = LIBC_NAMESPACE::cpp::bit_cast<uint32_t>(floatResult);
 
-    uint64_t doubleRaw = LIBC_NAMESPACE::cpp::bit_cast<uint64_t>(doubleResult);
+  uint64_t doubleRaw = LIBC_NAMESPACE::cpp::bit_cast<uint64_t>(doubleResult);
 
-    if (!(expectedFloatRaw == floatRaw)) {
-      if (expectedFloatRaw == floatRaw + 1 ||
-          expectedFloatRaw == floatRaw - 1) {
-        curBitDiffs++;
-        if (expectedFloatRaw == floatRaw + 1) {
-          detailedBitDiffs[0] = detailedBitDiffs[0] + 1; // float low
-        } else {
-          detailedBitDiffs[1] = detailedBitDiffs[1] + 1; // float high
-        }
+  if (!(expectedFloatRaw == floatRaw)) {
+    if (expectedFloatRaw == floatRaw + 1 || expectedFloatRaw == floatRaw - 1) {
+      curBitDiffs++;
+      if (expectedFloatRaw == floatRaw + 1) {
+        detailedBitDiffs[0] = detailedBitDiffs[0] + 1; // float low
       } else {
-        curFails++;
-      }
-      if (curFails + curBitDiffs < 10) {
-        LIBC_NAMESPACE::printf("Float fail for '%s'. Expected %x but got %x\n",
-                               num, expectedFloatRaw, floatRaw);
+        detailedBitDiffs[1] = detailedBitDiffs[1] + 1; // float high
       }
+    } else {
+      curFails++;
     }
+    if (curFails + curBitDiffs < 10) {
+      LIBC_NAMESPACE::printf("Float fail for '%s'. Expected %x but got %x\n",
+                             num, expectedFloatRaw, floatRaw);
+    }
+  }
 
-    if (!(expectedDoubleRaw == doubleRaw)) {
-      if (expectedDoubleRaw == doubleRaw + 1 ||
-          expectedDoubleRaw == doubleRaw - 1) {
-        curBitDiffs++;
-        if (expectedDoubleRaw == doubleRaw + 1) {
-          detailedBitDiffs[2] = detailedBitDiffs[2] + 1; // double low
-        } else {
-          detailedBitDiffs[3] = detailedBitDiffs[3] + 1; // double high
-        }
+  if (!(expectedDoubleRaw == doubleRaw)) {
+    if (expectedDoubleRaw == doubleRaw + 1 ||
+        expectedDoubleRaw == doubleRaw - 1) {
+      curBitDiffs++;
+      if (expectedDoubleRaw == doubleRaw + 1) {
+        detailedBitDiffs[2] = detailedBitDiffs[2] + 1; // double low
       } else {
-        curFails++;
-      }
-      if (curFails + curBitDiffs < 10) {
-        LIBC_NAMESPACE::printf(
-            "Double fail for '%s'. Expected %lx but got %lx\n", num,
-            expectedDoubleRaw, doubleRaw);
+        detailedBitDiffs[3] = detailedBitDiffs[3] + 1; // double high
       }
+    } else {
+      curFails++;
+    }
+    if (curFails + curBitDiffs < 10) {
+      LIBC_NAMESPACE::printf("Double fail for '%s'. Expected %lx but got %lx\n",
+                             num, expectedDoubleRaw, doubleRaw);
     }
   }
+}
+
+int checkBuffer(int *totalFails, int *totalBitDiffs, int *detailedBitDiffs,
+                int *total) {
+  const char *lines[6] = {"3C00 3F800000 3FF0000000000000 1",
+                          "3D00 3FA00000 3FF4000000000000 1.25",
+                          "3D9A 3FB33333 3FF6666666666666 1.4",
+                          "57B7 42F6E979 405EDD2F1A9FBE77 123.456",
+                          "622A 44454000 4088A80000000000 789",
+                          "7C00 7F800000 7FF0000000000000 123.456e789"};
+
+  int32_t curFails = 0;    // Only counts actual failures, not bitdiffs.
+  int32_t curBitDiffs = 0; // A bitdiff is when the expected result and actual
+  // result are off by +/- 1 bit.
+
+  for (uint8_t i = 0; i < 6; i++) {
+    auto line = const_cast<char *>(lines[i]);
+    parseLine(line, total, detailedBitDiffs, curFails, curBitDiffs);
+  }
+
+  *totalBitDiffs += curBitDiffs;
+  *totalFails += curFails;
+
+  if (curFails > 1 || curBitDiffs > 1) {
+    return 2;
+  }
+  return 0;
+}
+
+int checkFile(char *inputFileName, int *totalFails, int *totalBitDiffs,
+              int *detailedBitDiffs, int *total) {
+  int32_t curFails = 0;    // Only counts actual failures, not bitdiffs.
+  int32_t curBitDiffs = 0; // A bitdiff is when the expected result and actual
+  // result are off by +/- 1 bit.
+  char line[100];
+
+  auto *fileHandle = LIBC_NAMESPACE::fopen(inputFileName, "r");
+
+  if (!fileHandle) {
+    LIBC_NAMESPACE::printf("file '%s' failed to open. Exiting.\n",
+                           inputFileName);
+    return 1;
+  }
+
+  while (LIBC_NAMESPACE::fgets(line, sizeof(line), fileHandle)) {
+    parseLine(line, total, detailedBitDiffs, curFails, curBitDiffs);
+  }
 
   LIBC_NAMESPACE::fclose(fileHandle);
 
@@ -141,7 +172,16 @@ int checkFile(char *inputFileName, int *totalFails, int *totalBitDiffs,
   return 0;
 }
 
-TEST(LlvmLibcStrToFloatComparisonTest, CheckFile) {
+int updateResult(int result, int curResult) {
+  if (curResult == 1) {
+    result = 1;
+  } else if (curResult == 2) {
+    result = 2;
+  }
+  return result;
+}
+
+TEST(LlvmLibcStrToFloatComparisonTest, CheckFloats) {
   int result = 0;
   int fails = 0;
 
@@ -155,15 +195,17 @@ TEST(LlvmLibcStrToFloatComparisonTest, CheckFile) {
   int total = 0;
 
   char *files = LIBC_NAMESPACE::getenv("FILES");
-  files = LIBC_NAMESPACE::strdup(files);
-  for (char *file = LIBC_NAMESPACE::strtok(files, ","); file != nullptr;
-       file = LIBC_NAMESPACE::strtok(nullptr, ",")) {
-    int curResult =
-        checkFile(file, &fails, &bitdiffs, detailedBitDiffs, &total);
-    if (curResult == 1) {
-      result = 1;
-    } else if (curResult == 2) {
-      result = 2;
+
+  if (files == nullptr) {
+    int curResult = checkBuffer(&fails, &bitdiffs, detailedBitDiffs, &total);
+    result = updateResult(result, curResult);
+  } else {
+    files = LIBC_NAMESPACE::strdup(files);
+    for (char *file = LIBC_NAMESPACE::strtok(files, ","); file != nullptr;
+         file = LIBC_NAMESPACE::strtok(nullptr, ",")) {
+      int curResult =
+          checkFile(file, &fails, &bitdiffs, detailedBitDiffs, &total);
+      result = updateResult(result, curResult);
     }
   }
 

>From ced000538be76a0036cfb8ebf9f3bfa01ae6ec44 Mon Sep 17 00:00:00 2001
From: bassiounix <muhammad.m.bassiouni at gmail.com>
Date: Sun, 6 Apr 2025 00:59:35 +0200
Subject: [PATCH 18/22] remove `FILES` env variable

---
 libc/test/src/__support/CMakeLists.txt | 1 -
 1 file changed, 1 deletion(-)

diff --git a/libc/test/src/__support/CMakeLists.txt b/libc/test/src/__support/CMakeLists.txt
index 971389ee6a3da..b50bf245f202c 100644
--- a/libc/test/src/__support/CMakeLists.txt
+++ b/libc/test/src/__support/CMakeLists.txt
@@ -256,7 +256,6 @@ add_libc_test(
     libc-support-tests
   SRCS
     str_to_float_comparison_test.cpp
-  ENV "FILES=${CMAKE_CURRENT_SOURCE_DIR}/str_to_float_comparison_data.txt"
   DEPENDS
     libc.src.stdio.printf
     libc.src.stdio.fopen

>From 71947544b6e782ed510753e278d39d90f74eb89f Mon Sep 17 00:00:00 2001
From: bassiounix <muhammad.m.bassiouni at gmail.com>
Date: Sun, 6 Apr 2025 01:43:04 +0200
Subject: [PATCH 19/22] pack values to `ParseResult struct`

---
 .../str_to_float_comparison_test.cpp          | 72 +++++++++++--------
 1 file changed, 41 insertions(+), 31 deletions(-)

diff --git a/libc/test/src/__support/str_to_float_comparison_test.cpp b/libc/test/src/__support/str_to_float_comparison_test.cpp
index 6745e21f0c8ca..07e31a19178ae 100644
--- a/libc/test/src/__support/str_to_float_comparison_test.cpp
+++ b/libc/test/src/__support/str_to_float_comparison_test.cpp
@@ -34,6 +34,13 @@
 // ./libc_str_to_float_comparison_test <path/to/dataset/repo>/data/*
 // It will take a few seconds to run.
 
+struct ParseResult {
+  uint32_t totalFails;
+  uint32_t totalBitDiffs;
+  uint32_t detailedBitDiffs[4];
+  uint32_t total;
+};
+
 static inline uint32_t hexCharToU32(char in) {
   return in > '9' ? in + 10 - 'A' : in - '0';
 }
@@ -56,13 +63,13 @@ static inline uint64_t fastHexToU64(const char *inStr) {
   return result;
 }
 
-static void parseLine(char *line, int *total, int *detailedBitDiffs,
-                      int32_t &curFails, int32_t &curBitDiffs) {
+static void parseLine(char *line, ParseResult &parseResult, int32_t &curFails,
+                      int32_t &curBitDiffs) {
 
   if (line[0] == '#') {
     return;
   }
-  *total = *total + 1;
+  parseResult.total += 1;
   uint32_t expectedFloatRaw;
   uint64_t expectedDoubleRaw;
 
@@ -83,9 +90,11 @@ static void parseLine(char *line, int *total, int *detailedBitDiffs,
     if (expectedFloatRaw == floatRaw + 1 || expectedFloatRaw == floatRaw - 1) {
       curBitDiffs++;
       if (expectedFloatRaw == floatRaw + 1) {
-        detailedBitDiffs[0] = detailedBitDiffs[0] + 1; // float low
+        parseResult.detailedBitDiffs[0] =
+            parseResult.detailedBitDiffs[0] + 1; // float low
       } else {
-        detailedBitDiffs[1] = detailedBitDiffs[1] + 1; // float high
+        parseResult.detailedBitDiffs[1] =
+            parseResult.detailedBitDiffs[1] + 1; // float high
       }
     } else {
       curFails++;
@@ -101,9 +110,11 @@ static void parseLine(char *line, int *total, int *detailedBitDiffs,
         expectedDoubleRaw == doubleRaw - 1) {
       curBitDiffs++;
       if (expectedDoubleRaw == doubleRaw + 1) {
-        detailedBitDiffs[2] = detailedBitDiffs[2] + 1; // double low
+        parseResult.detailedBitDiffs[2] =
+            parseResult.detailedBitDiffs[2] + 1; // double low
       } else {
-        detailedBitDiffs[3] = detailedBitDiffs[3] + 1; // double high
+        parseResult.detailedBitDiffs[3] =
+            parseResult.detailedBitDiffs[3] + 1; // double high
       }
     } else {
       curFails++;
@@ -115,8 +126,7 @@ static void parseLine(char *line, int *total, int *detailedBitDiffs,
   }
 }
 
-int checkBuffer(int *totalFails, int *totalBitDiffs, int *detailedBitDiffs,
-                int *total) {
+int checkBuffer(ParseResult &parseResult) {
   const char *lines[6] = {"3C00 3F800000 3FF0000000000000 1",
                           "3D00 3FA00000 3FF4000000000000 1.25",
                           "3D9A 3FB33333 3FF6666666666666 1.4",
@@ -130,11 +140,11 @@ int checkBuffer(int *totalFails, int *totalBitDiffs, int *detailedBitDiffs,
 
   for (uint8_t i = 0; i < 6; i++) {
     auto line = const_cast<char *>(lines[i]);
-    parseLine(line, total, detailedBitDiffs, curFails, curBitDiffs);
+    parseLine(line, parseResult, curFails, curBitDiffs);
   }
 
-  *totalBitDiffs += curBitDiffs;
-  *totalFails += curFails;
+  parseResult.totalBitDiffs += curBitDiffs;
+  parseResult.totalFails += curFails;
 
   if (curFails > 1 || curBitDiffs > 1) {
     return 2;
@@ -142,8 +152,7 @@ int checkBuffer(int *totalFails, int *totalBitDiffs, int *detailedBitDiffs,
   return 0;
 }
 
-int checkFile(char *inputFileName, int *totalFails, int *totalBitDiffs,
-              int *detailedBitDiffs, int *total) {
+int checkFile(char *inputFileName, ParseResult &parseResult) {
   int32_t curFails = 0;    // Only counts actual failures, not bitdiffs.
   int32_t curBitDiffs = 0; // A bitdiff is when the expected result and actual
   // result are off by +/- 1 bit.
@@ -158,13 +167,13 @@ int checkFile(char *inputFileName, int *totalFails, int *totalBitDiffs,
   }
 
   while (LIBC_NAMESPACE::fgets(line, sizeof(line), fileHandle)) {
-    parseLine(line, total, detailedBitDiffs, curFails, curBitDiffs);
+    parseLine(line, parseResult, curFails, curBitDiffs);
   }
 
   LIBC_NAMESPACE::fclose(fileHandle);
 
-  *totalBitDiffs += curBitDiffs;
-  *totalFails += curFails;
+  parseResult.totalBitDiffs += curBitDiffs;
+  parseResult.totalFails += curFails;
 
   if (curFails > 1 || curBitDiffs > 1) {
     return 2;
@@ -183,38 +192,39 @@ int updateResult(int result, int curResult) {
 
 TEST(LlvmLibcStrToFloatComparisonTest, CheckFloats) {
   int result = 0;
-  int fails = 0;
 
   // Bitdiffs are cases where the expected result and actual result only differ
   // by +/- the least significant bit. They are tracked separately from larger
   // failures since a bitdiff is most likely the result of a rounding error, and
   // splitting them off makes them easier to track down.
-  int bitdiffs = 0;
-  int detailedBitDiffs[4] = {0, 0, 0, 0};
 
-  int total = 0;
+  ParseResult parseResult = {
+      .totalFails = 0,
+      .totalBitDiffs = 0,
+      .detailedBitDiffs = {0, 0, 0, 0},
+      .total = 0,
+  };
 
   char *files = LIBC_NAMESPACE::getenv("FILES");
 
   if (files == nullptr) {
-    int curResult = checkBuffer(&fails, &bitdiffs, detailedBitDiffs, &total);
+    int curResult = checkBuffer(parseResult);
     result = updateResult(result, curResult);
   } else {
     files = LIBC_NAMESPACE::strdup(files);
     for (char *file = LIBC_NAMESPACE::strtok(files, ","); file != nullptr;
          file = LIBC_NAMESPACE::strtok(nullptr, ",")) {
-      int curResult =
-          checkFile(file, &fails, &bitdiffs, detailedBitDiffs, &total);
+      int curResult = checkFile(file, parseResult);
       result = updateResult(result, curResult);
     }
   }
 
   EXPECT_EQ(result, 0);
-  EXPECT_EQ(fails, 0);
-  EXPECT_EQ(bitdiffs, 0);
-  EXPECT_EQ(detailedBitDiffs[0], 0); // float low
-  EXPECT_EQ(detailedBitDiffs[1], 0); // float high
-  EXPECT_EQ(detailedBitDiffs[2], 0); // double low
-  EXPECT_EQ(detailedBitDiffs[3], 0); // double high
-  EXPECT_EQ(total, 6);
+  EXPECT_EQ(parseResult.totalFails, 0u);
+  EXPECT_EQ(parseResult.totalBitDiffs, 0u);
+  EXPECT_EQ(parseResult.detailedBitDiffs[0], 0u); // float low
+  EXPECT_EQ(parseResult.detailedBitDiffs[1], 0u); // float high
+  EXPECT_EQ(parseResult.detailedBitDiffs[2], 0u); // double low
+  EXPECT_EQ(parseResult.detailedBitDiffs[3], 0u); // double high
+  EXPECT_EQ(parseResult.total, 6u);
 }

>From bcd11e0111e9d24b95080a3c34156a3ac8b941a6 Mon Sep 17 00:00:00 2001
From: bassiounix <muhammad.m.bassiouni at gmail.com>
Date: Sun, 6 Apr 2025 01:57:08 +0200
Subject: [PATCH 20/22] Add `ParseStatus` to parse operations

---
 .../str_to_float_comparison_test.cpp          | 42 +++++++++++--------
 1 file changed, 24 insertions(+), 18 deletions(-)

diff --git a/libc/test/src/__support/str_to_float_comparison_test.cpp b/libc/test/src/__support/str_to_float_comparison_test.cpp
index 07e31a19178ae..1c4962960999f 100644
--- a/libc/test/src/__support/str_to_float_comparison_test.cpp
+++ b/libc/test/src/__support/str_to_float_comparison_test.cpp
@@ -41,6 +41,12 @@ struct ParseResult {
   uint32_t total;
 };
 
+enum class ParseStatus : uint8_t {
+  SUCCESS,
+  FILE_ERROR,
+  PARSE_ERROR,
+};
+
 static inline uint32_t hexCharToU32(char in) {
   return in > '9' ? in + 10 - 'A' : in - '0';
 }
@@ -126,7 +132,7 @@ static void parseLine(char *line, ParseResult &parseResult, int32_t &curFails,
   }
 }
 
-int checkBuffer(ParseResult &parseResult) {
+ParseStatus checkBuffer(ParseResult &parseResult) {
   const char *lines[6] = {"3C00 3F800000 3FF0000000000000 1",
                           "3D00 3FA00000 3FF4000000000000 1.25",
                           "3D9A 3FB33333 3FF6666666666666 1.4",
@@ -147,12 +153,12 @@ int checkBuffer(ParseResult &parseResult) {
   parseResult.totalFails += curFails;
 
   if (curFails > 1 || curBitDiffs > 1) {
-    return 2;
+    return ParseStatus::PARSE_ERROR;
   }
-  return 0;
+  return ParseStatus::SUCCESS;
 }
 
-int checkFile(char *inputFileName, ParseResult &parseResult) {
+ParseStatus checkFile(char *inputFileName, ParseResult &parseResult) {
   int32_t curFails = 0;    // Only counts actual failures, not bitdiffs.
   int32_t curBitDiffs = 0; // A bitdiff is when the expected result and actual
   // result are off by +/- 1 bit.
@@ -163,7 +169,7 @@ int checkFile(char *inputFileName, ParseResult &parseResult) {
   if (!fileHandle) {
     LIBC_NAMESPACE::printf("file '%s' failed to open. Exiting.\n",
                            inputFileName);
-    return 1;
+    return ParseStatus::FILE_ERROR;
   }
 
   while (LIBC_NAMESPACE::fgets(line, sizeof(line), fileHandle)) {
@@ -176,22 +182,22 @@ int checkFile(char *inputFileName, ParseResult &parseResult) {
   parseResult.totalFails += curFails;
 
   if (curFails > 1 || curBitDiffs > 1) {
-    return 2;
+    return ParseStatus::PARSE_ERROR;
   }
-  return 0;
+  return ParseStatus::SUCCESS;
 }
 
-int updateResult(int result, int curResult) {
-  if (curResult == 1) {
-    result = 1;
-  } else if (curResult == 2) {
-    result = 2;
+ParseStatus updateResult(ParseStatus result, ParseStatus curResult) {
+  if (curResult == ParseStatus::FILE_ERROR) {
+    result = ParseStatus::FILE_ERROR;
+  } else if (curResult == ParseStatus::PARSE_ERROR) {
+    result = ParseStatus::PARSE_ERROR;
   }
   return result;
 }
 
 TEST(LlvmLibcStrToFloatComparisonTest, CheckFloats) {
-  int result = 0;
+  ParseStatus parseStatus = ParseStatus::SUCCESS;
 
   // Bitdiffs are cases where the expected result and actual result only differ
   // by +/- the least significant bit. They are tracked separately from larger
@@ -208,18 +214,18 @@ TEST(LlvmLibcStrToFloatComparisonTest, CheckFloats) {
   char *files = LIBC_NAMESPACE::getenv("FILES");
 
   if (files == nullptr) {
-    int curResult = checkBuffer(parseResult);
-    result = updateResult(result, curResult);
+    ParseStatus curResult = checkBuffer(parseResult);
+    parseStatus = updateResult(parseStatus, curResult);
   } else {
     files = LIBC_NAMESPACE::strdup(files);
     for (char *file = LIBC_NAMESPACE::strtok(files, ","); file != nullptr;
          file = LIBC_NAMESPACE::strtok(nullptr, ",")) {
-      int curResult = checkFile(file, parseResult);
-      result = updateResult(result, curResult);
+      ParseStatus curResult = checkFile(file, parseResult);
+      parseStatus = updateResult(parseStatus, curResult);
     }
   }
 
-  EXPECT_EQ(result, 0);
+  EXPECT_EQ(parseStatus, ParseStatus::SUCCESS);
   EXPECT_EQ(parseResult.totalFails, 0u);
   EXPECT_EQ(parseResult.totalBitDiffs, 0u);
   EXPECT_EQ(parseResult.detailedBitDiffs[0], 0u); // float low

>From c90a4da67caae7aa9aa78742b14b242c0a25c62c Mon Sep 17 00:00:00 2001
From: bassiounix <muhammad.m.bassiouni at gmail.com>
Date: Mon, 7 Apr 2025 07:26:59 +0200
Subject: [PATCH 21/22] mark `lines` array as `constexpr`

---
 .../src/__support/str_to_float_comparison_test.cpp  | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)

diff --git a/libc/test/src/__support/str_to_float_comparison_test.cpp b/libc/test/src/__support/str_to_float_comparison_test.cpp
index 1c4962960999f..4bd91eee1f0b3 100644
--- a/libc/test/src/__support/str_to_float_comparison_test.cpp
+++ b/libc/test/src/__support/str_to_float_comparison_test.cpp
@@ -133,12 +133,13 @@ static void parseLine(char *line, ParseResult &parseResult, int32_t &curFails,
 }
 
 ParseStatus checkBuffer(ParseResult &parseResult) {
-  const char *lines[6] = {"3C00 3F800000 3FF0000000000000 1",
-                          "3D00 3FA00000 3FF4000000000000 1.25",
-                          "3D9A 3FB33333 3FF6666666666666 1.4",
-                          "57B7 42F6E979 405EDD2F1A9FBE77 123.456",
-                          "622A 44454000 4088A80000000000 789",
-                          "7C00 7F800000 7FF0000000000000 123.456e789"};
+  constexpr const char *lines[6] = {
+      "3C00 3F800000 3FF0000000000000 1",
+      "3D00 3FA00000 3FF4000000000000 1.25",
+      "3D9A 3FB33333 3FF6666666666666 1.4",
+      "57B7 42F6E979 405EDD2F1A9FBE77 123.456",
+      "622A 44454000 4088A80000000000 789",
+      "7C00 7F800000 7FF0000000000000 123.456e789"};
 
   int32_t curFails = 0;    // Only counts actual failures, not bitdiffs.
   int32_t curBitDiffs = 0; // A bitdiff is when the expected result and actual

>From 73dd1f587f1be7c8014ec8a249f38dc6dbef0a71 Mon Sep 17 00:00:00 2001
From: bassiounix <muhammad.m.bassiouni at gmail.com>
Date: Wed, 9 Apr 2025 10:43:47 +0200
Subject: [PATCH 22/22] apply suggested changes

---
 .../__support/str_to_float_comparison_test.cpp  | 17 ++++++++---------
 1 file changed, 8 insertions(+), 9 deletions(-)

diff --git a/libc/test/src/__support/str_to_float_comparison_test.cpp b/libc/test/src/__support/str_to_float_comparison_test.cpp
index 4bd91eee1f0b3..a87d60f73cbac 100644
--- a/libc/test/src/__support/str_to_float_comparison_test.cpp
+++ b/libc/test/src/__support/str_to_float_comparison_test.cpp
@@ -69,8 +69,8 @@ static inline uint64_t fastHexToU64(const char *inStr) {
   return result;
 }
 
-static void parseLine(char *line, ParseResult &parseResult, int32_t &curFails,
-                      int32_t &curBitDiffs) {
+static void parseLine(const char *line, ParseResult &parseResult,
+                      int32_t &curFails, int32_t &curBitDiffs) {
 
   if (line[0] == '#') {
     return;
@@ -82,7 +82,7 @@ static void parseLine(char *line, ParseResult &parseResult, int32_t &curFails,
   expectedFloatRaw = fastHexToU32(line + 5);
   expectedDoubleRaw = fastHexToU64(line + 14);
 
-  char *num = line + 31;
+  const char *num = line + 31;
 
   float floatResult = LIBC_NAMESPACE::strtof(num, nullptr);
 
@@ -133,7 +133,7 @@ static void parseLine(char *line, ParseResult &parseResult, int32_t &curFails,
 }
 
 ParseStatus checkBuffer(ParseResult &parseResult) {
-  constexpr const char *lines[6] = {
+  constexpr const char *LINES[] = {
       "3C00 3F800000 3FF0000000000000 1",
       "3D00 3FA00000 3FF4000000000000 1.25",
       "3D9A 3FB33333 3FF6666666666666 1.4",
@@ -145,9 +145,8 @@ ParseStatus checkBuffer(ParseResult &parseResult) {
   int32_t curBitDiffs = 0; // A bitdiff is when the expected result and actual
   // result are off by +/- 1 bit.
 
-  for (uint8_t i = 0; i < 6; i++) {
-    auto line = const_cast<char *>(lines[i]);
-    parseLine(line, parseResult, curFails, curBitDiffs);
+  for (uint8_t i = 0; i < sizeof(LINES) / sizeof(LINES[0]); i++) {
+    parseLine(LINES[i], parseResult, curFails, curBitDiffs);
   }
 
   parseResult.totalBitDiffs += curBitDiffs;
@@ -163,7 +162,7 @@ ParseStatus checkFile(char *inputFileName, ParseResult &parseResult) {
   int32_t curFails = 0;    // Only counts actual failures, not bitdiffs.
   int32_t curBitDiffs = 0; // A bitdiff is when the expected result and actual
   // result are off by +/- 1 bit.
-  char line[100];
+  char line[1000];
 
   auto *fileHandle = LIBC_NAMESPACE::fopen(inputFileName, "r");
 
@@ -233,5 +232,5 @@ TEST(LlvmLibcStrToFloatComparisonTest, CheckFloats) {
   EXPECT_EQ(parseResult.detailedBitDiffs[1], 0u); // float high
   EXPECT_EQ(parseResult.detailedBitDiffs[2], 0u); // double low
   EXPECT_EQ(parseResult.detailedBitDiffs[3], 0u); // double high
-  EXPECT_EQ(parseResult.total, 6u);
+  LIBC_NAMESPACE::printf("Total lines: %d\n", parseResult.total);
 }



More information about the libc-commits mailing list