Skip to content

Commit 6eb6fae

Browse files
committed
make test support more than one test file
1 parent 70ecf01 commit 6eb6fae

File tree

2 files changed

+28
-19
lines changed

2 files changed

+28
-19
lines changed

libc/test/src/__support/CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,8 @@ if(NOT LIBC_TARGET_OS_IS_GPU)
254254
str_to_float_comparison_test
255255
SUITE
256256
libc-support-tests
257+
NO_RUN_POSTBUILD
258+
ENV "FILES=${CMAKE_CURRENT_SOURCE_DIR}/str_to_float_comparison_data.txt"
257259
SRCS
258260
str_to_float_comparison_test.cpp
259261
DEPENDS
@@ -263,7 +265,9 @@ if(NOT LIBC_TARGET_OS_IS_GPU)
263265
libc.src.stdio.fgets
264266
libc.src.stdlib.strtof
265267
libc.src.stdlib.strtod
266-
NO_RUN_POSTBUILD
268+
libc.src.stdlib.getenv
269+
libc.src.string.strtok
270+
libc.src.__support.CPP.bit
267271
)
268272
endif()
269273

libc/test/src/__support/str_to_float_comparison_test.cpp

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9+
#include "src/__support/CPP/bit.h"
910
#include "src/stdio/fclose.h"
1011
#include "src/stdio/fgets.h"
1112
#include "src/stdio/fopen.h"
1213
#include "src/stdio/printf.h"
14+
#include "src/stdlib/getenv.h"
1315
#include "src/stdlib/strtod.h"
1416
#include "src/stdlib/strtof.h"
17+
#include "src/string/strtok.h"
1518
#include "test/UnitTest/Test.h"
1619
#include <stdint.h>
1720

@@ -57,8 +60,8 @@ int checkFile(char *inputFileName, int *totalFails, int *totalBitDiffs,
5760
int32_t curFails = 0; // Only counts actual failures, not bitdiffs.
5861
int32_t curBitDiffs = 0; // A bitdiff is when the expected result and actual
5962
// result are off by +/- 1 bit.
60-
char *line = nullptr;
61-
char *num = nullptr;
63+
char line[100];
64+
char *num;
6265

6366
auto *fileHandle = LIBC_NAMESPACE::fopen(inputFileName, "r");
6467

@@ -68,7 +71,7 @@ int checkFile(char *inputFileName, int *totalFails, int *totalBitDiffs,
6871
return 1;
6972
}
7073

71-
while (LIBC_NAMESPACE::fgets(line, 100, fileHandle)) {
74+
while (LIBC_NAMESPACE::fgets(line, sizeof(line), fileHandle)) {
7275
if (line[0] == '#') {
7376
continue;
7477
}
@@ -84,9 +87,9 @@ int checkFile(char *inputFileName, int *totalFails, int *totalBitDiffs,
8487

8588
double doubleResult = LIBC_NAMESPACE::strtod(num, nullptr);
8689

87-
uint32_t floatRaw = *(uint32_t *)(&floatResult);
90+
uint32_t floatRaw = LIBC_NAMESPACE::cpp::bit_cast<uint32_t>(floatResult);
8891

89-
uint64_t doubleRaw = *(uint64_t *)(&doubleResult);
92+
uint64_t doubleRaw = LIBC_NAMESPACE::cpp::bit_cast<uint64_t>(doubleResult);
9093

9194
if (!(expectedFloatRaw == floatRaw)) {
9295
if (expectedFloatRaw == floatRaw + 1 ||
@@ -150,22 +153,24 @@ TEST(LlvmLibcStrToFloatComparisonTest, CheckFile) {
150153

151154
int total = 0;
152155

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;
156+
char *files = LIBC_NAMESPACE::getenv("FILES");
157+
for (char *file = LIBC_NAMESPACE::strtok(files, ","); file != nullptr;
158+
file = LIBC_NAMESPACE::strtok(nullptr, ",")) {
159+
int curResult =
160+
checkFile(file, &fails, &bitdiffs, detailedBitDiffs, &total);
161+
if (curResult == 1) {
162+
result = 1;
163+
} else if (curResult == 2) {
164+
result = 2;
165+
}
161166
}
162167

163168
EXPECT_EQ(result, 0);
164169
EXPECT_EQ(fails, 0);
165170
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);
171+
EXPECT_EQ(detailedBitDiffs[0], 0); // float low
172+
EXPECT_EQ(detailedBitDiffs[1], 0); // float high
173+
EXPECT_EQ(detailedBitDiffs[2], 0); // double low
174+
EXPECT_EQ(detailedBitDiffs[3], 0); // double high
175+
EXPECT_EQ(total, 6);
171176
}

0 commit comments

Comments
 (0)