LspCpp/third_party/utfcpp/tests/negative.cpp
raw| 1 | #include "utf8.h" |
| 2 | using namespace utf8; |
| 3 | |
| 4 | #include <string> |
| 5 | #include <iostream> |
| 6 | #include <fstream> |
| 7 | #include <algorithm> |
| 8 | using namespace std; |
| 9 | |
| 10 | const unsigned INVALID_LINES[] = { 75, 76, 83, 84, 85, 93, 102, 103, 105, 106, 107, 108, 109, 110, 114, 115, 116, 117, 124, 125, 130, 135, 140, 145, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 169, 175, 176, 177, 207, 208, 209, 210, 211, 220, 221, 222, 223, 224, 232, 233, 234, 235, 236, 247, 248, 249, 250, 251, 252, 253, 257, 258, 259, 260, 261, 262, 263, 264}; |
| 11 | const unsigned* INVALID_LINES_END = INVALID_LINES + sizeof(INVALID_LINES)/sizeof(unsigned); |
| 12 | |
| 13 | int main(int argc, char** argv) |
| 14 | { |
| 15 | string test_file_path; |
| 16 | if (argc == 2) |
| 17 | test_file_path = argv[1]; |
| 18 | else { |
| 19 | cout << "Wrong number of arguments" << endl; |
| 20 | return 1; |
| 21 | } |
| 22 | // Open the test file |
| 23 | ifstream fs8(test_file_path.c_str()); |
| 24 | if (!fs8.is_open()) { |
| 25 | cout << "Could not open " << test_file_path << endl; |
| 26 | return 1; |
| 27 | } |
| 28 | |
| 29 | // Read it line by line |
| 30 | unsigned int line_count = 0; |
| 31 | char byte; |
| 32 | while (!fs8.eof()) { |
| 33 | string line; |
| 34 | while ((byte = static_cast<char>(fs8.get())) != '\n' && !fs8.eof()) |
| 35 | line.push_back(byte); |
| 36 | |
| 37 | line_count++; |
| 38 | bool expected_valid = (find(INVALID_LINES, INVALID_LINES_END, line_count) == INVALID_LINES_END); |
| 39 | // Print out lines that contain unexpected invalid UTF-8 |
| 40 | if (!is_valid(line.begin(), line.end())) { |
| 41 | if (expected_valid) { |
| 42 | cout << "Unexpected invalid utf-8 at line " << line_count << '\n'; |
| 43 | return 1; |
| 44 | } |
| 45 | |
| 46 | // try fixing it: |
| 47 | string fixed_line; |
| 48 | replace_invalid(line.begin(), line.end(), back_inserter(fixed_line)); |
| 49 | if (!is_valid(fixed_line.begin(), fixed_line.end())) { |
| 50 | cout << "replace_invalid() resulted in an invalid utf-8 at line " << line_count << '\n'; |
| 51 | return 1; |
| 52 | } |
| 53 | } |
| 54 | else if (!expected_valid) { |
| 55 | cout << "Invalid utf-8 NOT detected at line " << line_count << '\n'; |
| 56 | return 1; |
| 57 | } |
| 58 | } |
| 59 | } |