[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
Tue Apr 1 20:04:16 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 1/3] [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 2/3] 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 3/3] 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);
}
More information about the libc-commits
mailing list