1
2 #include <iostream>
3 #include <limits>
4
5 #include "simdjson.h"
6 #include "test_macros.h"
7
8 // we define our own asserts to get around NDEBUG
9 #ifndef ASSERT
10 #define ASSERT(x) \
11 { if (!(x)) { \
12 abort (); \
13 } \
14 }
15 #endif
16
17 using namespace simdjson;
18
make_json(const std::string value)19 static const std::string make_json(const std::string value) {
20 const std::string s = "{\"key\": ";
21 return s + value + "}";
22 }
23
24 // e.g. make_json(123) => {"key": 123} as string
make_json(T value)25 template <typename T> static const std::string make_json(T value) {
26 return make_json(std::to_string(value));
27 }
28
29 template <typename T>
parse_and_validate(const std::string src,T expected)30 static bool parse_and_validate(const std::string src, T expected) {
31 std::cout << "src: " << src << ", ";
32 const padded_string pstr{src};
33 simdjson::dom::parser parser;
34
35 if constexpr (std::is_same<int64_t, T>::value) {
36 int64_t actual;
37 ASSERT_SUCCESS( parser.parse(pstr)["key"].get(actual) );
38 std::cout << std::boolalpha << "test: " << (expected == actual) << std::endl;
39 ASSERT_EQUAL( expected, actual );
40 } else {
41 uint64_t actual;
42 ASSERT_SUCCESS( parser.parse(pstr)["key"].get(actual) );
43 std::cout << std::boolalpha << "test: " << (expected == actual) << std::endl;
44 ASSERT_EQUAL( expected, actual );
45 }
46 return true;
47 }
48
parse_and_check_signed(const std::string src)49 static bool parse_and_check_signed(const std::string src) {
50 std::cout << "src: " << src << ", expecting signed" << std::endl;
51 const padded_string pstr{src};
52 simdjson::dom::parser parser;
53 simdjson::dom::element value;
54 ASSERT_SUCCESS( parser.parse(pstr).get_object()["key"].get(value) );
55 ASSERT_EQUAL( value.is<int64_t>(), true );
56 return true;
57 }
58
parse_and_check_unsigned(const std::string src)59 static bool parse_and_check_unsigned(const std::string src) {
60 std::cout << "src: " << src << ", expecting signed" << std::endl;
61 const padded_string pstr{src};
62 simdjson::dom::parser parser;
63 simdjson::dom::element value;
64 ASSERT_SUCCESS( parser.parse(pstr).get_object()["key"].get(value) );
65 ASSERT_EQUAL( value.is<uint64_t>(), true );
66 return true;
67 }
68
main()69 int main() {
70 using std::numeric_limits;
71 constexpr auto int64_max = numeric_limits<int64_t>::max();
72 constexpr auto int64_min = numeric_limits<int64_t>::lowest();
73 constexpr auto uint64_max = numeric_limits<uint64_t>::max();
74 constexpr auto uint64_min = numeric_limits<uint64_t>::lowest();
75 constexpr auto int64_max_plus1 = static_cast<uint64_t>(int64_max) + 1;
76 if (true
77 && parse_and_validate(make_json(int64_max), int64_max)
78 && parse_and_validate(make_json(uint64_max), uint64_max)
79 && parse_and_validate(make_json(uint64_min), uint64_min)
80 && parse_and_validate(make_json(int64_min), int64_min)
81 && parse_and_validate(make_json(uint64_max), uint64_max)
82 && parse_and_validate(make_json(uint64_min), uint64_min)
83 && parse_and_validate(make_json(int64_max_plus1), int64_max_plus1)
84 && parse_and_check_signed(make_json(int64_max))
85 && parse_and_check_unsigned(make_json(uint64_max))
86 ) {
87 std::cout << "All ok." << std::endl;
88 return EXIT_SUCCESS;
89 }
90 return EXIT_FAILURE;
91 }
92
93