Skip to content

Commit 3dcc188

Browse files
committed
Add more tests to cmake
1 parent 10b7556 commit 3dcc188

File tree

8 files changed

+75
-59
lines changed

8 files changed

+75
-59
lines changed

CMakeLists.txt

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ endif()
4242
option(SIMDJSON_SANITIZE "Sanitize addresses" OFF)
4343
option(SIMDJSON_GOOGLE_BENCHMARKS "compile the Google Benchmark benchmarks" OFF)
4444
option(SIMDJSON_ENABLE_THREADS "enable threaded operation" ON)
45-
option(SIMDJSON_EXCEPTIONS "Enable simdjson's exception-throwing interface" ON)
45+
option(SIMDJSON_NO_EXCEPTIONS "Disable simdjson's exception-throwing interface" OFF)
4646

4747
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/tools/cmake")
4848

@@ -58,12 +58,8 @@ add_subdirectory(src)
5858
#
5959
# Compile tools / tests / benchmarks
6060
#
61-
set (TEST_DATA_DIR "${CMAKE_CURRENT_SOURCE_DIR}/jsonchecker/")
62-
set (BENCHMARK_DATA_DIR "${CMAKE_CURRENT_SOURCE_DIR}/jsonexamples/")
63-
add_definitions(-DSIMDJSON_TEST_DATA_DIR="${TEST_DATA_DIR}")
64-
add_definitions(-DSIMDJSON_BENCHMARK_DATA_DIR="${TEST_DATA_DIR}")
65-
add_definitions(-DJSON_TEST_PATH="${BENCHMARK_DATA_DIR}/twitter.json")
66-
add_definitions(-DSIMDJSON_AMAZON_CELLPHONES_NDJSON_PATH="${PROJECT_SOURCE_DIR}/jsonexamples/amazon_cellphones.ndjson")
61+
add_definitions(-DSIMDJSON_TEST_DATA_DIR="${CMAKE_CURRENT_SOURCE_DIR}/jsonchecker/")
62+
add_definitions(-DSIMDJSON_BENCHMARK_DATA_DIR="${CMAKE_CURRENT_SOURCE_DIR}/jsonexamples/")
6763

6864
enable_testing()
6965

benchmark/bench_dom_api.cpp

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,16 @@ using namespace simdjson;
66
using namespace benchmark;
77
using namespace std;
88

9-
#ifndef SIMDJSON_TWITTER_JSON_PATH
10-
#define SIMDJSON_TWITTER_JSON_PATH "jsonexamples/twitter.json"
11-
#endif
12-
139
const padded_string EMPTY_ARRAY("[]", 2);
1410

11+
const char *TWITTER_JSON = SIMDJSON_BENCHMARK_DATA_DIR "";
12+
1513
#if SIMDJSON_EXCEPTIONS
1614

1715
static void twitter_count(State& state) {
1816
// Prints the number of results in twitter.json
1917
dom::parser parser;
20-
dom::element doc = parser.load(SIMDJSON_TWITTER_JSON_PATH);
18+
dom::element doc = parser.load(TWITTER_JSON);
2119
for (auto _ : state) {
2220
uint64_t result_count = doc["search_metadata"]["count"];
2321
if (result_count != 100) { return; }
@@ -29,7 +27,7 @@ SIMDJSON_PUSH_DISABLE_WARNINGS
2927
SIMDJSON_DISABLE_DEPRECATED_WARNING
3028
static void iterator_twitter_count(State& state) {
3129
// Prints the number of results in twitter.json
32-
padded_string json = padded_string::load(SIMDJSON_TWITTER_JSON_PATH);
30+
padded_string json = padded_string::load(TWITTER_JSON);
3331
ParsedJson pj = build_parsed_json(json);
3432
for (auto _ : state) {
3533
ParsedJson::Iterator iter(pj);
@@ -48,7 +46,7 @@ SIMDJSON_POP_DISABLE_WARNINGS
4846
static void twitter_default_profile(State& state) {
4947
// Count unique users with a default profile.
5048
dom::parser parser;
51-
dom::element doc = parser.load(SIMDJSON_TWITTER_JSON_PATH);
49+
dom::element doc = parser.load(TWITTER_JSON);
5250
for (auto _ : state) {
5351
set<string_view> default_users;
5452
for (dom::object tweet : doc["statuses"].get<dom::array>()) {
@@ -65,7 +63,7 @@ BENCHMARK(twitter_default_profile);
6563
static void twitter_image_sizes(State& state) {
6664
// Count unique image sizes
6765
dom::parser parser;
68-
dom::element doc = parser.load(SIMDJSON_TWITTER_JSON_PATH);
66+
dom::element doc = parser.load(TWITTER_JSON);
6967
for (auto _ : state) {
7068
set<tuple<uint64_t, uint64_t>> image_sizes;
7169
for (dom::object tweet : doc["statuses"].get<dom::array>()) {
@@ -88,7 +86,7 @@ BENCHMARK(twitter_image_sizes);
8886
static void error_code_twitter_count(State& state) noexcept {
8987
// Prints the number of results in twitter.json
9088
dom::parser parser;
91-
dom::element doc = parser.load(SIMDJSON_TWITTER_JSON_PATH);
89+
dom::element doc = parser.load(TWITTER_JSON);
9290
for (auto _ : state) {
9391
auto [value, error] = doc["search_metadata"]["count"].get<uint64_t>();
9492
if (error) { return; }
@@ -100,7 +98,7 @@ BENCHMARK(error_code_twitter_count);
10098
static void error_code_twitter_default_profile(State& state) noexcept {
10199
// Count unique users with a default profile.
102100
dom::parser parser;
103-
dom::element doc = parser.load(SIMDJSON_TWITTER_JSON_PATH);
101+
dom::element doc = parser.load(TWITTER_JSON);
104102
for (auto _ : state) {
105103
set<string_view> default_users;
106104

@@ -127,7 +125,7 @@ SIMDJSON_PUSH_DISABLE_WARNINGS
127125
SIMDJSON_DISABLE_DEPRECATED_WARNING
128126
static void iterator_twitter_default_profile(State& state) {
129127
// Count unique users with a default profile.
130-
padded_string json = padded_string::load(SIMDJSON_TWITTER_JSON_PATH);
128+
padded_string json = padded_string::load(TWITTER_JSON);
131129
ParsedJson pj = build_parsed_json(json);
132130
for (auto _ : state) {
133131
set<string_view> default_users;
@@ -167,7 +165,7 @@ BENCHMARK(iterator_twitter_default_profile);
167165
static void error_code_twitter_image_sizes(State& state) noexcept {
168166
// Count unique image sizes
169167
dom::parser parser;
170-
dom::element doc = parser.load(SIMDJSON_TWITTER_JSON_PATH);
168+
dom::element doc = parser.load(TWITTER_JSON);
171169
for (auto _ : state) {
172170
set<tuple<uint64_t, uint64_t>> image_sizes;
173171
auto [statuses, error] = doc["statuses"].get<dom::array>();
@@ -196,7 +194,7 @@ SIMDJSON_PUSH_DISABLE_WARNINGS
196194
SIMDJSON_DISABLE_DEPRECATED_WARNING
197195
static void iterator_twitter_image_sizes(State& state) {
198196
// Count unique image sizes
199-
padded_string json = padded_string::load(SIMDJSON_TWITTER_JSON_PATH);
197+
padded_string json = padded_string::load(TWITTER_JSON);
200198
ParsedJson pj = build_parsed_json(json);
201199
for (auto _ : state) {
202200
set<tuple<uint64_t, uint64_t>> image_sizes;
@@ -254,7 +252,7 @@ BENCHMARK(iterator_twitter_image_sizes);
254252

255253
static void print_json(State& state) noexcept {
256254
// Prints the number of results in twitter.json
257-
padded_string json = get_corpus(SIMDJSON_TWITTER_JSON_PATH);
255+
padded_string json = get_corpus(TWITTER_JSON);
258256
dom::parser parser;
259257
if (int error = json_parse(json, parser); error != SUCCESS) { cerr << error_message(error) << endl; return; }
260258
for (auto _ : state) {

src/CMakeLists.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,13 @@ else()
99
endif()
1010

1111
target_sources(simdjson PRIVATE simdjson.cpp)
12-
target_include_directories(simdjson PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>)
1312
target_include_directories(simdjson PRIVATE .)
13+
target_include_directories(simdjson PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>)
14+
15+
if(SIMDJSON_NO_EXCEPTIONS)
16+
message(STATUS "simdjson exception interface turned off. Code that does not check error codes will not compile.")
17+
target_compile_definitions(simdjson PUBLIC SIMDJSON_EXCEPTIONS=0)
18+
endif()
1419

1520
if(${CMAKE_C_COMPILER_ID} MATCHES "Intel") # icc / icpc
1621
# prevent shared libraries from depending on Intel provided libraries

tests/CMakeLists.txt

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,42 @@
11
# Helper so we don't have to repeat ourselves so much
2-
function(add_cpp_test TEST_NAME)
3-
add_executable(${TEST_NAME} ${TEST_NAME}.cpp)
2+
function(add_cpp_test TEST_NAME TEST_FILE)
3+
add_executable(${TEST_NAME} ${TEST_FILE})
44
add_test(${TEST_NAME} ${TEST_NAME})
55
endfunction(add_cpp_test)
66

7+
# Sets a target to only build when you run the test, and expect failure
8+
function(add_compile_fail_test TEST_NAME)
9+
set_target_properties(${TEST_NAME} PROPERTIES
10+
EXCLUDE_FROM_ALL TRUE
11+
EXCLUDE_FROM_DEFAULT_BUILD TRUE)
12+
add_test(NAME ${TEST_NAME}
13+
COMMAND ${CMAKE_COMMAND} --build . --target ${TEST_NAME} --config $<CONFIGURATION>
14+
WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
15+
set_tests_properties(${TEST_NAME} PROPERTIES WILL_FAIL TRUE)
16+
endfunction(add_compile_fail_test)
717

818
link_libraries(simdjson)
919

10-
add_cpp_test(basictests)
11-
add_cpp_test(errortests)
12-
add_cpp_test(jsoncheck)
13-
add_cpp_test(parse_many_test)
14-
add_cpp_test(pointercheck)
15-
add_cpp_test(integer_tests)
20+
# add_executable(allparserscheckfile allparserscheckfile.cpp)
21+
add_cpp_test(basictests basictests.cpp)
22+
add_cpp_test(errortests errortests.cpp)
23+
add_cpp_test(integer_tests integer_tests.cpp)
24+
add_cpp_test(jsoncheck jsoncheck.cpp)
25+
# add_executable(numberparsingcheck numberparsingcheck.cpp)
26+
add_cpp_test(parse_many_test parse_many_test.cpp)
27+
add_cpp_test(pointercheck pointercheck.cpp)
28+
# add_cpp_test(stringparsingcheck stringparsingcheck.cpp)
29+
30+
# Compile-only tests
31+
add_executable(readme_examples readme_examples.cpp)
32+
add_executable(readme_examples_noexceptions readme_examples_noexceptions.cpp)
33+
target_compile_options(readme_examples_noexceptions PRIVATE -fno-exceptions)
34+
35+
# Test that readme_examples does NOT compile when SIMDJSON_EXCEPTIONS=0 (i.e. test that the define
36+
# works even if exceptions are on)
37+
add_executable(readme_examples_will_fail_with_exceptions_off readme_examples.cpp)
38+
target_compile_definitions(readme_examples_will_fail_with_exceptions_off PRIVATE SIMDJSON_EXCEPTIONS=0)
39+
add_compile_fail_test(readme_examples_will_fail_with_exceptions_off)
1640

1741
if(MSVC)
1842
include_directories($<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/windows>)

tests/basictests.cpp

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,11 @@
1717

1818
#include "simdjson.h"
1919

20-
#ifndef SIMDJSON_TWITTER_JSON_PATH
21-
#define SIMDJSON_TWITTER_JSON_PATH "jsonexamples/twitter.json"
22-
#endif
23-
#ifndef SIMDJSON_AMAZON_CELLPHONES_NDJSON_PATH
24-
#define SIMDJSON_AMAZON_CELLPHONES_NDJSON_PATH "jsonexamples/amazon_cellphones.ndjson"
20+
#ifndef SIMDJSON_BENCHMARK_DATA_DIR
21+
#define SIMDJSON_BENCHMARK_DATA_DIR "jsonexamples/"
2522
#endif
23+
const char *TWITTER_JSON = SIMDJSON_BENCHMARK_DATA_DIR "twitter.json";
24+
const char *AMAZON_CELLPHONES_NDJSON = SIMDJSON_BENCHMARK_DATA_DIR "amazon_cellphones.ndjson";
2625

2726
#define ASSERT_EQUAL(ACTUAL, EXPECTED) if ((ACTUAL) != (EXPECTED)) { std::cerr << "Expected " << #ACTUAL << " to be " << (EXPECTED) << ", got " << (ACTUAL) << " instead!" << std::endl; return false; }
2827
#define ASSERT_TRUE(ACTUAL) ASSERT_EQUAL(ACTUAL, true)
@@ -493,18 +492,18 @@ namespace parse_api_tests {
493492
// }
494493

495494
bool parser_load() {
496-
std::cout << "Running " << __func__ << std::endl;
495+
std::cout << "Running " << __func__ << " on " << TWITTER_JSON << std::endl;
497496
dom::parser parser;
498-
auto [doc, error] = parser.load(SIMDJSON_TWITTER_JSON_PATH);
497+
auto [doc, error] = parser.load(TWITTER_JSON);
499498
if (error) { cerr << error << endl; return false; }
500499
if (!doc.is<dom::object>()) { cerr << "Document did not parse as an object" << endl; return false; }
501500
return true;
502501
}
503502
bool parser_load_many() {
504-
std::cout << "Running " << __func__ << std::endl;
503+
std::cout << "Running " << __func__ << " on " << AMAZON_CELLPHONES_NDJSON << std::endl;
505504
dom::parser parser;
506505
int count = 0;
507-
for (auto [doc, error] : parser.load_many(SIMDJSON_AMAZON_CELLPHONES_NDJSON_PATH)) {
506+
for (auto [doc, error] : parser.load_many(AMAZON_CELLPHONES_NDJSON)) {
508507
if (error) { cerr << error << endl; return false; }
509508
if (!doc.is<dom::array>()) { cerr << "Document did not parse as an array" << endl; return false; }
510509
count++;
@@ -537,15 +536,15 @@ namespace parse_api_tests {
537536
bool parser_load_exception() {
538537
std::cout << "Running " << __func__ << std::endl;
539538
dom::parser parser;
540-
const element doc = parser.load(SIMDJSON_TWITTER_JSON_PATH);
539+
const element doc = parser.load(TWITTER_JSON);
541540
if (!doc.is<dom::object>()) { cerr << "Document did not parse as an object" << endl; return false; }
542541
return true;
543542
}
544543
bool parser_load_many_exception() {
545544
std::cout << "Running " << __func__ << std::endl;
546545
dom::parser parser;
547546
int count = 0;
548-
for (const element doc : parser.load_many(SIMDJSON_AMAZON_CELLPHONES_NDJSON_PATH)) {
547+
for (const element doc : parser.load_many(AMAZON_CELLPHONES_NDJSON)) {
549548
if (!doc.is<dom::array>()) { cerr << "Document did not parse as an array" << endl; return false; }
550549
count++;
551550
}
@@ -869,7 +868,7 @@ namespace dom_api_tests {
869868
std::cout << "Running " << __func__ << std::endl;
870869
// Prints the number of results in twitter.json
871870
dom::parser parser;
872-
auto [result_count, error] = parser.load(SIMDJSON_TWITTER_JSON_PATH)["search_metadata"]["count"].get<uint64_t>();
871+
auto [result_count, error] = parser.load(TWITTER_JSON)["search_metadata"]["count"].get<uint64_t>();
873872
if (error) { cerr << "Error: " << error << endl; return false; }
874873
if (result_count != 100) { cerr << "Expected twitter.json[metadata_count][count] = 100, got " << result_count << endl; return false; }
875874
return true;
@@ -880,7 +879,7 @@ namespace dom_api_tests {
880879
// Print users with a default profile.
881880
set<string_view> default_users;
882881
dom::parser parser;
883-
auto [tweets, error] = parser.load(SIMDJSON_TWITTER_JSON_PATH)["statuses"].get<dom::array>();
882+
auto [tweets, error] = parser.load(TWITTER_JSON)["statuses"].get<dom::array>();
884883
if (error) { cerr << "Error: " << error << endl; return false; }
885884
for (auto tweet : tweets) {
886885
object user;
@@ -905,7 +904,7 @@ namespace dom_api_tests {
905904
// Print image names and sizes
906905
set<pair<uint64_t, uint64_t>> image_sizes;
907906
dom::parser parser;
908-
auto [tweets, error] = parser.load(SIMDJSON_TWITTER_JSON_PATH)["statuses"].get<dom::array>();
907+
auto [tweets, error] = parser.load(TWITTER_JSON)["statuses"].get<dom::array>();
909908
if (error) { cerr << "Error: " << error << endl; return false; }
910909
for (auto tweet : tweets) {
911910
auto [media, not_found] = tweet["entities"]["media"].get<dom::array>();
@@ -1043,7 +1042,7 @@ namespace dom_api_tests {
10431042
std::cout << "Running " << __func__ << std::endl;
10441043
// Prints the number of results in twitter.json
10451044
dom::parser parser;
1046-
element doc = parser.load(SIMDJSON_TWITTER_JSON_PATH);
1045+
element doc = parser.load(TWITTER_JSON);
10471046
uint64_t result_count = doc["search_metadata"]["count"];
10481047
if (result_count != 100) { cerr << "Expected twitter.json[metadata_count][count] = 100, got " << result_count << endl; return false; }
10491048
return true;
@@ -1054,7 +1053,7 @@ namespace dom_api_tests {
10541053
// Print users with a default profile.
10551054
set<string_view> default_users;
10561055
dom::parser parser;
1057-
element doc = parser.load(SIMDJSON_TWITTER_JSON_PATH);
1056+
element doc = parser.load(TWITTER_JSON);
10581057
for (object tweet : doc["statuses"].get<dom::array>()) {
10591058
object user = tweet["user"];
10601059
if (user["default_profile"]) {
@@ -1070,7 +1069,7 @@ namespace dom_api_tests {
10701069
// Print image names and sizes
10711070
set<pair<uint64_t, uint64_t>> image_sizes;
10721071
dom::parser parser;
1073-
element doc = parser.load(SIMDJSON_TWITTER_JSON_PATH);
1072+
element doc = parser.load(TWITTER_JSON);
10741073
for (object tweet : doc["statuses"].get<dom::array>()) {
10751074
auto [media, not_found] = tweet["entities"]["media"];
10761075
if (!not_found) {

tests/errortests.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@
1414
using namespace simdjson;
1515
using namespace std;
1616

17-
#ifndef SIMDJSON_TWITTER_JSON_PATH
18-
#define SIMDJSON_TWITTER_JSON_PATH "jsonexamples/twitter.json"
17+
#ifndef SIMDJSON_BENCHMARK_DATA_DIR
18+
#define SIMDJSON_BENCHMARK_DATA_DIR "jsonexamples/"
1919
#endif
20+
const char *TWITTER_JSON = SIMDJSON_BENCHMARK_DATA_DIR "twitter.json";
2021

2122
#define TEST_START() { cout << "Running " << __func__ << " ..." << endl; }
2223
#define ASSERT_ERROR(ACTUAL, EXPECTED) if ((ACTUAL) != (EXPECTED)) { cerr << "FAIL: Unexpected error \"" << (ACTUAL) << "\" (expected \"" << (EXPECTED) << "\")" << endl; return false; }
@@ -27,14 +28,14 @@ namespace parser_load {
2728
bool parser_load_capacity() {
2829
TEST_START();
2930
dom::parser parser(1); // 1 byte max capacity
30-
auto [doc, error] = parser.load(SIMDJSON_TWITTER_JSON_PATH);
31+
auto [doc, error] = parser.load(TWITTER_JSON);
3132
ASSERT_ERROR(error, CAPACITY);
3233
TEST_SUCCEED();
3334
}
3435
bool parser_load_many_capacity() {
3536
TEST_START();
3637
dom::parser parser(1); // 1 byte max capacity
37-
for (auto [doc, error] : parser.load_many(SIMDJSON_TWITTER_JSON_PATH)) {
38+
for (auto [doc, error] : parser.load_many(TWITTER_JSON)) {
3839
ASSERT_ERROR(error, CAPACITY);
3940
TEST_SUCCEED();
4041
}

tests/singleheadertest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
using namespace simdjson;
66

77
int main() {
8-
const char *filename = SIMDJSON_TWITTER_JSON_PATH;
8+
const char *filename = SIMDJSON_BENCHMARK_DATA_DIR "/twitter.json";
99
padded_string p = get_corpus(filename);
1010
dom::parser parser;
1111
auto [doc, error] = parser.parse(p);

tools/cmake/FindOptions.cmake

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,6 @@ else ()
1818
set (X64 FALSE)
1919
endif ()
2020

21-
if(SIMDJSON_EXCEPTIONS)
22-
set(OPT_FLAGS "${OPT_FLAGS} -DSIMDJSON_EXCEPTIONS=1")
23-
else()
24-
message(STATUS "simdjson exception interface turned off. Code that does not check error codes will not compile.")
25-
set(OPT_FLAGS "${OPT_FLAGS} -DSIMDJSON_EXCEPTIONS=0")
26-
endif()
27-
2821
if(MSVC)
2922
set(CXXSTD_FLAGS "/std:c++17")
3023
else()

0 commit comments

Comments
 (0)