23 #include <system_error>
27 return (C ==
'+' || C ==
'-');
35 case 'E':
return true;
36 default:
return false;
42 case '0':
case '1':
case '2':
case '3':
case '4':
43 case '5':
case '6':
case '7':
case '8':
case '9':
44 case '.':
return true;
49 static const char *
BackupNumber(
const char *Pos,
const char *FirstChar) {
54 bool HasPeriod =
false;
81 const char *F1End,
const char *F2End,
82 double AbsTolerance,
double RelTolerance,
83 std::string *ErrorMsg) {
84 const char *F1NumEnd, *F2NumEnd;
85 double V1 = 0.0,
V2 = 0.0;
89 while (isspace(static_cast<unsigned char>(*F1P)) && F1P != F1End)
91 while (isspace(static_cast<unsigned char>(*F2P)) && F2P != F2End)
103 V1 = strtod(F1P, const_cast<char**>(&F1NumEnd));
104 V2 = strtod(F2P, const_cast<char**>(&F2NumEnd));
106 if (*F1NumEnd ==
'D' || *F1NumEnd ==
'd') {
110 StrTmp[
static_cast<unsigned>(F1NumEnd-F1P)] =
'e';
112 V1 = strtod(&StrTmp[0], const_cast<char**>(&F1NumEnd));
113 F1NumEnd = F1P + (F1NumEnd-&StrTmp[0]);
116 if (*F2NumEnd ==
'D' || *F2NumEnd ==
'd') {
120 StrTmp[
static_cast<unsigned>(F2NumEnd-F2P)] =
'e';
122 V2 = strtod(&StrTmp[0], const_cast<char**>(&F2NumEnd));
123 F2NumEnd = F2P + (F2NumEnd-&StrTmp[0]);
127 if (F1NumEnd == F1P || F2NumEnd == F2P) {
129 *ErrorMsg =
"FP Comparison failed, not a numeric difference between '";
131 *ErrorMsg +=
"' and '";
148 if (Diff > RelTolerance) {
151 <<
"Compared: " << V1 <<
" and " <<
V2 <<
'\n'
152 <<
"abs. diff = " <<
std::abs(V1-
V2) <<
" rel.diff = " << Diff <<
'\n'
153 <<
"Out of tolerance: rel/abs: " << RelTolerance <<
'/'
161 F1P = F1NumEnd; F2P = F2NumEnd;
175 double AbsTol,
double RelTol,
176 std::string *
Error) {
180 if (std::error_code EC = F1OrErr.
getError()) {
182 *Error = EC.message();
188 if (std::error_code EC = F2OrErr.
getError()) {
190 *Error = EC.message();
200 const char *F1P = File1Start;
201 const char *F2P = File2Start;
206 if (A_size == B_size &&
207 std::memcmp(File1Start, File2Start, A_size) == 0)
211 if (AbsTol == 0 && RelTol == 0) {
213 *Error =
"Files differ without tolerance allowance";
217 bool CompareFailed =
false;
220 while (F1P < File1End && F2P < File2End && *F1P == *F2P)
223 if (F1P >= File1End || F2P >= File2End)
break;
233 if (
CompareNumbers(F1P, F2P, File1End, File2End, AbsTol, RelTol, Error)) {
234 CompareFailed =
true;
241 bool F1AtEnd = F1P >= File1End;
242 bool F2AtEnd = F2P >= File2End;
243 if (!CompareFailed && (!F1AtEnd || !F2AtEnd)) {
252 if (
CompareNumbers(F1P, F2P, File1End, File2End, AbsTol, RelTol, Error))
253 CompareFailed =
true;
256 if (F1P < File1End || F2P < File2End)
257 CompareFailed =
true;
260 return CompareFailed;
std::error_code getError() const
Represents either an error or a value T.
const char * getBufferStart() const
static const char * BackupNumber(const char *Pos, const char *FirstChar)
static const char * EndOfNumber(const char *Pos)
EndOfNumber - Return the first character that is not part of the specified number.
int DiffFilesWithTolerance(StringRef FileA, StringRef FileB, double AbsTol, double RelTol, std::string *Error=nullptr)
DiffFilesWithTolerance - Compare the two files specified, returning 0 if the files match...
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
static bool CompareNumbers(const char *&F1P, const char *&F2P, const char *F1End, const char *F2End, double AbsTolerance, double RelTolerance, std::string *ErrorMsg)
CompareNumbers - compare two numbers, returning true if they are different.
static bool isNumberChar(char C)
static bool isSignedChar(char C)
This interface provides simple read-only access to a block of memory, and provides simple methods for...
size_t getBufferSize() const
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFile(const Twine &Filename, int64_t FileSize=-1, bool RequiresNullTerminator=true, bool IsVolatileSize=false)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful, otherwise returning null.
APFloat abs(APFloat X)
Returns the absolute value of the argument.
const char * getBufferEnd() const
A raw_ostream that writes to an std::string.
StringRef - Represent a constant reference to a string, i.e.
static bool isExponentChar(char C)