-
Notifications
You must be signed in to change notification settings - Fork 238
/
test_array.cxx
699 lines (578 loc) · 22.8 KB
/
test_array.cxx
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
#include <pqxx/transaction>
#include "../test_helpers.hxx"
// Test program for libpqxx array parsing.
using namespace std::literals;
namespace pqxx
{
template<>
struct nullness<array_parser::juncture> : no_null<array_parser::juncture>
{};
inline std::string to_string(pqxx::array_parser::juncture const &j)
{
using junc = pqxx::array_parser::juncture;
switch (j)
{
case junc::row_start: return "row_start";
case junc::row_end: return "row_end";
case junc::null_value: return "null_value";
case junc::string_value: return "string_value";
case junc::done: return "done";
default: return "UNKNOWN JUNCTURE: " + to_string(static_cast<int>(j));
}
}
} // namespace pqxx
namespace
{
void test_empty_arrays()
{
std::pair<pqxx::array_parser::juncture, std::string> output;
// Parsing a null pointer just immediately returns "done".
output = pqxx::array_parser(std::string_view()).get_next();
PQXX_CHECK_EQUAL(
output.first, pqxx::array_parser::juncture::done,
"get_next on null array did not return done.");
PQXX_CHECK_EQUAL(output.second, "", "Unexpected nonempty output.");
// Parsing an empty array string immediately returns "done".
output = pqxx::array_parser("").get_next();
PQXX_CHECK_EQUAL(
output.first, pqxx::array_parser::juncture::done,
"get_next on an empty array string did not return done.");
PQXX_CHECK_EQUAL(output.second, "", "Unexpected nonempty output.");
// Parsing an empty array returns "row_start", "row_end", "done".
pqxx::array_parser empty_parser("{}");
output = empty_parser.get_next();
PQXX_CHECK_EQUAL(
output.first, pqxx::array_parser::juncture::row_start,
"Empty array did not start with row_start.");
PQXX_CHECK_EQUAL(output.second, "", "Unexpected nonempty output.");
output = empty_parser.get_next();
PQXX_CHECK_EQUAL(
output.first, pqxx::array_parser::juncture::row_end,
"Empty array did not end with row_end.");
PQXX_CHECK_EQUAL(output.second, "", "Unexpected nonempty output.");
output = empty_parser.get_next();
PQXX_CHECK_EQUAL(
output.first, pqxx::array_parser::juncture::done,
"Empty array did not conclude with done.");
PQXX_CHECK_EQUAL(output.second, "", "Unexpected nonempty output.");
}
void test_array_null_value()
{
std::pair<pqxx::array_parser::juncture, std::string> output;
pqxx::array_parser containing_null("{NULL}");
output = containing_null.get_next();
PQXX_CHECK_EQUAL(
output.first, pqxx::array_parser::juncture::row_start,
"Array containing null did not start with row_start.");
PQXX_CHECK_EQUAL(output.second, "", "Unexpected nonempty output.");
output = containing_null.get_next();
PQXX_CHECK_EQUAL(
output.first, pqxx::array_parser::juncture::null_value,
"Array containing null did not return null_value.");
PQXX_CHECK_EQUAL(output.second, "", "Null value was not empty.");
output = containing_null.get_next();
PQXX_CHECK_EQUAL(
output.first, pqxx::array_parser::juncture::row_end,
"Array containing null did not end with row_end.");
PQXX_CHECK_EQUAL(output.second, "", "Unexpected nonempty output.");
output = containing_null.get_next();
PQXX_CHECK_EQUAL(
output.first, pqxx::array_parser::juncture::done,
"Array containing null did not conclude with done.");
PQXX_CHECK_EQUAL(output.second, "", "Unexpected nonempty output.");
}
void test_array_double_quoted_string()
{
std::pair<pqxx::array_parser::juncture, std::string> output;
pqxx::array_parser parser("{\"item\"}");
output = parser.get_next();
PQXX_CHECK_EQUAL(
output.first, pqxx::array_parser::juncture::row_start,
"Array did not start with row_start.");
output = parser.get_next();
PQXX_CHECK_EQUAL(
output.first, pqxx::array_parser::juncture::string_value,
"Array did not return string_value.");
PQXX_CHECK_EQUAL(output.second, "item", "Unexpected string value.");
output = parser.get_next();
PQXX_CHECK_EQUAL(
output.first, pqxx::array_parser::juncture::row_end,
"Array did not end with row_end.");
PQXX_CHECK_EQUAL(output.second, "", "Unexpected nonempty output.");
output = parser.get_next();
PQXX_CHECK_EQUAL(
output.first, pqxx::array_parser::juncture::done,
"Array did not conclude with done.");
PQXX_CHECK_EQUAL(output.second, "", "Unexpected nonempty output.");
}
void test_array_double_quoted_escaping()
{
std::pair<pqxx::array_parser::juncture, std::string> output;
pqxx::array_parser parser(R"--({"don''t\\ care"})--");
output = parser.get_next();
PQXX_CHECK_EQUAL(
output.first, pqxx::array_parser::juncture::row_start,
"Array did not start with row_start.");
output = parser.get_next();
PQXX_CHECK_EQUAL(
output.first, pqxx::array_parser::juncture::string_value,
"Array did not return string_value.");
PQXX_CHECK_EQUAL(output.second, "don''t\\ care", "Unexpected string value.");
output = parser.get_next();
PQXX_CHECK_EQUAL(
output.first, pqxx::array_parser::juncture::row_end,
"Array did not end with row_end.");
PQXX_CHECK_EQUAL(output.second, "", "Unexpected nonempty output.");
output = parser.get_next();
PQXX_CHECK_EQUAL(
output.first, pqxx::array_parser::juncture::done,
"Array did not conclude with done.");
PQXX_CHECK_EQUAL(output.second, "", "Unexpected nonempty output.");
}
// A pair of double quotes in a double-quoted string is an escaped quote.
void test_array_double_double_quoted_string()
{
std::pair<pqxx::array_parser::juncture, std::string> output;
pqxx::array_parser parser{R"--({"3"" steel"})--"};
output = parser.get_next();
PQXX_CHECK_EQUAL(
output.first, pqxx::array_parser::juncture::row_start,
"Array did not start with row_start.");
output = parser.get_next();
PQXX_CHECK_EQUAL(
output.first, pqxx::array_parser::juncture::string_value,
"Array did not return string_value.");
PQXX_CHECK_EQUAL(output.second, "3\" steel", "Unexpected string value.");
}
void test_array_unquoted_string()
{
std::pair<pqxx::array_parser::juncture, std::string> output;
pqxx::array_parser parser("{item}");
output = parser.get_next();
PQXX_CHECK_EQUAL(
output.first, pqxx::array_parser::juncture::row_start,
"Array did not start with row_start.");
output = parser.get_next();
PQXX_CHECK_EQUAL(
output.first, pqxx::array_parser::juncture::string_value,
"Array did not return string_value.");
PQXX_CHECK_EQUAL(output.second, "item", "Unexpected string value.");
output = parser.get_next();
PQXX_CHECK_EQUAL(
output.first, pqxx::array_parser::juncture::row_end,
"Array did not end with row_end.");
PQXX_CHECK_EQUAL(output.second, "", "Unexpected nonempty output.");
output = parser.get_next();
PQXX_CHECK_EQUAL(
output.first, pqxx::array_parser::juncture::done,
"Array did not conclude with done.");
PQXX_CHECK_EQUAL(output.second, "", "Unexpected nonempty output.");
}
void test_array_multiple_values()
{
std::pair<pqxx::array_parser::juncture, std::string> output;
pqxx::array_parser parser("{1,2}");
output = parser.get_next();
PQXX_CHECK_EQUAL(
output.first, pqxx::array_parser::juncture::row_start,
"Array did not start with row_start.");
output = parser.get_next();
PQXX_CHECK_EQUAL(
output.first, pqxx::array_parser::juncture::string_value,
"Array did not return string_value.");
PQXX_CHECK_EQUAL(output.second, "1", "Unexpected string value.");
output = parser.get_next();
PQXX_CHECK_EQUAL(
output.first, pqxx::array_parser::juncture::string_value,
"Array did not return string_value.");
PQXX_CHECK_EQUAL(output.second, "2", "Unexpected string value.");
output = parser.get_next();
PQXX_CHECK_EQUAL(
output.first, pqxx::array_parser::juncture::row_end,
"Array did not end with row_end.");
PQXX_CHECK_EQUAL(output.second, "", "Unexpected nonempty output.");
output = parser.get_next();
PQXX_CHECK_EQUAL(
output.first, pqxx::array_parser::juncture::done,
"Array did not conclude with done.");
PQXX_CHECK_EQUAL(output.second, "", "Unexpected nonempty output.");
}
void test_nested_array()
{
std::pair<pqxx::array_parser::juncture, std::string> output;
pqxx::array_parser parser("{{item}}");
output = parser.get_next();
PQXX_CHECK_EQUAL(
output.first, pqxx::array_parser::juncture::row_start,
"Array did not start with row_start.");
output = parser.get_next();
PQXX_CHECK_EQUAL(
output.first, pqxx::array_parser::juncture::row_start,
"Nested array did not start 2nd dimension with row_start.");
output = parser.get_next();
PQXX_CHECK_EQUAL(
output.first, pqxx::array_parser::juncture::string_value,
"Array did not return string_value.");
PQXX_CHECK_EQUAL(output.second, "item", "Unexpected string value.");
output = parser.get_next();
PQXX_CHECK_EQUAL(
output.first, pqxx::array_parser::juncture::row_end,
"Nested array did not end 2nd dimension with row_end.");
PQXX_CHECK_EQUAL(output.second, "", "Unexpected nonempty output.");
output = parser.get_next();
PQXX_CHECK_EQUAL(
output.first, pqxx::array_parser::juncture::row_end,
"Array did not end with row_end.");
PQXX_CHECK_EQUAL(output.second, "", "Unexpected nonempty output.");
output = parser.get_next();
PQXX_CHECK_EQUAL(
output.first, pqxx::array_parser::juncture::done,
"Array did not conclude with done.");
PQXX_CHECK_EQUAL(output.second, "", "Unexpected nonempty output.");
}
void test_nested_array_with_multiple_entries()
{
std::pair<pqxx::array_parser::juncture, std::string> output;
pqxx::array_parser parser("{{1,2},{3,4}}");
output = parser.get_next();
PQXX_CHECK_EQUAL(
output.first, pqxx::array_parser::juncture::row_start,
"Array did not start with row_start.");
output = parser.get_next();
PQXX_CHECK_EQUAL(
output.first, pqxx::array_parser::juncture::row_start,
"Nested array did not start 2nd dimension with row_start.");
output = parser.get_next();
PQXX_CHECK_EQUAL(
output.first, pqxx::array_parser::juncture::string_value,
"Array did not return string_value.");
PQXX_CHECK_EQUAL(output.second, "1", "Unexpected string value.");
output = parser.get_next();
PQXX_CHECK_EQUAL(
output.first, pqxx::array_parser::juncture::string_value,
"Array did not return string_value.");
PQXX_CHECK_EQUAL(output.second, "2", "Unexpected string value.");
output = parser.get_next();
PQXX_CHECK_EQUAL(
output.first, pqxx::array_parser::juncture::row_end,
"Nested array did not end 2nd dimension with row_end.");
PQXX_CHECK_EQUAL(output.second, "", "Unexpected nonempty output.");
output = parser.get_next();
PQXX_CHECK_EQUAL(
output.first, pqxx::array_parser::juncture::row_start,
"Nested array did not descend to 2nd dimension with row_start.");
output = parser.get_next();
PQXX_CHECK_EQUAL(
output.first, pqxx::array_parser::juncture::string_value,
"Array did not return string_value.");
PQXX_CHECK_EQUAL(output.second, "3", "Unexpected string value.");
output = parser.get_next();
PQXX_CHECK_EQUAL(
output.first, pqxx::array_parser::juncture::string_value,
"Array did not return string_value.");
PQXX_CHECK_EQUAL(output.second, "4", "Unexpected string value.");
output = parser.get_next();
PQXX_CHECK_EQUAL(
output.first, pqxx::array_parser::juncture::row_end,
"Nested array did not leave 2nd dimension with row_end.");
PQXX_CHECK_EQUAL(output.second, "", "Unexpected nonempty output.");
output = parser.get_next();
PQXX_CHECK_EQUAL(
output.first, pqxx::array_parser::juncture::row_end,
"Array did not end with row_end.");
PQXX_CHECK_EQUAL(output.second, "", "Unexpected nonempty output.");
output = parser.get_next();
PQXX_CHECK_EQUAL(
output.first, pqxx::array_parser::juncture::done,
"Array did not conclude with done.");
PQXX_CHECK_EQUAL(output.second, "", "Unexpected nonempty output.");
}
void test_generate_empty_array()
{
PQXX_CHECK_EQUAL(
pqxx::to_string(std::vector<int>{}), "{}",
"Basic array output is not as expected.");
PQXX_CHECK_EQUAL(
pqxx::to_string(std::vector<std::string>{}), "{}",
"String array comes out different.");
}
void test_generate_null_value()
{
PQXX_CHECK_EQUAL(
pqxx::to_string(std::vector<char const *>{nullptr}), "{NULL}",
"Null array value did not come out as expected.");
}
void test_generate_single_item()
{
PQXX_CHECK_EQUAL(
pqxx::to_string(std::vector<int>{42}), "{42}",
"Numeric conversion came out wrong.");
PQXX_CHECK_EQUAL(
pqxx::to_string(std::vector<char const *>{"foo"}), "{\"foo\"}",
"String array conversion came out wrong.");
}
void test_generate_multiple_items()
{
PQXX_CHECK_EQUAL(
pqxx::to_string(std::vector<int>{5, 4, 3, 2}), "{5,4,3,2}",
"Array with multiple values is not correct.");
PQXX_CHECK_EQUAL(
pqxx::to_string(std::vector<std::string>{"foo", "bar"}),
"{\"foo\",\"bar\"}", "Array with multiple strings came out wrong.");
}
void test_generate_nested_array()
{
PQXX_CHECK_EQUAL(
pqxx::to_string(std::vector<std::vector<int>>{{1, 2}, {3, 4}}),
"{{1,2},{3,4}}", "Nested arrays don't work right.");
}
void test_generate_escaped_strings()
{
PQXX_CHECK_EQUAL(
pqxx::to_string(std::vector<std::string>{"a\\b"}), "{\"a\\\\b\"}",
"Backslashes are not escaped properly.");
PQXX_CHECK_EQUAL(
pqxx::to_string(std::vector<std::string>{"x\"y\""}), "{\"x\\\"y\\\"\"}",
"Double quotes are not escaped properly.");
}
void test_array_generate()
{
test_generate_empty_array();
test_generate_null_value();
test_generate_single_item();
test_generate_multiple_items();
test_generate_nested_array();
test_generate_escaped_strings();
}
void test_array_roundtrip()
{
pqxx::connection c;
pqxx::work w{c};
std::vector<int> const in{0, 1, 2, 3, 5};
auto const r1{w.exec1("SELECT " + c.quote(in) + "::integer[]")};
pqxx::array_parser parser{r1[0].view()};
auto item{parser.get_next()};
PQXX_CHECK_EQUAL(
item.first, pqxx::array_parser::juncture::row_start,
"Array did not start with row_start.");
std::vector<int> out;
for (item = parser.get_next();
item.first == pqxx::array_parser::juncture::string_value;
item = parser.get_next())
{
out.push_back(pqxx::from_string<int>(item.second));
}
PQXX_CHECK_EQUAL(
item.first, pqxx::array_parser::juncture::row_end,
"Array values did not end in row_end.");
PQXX_CHECK_EQUAL(
std::size(out), std::size(in), "Array came back with different length.");
for (std::size_t i{0}; i < std::size(in); ++i)
PQXX_CHECK_EQUAL(out[i], in[i], "Array element has changed.");
item = parser.get_next();
PQXX_CHECK_EQUAL(
item.first, pqxx::array_parser::juncture::done,
"Array did not end in done.");
}
void test_array_strings()
{
std::vector<std::string_view> inputs{
"", "null", "NULL", "\\N", "'", "''", "\\", "\n\t",
"\\n", "\"", "\"\"", "a b", "a<>b", "{", "}", "{}",
};
pqxx::connection conn;
pqxx::work tx{conn};
for (auto const &input : inputs)
{
auto const r{tx.exec_params("SELECT ARRAY[$1]", input)};
pqxx::array_parser parser{r[0][0].as<std::string_view>()};
auto [start_juncture, start_value]{parser.get_next()};
pqxx::ignore_unused(start_value);
PQXX_CHECK_EQUAL(
start_juncture, pqxx::array_parser::juncture::row_start, "Bad start.");
auto [value_juncture, value]{parser.get_next()};
PQXX_CHECK_EQUAL(
value_juncture, pqxx::array_parser::juncture::string_value,
"Bad value juncture.");
PQXX_CHECK_EQUAL(value, input, "Bad array value roundtrip.");
}
}
void test_array_parses_real_arrays()
{
pqxx::connection conn;
pqxx::work tx{conn};
auto const empty_s{tx.query_value<std::string>("SELECT ARRAY[]::integer[]")};
pqxx::array<int> empty_a{empty_s, conn};
PQXX_CHECK_EQUAL(
empty_a.dimensions(), 1u, "Unexpected dimension count for empty array.");
PQXX_CHECK_EQUAL(
empty_a.sizes(), (std::array<std::size_t, 1u>{0u}),
"Unexpected sizes for empty array.");
auto const onedim_s{tx.query_value<std::string>("SELECT ARRAY[0, 1, 2]")};
pqxx::array<int> onedim_a{onedim_s, conn};
PQXX_CHECK_EQUAL(
onedim_a.dimensions(), 1u,
"Unexpected dimension count for one-dimensional array.");
PQXX_CHECK_EQUAL(
onedim_a.sizes(), (std::array<std::size_t, 1u>{3u}),
"Unexpected sizes for one-dimensional array.");
PQXX_CHECK_EQUAL(onedim_a[0], 0, "Bad data in one-dimensional array.");
PQXX_CHECK_EQUAL(
onedim_a[2], 2, "Array started off OK but later data was bad.");
auto const null_s{
tx.query_value<std::string>("SELECT ARRAY[NULL]::integer[]")};
PQXX_CHECK_THROWS(
(pqxx::array<int>{null_s, conn}), pqxx::unexpected_null,
"Not getting unexpected_null from array parser.");
auto const twodim_s{tx.query_value<std::string>("SELECT ARRAY[[1], [2]]")};
pqxx::array<int, 2> twodim_a{twodim_s, conn};
PQXX_CHECK_EQUAL(
twodim_a.dimensions(), 2u,
"Wrong number of dimensions on multi-dimensional array.");
PQXX_CHECK_EQUAL(
twodim_a.sizes(), (std::array<std::size_t, 2>{2u, 1u}),
"Wrong sizes on multidim array.");
auto const string_s{tx.query_value<std::string>("SELECT ARRAY['Hello']")};
pqxx::array<std::string> string_a{string_s, conn};
PQXX_CHECK_EQUAL(string_a[0], "Hello", "String field came out wrong.");
auto const fake_null_s{tx.query_value<std::string>("SELECT ARRAY['NULL']")};
pqxx::array<std::string> fake_null_a{string_s, conn};
PQXX_CHECK_EQUAL(
fake_null_a[0], "Hello", "String field 'NULL' came out wrong.");
auto const nulls_s{
tx.query_value<std::string>("SELECT ARRAY[NULL, 'NULL']")};
pqxx::array<std::optional<std::string>> nulls_a{nulls_s, conn};
PQXX_CHECK(not nulls_a[0].has_value(), "Null string cvame out with value.");
PQXX_CHECK(nulls_a[1].has_value(), "String 'NULL' came out as null.");
PQXX_CHECK_EQUAL(
nulls_a[1].value(), "NULL", "String 'NULL' came out wrong.");
}
void test_array_rejects_malformed_simple_int_arrays()
{
pqxx::connection conn;
std::string_view const bad_arrays[]{
""sv, "null"sv, ","sv, "1"sv, "{"sv, "}"sv, "}{"sv,
"{}{"sv, "{{}"sv, "{}}"sv, "{{}}"sv, "{1"sv, "{1,"sv, "{,}"sv,
"{1,}"sv, "{,1}"sv, "{1,{}}"sv, "{x}"sv, "{1,{2,3}}"sv,
};
for (auto bad : bad_arrays)
PQXX_CHECK_THROWS(
(pqxx::array<int>{bad, conn}), pqxx::conversion_error,
"No conversion_error for '" + std::string{bad} + "'.");
}
void test_array_rejects_malformed_simple_string_arrays()
{
pqxx::connection conn;
std::string_view const bad_arrays[]{
""sv, "null"sv, "1"sv, ","sv, "{"sv, "}"sv,
"}{"sv, "{}{"sv, "{{}"sv, "{}}"sv, "{{}}"sv, "{1"sv,
"{1,"sv, "{,}"sv, "{1,}"sv, "{,1}"sv, "{1,{}}"sv,
};
for (auto bad : bad_arrays)
PQXX_CHECK_THROWS(
(pqxx::array<std::string>{bad, conn}), pqxx::conversion_error,
"No conversion_error for '" + std::string{bad} + "'.");
}
void test_array_rejects_malformed_twodimensional_arrays()
{
pqxx::connection conn;
std::string_view const bad_arrays[]{
""sv,
"{}"sv,
"{null}"sv,
"{{1},{2,3}}"sv,
};
for (auto bad : bad_arrays)
PQXX_CHECK_THROWS(
(pqxx::array<std::string, 2>{bad, conn}), pqxx::conversion_error,
"No conversion_error for '" + std::string{bad} + "'.");
}
void test_array_parses_quoted_strings()
{
pqxx::connection conn;
pqxx::array<std::string> const a{R"x({"\"'"})x", conn};
PQXX_CHECK_EQUAL(a[0], R"x("')x", "String in array did not unescape right.");
}
void test_array_parses_multidim_arrays()
{
pqxx::connection conn;
pqxx::array<int, 2u> const a{"{{0,1},{2,3}}", conn};
PQXX_CHECK_EQUAL(a.at(0u, 0u), 0, "Indexing is wrong.");
PQXX_CHECK_EQUAL(a.at(1u, 0u), 2, "Indexing seems to confuse dimensions.");
PQXX_CHECK_EQUAL(a.at(1u, 1u), 3, "Indexing at higher indexes goes wrong.");
}
void test_array_at_checks_bounds()
{
pqxx::connection conn;
pqxx::array<int> const simple{"{0, 1, 2}", conn};
PQXX_CHECK_EQUAL(simple.at(0), 0, "Array indexing does not work.");
PQXX_CHECK_EQUAL(simple.at(2), 2, "Nonzero array indexing goes wrong.");
PQXX_CHECK_THROWS(
simple.at(3), pqxx::range_error, "No bounds checking on array::at().");
PQXX_CHECK_THROWS(
simple.at(-1), pqxx::range_error,
"Negative index does not throw range_error.");
pqxx::array<int, 2> const multi{"{{0,1},{2,3},{4,5}}", conn};
PQXX_CHECK_EQUAL(
multi.at(0, 0), 0, "Multidim array indexing does not work.");
PQXX_CHECK_EQUAL(multi.at(1, 1), 3, "Nonzero multidim indexing goes wrong.");
PQXX_CHECK_EQUAL(multi.at(2, 1), 5, "Multidim top element went wrong.");
PQXX_CHECK_THROWS(
multi.at(3, 0), pqxx::range_error,
"Out-of-bounds on outer dimension was not detected.");
PQXX_CHECK_THROWS(
multi.at(0, 2), pqxx::range_error,
"Out-of-bounds on inner dimension was not detected.");
PQXX_CHECK_THROWS(
multi.at(0, -1), pqxx::range_error,
"Negative inner index was not detected.");
PQXX_CHECK_THROWS(
multi.at(-1, 0), pqxx::range_error,
"Negative outer index was not detected.");
}
void test_array_iterates_in_row_major_order()
{
pqxx::connection conn;
pqxx::work tx{conn};
auto const array_s{tx.query_value<std::string>(
"SELECT ARRAY[[1, 2, 3], [4, 5, 6], [7, 8, 9]]")};
pqxx::array<int, 2> array{array_s, conn};
auto it{array.cbegin()};
PQXX_CHECK_EQUAL(*it, 1, "Iteration started off wrong.");
++it;
++it;
PQXX_CHECK_EQUAL(*it, 3, "Iteration seems to have taken the wrong order.");
++it;
PQXX_CHECK_EQUAL(*it, 4, "Iteration did not jump to the next dimension.");
it += 6;
PQXX_CHECK(it == array.cend(), "Array cend() not where I expected.");
PQXX_CHECK_EQUAL(*(array.cend() - 1), 9, "Iteration did not end well.");
PQXX_CHECK_EQUAL(*array.crbegin(), 9, "Bad crbegin().");
PQXX_CHECK_EQUAL(*(array.crend() - 1), 1, "Bad crend().");
PQXX_CHECK_EQUAL(std::size(array), 9u, "Bad array size.");
// C++20: Use std::ssize() instead.
PQXX_CHECK_EQUAL(array.ssize(), 9, "Bad array ssize().");
PQXX_CHECK_EQUAL(array.front(), 1, "Bad front().");
PQXX_CHECK_EQUAL(array.back(), 9, "Bad back().");
}
PQXX_REGISTER_TEST(test_empty_arrays);
PQXX_REGISTER_TEST(test_array_null_value);
PQXX_REGISTER_TEST(test_array_double_quoted_string);
PQXX_REGISTER_TEST(test_array_double_quoted_escaping);
PQXX_REGISTER_TEST(test_array_double_double_quoted_string);
PQXX_REGISTER_TEST(test_array_unquoted_string);
PQXX_REGISTER_TEST(test_array_multiple_values);
PQXX_REGISTER_TEST(test_nested_array);
PQXX_REGISTER_TEST(test_nested_array_with_multiple_entries);
PQXX_REGISTER_TEST(test_array_generate);
PQXX_REGISTER_TEST(test_array_roundtrip);
PQXX_REGISTER_TEST(test_array_strings);
PQXX_REGISTER_TEST(test_array_parses_real_arrays);
PQXX_REGISTER_TEST(test_array_rejects_malformed_simple_int_arrays);
PQXX_REGISTER_TEST(test_array_rejects_malformed_simple_string_arrays);
PQXX_REGISTER_TEST(test_array_rejects_malformed_twodimensional_arrays);
PQXX_REGISTER_TEST(test_array_parses_quoted_strings);
PQXX_REGISTER_TEST(test_array_parses_multidim_arrays);
PQXX_REGISTER_TEST(test_array_at_checks_bounds);
PQXX_REGISTER_TEST(test_array_iterates_in_row_major_order);
} // namespace