Skip to content

Commit 70ecf01

Browse files
committed
remove executable and convert to unit test
1 parent b645618 commit 70ecf01

File tree

2 files changed

+54
-64
lines changed

2 files changed

+54
-64
lines changed

libc/test/src/__support/CMakeLists.txt

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -249,31 +249,22 @@ add_libc_test(
249249
libc.src.__support.memory_size
250250
)
251251

252-
# FIXME: We shouldn't have regular executables created because we could be
253-
# cross-compiling the tests and running through an emulator.
254252
if(NOT LIBC_TARGET_OS_IS_GPU)
255-
add_executable(
256-
libc_str_to_float_comparison_test
257-
str_to_float_comparison_test.cpp
258-
)
259-
260-
target_link_libraries(libc_str_to_float_comparison_test
261-
PRIVATE
262-
"${LIBC_TARGET}"
263-
)
264-
265-
add_executable(
266-
libc_system_str_to_float_comparison_test
267-
str_to_float_comparison_test.cpp
253+
add_libc_test(
254+
str_to_float_comparison_test
255+
SUITE
256+
libc-support-tests
257+
SRCS
258+
str_to_float_comparison_test.cpp
259+
DEPENDS
260+
libc.src.stdio.printf
261+
libc.src.stdio.fopen
262+
libc.src.stdio.fclose
263+
libc.src.stdio.fgets
264+
libc.src.stdlib.strtof
265+
libc.src.stdlib.strtod
266+
NO_RUN_POSTBUILD
268267
)
269-
270-
set(float_test_file ${CMAKE_CURRENT_SOURCE_DIR}/str_to_float_comparison_data.txt)
271-
272-
add_custom_command(TARGET libc_str_to_float_comparison_test
273-
POST_BUILD
274-
COMMAND ${CMAKE_CROSSCOMPILING_EMULATOR} $<TARGET_FILE:libc_str_to_float_comparison_test> ${float_test_file}
275-
COMMENT "Test the strtof and strtod implementations against precomputed results."
276-
VERBATIM)
277268
endif()
278269

279270
add_subdirectory(CPP)

libc/test/src/__support/str_to_float_comparison_test.cpp

Lines changed: 40 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
// #include "src/__support/str_float_conv_utils.h"
10-
11-
// #include "src/__support/FPUtil/FPBits.h"
12-
9+
#include "src/stdio/fclose.h"
10+
#include "src/stdio/fgets.h"
11+
#include "src/stdio/fopen.h"
12+
#include "src/stdio/printf.h"
13+
#include "src/stdlib/strtod.h"
14+
#include "src/stdlib/strtof.h"
15+
#include "test/UnitTest/Test.h"
1316
#include <stdint.h>
14-
#include <stdio.h>
15-
#include <stdlib.h>
1617

1718
// The intent of this test is to read in files in the format used in this test
1819
// dataset: https://github.com/nigeltao/parse-number-fxx-test-data
@@ -56,17 +57,18 @@ int checkFile(char *inputFileName, int *totalFails, int *totalBitDiffs,
5657
int32_t curFails = 0; // Only counts actual failures, not bitdiffs.
5758
int32_t curBitDiffs = 0; // A bitdiff is when the expected result and actual
5859
// result are off by +/- 1 bit.
59-
char line[100];
60-
char num[100];
60+
char *line = nullptr;
61+
char *num = nullptr;
6162

62-
auto *fileHandle = fopen(inputFileName, "r");
63+
auto *fileHandle = LIBC_NAMESPACE::fopen(inputFileName, "r");
6364

6465
if (!fileHandle) {
65-
printf("file '%s' failed to open. Exiting.\n", inputFileName);
66+
LIBC_NAMESPACE::printf("file '%s' failed to open. Exiting.\n",
67+
inputFileName);
6668
return 1;
6769
}
6870

69-
while (fgets(line, sizeof(line), fileHandle)) {
71+
while (LIBC_NAMESPACE::fgets(line, 100, fileHandle)) {
7072
if (line[0] == '#') {
7173
continue;
7274
}
@@ -76,11 +78,11 @@ int checkFile(char *inputFileName, int *totalFails, int *totalBitDiffs,
7678

7779
expectedFloatRaw = fastHexToU32(line + 5);
7880
expectedDoubleRaw = fastHexToU64(line + 14);
79-
sscanf(line + 31, "%s", num);
81+
num = line + 31;
8082

81-
float floatResult = strtof(num, nullptr);
83+
float floatResult = LIBC_NAMESPACE::strtof(num, nullptr);
8284

83-
double doubleResult = strtod(num, nullptr);
85+
double doubleResult = LIBC_NAMESPACE::strtod(num, nullptr);
8486

8587
uint32_t floatRaw = *(uint32_t *)(&floatResult);
8688

@@ -99,8 +101,8 @@ int checkFile(char *inputFileName, int *totalFails, int *totalBitDiffs,
99101
curFails++;
100102
}
101103
if (curFails + curBitDiffs < 10) {
102-
printf("Float fail for '%s'. Expected %x but got %x\n", num,
103-
expectedFloatRaw, floatRaw);
104+
LIBC_NAMESPACE::printf("Float fail for '%s'. Expected %x but got %x\n",
105+
num, expectedFloatRaw, floatRaw);
104106
}
105107
}
106108

@@ -117,13 +119,14 @@ int checkFile(char *inputFileName, int *totalFails, int *totalBitDiffs,
117119
curFails++;
118120
}
119121
if (curFails + curBitDiffs < 10) {
120-
printf("Double fail for '%s'. Expected %lx but got %lx\n", num,
121-
expectedDoubleRaw, doubleRaw);
122+
LIBC_NAMESPACE::printf(
123+
"Double fail for '%s'. Expected %lx but got %lx\n", num,
124+
expectedDoubleRaw, doubleRaw);
122125
}
123126
}
124127
}
125128

126-
fclose(fileHandle);
129+
LIBC_NAMESPACE::fclose(fileHandle);
127130

128131
*totalBitDiffs += curBitDiffs;
129132
*totalFails += curFails;
@@ -134,7 +137,7 @@ int checkFile(char *inputFileName, int *totalFails, int *totalBitDiffs,
134137
return 0;
135138
}
136139

137-
int main(int argc, char *argv[]) {
140+
TEST(LlvmLibcStrToFloatComparisonTest, CheckFile) {
138141
int result = 0;
139142
int fails = 0;
140143

@@ -146,27 +149,23 @@ int main(int argc, char *argv[]) {
146149
int detailedBitDiffs[4] = {0, 0, 0, 0};
147150

148151
int total = 0;
149-
for (int i = 1; i < argc; i++) {
150-
printf("Starting file %s\n", argv[i]);
151-
int curResult =
152-
checkFile(argv[i], &fails, &bitdiffs, detailedBitDiffs, &total);
153-
if (curResult == 1) {
154-
result = 1;
155-
break;
156-
} else if (curResult == 2) {
157-
result = 2;
158-
}
152+
153+
char filename[] = "str_to_float_comparison_data.txt";
154+
LIBC_NAMESPACE::printf("Starting file %s\n", filename);
155+
int curResult =
156+
checkFile(filename, &fails, &bitdiffs, detailedBitDiffs, &total);
157+
if (curResult == 1) {
158+
result = 1;
159+
} else if (curResult == 2) {
160+
result = 2;
159161
}
160-
printf("Results:\n"
161-
"Total significant failed conversions: %d\n"
162-
"Total conversions off by +/- 1 bit: %d\n"
163-
"\t%d\tfloat low\n"
164-
"\t%d\tfloat high\n"
165-
"\t%d\tdouble low\n"
166-
"\t%d\tdouble high\n"
167-
"Total lines: %d\n",
168-
fails, bitdiffs, detailedBitDiffs[0], detailedBitDiffs[1],
169-
detailedBitDiffs[2], detailedBitDiffs[3], total);
170162

171-
return result;
163+
EXPECT_EQ(result, 0);
164+
EXPECT_EQ(fails, 0);
165+
EXPECT_EQ(bitdiffs, 0);
166+
EXPECT_EQ(detailedBitDiffs[0], 0);
167+
EXPECT_EQ(detailedBitDiffs[1], 0);
168+
EXPECT_EQ(detailedBitDiffs[2], 0);
169+
EXPECT_EQ(detailedBitDiffs[3], 0);
170+
EXPECT_EQ(total, 0);
172171
}

0 commit comments

Comments
 (0)