Skip to content

Commit b645618

Browse files
committed
[libc][test] make test independent of C++ headers
1 parent 0106e5a commit b645618

File tree

1 file changed

+32
-33
lines changed

1 file changed

+32
-33
lines changed

libc/test/src/__support/str_to_float_comparison_test.cpp

+32-33
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,11 @@
88

99
// #include "src/__support/str_float_conv_utils.h"
1010

11-
#include <stdlib.h> // For string to float functions
12-
1311
// #include "src/__support/FPUtil/FPBits.h"
1412

15-
#include <cstdint>
16-
#include <fstream>
17-
#include <iostream>
18-
#include <string>
13+
#include <stdint.h>
14+
#include <stdio.h>
15+
#include <stdlib.h>
1916

2017
// The intent of this test is to read in files in the format used in this test
2118
// dataset: https://github.com/nigeltao/parse-number-fxx-test-data
@@ -59,30 +56,31 @@ int checkFile(char *inputFileName, int *totalFails, int *totalBitDiffs,
5956
int32_t curFails = 0; // Only counts actual failures, not bitdiffs.
6057
int32_t curBitDiffs = 0; // A bitdiff is when the expected result and actual
6158
// result are off by +/- 1 bit.
62-
std::string line;
63-
std::string num;
59+
char line[100];
60+
char num[100];
6461

65-
std::ifstream fileStream(inputFileName, std::ifstream::in);
62+
auto *fileHandle = fopen(inputFileName, "r");
6663

67-
if (!fileStream.is_open()) {
68-
std::cout << "file '" << inputFileName << "' failed to open. Exiting.\n";
64+
if (!fileHandle) {
65+
printf("file '%s' failed to open. Exiting.\n", inputFileName);
6966
return 1;
7067
}
71-
while (getline(fileStream, line)) {
68+
69+
while (fgets(line, sizeof(line), fileHandle)) {
7270
if (line[0] == '#') {
7371
continue;
7472
}
7573
*total = *total + 1;
7674
uint32_t expectedFloatRaw;
7775
uint64_t expectedDoubleRaw;
7876

79-
expectedFloatRaw = fastHexToU32(line.c_str() + 5);
80-
expectedDoubleRaw = fastHexToU64(line.c_str() + 14);
81-
num = line.substr(31);
77+
expectedFloatRaw = fastHexToU32(line + 5);
78+
expectedDoubleRaw = fastHexToU64(line + 14);
79+
sscanf(line + 31, "%s", num);
8280

83-
float floatResult = strtof(num.c_str(), nullptr);
81+
float floatResult = strtof(num, nullptr);
8482

85-
double doubleResult = strtod(num.c_str(), nullptr);
83+
double doubleResult = strtod(num, nullptr);
8684

8785
uint32_t floatRaw = *(uint32_t *)(&floatResult);
8886

@@ -101,9 +99,8 @@ int checkFile(char *inputFileName, int *totalFails, int *totalBitDiffs,
10199
curFails++;
102100
}
103101
if (curFails + curBitDiffs < 10) {
104-
std::cout << "Float fail for '" << num << "'. Expected " << std::hex
105-
<< expectedFloatRaw << " but got " << floatRaw << "\n"
106-
<< std::dec;
102+
printf("Float fail for '%s'. Expected %x but got %x\n", num,
103+
expectedFloatRaw, floatRaw);
107104
}
108105
}
109106

@@ -120,14 +117,13 @@ int checkFile(char *inputFileName, int *totalFails, int *totalBitDiffs,
120117
curFails++;
121118
}
122119
if (curFails + curBitDiffs < 10) {
123-
std::cout << "Double fail for '" << num << "'. Expected " << std::hex
124-
<< expectedDoubleRaw << " but got " << doubleRaw << "\n"
125-
<< std::dec;
120+
printf("Double fail for '%s'. Expected %lx but got %lx\n", num,
121+
expectedDoubleRaw, doubleRaw);
126122
}
127123
}
128124
}
129125

130-
fileStream.close();
126+
fclose(fileHandle);
131127

132128
*totalBitDiffs += curBitDiffs;
133129
*totalFails += curFails;
@@ -151,7 +147,7 @@ int main(int argc, char *argv[]) {
151147

152148
int total = 0;
153149
for (int i = 1; i < argc; i++) {
154-
std::cout << "Starting file " << argv[i] << "\n";
150+
printf("Starting file %s\n", argv[i]);
155151
int curResult =
156152
checkFile(argv[i], &fails, &bitdiffs, detailedBitDiffs, &total);
157153
if (curResult == 1) {
@@ -161,13 +157,16 @@ int main(int argc, char *argv[]) {
161157
result = 2;
162158
}
163159
}
164-
std::cout << "Results:\n"
165-
<< "Total significant failed conversions: " << fails << "\n"
166-
<< "Total conversions off by +/- 1 bit: " << bitdiffs << "\n"
167-
<< "\t" << detailedBitDiffs[0] << "\tfloat low\n"
168-
<< "\t" << detailedBitDiffs[1] << "\tfloat high\n"
169-
<< "\t" << detailedBitDiffs[2] << "\tdouble low\n"
170-
<< "\t" << detailedBitDiffs[3] << "\tdouble high\n"
171-
<< "Total lines: " << total << "\n";
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);
170+
172171
return result;
173172
}

0 commit comments

Comments
 (0)