File: 118_type.t

package info (click to toggle)
libcpanel-json-xs-perl 4.25-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 2,856 kB
  • sloc: perl: 1,065; makefile: 8
file content (619 lines) | stat: -rw-r--r-- 30,786 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
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
use strict;
use warnings;

use Config;

use Cpanel::JSON::XS;
use Cpanel::JSON::XS::Type;

my $have_weaken;
BEGIN {
    if (eval { require Scalar::Util }) {
        Scalar::Util->import('weaken');
        $have_weaken = 1;
    }
    $have_weaken = 0 if $] < 5.008;
}

use Test::More tests => 381;

my $cjson = Cpanel::JSON::XS->new->canonical->allow_nonref->require_types;
my $bigcjson = Cpanel::JSON::XS->new->canonical->allow_nonref->require_types->allow_bignum;

foreach my $false (Cpanel::JSON::XS::false, undef, 0, 0.0, 0E0, !!0, !1, "0", "", \0) {
    is($cjson->encode($false, JSON_TYPE_BOOL), 'false');
}

foreach my $true (Cpanel::JSON::XS::true, 1, !!1, !0, 2, 3, 100, -1, -100, 1.0, 1.5, 1E1, "0E0", "0 but true", "1", "2", "100", "-1", "-1", "false", "true", "string", \1) {
    is($cjson->encode($true, JSON_TYPE_BOOL), 'true');
    is($cjson->encode($true, JSON_TYPE_BOOL_OR_NULL), 'true');
}

foreach my $zero (0, 0.0, 0E0, "0") {
    is($cjson->encode($zero, JSON_TYPE_BOOL), 'false');
    is($cjson->encode($zero, JSON_TYPE_INT), '0');
    is($cjson->encode($zero, JSON_TYPE_FLOAT), '0.0');
    is($cjson->encode($zero, JSON_TYPE_STRING), '"0"');
    is($cjson->encode($zero, JSON_TYPE_BOOL_OR_NULL), 'false');
    is($cjson->encode($zero, JSON_TYPE_INT_OR_NULL), '0');
    is($cjson->encode($zero, JSON_TYPE_FLOAT_OR_NULL), '0.0');
    is($cjson->encode($zero, JSON_TYPE_STRING_OR_NULL), '"0"');
}

foreach my $ten (10, 10.0, 1E1, "10") {
    is($cjson->encode($ten, JSON_TYPE_BOOL), 'true');
    is($cjson->encode($ten, json_type_anyof(JSON_TYPE_BOOL)), 'true');
    is($cjson->encode($ten, json_type_anyof(JSON_TYPE_BOOL, [])), 'true');
    is($cjson->encode($ten, JSON_TYPE_INT), '10');
    is($cjson->encode($ten, json_type_anyof(JSON_TYPE_INT)), '10');
    is($cjson->encode($ten, json_type_anyof([], JSON_TYPE_INT)), '10');
    is($cjson->encode($ten, JSON_TYPE_FLOAT), '10.0');
    is($cjson->encode($ten, json_type_anyof({}, JSON_TYPE_FLOAT)), '10.0');
    is($cjson->encode($ten, json_type_anyof(JSON_TYPE_FLOAT, {})), '10.0');
    is($cjson->encode($ten, JSON_TYPE_STRING), '"10"');
    is($cjson->encode($ten, json_type_anyof([], JSON_TYPE_STRING, {})), '"10"');
    is($cjson->encode($ten, json_type_anyof({}, JSON_TYPE_STRING, [])), '"10"');
    is($cjson->encode($ten, JSON_TYPE_BOOL_OR_NULL), 'true');
    is($cjson->encode($ten, json_type_anyof(JSON_TYPE_BOOL_OR_NULL)), 'true');
    is($cjson->encode($ten, json_type_anyof([], JSON_TYPE_BOOL_OR_NULL)), 'true');
    is($cjson->encode($ten, JSON_TYPE_INT_OR_NULL), '10');
    is($cjson->encode($ten, json_type_anyof(JSON_TYPE_INT_OR_NULL)), '10');
    is($cjson->encode($ten, json_type_anyof({}, JSON_TYPE_INT_OR_NULL)), '10');
    is($cjson->encode($ten, JSON_TYPE_FLOAT_OR_NULL), '10.0');
    is($cjson->encode($ten, json_type_anyof(JSON_TYPE_FLOAT_OR_NULL)), '10.0');
    is($cjson->encode($ten, json_type_anyof(JSON_TYPE_FLOAT_OR_NULL, [])), '10.0');
    is($cjson->encode($ten, JSON_TYPE_STRING_OR_NULL), '"10"');
    is($cjson->encode($ten, json_type_anyof(JSON_TYPE_STRING_OR_NULL)), '"10"');
    is($cjson->encode($ten, json_type_anyof(JSON_TYPE_STRING_OR_NULL, {})), '"10"');
}

is($cjson->encode(Cpanel::JSON::XS::false, JSON_TYPE_BOOL), 'false');
is($cjson->encode(Cpanel::JSON::XS::false, JSON_TYPE_INT), '0');
is($cjson->encode(Cpanel::JSON::XS::false, JSON_TYPE_FLOAT), '0.0');
is($cjson->encode(Cpanel::JSON::XS::false, JSON_TYPE_STRING), '"false"');
is($cjson->encode(Cpanel::JSON::XS::false, json_type_anyof([], {}, JSON_TYPE_BOOL)), 'false');

is($cjson->encode(Cpanel::JSON::XS::true, JSON_TYPE_BOOL), 'true');
is($cjson->encode(Cpanel::JSON::XS::true, JSON_TYPE_INT), '1');
is($cjson->encode(Cpanel::JSON::XS::true, JSON_TYPE_FLOAT), '1.0');
is($cjson->encode(Cpanel::JSON::XS::true, JSON_TYPE_STRING), '"true"');
is($cjson->encode(Cpanel::JSON::XS::true, json_type_anyof([], {}, JSON_TYPE_BOOL)), 'true');

is($cjson->encode(undef, JSON_TYPE_BOOL_OR_NULL), 'null');
is($cjson->encode(undef, JSON_TYPE_INT_OR_NULL), 'null');
is($cjson->encode(undef, JSON_TYPE_FLOAT_OR_NULL), 'null');
is($cjson->encode(undef, JSON_TYPE_STRING_OR_NULL), 'null');
is($cjson->encode(undef, json_type_null_or_anyof([])), 'null');
is($cjson->encode(undef, json_type_null_or_anyof({})), 'null');

is($cjson->encode(int("NaN"), JSON_TYPE_INT), '0');
is($cjson->encode('NaN', JSON_TYPE_INT), '0');

if ($Config{ivsize} == 4) {
  # values around signed IV_MAX should work correctly as they can be represented by unsigned UV
  is($cjson->encode( '2147483646', JSON_TYPE_INT),  '2147483646');  #  2^31-2
  is($cjson->encode( '2147483647', JSON_TYPE_INT),  '2147483647');  #  2^31-1
  is($cjson->encode( '2147483648', JSON_TYPE_INT),  '2147483648');  #  2^31
  is($cjson->encode( '2147483649', JSON_TYPE_INT),  '2147483649');  #  2^31+1
  is($cjson->encode( '2147483650', JSON_TYPE_INT),  '2147483650');  #  2^31+2

  # values up to signed IV_MIN should work correctly too
  is($cjson->encode('-2147483646', JSON_TYPE_INT), '-2147483646');  # -2^31+2
  is($cjson->encode('-2147483647', JSON_TYPE_INT), '-2147483647');  # -2^31+1
  is($cjson->encode('-2147483648', JSON_TYPE_INT), '-2147483648');  # -2^31

  # values up to unsigned UV_MAX should work correctly too
  is($cjson->encode( '4294967294', JSON_TYPE_INT),  '4294967294');  #  2^32-2
  is($cjson->encode( '4294967295', JSON_TYPE_INT),  '4294967295');  #  2^32-1

  # those values are rounded to unsigned UV_MAX or signed IV_MIN
  is($cjson->encode( '4294967296', JSON_TYPE_INT),  '4294967295');  #  2^32
  is($cjson->encode( '4294967297', JSON_TYPE_INT),  '4294967295');  #  2^32+1
  is($cjson->encode( '4294967298', JSON_TYPE_INT),  '4294967295');  #  2^32+2
  is($cjson->encode('-2147483649', JSON_TYPE_INT), '-2147483648');  # -2^31-1
  is($cjson->encode('-2147483650', JSON_TYPE_INT), '-2147483648');  # -2^31-2

  # those floating point values are rounded to unsigned UV_MAX or signed IV_MIN too
  is($cjson->encode( 4294967296.5, JSON_TYPE_INT),  '4294967295');  #  2^32
  is($cjson->encode( 4294967297.5, JSON_TYPE_INT),  '4294967295');  #  2^32+1
  is($cjson->encode( 4294967298.5, JSON_TYPE_INT),  '4294967295');  #  2^32+2
  is($cjson->encode(-2147483649.5, JSON_TYPE_INT), '-2147483648');  # -2^31-1
  is($cjson->encode(-2147483650.5, JSON_TYPE_INT), '-2147483648');  # -2^31-2

  is($cjson->encode(  int( 'Inf'), JSON_TYPE_INT), ($] >= 5.008 && $] < 5.008008) ? '0' :  '4294967295');
  is($cjson->encode(  int('-Inf'), JSON_TYPE_INT), ($] >= 5.008 && $] < 5.008008) ? '0' : '-2147483648');

  is($cjson->encode(        'Inf', JSON_TYPE_INT),  '4294967295');
  is($cjson->encode(       '-Inf', JSON_TYPE_INT), '-2147483648');
  is($cjson->encode(      9**9**9, JSON_TYPE_INT),  '4294967295');
  is($cjson->encode(     -9**9**9, JSON_TYPE_INT), '-2147483648');
} else {
SKIP: {
  skip "unknown ivsize $Config{ivsize}", 26 if $Config{ivsize} != 8;

  # values around signed IV_MAX should work correctly as they can be represented by unsigned UV
  is($cjson->encode( '9223372036854775806', JSON_TYPE_INT),  '9223372036854775806');  #  2^63-2
  is($cjson->encode( '9223372036854775807', JSON_TYPE_INT),  '9223372036854775807');  #  2^63-1
  is($cjson->encode( '9223372036854775808', JSON_TYPE_INT),  '9223372036854775808');  #  2^63
  is($cjson->encode( '9223372036854775809', JSON_TYPE_INT),  '9223372036854775809');  #  2^63+1
  is($cjson->encode( '9223372036854775810', JSON_TYPE_INT),  '9223372036854775810');  #  2^63+2

  # values up to signed IV_MIN should work correctly too
  is($cjson->encode('-9223372036854775806', JSON_TYPE_INT), '-9223372036854775806');  # -2^63+2
  is($cjson->encode('-9223372036854775807', JSON_TYPE_INT), '-9223372036854775807');  # -2^63+1
  is($cjson->encode('-9223372036854775808', JSON_TYPE_INT), '-9223372036854775808');  # -2^63

  # values up to unsigned UV_MAX should work correctly too
  is($cjson->encode('18446744073709551614', JSON_TYPE_INT), '18446744073709551614');  #  2^64-2
  is($cjson->encode('18446744073709551615', JSON_TYPE_INT), '18446744073709551615');  #  2^64-1

  # those values are rounded to unsigned UV_MAX or signed IV_MIN
  is($cjson->encode('18446744073709551616', JSON_TYPE_INT), '18446744073709551615');  #  2^64
  is($cjson->encode('18446744073709551617', JSON_TYPE_INT), '18446744073709551615');  #  2^64+1
  is($cjson->encode('18446744073709551618', JSON_TYPE_INT), '18446744073709551615');  #  2^64+2
  is($cjson->encode('-9223372036854775809', JSON_TYPE_INT), '-9223372036854775808');  # -2^63-1
  is($cjson->encode('-9223372036854775810', JSON_TYPE_INT), '-9223372036854775808');  # -2^63-2

  # those floating point values are rounded to unsigned UV_MAX or signed IV_MIN too
  is($cjson->encode(18446744073709551616.5, JSON_TYPE_INT), '18446744073709551615');  #  2^64
  is($cjson->encode(18446744073709551617.5, JSON_TYPE_INT), '18446744073709551615');  #  2^64+1
  is($cjson->encode(18446744073709551618.5, JSON_TYPE_INT), '18446744073709551615');  #  2^64+2
  is($cjson->encode(-9223372036854775809.5, JSON_TYPE_INT), '-9223372036854775808');  # -2^63-1
  is($cjson->encode(-9223372036854775810.5, JSON_TYPE_INT), '-9223372036854775808');  # -2^63-2

  is($cjson->encode(           int( 'Inf'), JSON_TYPE_INT), ($] >= 5.008 && $] < 5.008008) ? '0' : '18446744073709551615');
  is($cjson->encode(           int('-Inf'), JSON_TYPE_INT), ($] >= 5.008 && $] < 5.008008) ? '0' : '-9223372036854775808');

  is($cjson->encode(                 'Inf', JSON_TYPE_INT), '18446744073709551615');
  is($cjson->encode(                '-Inf', JSON_TYPE_INT), '-9223372036854775808');
  is($cjson->encode(               9**9**9, JSON_TYPE_INT), '18446744073709551615');
  is($cjson->encode(              -9**9**9, JSON_TYPE_INT), '-9223372036854775808');
 }
}

SKIP: {
  skip 'requires Math::BigInt', 5 unless eval { require Math::BigInt };
  # integer string values outside of range [IV_MIN, UV_MAX] with enabled bignum
  is($bigcjson->encode('18446744073709551616', JSON_TYPE_INT), '18446744073709551616');  #  2^64
  is($bigcjson->encode('18446744073709551617', JSON_TYPE_INT), '18446744073709551617');  #  2^64+1
  is($bigcjson->encode('18446744073709551618', JSON_TYPE_INT), '18446744073709551618');  #  2^64+2
  is($bigcjson->encode('-9223372036854775809', JSON_TYPE_INT), '-9223372036854775809');  # -2^63-1
  is($bigcjson->encode('-9223372036854775810', JSON_TYPE_INT), '-9223372036854775810');  # -2^63-2
}

SKIP: {
  skip 'requires Math::BigFloat 1.16', 6 unless eval { require Math::BigFloat; Math::BigFloat->VERSION(1.16) };
  # float string values outside of range [IV_MIN, UV_MAX] with enabled bignum
  is($bigcjson->encode('18446744073709551616.5', JSON_TYPE_INT), '18446744073709551616');  #  2^64
  is($bigcjson->encode('18446744073709551617.5', JSON_TYPE_INT), '18446744073709551617');  #  2^64+1
  is($bigcjson->encode('18446744073709551618.5', JSON_TYPE_INT), '18446744073709551618');  #  2^64+2
  is($bigcjson->encode('-9223372036854775809.5', JSON_TYPE_INT), '-9223372036854775809');  # -2^63-1
  is($bigcjson->encode('-9223372036854775810.5', JSON_TYPE_INT), '-9223372036854775810');  # -2^63-2
  is($bigcjson->encode(  '7.37869762948382e+19', JSON_TYPE_INT), '73786976294838200000');
}

SKIP: {
  skip 'requires Math::BigInt', 8 unless eval { require Math::BigInt };

  # Math::BigInt values outside of range [IV_MIN, UV_MAX] with enabled bignum
  is($bigcjson->encode(Math::BigInt->new('18446744073709551616'), JSON_TYPE_INT), '18446744073709551616');  #  2^64
  is($bigcjson->encode(Math::BigInt->new('18446744073709551617'), JSON_TYPE_INT), '18446744073709551617');  #  2^64+1
  is($bigcjson->encode(Math::BigInt->new('18446744073709551618'), JSON_TYPE_INT), '18446744073709551618');  #  2^64+2
  is($bigcjson->encode(Math::BigInt->new('-9223372036854775809'), JSON_TYPE_INT), '-9223372036854775809');  # -2^63-1
  is($bigcjson->encode(Math::BigInt->new('-9223372036854775810'), JSON_TYPE_INT), '-9223372036854775810');  # -2^63-2

  is($bigcjson->encode(Math::BigInt->new('NaN'), JSON_TYPE_INT), '0');

  if ($Config{ivsize} == 4) {
    is($bigcjson->encode(Math::BigInt->new('+inf'), JSON_TYPE_INT), '4294967295');
    is($bigcjson->encode(Math::BigInt->new('-inf'), JSON_TYPE_INT), '-2147483648');
  } elsif ($Config{ivsize} == 8 && $] > 5.008) {
    is($bigcjson->encode(Math::BigInt->new('+inf'), JSON_TYPE_INT), '18446744073709551615');
    is($bigcjson->encode(Math::BigInt->new('-inf'), JSON_TYPE_INT), '-9223372036854775808');
  } else {
    skip "unknown ivsize $Config{ivsize} or too old", 2;
  }
}

SKIP: {
  skip 'requires Math::BigFloat 1.16', 8 unless eval { require Math::BigFloat; Math::BigFloat->VERSION(1.16) };
  # Math::BigFloat values outside of range [IV_MIN, UV_MAX] with enabled bignum
  is($bigcjson->encode(Math::BigFloat->new('18446744073709551616.5'), JSON_TYPE_INT), '18446744073709551616');  #  2^64
  is($bigcjson->encode(Math::BigFloat->new('18446744073709551617.5'), JSON_TYPE_INT), '18446744073709551617');  #  2^64+1
  is($bigcjson->encode(Math::BigFloat->new('18446744073709551618.5'), JSON_TYPE_INT), '18446744073709551618');  #  2^64+2
  is($bigcjson->encode(Math::BigFloat->new('-9223372036854775809.5'), JSON_TYPE_INT), '-9223372036854775809');  # -2^63-1
  is($bigcjson->encode(Math::BigFloat->new('-9223372036854775810.5'), JSON_TYPE_INT), '-9223372036854775810');  # -2^63-2

  is($bigcjson->encode(Math::BigFloat->new('NaN'), JSON_TYPE_INT), '0');
  if ($Config{ivsize} == 4) {
    is($bigcjson->encode(Math::BigFloat->new('+inf'), JSON_TYPE_INT), '4294967295');
    is($bigcjson->encode(Math::BigFloat->new('-inf'), JSON_TYPE_INT), '-2147483648');
  } elsif ($Config{ivsize} == 8) {
    is($bigcjson->encode(Math::BigFloat->new('+inf'), JSON_TYPE_INT), '18446744073709551615');
    is($bigcjson->encode(Math::BigFloat->new('-inf'), JSON_TYPE_INT), '-9223372036854775808');
  } else {
    skip "unknown ivsize $Config{ivsize}", 2;
  }
}

my $fltinf;
if ($Config{nvtype} eq 'long double' &&
    (exists $Config{longdblkind}
      ? $Config{longdblkind} == 3
      : $Config{archname} =~ /86/)) {
  $fltinf = '1.18973149535723177e+4932';
} elsif ($Config{nvtype} eq 'double' && $Config{nvsize} == 8) {
  $fltinf = '1.79769313486232e+308';
}
is($cjson->encode(   int("NaN"), JSON_TYPE_FLOAT), '0.0');
is($cjson->encode(        'NaN', JSON_TYPE_FLOAT), '0.0');
SKIP: {
  skip "unknown nvtype $Config{nvtype}, " .
    exists $Config{longdblkind} ? "longdblkind $Config{longdblkind}" : "", 6
      unless $fltinf;
  is($cjson->encode(  int( 'Inf'), JSON_TYPE_FLOAT), ($] >= 5.008 && $] < 5.008008) ? '0.0' :  $fltinf);
  is($cjson->encode(  int('-Inf'), JSON_TYPE_FLOAT), ($] >= 5.008 && $] < 5.008008) ? '0.0' : "-$fltinf");
  is($cjson->encode(        'Inf', JSON_TYPE_FLOAT),  $fltinf);
  is($cjson->encode(       '-Inf', JSON_TYPE_FLOAT), "-$fltinf");
  is($cjson->encode(      9**9**9, JSON_TYPE_FLOAT),  $fltinf);
  is($cjson->encode(     -9**9**9, JSON_TYPE_FLOAT), "-$fltinf");
}

SKIP: {
  skip 'requires Math::BigFloat', 20 unless eval { require Math::BigFloat };
  skip 'unknown NV_MAX', 20 if !$fltinf;
  skip 'too old', 20 if $] < 5.008;
  is($bigcjson->encode(   int("NaN"), JSON_TYPE_FLOAT), '0.0');
  is($bigcjson->encode(        'NaN', JSON_TYPE_FLOAT), '0.0');
  is($bigcjson->encode(  int( 'Inf'), JSON_TYPE_FLOAT), ($] >= 5.008 && $] < 5.008008) ? '0.0' :  $fltinf);
  is($bigcjson->encode(  int('-Inf'), JSON_TYPE_FLOAT), ($] >= 5.008 && $] < 5.008008) ? '0.0' : "-$fltinf");
  is($bigcjson->encode(        'Inf', JSON_TYPE_FLOAT),  $fltinf);
  is($bigcjson->encode(       '-Inf', JSON_TYPE_FLOAT), "-$fltinf");
  is($bigcjson->encode(      9**9**9, JSON_TYPE_FLOAT),  $fltinf);
  is($bigcjson->encode(     -9**9**9, JSON_TYPE_FLOAT), "-$fltinf");

  # integer string values outside of range [IV_MIN, UV_MAX] with enabled bignum
  is($bigcjson->encode('18446744073709551616', JSON_TYPE_FLOAT), '18446744073709551616.0');  #  2^64
  is($bigcjson->encode('18446744073709551617', JSON_TYPE_FLOAT), '18446744073709551617.0');  #  2^64+1
  is($bigcjson->encode('18446744073709551618', JSON_TYPE_FLOAT), '18446744073709551618.0');  #  2^64+2
  is($bigcjson->encode('-9223372036854775809', JSON_TYPE_FLOAT), '-9223372036854775809.0');  # -2^63-1
  is($bigcjson->encode('-9223372036854775810', JSON_TYPE_FLOAT), '-9223372036854775810.0');  # -2^63-2

  # float string values outside of range [IV_MIN, UV_MAX] with enabled bignum
  is($bigcjson->encode('18446744073709551616.5', JSON_TYPE_FLOAT), '18446744073709551616.5');  #  2^64
  is($bigcjson->encode('18446744073709551617.5', JSON_TYPE_FLOAT), '18446744073709551617.5');  #  2^64+1
  is($bigcjson->encode('18446744073709551618.5', JSON_TYPE_FLOAT), '18446744073709551618.5');  #  2^64+2
  is($bigcjson->encode('-9223372036854775809.5', JSON_TYPE_FLOAT), '-9223372036854775809.5');  # -2^63-1
  is($bigcjson->encode('-9223372036854775810.5', JSON_TYPE_FLOAT), '-9223372036854775810.5');  # -2^63-2
  is($bigcjson->encode(  '7.37869762948382e+19', JSON_TYPE_FLOAT), '73786976294838200000.0');
  is($bigcjson->encode('7.37869762948382123456789e+19', JSON_TYPE_FLOAT), '73786976294838212345.6789');
}

SKIP: {
  skip 'requires Math::BigInt', 8 unless eval { require Math::BigInt };
  skip 'requires Math::BigFloat', 8 unless eval { require Math::BigFloat };
  skip "unknown nvtype $Config{nvtype}, longdblkind $Config{longdblkind}", 8
      unless $fltinf;
  skip 'too old', 8 if $] < 5.008;

  # Math::BigInt values with enabled bignum
  is($bigcjson->encode(Math::BigInt->new('18446744073709551616'), JSON_TYPE_FLOAT), '18446744073709551616.0');  #  2^64
  is($bigcjson->encode(Math::BigInt->new('18446744073709551617'), JSON_TYPE_FLOAT), '18446744073709551617.0');  #  2^64+1
  is($bigcjson->encode(Math::BigInt->new('18446744073709551618'), JSON_TYPE_FLOAT), '18446744073709551618.0');  #  2^64+2
  is($bigcjson->encode(Math::BigInt->new('-9223372036854775809'), JSON_TYPE_FLOAT), '-9223372036854775809.0');  # -2^63-1
  is($bigcjson->encode(Math::BigInt->new('-9223372036854775810'), JSON_TYPE_FLOAT), '-9223372036854775810.0');  # -2^63-2

  is($bigcjson->encode(Math::BigInt->new('NaN'), JSON_TYPE_FLOAT), '0.0');
  is($bigcjson->encode(Math::BigInt->new('+inf'), JSON_TYPE_FLOAT), $fltinf);
  is($bigcjson->encode(Math::BigInt->new('-inf'), JSON_TYPE_FLOAT), "-$fltinf");
}

SKIP: {
  skip 'requires Math::BigFloat', 8 unless eval { require Math::BigFloat };
  skip "unknown nvtype $Config{nvtype}, longdblkind $Config{longdblkind}", 8
      unless $fltinf;
  skip 'too old', 8 if $] < 5.008;

  # Math::BigFloat values with enabled bignum
  is($bigcjson->encode(Math::BigFloat->new('18446744073709551616.5'), JSON_TYPE_FLOAT), '18446744073709551616.5');  #  2^64
  is($bigcjson->encode(Math::BigFloat->new('18446744073709551617.5'), JSON_TYPE_FLOAT), '18446744073709551617.5');  #  2^64+1
  is($bigcjson->encode(Math::BigFloat->new('18446744073709551618.5'), JSON_TYPE_FLOAT), '18446744073709551618.5');  #  2^64+2
  is($bigcjson->encode(Math::BigFloat->new('-9223372036854775809.5'), JSON_TYPE_FLOAT), '-9223372036854775809.5');  # -2^63-1
  is($bigcjson->encode(Math::BigFloat->new('-9223372036854775810.5'), JSON_TYPE_FLOAT), '-9223372036854775810.5');  # -2^63-2

  is($bigcjson->encode(Math::BigFloat->new('NaN'), JSON_TYPE_FLOAT), '0.0');
  is($bigcjson->encode(Math::BigFloat->new('+inf'), JSON_TYPE_FLOAT), $fltinf);
  is($bigcjson->encode(Math::BigFloat->new('-inf'), JSON_TYPE_FLOAT), "-$fltinf");
}

SKIP: {
  skip 'requires Math::BigInt', 1 unless eval { require Math::BigInt };
  my $stringified_int = '-9223372036854775810';
  do { my $temp = $stringified_int + 10 };
  is($bigcjson->encode($stringified_int, JSON_TYPE_INT), '-9223372036854775810');
}

is(encode_json([10, "10", 10.25], [JSON_TYPE_INT, JSON_TYPE_INT, JSON_TYPE_STRING]), '[10,10,"10.25"]');
is(encode_json([10, "10", 10.25], json_type_arrayof(JSON_TYPE_INT)), '[10,10,10]');
is(encode_json([10, "10", 10.25], json_type_anyof(json_type_arrayof(JSON_TYPE_INT))), '[10,10,10]');
is(encode_json([10, "10", 10.25], json_type_null_or_anyof(json_type_arrayof(JSON_TYPE_INT))), '[10,10,10]');

is(encode_json(
    [
            10,
            [
                        11,
                        12,
                        [
                            13,
                        ],
                        14,
            ],
            15,
    ],

    json_type_anyof(
        JSON_TYPE_BOOL,
        [
            JSON_TYPE_INT,
            json_type_anyof(
                json_type_arrayof(
                    json_type_anyof(
                        json_type_arrayof(
                            JSON_TYPE_STRING
                        ),
                        JSON_TYPE_INT,
                    )
                ),
            ),
            JSON_TYPE_FLOAT,
        ],
    )
), '[10,[11,12,["13"],14],15.0]');

{
    my $true = Cpanel::JSON::XS::true;
    my $perl_struct = [ $true, $true, $true, $true ];
    my $type_spec = [ JSON_TYPE_BOOL, JSON_TYPE_INT, JSON_TYPE_FLOAT, JSON_TYPE_STRING ];
    my $json_string = encode_json($perl_struct, $type_spec);
    is($json_string, '[true,1,1.0,"true"]');
}

{
    my $perl_struct = [ 1, 1, "1", undef ];
    my $type_spec = [ JSON_TYPE_INT, JSON_TYPE_STRING, JSON_TYPE_INT_OR_NULL,
                      JSON_TYPE_STRING_OR_NULL ];
    my $json_string = encode_json($perl_struct, $type_spec);
    is($json_string, '[1,"1",1,null]');
}

{
    my $perl_struct = [ "1", 1, 1.0 ];
    my $type_spec = json_type_arrayof(JSON_TYPE_INT);
    my $json_string = encode_json($perl_struct, $type_spec);
    is($json_string, '[1,1,1]');
}

{
    my $perl_struct = { key1 => 1, key2 => "2", key3 => 1 };
    my $type_spec = { key1 => JSON_TYPE_STRING, key2 => JSON_TYPE_INT,
                      key3 => JSON_TYPE_BOOL };
    my $json_string = $cjson->encode($perl_struct, $type_spec);
    is($json_string, '{"key1":"1","key2":2,"key3":true}');
}

{
    my $perl_struct = { key1 => "1", key2 => 2 };
    my $type_spec = { key1 => JSON_TYPE_INT, key2 => JSON_TYPE_STRING,
                      key3 => JSON_TYPE_BOOL };
    my $json_string = $cjson->encode($perl_struct, $type_spec);
    is($json_string, '{"key1":1,"key2":"2"}');
}

{
    my $perl_struct = { key1 => "value1", key2 => "value2", key3 => 0, key4 => 1,
                        key5 => "string", key6 => "string2" };
    my $type_spec = json_type_hashof(JSON_TYPE_STRING);
    my $json_string = $cjson->encode($perl_struct, $type_spec);
    is($json_string, '{"key1":"value1","key2":"value2","key3":"0","key4":"1",'
       .'"key5":"string","key6":"string2"}');
}

{
    my $perl_struct = [ "1", [ 10, 20 ], 13, { "key" => "string" }, [ "1", "2" ],
                        { "key" => 12 } ];
    my $type_spec = json_type_arrayof(json_type_anyof(JSON_TYPE_INT,
                        [ JSON_TYPE_INT, JSON_TYPE_INT ], { "key" => JSON_TYPE_STRING }));
    my $json_string = $cjson->encode($perl_struct, $type_spec);
    is($json_string, '[1,[10,20],13,{"key":"string"},[1,2],{"key":"12"}]');
}

{
    my $perl_struct = { key1 => { key2 => [ 10, "10", 10.6 ] }, key3 => "10.5" };
    my $type_spec = { key1 => json_type_anyof(JSON_TYPE_FLOAT,
                                json_type_hashof(json_type_arrayof(JSON_TYPE_INT))),
                      key3 => JSON_TYPE_FLOAT };
    my $json_string = $cjson->encode($perl_struct, $type_spec);
    is($json_string, '{"key1":{"key2":[10,10,10]},"key3":10.5}');
}

{
    my $perl_struct = { key1 => [ "10", 10, 10.5, Cpanel::JSON::XS::true ],
                        key2 => { key => "string" }, key3 => Cpanel::JSON::XS::false };
    my $type_spec = json_type_hashof(json_type_anyof(json_type_arrayof(JSON_TYPE_INT),
                        json_type_hashof(JSON_TYPE_STRING), JSON_TYPE_BOOL));
    my $json_string = $cjson->encode($perl_struct, $type_spec);
    is($json_string, '{"key1":[10,10,10,1],"key2":{"key":"string"},"key3":false}');
}


SKIP: {
    skip "no Scalar::Util in $]", 2 unless $have_weaken;
    my $weakref;
    {
        my $perl_struct = { key1 => 'string', key2 => '10',
                            key3 => { key1 => 'level1', key2 => '20',
                                      key3 => { key1 => 'level2', key2 => 30 } } };
        my $type_spec = { key1 => JSON_TYPE_STRING, key2 => JSON_TYPE_INT };
        $type_spec->{key3} = $type_spec;
        weaken($type_spec->{key3});
        my $json_string = $cjson->encode($perl_struct, $type_spec);
        is($json_string, '{"key1":"string","key2":10,"key3":'
           .'{"key1":"level1","key2":20,"key3":{"key1":"level2","key2":30}}}');
        $weakref = $type_spec;
        weaken($weakref);
    }
    ok(not defined $weakref);
}

SKIP: {
    skip "no Scalar::Util in $]", 2 unless $have_weaken;
    my $weakref;
    {
        my $perl_struct = [ "10", 10.2, undef, 10, [ [ "10", 10 ], 10.3, undef ], 10 ];
        my $type_arrayof = json_type_arrayof(my $type_spec);
        $type_spec = json_type_anyof(JSON_TYPE_INT_OR_NULL, $type_arrayof);
        ${$type_arrayof} = $type_spec;
        weaken(${$type_arrayof});
        my $json_string = $cjson->encode($perl_struct, $type_spec);
        is($json_string, '[10,10,null,10,[[10,10],10,null],10]');
        $weakref = $type_spec;
        weaken($weakref);
    }
    ok(not defined $weakref);
}

SKIP: {
    skip "no Scalar::Util in $]", 2 unless $have_weaken;
    my $weakref;
    {
        my $perl_struct = { type => "TYPE", value => "VALUE",
                            position => { line => 10, column => 11 },
                            content => [
                                        { type => "TYPE2", value => "VALUE2",
                                          position => { line => 12, column => 13 } } ] };
        my $type_spec = { type => JSON_TYPE_STRING, value => 0,
                          position => { line => JSON_TYPE_INT, column => JSON_TYPE_INT } };
        my $type_spec_content = json_type_arrayof($type_spec);
        weaken(${$type_spec_content});
        $type_spec->{content} = $type_spec_content;
        my $json_string = $cjson->encode($perl_struct, $type_spec);
        is ($json_string,
            '{"content":[{"position":{"column":13,"line":12},"type":"TYPE2","value":"VALUE2"}],'.
            '"position":{"column":11,"line":10},"type":"TYPE","value":"VALUE"}');
        $weakref = $type_spec;
        weaken($weakref);
    }
    ok(not defined $weakref);
}

ok(!defined eval { json_type_anyof(JSON_TYPE_STRING, JSON_TYPE_INT) });
like($@, qr/Only one scalar type can be specified in anyof/);

ok(!defined eval { json_type_anyof([ JSON_TYPE_STRING ], [ JSON_TYPE_INT ]) });
like($@, qr/Only one array type can be specified in anyof/);

ok(!defined eval { json_type_anyof({ key => JSON_TYPE_STRING }, { key => JSON_TYPE_INT }) });
like($@, qr/Only one hash type can be specified in anyof/);

ok(!defined eval { json_type_anyof([ JSON_TYPE_STRING ], json_type_arrayof(JSON_TYPE_INT)) });
like($@, qr/Only one array type can be specified in anyof/);

ok(!defined eval { json_type_anyof({ key => JSON_TYPE_STRING }, json_type_hashof(JSON_TYPE_INT)) });
like($@, qr/Only one hash type can be specified in anyof/);

ok(!defined eval { json_type_anyof(json_type_arrayof(JSON_TYPE_STRING), json_type_arrayof(JSON_TYPE_INT)) });
like($@, qr/Only one array type can be specified in anyof/);

ok(!defined eval { json_type_anyof(json_type_hashof(JSON_TYPE_STRING), json_type_hashof(JSON_TYPE_INT)) });
like($@, qr/Only one hash type can be specified in anyof/);

ok(!defined eval { json_type_anyof(bless({}, 'Object')) });
like($@, qr/Only scalar, array or hash can be specified in anyof/);

ok(!defined eval { json_type_null_or_anyof(JSON_TYPE_STRING) });
like($@, qr/Scalar cannot be specified in null_or_anyof/);

ok(!defined eval { json_type_arrayof(JSON_TYPE_STRING, JSON_TYPE_INT) });
like($@, qr/Exactly one type must be specified in arrayof/);

ok(!defined eval { json_type_hashof(JSON_TYPE_STRING, JSON_TYPE_INT) });
like($@, qr/Exactly one type must be specified in hashof/);

foreach my $val (['0', JSON_TYPE_INT], ['0.0', JSON_TYPE_FLOAT], ['""', JSON_TYPE_STRING]) {
    my $warn;
    local $SIG{__WARN__} = sub { $warn = $_[0] };
    is($cjson->encode(undef, $val->[1]), $val->[0]); my $line = __LINE__;
    like($warn, qr/Use of uninitialized value in (?:XS )?subroutine entry at \Q$0\E line \Q$line\E/);
}

foreach my $type (JSON_TYPE_BOOL_OR_NULL, JSON_TYPE_INT_OR_NULL, JSON_TYPE_FLOAT_OR_NULL, JSON_TYPE_STRING_OR_NULL) {
    my $warn;
    local $SIG{__WARN__} = sub { $warn = $_[0] };
    is($cjson->encode(undef, $type), 'null');
    ok(!defined $warn);
}

foreach my $val (['0', JSON_TYPE_INT], ['0.0', JSON_TYPE_FLOAT]) {
    my $warn;
    local $SIG{__WARN__} = sub { $warn = $_[0] };
    is($cjson->encode(my $str = 'string_value', $val->[1]), $val->[0]); my $line = __LINE__;
    like($warn, qr/Argument "string_value" isn't numeric in (?:XS )?subroutine entry at \Q$0\E line \Q$line\E/);
}

SKIP: {
    skip "no Scalar::Util in $]", 1 unless $have_weaken;
    my $struct = {};
    $struct->{recursive} = json_type_arrayof(json_type_weaken($struct));
    my $weakref = $struct->{recursive};
    weaken($weakref);
    undef $struct;
    ok(!defined $weakref);
}

SKIP: {
    skip "no Scalar::Util in $]", 1 unless $have_weaken;
    my $struct = {};
    $struct->{recursive} = json_type_hashof(json_type_weaken($struct));
    my $weakref = $struct->{recursive};
    weaken($weakref);
    undef $struct;
    ok(!defined $weakref);
}

SKIP: {
    skip "no Scalar::Util in $]", 1 unless $have_weaken;
    my $struct = {};
    $struct->{recursive} = json_type_anyof(json_type_weaken($struct));
    my $weakref = $struct->{recursive};
    weaken($weakref);
    undef $struct;
    ok(!defined $weakref);
}

ok(!defined eval { $cjson->encode(1) });
like($@, qr/type for '1' was not specified/);

ok(!defined eval { $cjson->encode(1, undef) });
like($@, qr/type for '1' was not specified/);

ok(!defined eval { $cjson->encode([1]) });
like($@, qr/type for 'ARRAY\(.*\)' was not specified/);

ok(!defined eval { $cjson->encode([1], undef) });
like($@, qr/type for 'ARRAY\(.*\)' was not specified/);

ok(!defined eval { $cjson->encode([1], [undef]) });
like($@, qr/type for '1' was not specified/);

ok(!defined eval { $cjson->encode({ key => 1 }) });
like($@, qr/type for 'HASH\(.*\)' was not specified/);

ok(!defined eval { $cjson->encode({ key => 1 }, undef) });
like($@, qr/type for 'HASH\(.*\)' was not specified/);

ok(!defined eval { $cjson->encode({ key => 1 }, { key => undef }) });
like($@, qr/type for '1' was not specified/);

ok(!defined eval { $cjson->encode(bless({}, 'Object'), JSON_TYPE_STRING) });
like($@, qr/encountered object.*but neither allow_blessed, convert_blessed nor allow_tags settings are enabled/);