#include <fstream>
#include <iomanip>
+#include <iterator>
+#include <vector>
+#include <algorithm>
+
#include <errno.h>
#include <sys/stat.h>
#include <inttypes.h>
-#define HEX(val) hex << setfill ('0') << setw (2) << (0xff & ((uint8_t) val))
+#define HEX32(val) hex << (0xffffffff & ((uint32_t) val))
+#define HEX8(val) hex << setfill ('0') << setw (2) << (0xff & ((uint8_t) val))
/**
* @brief get the size of file
return -ERANGE;
}
- off_t idx = 0;
- char byte;
- int err = 0;
-
ios_base::fmtflags flags (cerr.flags ());
ifs.seekg (0, ios::beg);
- while (ifs.read ((char *) &byte, 1)) {
- if (byte != output_data[idx]) {
- cerr << "Hex diff at " << idx << " (0x" << HEX (byte);
- cerr << " vs. 0x" << HEX (output_data[idx]) << ")\n";
- err = -EINVAL;
- break;
- }
- idx++;
+ vector<uint8_t> output_vec (output_data, output_data + output_size);
+ vector<uint8_t> golden_vec;
+ golden_vec.reserve (length);
+ golden_vec.insert (golden_vec.begin (), istreambuf_iterator<char> (ifs),
+ istreambuf_iterator<char> ());
+
+ /* find the first mismatch */
+ int err = 0;
+ auto mispair =
+ mismatch (golden_vec.begin (), golden_vec.end (), output_vec.begin ());
+ if (mispair.first != golden_vec.end ()) {
+ uint32_t idx = distance (golden_vec.begin (), mispair.first);
+
+ cerr << "[ERROR] Output mismatch detected!\n";
+ cerr << "- Index: 0x" << HEX32 (idx);
+ cerr << ", Golden: 0x" << HEX8 (*mispair.first);
+ cerr << ", Output: 0x" << HEX8 (*mispair.second);
+ cerr << "\n\n";
+
+ err = -EINVAL;
}
+
cerr.flags (flags);
return err;