forked from simdjson/simdjson
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrortests.cpp
More file actions
115 lines (108 loc) · 3.53 KB
/
errortests.cpp
File metadata and controls
115 lines (108 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <cinttypes>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <set>
#include <string_view>
#include "simdjson.h"
using namespace simdjson;
using namespace std;
#ifndef JSON_TEST_PATH
#define JSON_TEST_PATH "jsonexamples/twitter.json"
#endif
#define TEST_START() { cout << "Running " << __func__ << " ..." << endl; }
#define ASSERT_ERROR(ACTUAL, EXPECTED) if ((ACTUAL) != (EXPECTED)) { cerr << "FAIL: Unexpected error \"" << (ACTUAL) << "\" (expected \"" << (EXPECTED) << "\")" << endl; return false; }
#define TEST_FAIL(MESSAGE) { cerr << "FAIL: " << (MESSAGE) << endl; return false; }
#define TEST_SUCCEED() { return true; }
namespace parser_load {
const char * NONEXISTENT_FILE = "this_file_does_not_exit.json";
bool parser_load_capacity() {
TEST_START();
document::parser parser(1); // 1 byte max capacity
auto [doc, error] = parser.load(JSON_TEST_PATH);
ASSERT_ERROR(error, CAPACITY);
TEST_SUCCEED();
}
bool parser_load_many_capacity() {
TEST_START();
document::parser parser(1); // 1 byte max capacity
for (auto [doc, error] : parser.load_many(JSON_TEST_PATH)) {
ASSERT_ERROR(error, CAPACITY);
TEST_SUCCEED();
}
TEST_FAIL("No documents returned");
}
bool document_load_nonexistent() {
TEST_START();
auto [doc, error] = document::load(NONEXISTENT_FILE);
ASSERT_ERROR(error, IO_ERROR);
TEST_SUCCEED();
}
bool parser_load_nonexistent() {
TEST_START();
document::parser parser;
auto [doc, error] = parser.load(NONEXISTENT_FILE);
ASSERT_ERROR(error, IO_ERROR);
TEST_SUCCEED();
}
bool parser_load_many_nonexistent() {
TEST_START();
document::parser parser;
for (auto [doc, error] : parser.load_many(NONEXISTENT_FILE)) {
ASSERT_ERROR(error, IO_ERROR);
TEST_SUCCEED();
}
TEST_FAIL("No documents returned");
}
bool padded_string_load_nonexistent() {
TEST_START();
auto [str, error] = padded_string::load(NONEXISTENT_FILE);
ASSERT_ERROR(error, IO_ERROR);
TEST_SUCCEED();
}
bool document_load_chain() {
TEST_START();
auto [val, error] = document::load(NONEXISTENT_FILE)["foo"].as_uint64_t();
ASSERT_ERROR(error, IO_ERROR);
TEST_SUCCEED();
}
bool parser_load_chain() {
TEST_START();
document::parser parser;
auto [val, error] = parser.load(NONEXISTENT_FILE)["foo"].as_uint64_t();
ASSERT_ERROR(error, IO_ERROR);
TEST_SUCCEED();
}
bool parser_load_many_chain() {
TEST_START();
document::parser parser;
for (auto doc_result : parser.load_many(NONEXISTENT_FILE)) {
auto [val, error] = doc_result["foo"].as_uint64_t();
ASSERT_ERROR(error, IO_ERROR);
TEST_SUCCEED();
}
TEST_FAIL("No documents returned");
}
bool run() {
return parser_load_capacity() && parser_load_many_capacity()
&& parser_load_nonexistent() && parser_load_many_nonexistent() && document_load_nonexistent() && padded_string_load_nonexistent()
&& document_load_chain() && parser_load_chain() && parser_load_many_chain();
}
}
int main() {
// this is put here deliberately to check that the documentation is correct (README),
// should this fail to compile, you should update the documentation:
if (simdjson::active_implementation->name() == "unsupported") {
printf("unsupported CPU\n");
}
std::cout << "Running error tests." << std::endl;
if (!parser_load::run()) {
return EXIT_FAILURE;
}
std::cout << "Error tests are ok." << std::endl;
return EXIT_SUCCESS;
}