File: compile_time_no_validation_tests.cpp

package info (click to toggle)
simdjson 4.2.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 27,936 kB
  • sloc: cpp: 171,612; ansic: 19,122; sh: 1,126; python: 842; makefile: 47; ruby: 25; javascript: 13
file content (219 lines) | stat: -rw-r--r-- 6,323 bytes parent folder | download
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include "simdjson.h"
#include "test_ondemand.h"

using namespace simdjson;

#if SIMDJSON_SUPPORTS_CONCEPTS && SIMDJSON_STATIC_REFLECTION

// Test compile-time accessors without struct type validation
// These tests verify that the compile-time path parsing works even without
// providing a struct type for validation
namespace compile_time_no_validation_tests {

const padded_string TEST_JSON = R"({
  "name": "Alice",
  "age": 30,
  "address": {
    "city": "Boston",
    "zip": 12345
  },
  "scores": [95, 87, 92]
})"_padded;

// ============================================================================
// JSON Pointer without validation
// ============================================================================

bool test_pointer_simple_field() {
  TEST_START();
  ondemand::parser parser;
  auto doc = parser.iterate(TEST_JSON);

  std::string_view name;
  auto result = ondemand::json_path::at_pointer_compiled<"/name">(doc);
  ASSERT_SUCCESS(result.get(name));
  ASSERT_EQUAL(name, "Alice");

  TEST_SUCCEED();
}

bool test_pointer_integer_field() {
  TEST_START();
  ondemand::parser parser;
  auto doc = parser.iterate(TEST_JSON);

  int64_t age;
  auto result = ondemand::json_path::at_pointer_compiled<"/age">(doc);
  ASSERT_SUCCESS(result.get(age));
  ASSERT_EQUAL(age, 30);

  TEST_SUCCEED();
}

bool test_pointer_nested_field() {
  TEST_START();
  ondemand::parser parser;
  auto doc = parser.iterate(TEST_JSON);

  std::string_view city;
  auto result = ondemand::json_path::at_pointer_compiled<"/address/city">(doc);
  ASSERT_SUCCESS(result.get(city));
  ASSERT_EQUAL(city, "Boston");

  TEST_SUCCEED();
}

bool test_pointer_nested_integer() {
  TEST_START();
  ondemand::parser parser;
  auto doc = parser.iterate(TEST_JSON);

  int64_t zip;
  auto result = ondemand::json_path::at_pointer_compiled<"/address/zip">(doc);
  ASSERT_SUCCESS(result.get(zip));
  ASSERT_EQUAL(zip, 12345);

  TEST_SUCCEED();
}

bool test_pointer_array_access() {
  TEST_START();
  ondemand::parser parser;
  auto doc = parser.iterate(TEST_JSON);

  int64_t score;
  auto result = ondemand::json_path::at_pointer_compiled<"/scores/1">(doc);
  ASSERT_SUCCESS(result.get(score));
  ASSERT_EQUAL(score, 87);

  TEST_SUCCEED();
}

// ============================================================================
// JSON Path without validation
// ============================================================================

bool test_path_simple_field() {
  TEST_START();
  ondemand::parser parser;
  auto doc = parser.iterate(TEST_JSON);

  std::string_view name;
  auto result = ondemand::json_path::at_path_compiled<".name">(doc);
  ASSERT_SUCCESS(result.get(name));
  ASSERT_EQUAL(name, "Alice");

  TEST_SUCCEED();
}

bool test_path_nested_dot_notation() {
  TEST_START();
  ondemand::parser parser;
  auto doc = parser.iterate(TEST_JSON);

  std::string_view city;
  auto result = ondemand::json_path::at_path_compiled<".address.city">(doc);
  ASSERT_SUCCESS(result.get(city));
  ASSERT_EQUAL(city, "Boston");

  TEST_SUCCEED();
}

bool test_path_array_access() {
  TEST_START();
  ondemand::parser parser;
  auto doc = parser.iterate(TEST_JSON);

  int64_t score;
  auto result = ondemand::json_path::at_path_compiled<".scores[1]">(doc);
  ASSERT_SUCCESS(result.get(score));
  ASSERT_EQUAL(score, 87);

  TEST_SUCCEED();
}

bool test_path_bracket_notation() {
  TEST_START();
  ondemand::parser parser;
  auto doc = parser.iterate(TEST_JSON);

  int64_t age;
  auto result = ondemand::json_path::at_path_compiled<"[\"age\"]">(doc);
  ASSERT_SUCCESS(result.get(age));
  ASSERT_EQUAL(age, 30);

  TEST_SUCCEED();
}

bool test_path_mixed_notation() {
  TEST_START();
  ondemand::parser parser;
  auto doc = parser.iterate(TEST_JSON);

  int64_t zip;
  auto result = ondemand::json_path::at_path_compiled<".address[\"zip\"]">(doc);
  ASSERT_SUCCESS(result.get(zip));
  ASSERT_EQUAL(zip, 12345);

  TEST_SUCCEED();
}

bool test_path_all_bracket_notation() {
  TEST_START();
  ondemand::parser parser;
  auto doc = parser.iterate(TEST_JSON);

  std::string_view city;
  auto result = ondemand::json_path::at_path_compiled<"[\"address\"][\"city\"]">(doc);
  ASSERT_SUCCESS(result.get(city));
  ASSERT_EQUAL(city, "Boston");

  TEST_SUCCEED();
}

bool test_path_with_root_prefix() {
  TEST_START();
  ondemand::parser parser;
  auto doc = parser.iterate(TEST_JSON);

  std::string_view name;
  auto result = ondemand::json_path::at_path_compiled<"$.name">(doc);
  ASSERT_SUCCESS(result.get(name));
  ASSERT_EQUAL(name, "Alice");

  TEST_SUCCEED();
}

} // namespace compile_time_no_validation_tests

#endif // SIMDJSON_SUPPORTS_CONCEPTS && SIMDJSON_STATIC_REFLECTION

int main(int argc, char *argv[]) {
  (void)argc;
  (void)argv;
#if SIMDJSON_SUPPORTS_CONCEPTS && SIMDJSON_STATIC_REFLECTION
  std::cout << "Running compile-time accessor tests WITHOUT struct validation" << std::endl;

  // JSON Pointer tests
  if (!compile_time_no_validation_tests::test_pointer_simple_field()) { return EXIT_FAILURE; }
  if (!compile_time_no_validation_tests::test_pointer_integer_field()) { return EXIT_FAILURE; }
  if (!compile_time_no_validation_tests::test_pointer_nested_field()) { return EXIT_FAILURE; }
  if (!compile_time_no_validation_tests::test_pointer_nested_integer()) { return EXIT_FAILURE; }
  if (!compile_time_no_validation_tests::test_pointer_array_access()) { return EXIT_FAILURE; }

  // JSON Path tests
  if (!compile_time_no_validation_tests::test_path_simple_field()) { return EXIT_FAILURE; }
  if (!compile_time_no_validation_tests::test_path_nested_dot_notation()) { return EXIT_FAILURE; }
  if (!compile_time_no_validation_tests::test_path_array_access()) { return EXIT_FAILURE; }
  if (!compile_time_no_validation_tests::test_path_bracket_notation()) { return EXIT_FAILURE; }
  if (!compile_time_no_validation_tests::test_path_mixed_notation()) { return EXIT_FAILURE; }
  if (!compile_time_no_validation_tests::test_path_all_bracket_notation()) { return EXIT_FAILURE; }
  if (!compile_time_no_validation_tests::test_path_with_root_prefix()) { return EXIT_FAILURE; }

  std::cout << "All compile-time accessor tests WITHOUT validation passed!" << std::endl;
  return EXIT_SUCCESS;
#else
  std::cout << "Compile-time accessor tests require C++26 reflection support" << std::endl;
  return EXIT_SUCCESS;
#endif
}