6
6
//
7
7
// ===----------------------------------------------------------------------===//
8
8
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"
13
16
#include < stdint.h>
14
- #include < stdio.h>
15
- #include < stdlib.h>
16
17
17
18
// The intent of this test is to read in files in the format used in this test
18
19
// dataset: https://github.com/nigeltao/parse-number-fxx-test-data
@@ -56,17 +57,18 @@ int checkFile(char *inputFileName, int *totalFails, int *totalBitDiffs,
56
57
int32_t curFails = 0 ; // Only counts actual failures, not bitdiffs.
57
58
int32_t curBitDiffs = 0 ; // A bitdiff is when the expected result and actual
58
59
// result are off by +/- 1 bit.
59
- char line[ 100 ] ;
60
- char num[ 100 ] ;
60
+ char * line = nullptr ;
61
+ char * num = nullptr ;
61
62
62
- auto *fileHandle = fopen (inputFileName, " r" );
63
+ auto *fileHandle = LIBC_NAMESPACE:: fopen (inputFileName, " r" );
63
64
64
65
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);
66
68
return 1 ;
67
69
}
68
70
69
- while (fgets (line, sizeof (line) , fileHandle)) {
71
+ while (LIBC_NAMESPACE:: fgets (line, 100 , fileHandle)) {
70
72
if (line[0 ] == ' #' ) {
71
73
continue ;
72
74
}
@@ -76,11 +78,11 @@ int checkFile(char *inputFileName, int *totalFails, int *totalBitDiffs,
76
78
77
79
expectedFloatRaw = fastHexToU32 (line + 5 );
78
80
expectedDoubleRaw = fastHexToU64 (line + 14 );
79
- sscanf ( line + 31 , " %s " , num) ;
81
+ num = line + 31 ;
80
82
81
- float floatResult = strtof (num, nullptr );
83
+ float floatResult = LIBC_NAMESPACE:: strtof (num, nullptr );
82
84
83
- double doubleResult = strtod (num, nullptr );
85
+ double doubleResult = LIBC_NAMESPACE:: strtod (num, nullptr );
84
86
85
87
uint32_t floatRaw = *(uint32_t *)(&floatResult);
86
88
@@ -99,8 +101,8 @@ int checkFile(char *inputFileName, int *totalFails, int *totalBitDiffs,
99
101
curFails++;
100
102
}
101
103
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);
104
106
}
105
107
}
106
108
@@ -117,13 +119,14 @@ int checkFile(char *inputFileName, int *totalFails, int *totalBitDiffs,
117
119
curFails++;
118
120
}
119
121
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);
122
125
}
123
126
}
124
127
}
125
128
126
- fclose (fileHandle);
129
+ LIBC_NAMESPACE:: fclose (fileHandle);
127
130
128
131
*totalBitDiffs += curBitDiffs;
129
132
*totalFails += curFails;
@@ -134,7 +137,7 @@ int checkFile(char *inputFileName, int *totalFails, int *totalBitDiffs,
134
137
return 0 ;
135
138
}
136
139
137
- int main ( int argc, char *argv[] ) {
140
+ TEST (LlvmLibcStrToFloatComparisonTest, CheckFile ) {
138
141
int result = 0 ;
139
142
int fails = 0 ;
140
143
@@ -146,27 +149,23 @@ int main(int argc, char *argv[]) {
146
149
int detailedBitDiffs[4 ] = {0 , 0 , 0 , 0 };
147
150
148
151
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 ;
159
161
}
160
- printf (" Results:\n "
161
- " Total significant failed conversions: %d\n "
162
- " Total conversions off by +/- 1 bit: %d\n "
163
- " \t %d\t float low\n "
164
- " \t %d\t float high\n "
165
- " \t %d\t double low\n "
166
- " \t %d\t double high\n "
167
- " Total lines: %d\n " ,
168
- fails, bitdiffs, detailedBitDiffs[0 ], detailedBitDiffs[1 ],
169
- detailedBitDiffs[2 ], detailedBitDiffs[3 ], total);
170
162
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 );
172
171
}
0 commit comments