2020-03-08 05:10:23 +08:00
|
|
|
#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;
|
|
|
|
|
2020-04-07 00:45:45 +08:00
|
|
|
#ifndef SIMDJSON_BENCHMARK_DATA_DIR
|
|
|
|
#define SIMDJSON_BENCHMARK_DATA_DIR "jsonexamples/"
|
2020-03-08 05:10:23 +08:00
|
|
|
#endif
|
2020-04-07 00:45:45 +08:00
|
|
|
const char *TWITTER_JSON = SIMDJSON_BENCHMARK_DATA_DIR "twitter.json";
|
2020-03-08 05:10:23 +08:00
|
|
|
|
|
|
|
#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();
|
2020-03-29 02:43:41 +08:00
|
|
|
dom::parser parser(1); // 1 byte max capacity
|
2020-04-15 08:26:26 +08:00
|
|
|
auto error = parser.load(TWITTER_JSON).error();
|
2020-03-08 05:10:23 +08:00
|
|
|
ASSERT_ERROR(error, CAPACITY);
|
|
|
|
TEST_SUCCEED();
|
|
|
|
}
|
|
|
|
bool parser_load_many_capacity() {
|
|
|
|
TEST_START();
|
2020-03-29 02:43:41 +08:00
|
|
|
dom::parser parser(1); // 1 byte max capacity
|
2020-04-15 08:26:26 +08:00
|
|
|
for (auto doc : parser.load_many(TWITTER_JSON)) {
|
|
|
|
ASSERT_ERROR(doc.error(), CAPACITY);
|
2020-03-08 05:10:23 +08:00
|
|
|
TEST_SUCCEED();
|
|
|
|
}
|
|
|
|
TEST_FAIL("No documents returned");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool parser_load_nonexistent() {
|
|
|
|
TEST_START();
|
2020-03-29 02:43:41 +08:00
|
|
|
dom::parser parser;
|
2020-04-15 08:26:26 +08:00
|
|
|
auto error = parser.load(NONEXISTENT_FILE).error();
|
2020-03-08 05:10:23 +08:00
|
|
|
ASSERT_ERROR(error, IO_ERROR);
|
|
|
|
TEST_SUCCEED();
|
|
|
|
}
|
|
|
|
bool parser_load_many_nonexistent() {
|
|
|
|
TEST_START();
|
2020-03-29 02:43:41 +08:00
|
|
|
dom::parser parser;
|
2020-04-15 08:26:26 +08:00
|
|
|
for (auto doc : parser.load_many(NONEXISTENT_FILE)) {
|
|
|
|
ASSERT_ERROR(doc.error(), IO_ERROR);
|
2020-03-08 05:10:23 +08:00
|
|
|
TEST_SUCCEED();
|
|
|
|
}
|
|
|
|
TEST_FAIL("No documents returned");
|
|
|
|
}
|
|
|
|
bool padded_string_load_nonexistent() {
|
|
|
|
TEST_START();
|
2020-04-15 08:26:26 +08:00
|
|
|
auto error = padded_string::load(NONEXISTENT_FILE).error();
|
2020-03-08 05:10:23 +08:00
|
|
|
ASSERT_ERROR(error, IO_ERROR);
|
|
|
|
TEST_SUCCEED();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool parser_load_chain() {
|
|
|
|
TEST_START();
|
2020-03-29 02:43:41 +08:00
|
|
|
dom::parser parser;
|
2020-04-15 08:26:26 +08:00
|
|
|
auto error = parser.load(NONEXISTENT_FILE)["foo"].get<uint64_t>().error();
|
2020-03-08 05:10:23 +08:00
|
|
|
ASSERT_ERROR(error, IO_ERROR);
|
|
|
|
TEST_SUCCEED();
|
|
|
|
}
|
|
|
|
bool parser_load_many_chain() {
|
|
|
|
TEST_START();
|
2020-03-29 02:43:41 +08:00
|
|
|
dom::parser parser;
|
2020-03-27 04:51:38 +08:00
|
|
|
for (auto doc : parser.load_many(NONEXISTENT_FILE)) {
|
2020-04-15 08:26:26 +08:00
|
|
|
auto error = doc["foo"].get<uint64_t>().error();
|
2020-03-08 05:10:23 +08:00
|
|
|
ASSERT_ERROR(error, IO_ERROR);
|
|
|
|
TEST_SUCCEED();
|
|
|
|
}
|
|
|
|
TEST_FAIL("No documents returned");
|
|
|
|
}
|
|
|
|
bool run() {
|
|
|
|
return parser_load_capacity() && parser_load_many_capacity()
|
2020-03-26 05:43:23 +08:00
|
|
|
&& parser_load_nonexistent() && parser_load_many_nonexistent() && padded_string_load_nonexistent()
|
|
|
|
&& parser_load_chain() && parser_load_many_chain();
|
2020-03-08 05:10:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|