Skip to content

Commit 7194754

Browse files
committed
pack values to ParseResult struct
1 parent ced0005 commit 7194754

File tree

1 file changed

+41
-31
lines changed

1 file changed

+41
-31
lines changed

libc/test/src/__support/str_to_float_comparison_test.cpp

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@
3434
// ./libc_str_to_float_comparison_test <path/to/dataset/repo>/data/*
3535
// It will take a few seconds to run.
3636

37+
struct ParseResult {
38+
uint32_t totalFails;
39+
uint32_t totalBitDiffs;
40+
uint32_t detailedBitDiffs[4];
41+
uint32_t total;
42+
};
43+
3744
static inline uint32_t hexCharToU32(char in) {
3845
return in > '9' ? in + 10 - 'A' : in - '0';
3946
}
@@ -56,13 +63,13 @@ static inline uint64_t fastHexToU64(const char *inStr) {
5663
return result;
5764
}
5865

59-
static void parseLine(char *line, int *total, int *detailedBitDiffs,
60-
int32_t &curFails, int32_t &curBitDiffs) {
66+
static void parseLine(char *line, ParseResult &parseResult, int32_t &curFails,
67+
int32_t &curBitDiffs) {
6168

6269
if (line[0] == '#') {
6370
return;
6471
}
65-
*total = *total + 1;
72+
parseResult.total += 1;
6673
uint32_t expectedFloatRaw;
6774
uint64_t expectedDoubleRaw;
6875

@@ -83,9 +90,11 @@ static void parseLine(char *line, int *total, int *detailedBitDiffs,
8390
if (expectedFloatRaw == floatRaw + 1 || expectedFloatRaw == floatRaw - 1) {
8491
curBitDiffs++;
8592
if (expectedFloatRaw == floatRaw + 1) {
86-
detailedBitDiffs[0] = detailedBitDiffs[0] + 1; // float low
93+
parseResult.detailedBitDiffs[0] =
94+
parseResult.detailedBitDiffs[0] + 1; // float low
8795
} else {
88-
detailedBitDiffs[1] = detailedBitDiffs[1] + 1; // float high
96+
parseResult.detailedBitDiffs[1] =
97+
parseResult.detailedBitDiffs[1] + 1; // float high
8998
}
9099
} else {
91100
curFails++;
@@ -101,9 +110,11 @@ static void parseLine(char *line, int *total, int *detailedBitDiffs,
101110
expectedDoubleRaw == doubleRaw - 1) {
102111
curBitDiffs++;
103112
if (expectedDoubleRaw == doubleRaw + 1) {
104-
detailedBitDiffs[2] = detailedBitDiffs[2] + 1; // double low
113+
parseResult.detailedBitDiffs[2] =
114+
parseResult.detailedBitDiffs[2] + 1; // double low
105115
} else {
106-
detailedBitDiffs[3] = detailedBitDiffs[3] + 1; // double high
116+
parseResult.detailedBitDiffs[3] =
117+
parseResult.detailedBitDiffs[3] + 1; // double high
107118
}
108119
} else {
109120
curFails++;
@@ -115,8 +126,7 @@ static void parseLine(char *line, int *total, int *detailedBitDiffs,
115126
}
116127
}
117128

118-
int checkBuffer(int *totalFails, int *totalBitDiffs, int *detailedBitDiffs,
119-
int *total) {
129+
int checkBuffer(ParseResult &parseResult) {
120130
const char *lines[6] = {"3C00 3F800000 3FF0000000000000 1",
121131
"3D00 3FA00000 3FF4000000000000 1.25",
122132
"3D9A 3FB33333 3FF6666666666666 1.4",
@@ -130,20 +140,19 @@ int checkBuffer(int *totalFails, int *totalBitDiffs, int *detailedBitDiffs,
130140

131141
for (uint8_t i = 0; i < 6; i++) {
132142
auto line = const_cast<char *>(lines[i]);
133-
parseLine(line, total, detailedBitDiffs, curFails, curBitDiffs);
143+
parseLine(line, parseResult, curFails, curBitDiffs);
134144
}
135145

136-
*totalBitDiffs += curBitDiffs;
137-
*totalFails += curFails;
146+
parseResult.totalBitDiffs += curBitDiffs;
147+
parseResult.totalFails += curFails;
138148

139149
if (curFails > 1 || curBitDiffs > 1) {
140150
return 2;
141151
}
142152
return 0;
143153
}
144154

145-
int checkFile(char *inputFileName, int *totalFails, int *totalBitDiffs,
146-
int *detailedBitDiffs, int *total) {
155+
int checkFile(char *inputFileName, ParseResult &parseResult) {
147156
int32_t curFails = 0; // Only counts actual failures, not bitdiffs.
148157
int32_t curBitDiffs = 0; // A bitdiff is when the expected result and actual
149158
// result are off by +/- 1 bit.
@@ -158,13 +167,13 @@ int checkFile(char *inputFileName, int *totalFails, int *totalBitDiffs,
158167
}
159168

160169
while (LIBC_NAMESPACE::fgets(line, sizeof(line), fileHandle)) {
161-
parseLine(line, total, detailedBitDiffs, curFails, curBitDiffs);
170+
parseLine(line, parseResult, curFails, curBitDiffs);
162171
}
163172

164173
LIBC_NAMESPACE::fclose(fileHandle);
165174

166-
*totalBitDiffs += curBitDiffs;
167-
*totalFails += curFails;
175+
parseResult.totalBitDiffs += curBitDiffs;
176+
parseResult.totalFails += curFails;
168177

169178
if (curFails > 1 || curBitDiffs > 1) {
170179
return 2;
@@ -183,38 +192,39 @@ int updateResult(int result, int curResult) {
183192

184193
TEST(LlvmLibcStrToFloatComparisonTest, CheckFloats) {
185194
int result = 0;
186-
int fails = 0;
187195

188196
// Bitdiffs are cases where the expected result and actual result only differ
189197
// by +/- the least significant bit. They are tracked separately from larger
190198
// failures since a bitdiff is most likely the result of a rounding error, and
191199
// splitting them off makes them easier to track down.
192-
int bitdiffs = 0;
193-
int detailedBitDiffs[4] = {0, 0, 0, 0};
194200

195-
int total = 0;
201+
ParseResult parseResult = {
202+
.totalFails = 0,
203+
.totalBitDiffs = 0,
204+
.detailedBitDiffs = {0, 0, 0, 0},
205+
.total = 0,
206+
};
196207

197208
char *files = LIBC_NAMESPACE::getenv("FILES");
198209

199210
if (files == nullptr) {
200-
int curResult = checkBuffer(&fails, &bitdiffs, detailedBitDiffs, &total);
211+
int curResult = checkBuffer(parseResult);
201212
result = updateResult(result, curResult);
202213
} else {
203214
files = LIBC_NAMESPACE::strdup(files);
204215
for (char *file = LIBC_NAMESPACE::strtok(files, ","); file != nullptr;
205216
file = LIBC_NAMESPACE::strtok(nullptr, ",")) {
206-
int curResult =
207-
checkFile(file, &fails, &bitdiffs, detailedBitDiffs, &total);
217+
int curResult = checkFile(file, parseResult);
208218
result = updateResult(result, curResult);
209219
}
210220
}
211221

212222
EXPECT_EQ(result, 0);
213-
EXPECT_EQ(fails, 0);
214-
EXPECT_EQ(bitdiffs, 0);
215-
EXPECT_EQ(detailedBitDiffs[0], 0); // float low
216-
EXPECT_EQ(detailedBitDiffs[1], 0); // float high
217-
EXPECT_EQ(detailedBitDiffs[2], 0); // double low
218-
EXPECT_EQ(detailedBitDiffs[3], 0); // double high
219-
EXPECT_EQ(total, 6);
223+
EXPECT_EQ(parseResult.totalFails, 0u);
224+
EXPECT_EQ(parseResult.totalBitDiffs, 0u);
225+
EXPECT_EQ(parseResult.detailedBitDiffs[0], 0u); // float low
226+
EXPECT_EQ(parseResult.detailedBitDiffs[1], 0u); // float high
227+
EXPECT_EQ(parseResult.detailedBitDiffs[2], 0u); // double low
228+
EXPECT_EQ(parseResult.detailedBitDiffs[3], 0u); // double high
229+
EXPECT_EQ(parseResult.total, 6u);
220230
}

0 commit comments

Comments
 (0)