yyjson-sys 0.1.0

yyjson-sys is a low-level Rust FFI binding for yyjson, a high-performance JSON parsing library. It provides direct access to yyjson's C API. This crate is intended for building higher-level abstractions or integrating yyjson with Rust projects that require minimal overhead.
Documentation
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
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
// This file is used to test the functionality of number reading and writing.
// It contains various test data to detect how numbers are handled in different
// boundary cases. The results are compared with google/double-conversion to
// ensure accuracy.

#include "yyjson.h"
#include "yy_test_utils.h"
#include "goo_double_conv.h"
#include <locale.h>

#if !YYJSON_DISABLE_READER && !YYJSON_DISABLE_WRITER

// IEEE 754 binary floating-point format is used in the current environment.
#define FP_IEEE_754 GOO_HAS_IEEE_754

// Whether yyjson use libc's strtod/sprintf instead of its built-in functions.
#define FP_USE_LIBC (!FP_IEEE_754 || YYJSON_DISABLE_FAST_FP_CONV)

// The decimal precision required to serialize/deserialize a floating-point value.
#ifndef FLT_DECIMAL_DIG
#define FLT_DECIMAL_DIG 9
#endif
#ifndef DBL_DECIMAL_DIG
#define DBL_DECIMAL_DIG 17
#endif


// =============================================================================
// Number conversion helper
// =============================================================================

/// Convert raw to float.
static yy_inline f32 f32_from_raw(u32 u) {
    f32 f;
    memcpy((void *)&f, (void *)&u, sizeof(u));
    return f;
}

/// Convert raw to double.
static yy_inline f64 f64_from_raw(u64 u) {
    f64 f;
    memcpy((void *)&f, (void *)&u, sizeof(u));
    return f;
}

/// Current locale decimal point.
static char locale_decimal_point = '.';

/// Update locale decimal point.
static void update_locale_decimal_point(void) {
    struct lconv *conv = localeconv();
    char c = conv->decimal_point[0];
    yy_assertf(c && conv->decimal_point[1] == '\0',
               "locale decimal point is invalid: %s\n", conv->decimal_point);
    locale_decimal_point = c;
}

/// Replace decimal point to current locale.
static void decimal_point_to_locale(char *buf) {
    char *ptr = strchr(buf, '.');
    if (ptr) *ptr = locale_decimal_point;
}

/// Reset decimal point to default minimal "C" locale.
static void decimal_point_to_std(char *buf) {
    char *ptr = strchr(buf, locale_decimal_point);
    if (ptr) *ptr = '.';
}



// =============================================================================
// Number conversion (libc)
// =============================================================================

/// Read float from string (libc).
static usize libc_f32_read(const char *str, f32 *val) {
    bool has_locale = (locale_decimal_point != '.');
    if (has_locale) {
        char *dup = yy_str_copy(str);
        decimal_point_to_locale(dup);
        str = dup;
    }
    char *end = NULL;
    *val = strtof(str, &end);
    usize len = end ? (end - str) : 0;
    if (has_locale) free((void *)str);
    return len;
}

/// Read double from string (libc).
static usize libc_f64_read(const char *str, f64 *val) {
    bool has_locale = (locale_decimal_point != '.');
    if (has_locale) {
        char *dup = yy_str_copy(str);
        decimal_point_to_locale(dup);
        str = dup;
    }
    char *end = NULL;
    *val = strtod(str, &end);
    usize len = end ? (end - str) : 0;
    if (has_locale) free((void *)str);
    return len;
}

/// Write float to string shortest (libc).
static usize libc_f32_write(f32 val, char *buf, usize len) {
    if (isinf(val)) return snprintf(buf, len, (val > 0) ? "Infinity" : "-Infinity");
    if (isnan(val)) return snprintf(buf, len, "NaN");
    int out_len = snprintf(buf, len, "%.*g", FLT_DECIMAL_DIG, val);
    if (locale_decimal_point != '.') decimal_point_to_std(buf);
    return (out_len >= (int)len) ? 0 : out_len;
}

/// Write double to string shortest (libc).
static usize libc_f64_write(f64 val, char *buf, usize len) {
    if (isinf(val)) return snprintf(buf, len, (val > 0) ? "Infinity" : "-Infinity");
    if (isnan(val)) return snprintf(buf, len, "NaN");
    int out_len = snprintf(buf, len, "%.*g", DBL_DECIMAL_DIG, val);
    if (locale_decimal_point != '.') decimal_point_to_std(buf);
    return (out_len >= (int)len) ? 0 : out_len;
}

/// Write double to string with fixed-point notation (libc).
static usize libc_f64_write_fixed(f64 val, int prec, char *buf, usize len) {
    if (isinf(val)) return snprintf(buf, len, (val > 0) ? "Infinity" : "-Infinity");
    if (isnan(val)) return snprintf(buf, len, "NaN");
    int out_len = snprintf(buf, len, "%.*f", prec, val);
    if (locale_decimal_point != '.') decimal_point_to_std(buf);
    return (out_len >= (int)len) ? 0 : out_len;
}



// =============================================================================
// Number conversion (google/double-conversion)
// =============================================================================

/// Read float from string (google/double-conversion).
static usize goo_f32_read(const char *str, f32 *val) {
    int str_len = (int)strlen(str);
    *val = goo_strtof(str, str_len, &str_len);
    return (usize)str_len;
}

/// Read double from string (google/double-conversion).
static usize goo_f64_read(const char *str, f64 *val) {
    int str_len = (int)strlen(str);
    *val = goo_strtod(str, str_len, &str_len);
    return (usize)str_len;
}

/// Write float to string shortest (google/double-conversion).
static usize goo_f32_write(f32 val, char *buf, usize len) {
    return (usize)goo_ftoa(val, GOO_FMT_SHORTEST, 0, buf, (int)len);
}

/// Write double to string shortest (google/double-conversion).
static usize goo_f64_write(f64 val, char *buf, usize len) {
    return (usize)goo_dtoa(val, GOO_FMT_SHORTEST, 0, buf, (int)len);
}

/// Write double to string with fixed-point notation (google/double-conversion).
static usize goo_f64_write_fixed(f64 val, int prec, char *buf, usize len) {
    return (usize)goo_dtoa(val, GOO_FMT_FIXED, prec, buf, (int)len);
}



// =============================================================================
// Number conversion (common)
// =============================================================================

/// Read float from string.
static usize f32_read(const char *str, f32 *val) {
#if FP_IEEE_754
    return goo_f32_read(str, val);
#else
    return libc_f32_read(str, val);
#endif
}

/// Read double from string.
static usize f64_read(const char *str, f64 *val) {
#if !FP_USE_LIBC
    return goo_f64_read(str, val);
#else
    return libc_f64_read(str, val);
#endif
}

/// Write float to string shortest.
static usize f32_write(f32 val, char *buf, usize len) {
#if !FP_USE_LIBC
    return goo_f32_write(val, buf, len);
#else
    return libc_f32_write(val, buf, len);
#endif
}

/// Write double to string shortest.
static usize f64_write(f64 val, char *buf, usize len) {
#if !FP_USE_LIBC
    return goo_f64_write(val, buf, len);
#else
    return libc_f64_write(val, buf, len);
#endif
}

/// Write double to string with fixed-point notation.
static usize f64_write_fixed(f64 val, int prec, char *buf, usize len) {
#if !FP_USE_LIBC
    return goo_f64_write_fixed(val, prec, buf, len);
#else
    return libc_f64_write_fixed(val, prec, buf, len);
#endif
}



// =============================================================================
// Number string format checker
// =============================================================================

/// number type
typedef enum {
    NUM_TYPE_FAIL,      // not a JSON number
    NUM_TYPE_SINT,      // signed integer
    NUM_TYPE_UINT,      // unsigned integer
    NUM_TYPE_REAL,      // real number
    NUM_TYPE_LITERAL    // nan or inf literal (non standard)
} num_type;

/// number information
typedef struct {
    const char *str;
    usize len;
    num_type type;
    bool int_overflow;
    bool real_overflow;
    i64 i;
    u64 u;
    f64 f;
} num_info;

/// Whether this character is digit.
static yy_inline bool char_is_digit(char c) {
    return '0' <= c && c <= '9';
}

/// Whether this character is sign.
static yy_inline bool char_is_sign(char c) {
    return c == '-' || c == '+';
}

/// Check for overflow when reading an integer number (uint64/int64).
static yy_inline bool check_int_overflow(const char *str, num_type type) {
    if (type != NUM_TYPE_SINT && type != NUM_TYPE_UINT) return false;
    
    bool sign = (*str == '-');
    str += sign;
    usize str_len = strlen(str);
    if (str_len < 19) return false;
    
    const char *max = sign ? "9223372036854775808" : "18446744073709551615";
    usize max_len = strlen(max);
    if (str_len > max_len) return true;
    if (str_len == max_len && strcmp(str, max) > 0) return true;
    return false;
}

/// Check for overflow when reading a real number (double).
static yy_inline bool check_real_overflow(const char *str, num_type type) {
    if (type != NUM_TYPE_SINT && type != NUM_TYPE_UINT && type != NUM_TYPE_REAL) return false;
    
    f64 val = 0;
    if (!f64_read(str, &val)) return false;
    return !!isinf(val);
}

/// Check JSON number string and return its type.
/// This checks only the string format, not for numeric overflow.
static yy_inline num_type get_num_type(const char *str) {
    if (!str) return NUM_TYPE_FAIL;
    
    // optional sign
    bool sign = (*str == '-');
    str += sign;
    
    // must begin with a digit
    if (!char_is_digit(*str)) {
        if (!yy_str_cmp(str, "nan", true) ||
            !yy_str_cmp(str, "inf", true) ||
            !yy_str_cmp(str, "infinity", true)) return NUM_TYPE_LITERAL;
        return NUM_TYPE_FAIL;
    }
    
    // leading zeros are not allowed
    if (*str == '0' && char_is_digit(str[1])) return NUM_TYPE_FAIL;
    
    // one or more digits
    while (char_is_digit(*str)) str++;
    
    // ending with integer type
    if (*str == '\0') return sign ? NUM_TYPE_SINT : NUM_TYPE_UINT;
    
    // optional fraction part
    if (*str == '.') {
        str++;
        // one or more digits
        if (!char_is_digit(*str)) return NUM_TYPE_FAIL;
        while (char_is_digit(*str)) str++;
    }
    
    // optional exponent part
    if (*str == 'e' || *str == 'E') {
        str++;
        // optional sign
        if (*str == '-' || *str == '+') str++;
        // one or more digits
        if (!char_is_digit(*str)) return NUM_TYPE_FAIL;
        while (char_is_digit(*str)) str++;
    }
    
    // ending with real type
    if (*str == '\0') return NUM_TYPE_REAL;
    return NUM_TYPE_FAIL;
}

/// Get number information from a string.
static yy_inline num_info get_num_info(const char *str) {
    num_info info = { 0 };
    info.str = str;
    info.len = str ? strlen(str) : 0;
    info.type = get_num_type(str);
    
    if (info.type == NUM_TYPE_FAIL) {
        return info;
    }
    
    if (info.type == NUM_TYPE_LITERAL) {
        bool sign = *str == '-';
        str += sign;
        info.f = (*str == 'n' || *str == 'N') ? NAN : (sign ? -INFINITY : INFINITY);
        return info;
    }
    
    if (info.type == NUM_TYPE_UINT || info.type == NUM_TYPE_SINT) {
        info.int_overflow = check_int_overflow(str, info.type);
        if (!info.int_overflow) {
            if (info.type == NUM_TYPE_UINT) {
                yy_assert(sizeof(unsigned long long) >= sizeof(u64));
                info.u = (u64)strtoull(str, NULL, 10);
                info.f = (f64)info.u;
            } else {
                yy_assert(sizeof(signed long long) >= sizeof(i64));
                info.i = (i64)strtoll(str, NULL, 10);
                info.f = (f64)info.i;
            }
            return info;
        }
    }
    
    // real number and integer overflow number
    f64 val = 0;
    yy_assert(f64_read(str, &val) > 0);
    info.f = val;
    info.real_overflow = !!isinf(val);
    return info;
}

/// Check if the number string is in its most compact (shortest) form.
static yy_inline bool check_num_compact(const char *str, num_type type) {
    if (type == NUM_TYPE_SINT || type == NUM_TYPE_UINT) {
        return true;
    }
    
    if (type == NUM_TYPE_LITERAL) {
        bool sign = *str == '-';
        str += sign;
        if (*str == 'n' || *str == 'N') return !sign; // sign is unnecessary for NaN
        return true;
    }
    
    if (type == NUM_TYPE_REAL) {
        // get decimal point and exponent part
        const char *dot = NULL, *exp = NULL, *end = NULL;
        const char *cur = str;
        while (*cur) {
            if (*cur == '.') dot = cur;
            else if (*cur == 'e' || *cur == 'E') exp = cur;
            cur++;
        }
        end = cur;
        
        // check fraction part
        if (dot) {
            if (exp) {
                if (*(exp - 1) == '0' ||
                    *(exp - 1) == '.') return false; // 1.0e23, 1.e23 -> 1e23
            } else {
                if (*(end - 1) == '0' &&
                    *(end - 2) != '.' &&
                    !char_is_digit(*(end - 2))) return false; // 1.10 -> 1.1
            }
        }
        
        // check exponent part
        if (exp) {
            if (exp[1] == '+') return false; // 1e+23 -> 1e23
            if (exp[1] == '0') return false; // 1e023 ->  1e23
        }
        return true;
    }
    
    return false;
}



// =============================================================================
// Number read and write
// =============================================================================

/// Validate a real number's output.
static void validate_real_output(const char *str,
                                 void *val_ptr, yyjson_write_flag flg) {
#define expect(expr) yy_assertf(expr, "num: %.17g, flg: %u, out: [%s]", num, flg, str)
    
    yyjson_val *val = val_ptr;
    yy_assert(yyjson_is_real(val));
    
    /// global flag
    bool allow_inf_nan = (flg & YYJSON_WRITE_ALLOW_INF_AND_NAN) != 0;
    bool inf_nan_to_null = (flg & YYJSON_WRITE_INF_AND_NAN_AS_NULL) != 0;
    bool to_float = (flg & YYJSON_WRITE_FP_TO_FLOAT) != 0;
    u32 to_fixed = flg >> (32 - YYJSON_WRITE_FP_PREC_BITS);
    
    /// value flag, should override global flag
    bool val_to_float = ((u32)(val->tag >> 32) & YYJSON_WRITE_FP_TO_FLOAT) != 0;
    u32 val_to_fixed = (u32)(val->tag >> 32) >> (32 - YYJSON_WRITE_FP_PREC_BITS);
    if (val_to_fixed) to_fixed = val_to_fixed;
    if (val_to_float) to_float = val_to_float;
    
    /// `to fixed` should override `to float`
    if (to_fixed) to_float = false;
    
    char buf[64];
    f64 num = val->uni.f64;
    if (to_float) num = (f32)num;
    
    if (isfinite(num)) {
        expect(get_num_type(str) == NUM_TYPE_REAL);
        expect(check_num_compact(str, NUM_TYPE_REAL));
        
        if (to_fixed && (-1e21 < num && num < 1e21)) {
            // To fixed-point.
            // This will remove trailing zeros and reduce unnecessary precision,
            // so the output string may not be the same as libc/google's.
            f64_write_fixed(num, to_fixed, buf, sizeof(buf));
            f64 out_num;
            expect(f64_read(str, &out_num) > 0);
            expect(f64_read(buf, &num) > 0);
            expect(out_num == num);
            
            char *dot = strchr(str, '.');
            expect(dot != NULL);
            usize digits_after_dot = strlen(str) - (usize)(dot - str) - 1;
            expect(digits_after_dot <= (usize)to_fixed);
            
        } else {
#if FP_USE_LIBC
            // To shortest.
            // The libc's output string may not be the shortest.
            f64 out_num;
            expect(f64_read(str, &out_num) > 0);
            if (to_float) {
                expect((f32)out_num == (f32)num);
            } else {
                expect(out_num == num);
            }
#else
            // To shortest.
            // The output string should be exactly the same as google's.
            if (to_float) {
                f32_write((f32)num, buf, sizeof(buf));
            } else {
                f64_write(num, buf, sizeof(buf));
            }
            expect(!strcmp(str, buf));
#endif
        }
    } else {
        if (inf_nan_to_null) {
            expect(!strcmp(str, "null"));
        }
#if !YYJSON_DISABLE_NON_STANDARD
        else if (allow_inf_nan) {
            if (isnan(num)) {
                expect(!strcmp(str, "NaN"));
            } else if (num > 0) {
                expect(!strcmp(str, "Infinity"));
            } else {
                expect(!strcmp(str, "-Infinity"));
            }
        }
#endif
        else {
            expect(!str);
        }
    }
#undef expect
}


/// Test number write with info and flag.
static void test_num_write(num_info info, yyjson_write_flag flg) {
#define expect(expr) yy_assertf(expr, "num str: [%s], flg: %u", info.str, flg)
    
    bool to_float = (flg & YYJSON_WRITE_FP_TO_FLOAT) != 0;
    u32 to_fixed = flg >> (32 - YYJSON_WRITE_FP_PREC_BITS);
    
    yyjson_mut_doc *doc = yyjson_mut_doc_new(NULL);
    
    /// write as real number
    f64 num = info.f;
    if (to_float && !to_fixed) num = (f32)num;
    
    yyjson_mut_val *val = yyjson_mut_real(doc, num);
    yyjson_mut_doc_set_root(doc, val);
    char *str = yyjson_mut_write(doc, flg, NULL);
    validate_real_output(str, val, flg);
    free(str);
    
    /// write as uint/sint
    if (info.type == NUM_TYPE_UINT && !info.int_overflow) {
        char buf[64];
        snprintf(buf, sizeof(buf), "%llu", (unsigned long long)info.u);
        val = yyjson_mut_uint(doc, info.u);
        yyjson_mut_doc_set_root(doc, val);
        str = yyjson_mut_write(doc, flg, NULL);
        expect(!strcmp(str, buf));
        free(str);
    } else if (info.type == NUM_TYPE_SINT && !info.int_overflow) {
        char buf[64];
        snprintf(buf, sizeof(buf), "%lld", (signed long long)info.i);
        val = yyjson_mut_sint(doc, info.i);
        yyjson_mut_doc_set_root(doc, val);
        str = yyjson_mut_write(doc, flg, NULL);
        expect(!strcmp(str, buf));
        free(str);
    }
    
    yyjson_mut_doc_free(doc);
    
#undef expect
}

/// Test number read with info and flag.
static void test_num_read(num_info info, yyjson_read_flag flg) {
#define expect(expr) yy_assertf(expr, "num str: [%s], flg: %u", str, flg)
    
    const char *str = info.str;
    usize len = info.len;
    yyjson_doc *doc = yyjson_read(str, len, flg);
    yyjson_val *val = yyjson_doc_get_root(doc);
    
#if YYJSON_DISABLE_NON_STANDARD
    bool non_std = false;
#else
    bool non_std = true;
#endif
    bool flg_big_raw = (flg & YYJSON_READ_BIGNUM_AS_RAW) != 0;
    bool flg_num_raw = (flg & YYJSON_READ_NUMBER_AS_RAW) != 0;
    bool flg_inf_nan = (flg & YYJSON_READ_ALLOW_INF_AND_NAN) != 0;
    
    if (info.type == NUM_TYPE_FAIL) {
        /// not a valid number
        expect(val == NULL);
        
    } else if (info.type == NUM_TYPE_LITERAL) {
        /// nan/inf literal
        if (flg_inf_nan && non_std) {
            if (flg_num_raw) {
                expect(yyjson_is_raw(val) && !strcmp(yyjson_get_raw(val), str));
            } else if (isnan(info.f)) {
                expect(yyjson_is_real(val) && isnan(yyjson_get_real(val)));
            } else {
                expect(yyjson_is_real(val) && yyjson_get_real(val) == info.f);
            }
        } else {
            expect(val == NULL);
        }
        
    } else if (flg_num_raw) {
        /// uint/sint/real -> raw
        expect(yyjson_is_raw(val) && !strcmp(yyjson_get_raw(val), str));
        
    } else if (info.real_overflow) {
        /// uint/sint/real -> overflow
        if (flg_big_raw) {
            expect(yyjson_is_raw(val) && !strcmp(yyjson_get_raw(val), str));
        } else if (non_std && flg_inf_nan) {
            expect(yyjson_is_real(val) && yyjson_get_real(val) == info.f);
        } else {
            expect(val == NULL);
        }
        
    } else if (info.int_overflow) {
        /// uint/sint overflow -> real
        if (flg_big_raw) {
            expect(yyjson_is_raw(val) && !strcmp(yyjson_get_raw(val), str));
        } else {
            expect(yyjson_is_real(val) && yyjson_get_real(val) == info.f);
        }
        
    } else if (info.type == NUM_TYPE_UINT) {
        /// uint
        expect(yyjson_is_uint(val) && yyjson_get_uint(val) == info.u);
        
    } else if (info.type == NUM_TYPE_SINT) {
        /// sint
        expect(yyjson_is_sint(val) && yyjson_get_sint(val) == info.i);
        
    } else if (info.type == NUM_TYPE_REAL) {
        /// real
        expect(yyjson_is_real(val) && yyjson_get_real(val) == info.f);
        
    }
    
    yyjson_val val_out = { 0 };
    const char *ptr = yyjson_read_number(str, &val_out, flg, NULL, NULL);
    if (val) {
        expect(yyjson_equals(val, &val_out));
        expect(ptr == str + len);
    } else {
        expect(ptr != str + len);
    }
    
    yyjson_doc_free(doc);
    
#undef expect
}

/// Test number read and write.
static void test_num_info(num_info info) {
    /// test read
    test_num_read(info, YYJSON_READ_NOFLAG);
    test_num_read(info, YYJSON_READ_BIGNUM_AS_RAW);
    test_num_read(info, YYJSON_READ_NUMBER_AS_RAW);
    test_num_read(info, YYJSON_READ_ALLOW_INF_AND_NAN);
    test_num_read(info, YYJSON_READ_BIGNUM_AS_RAW | YYJSON_READ_ALLOW_INF_AND_NAN);
    test_num_read(info, YYJSON_READ_NUMBER_AS_RAW | YYJSON_READ_ALLOW_INF_AND_NAN);
    
    /// test write
    test_num_write(info, YYJSON_WRITE_NOFLAG);
    test_num_write(info, YYJSON_WRITE_ALLOW_INF_AND_NAN);
    test_num_write(info, YYJSON_WRITE_INF_AND_NAN_AS_NULL);
    
    /// test write fp format
    test_num_write(info, YYJSON_WRITE_FP_TO_FLOAT);
    test_num_write(info, YYJSON_WRITE_FP_TO_FLOAT | YYJSON_WRITE_ALLOW_INF_AND_NAN);
    test_num_write(info, YYJSON_WRITE_FP_TO_FLOAT | YYJSON_WRITE_INF_AND_NAN_AS_NULL);
    for (int i = 1; i <= 15; i++) {
        test_num_write(info, YYJSON_WRITE_FP_TO_FIXED(i));
    }
}

/// Test all numbers from the txt files.
static void test_all_files(void) {
    /// get all files in the /test/data/num directory
    char dir[YY_MAX_PATH];
    yy_path_combine(dir, YYJSON_TEST_DATA_PATH, "data", "num", NULL);
    int count;
    char **names = yy_dir_read(dir, &count);
    yy_assertf(names != NULL && count != 0, "read dir fail:%s\n", dir);
    
    for (int i = 0; i < count; i++) {
        /// get full path of this file, ignore hidden and non-txt file
        char *name = names[i];
        if (*name == '.') continue;
        if (!yy_str_has_suffix(name, ".txt")) continue;
        char path[YY_MAX_PATH];
        yy_path_combine(path, dir, name, NULL);
        
        /// read this file to memory
        yy_dat dat;
        bool file_suc = yy_dat_init_with_file(&dat, path);
        yy_assertf(file_suc == true, "file read fail: %s\n", path);
        
        /// iterate over each line of the file
        usize len;
        char *line;
        while ((line = yy_dat_read_line(&dat, &len))) {
            /// ignore empty line and comment
            if (len == 0 || line[0] == '#') continue;
            /// add a null-terminator
            line[len] = '\0';
            /// test one line
            test_num_info(get_num_info(line));
        }
        
        yy_dat_release(&dat);
    }
    
    yy_dir_free(names);
}

/// Test some random integer read/write.
static void test_random_int(void) {
    int count = 10000;
    char buf[64] = { 0 };
    char *end;
    
    num_info info = { 0 };
    info.str = buf;
    
    yy_rand_reset(0);
    for (int i = 0; i < count; i++) {
        u64 r = yy_rand_u64();
        info.len = (usize)snprintf(buf, 32, "%llu", (unsigned long long)r);
        info.u = r;
        info.type = NUM_TYPE_UINT;
        test_num_info(info);
    }
    
    yy_rand_reset(0);
    for (int i = 0; i < count; i++) {
        i64 r = (i64)(yy_rand_u64() | ((u64)1 << 63));
        info.len = (usize)snprintf(buf, 32, "%lld", (signed long long)r);
        info.i = r;
        info.type = NUM_TYPE_SINT;
        test_num_info(info);
    }
    
    yy_rand_reset(0);
    for (int i = 0; i < count; i++) {
        u32 r = yy_rand_u32();
        info.len = (usize)snprintf(buf, 32, "%lu", (unsigned long)r);
        info.u = r;
        info.type = NUM_TYPE_UINT;
        test_num_info(info);
    }
    
    yy_rand_reset(0);
    for (int i = 0; i < count; i++) {
        i32 r = (i32)(yy_rand_u32() | ((u32)1 << 31));
        info.len = (usize)snprintf(buf, 32, "%li", (signed long)r);
        info.i = r;
        info.type = NUM_TYPE_SINT;
        test_num_info(info);
    }
}

/// Test real number read/write fast (do not test all flags).
static void test_real_fast(f64 num, yyjson_alc *alc,
                           bool test_to_float,
                           bool test_to_fixed) {
    char buf[64] = { 0 };
    char *str;
    usize len;
    
    yyjson_val val = { 0 };
    yyjson_set_real(&val, num);
    
    /// double to shortest
    str = yyjson_val_write_opts(&val, 0, alc, &len, NULL);
    validate_real_output(str, &val, 0);
    if (str) {
        yyjson_val val_out = { 0 };
        const char *end = yyjson_read_number(str, &val_out, 0, alc, NULL);
        yy_assert(end && *end == '\0');
        yy_assert(val_out.uni.f64 == val.uni.f64);
        alc->free(alc->ctx, str);
    }
    
    /// float to shortest
    if (test_to_float) {
        yyjson_write_flag flg = YYJSON_WRITE_FP_TO_FLOAT;
        str = yyjson_val_write_opts(&val, flg, alc, &len, NULL);
        validate_real_output(str, &val, flg);
        if (str) {
            yyjson_val val2 = { 0 };
            const char *end = yyjson_read_number(str, &val2, 0, alc, NULL);
            yy_assert(end && *end == '\0');
            
            f64 num2;
            f64_read(str, &num2);
            yy_assert(val2.uni.f64 == num2);
            alc->free(alc->ctx, str);
        }
    }
    
    /// double to fixed
    if (test_to_fixed) {
        for (int prec = 1; prec <= 15; prec++) {
            yyjson_write_flag flg = YYJSON_WRITE_FP_TO_FIXED(prec);
            str = yyjson_val_write_opts(&val, flg, alc, &len, NULL);
            validate_real_output(str, &val, flg);
            if (str) {
                yyjson_val val2 = { 0 };
                const char *end = yyjson_read_number(str, &val2, 0, alc, NULL);
                yy_assert(end && *end == '\0');
                
                f64 num2;
                f64_read(str, &num2);
                yy_assert(val2.uni.f64 == num2);
                alc->free(alc->ctx, str);
            }
        }
    }
}

/// Test some random real number read/write.
static void test_random_real(void) {
    int count = 10000;
    char alc_buf[4096];
    yyjson_alc alc;
    yyjson_alc_pool_init(&alc, alc_buf, sizeof(alc_buf));
    
    yy_rand_reset(0);
    for (int i = 0; i < count; i++) {
        u64 r = yy_rand_u64();
        f64 f = f64_from_raw(r);
        test_real_fast(f, &alc, true, true);
    }
    
    yy_rand_reset(0);
    for (int i = 0; i < count; i++) {
        u32 r = yy_rand_u32();
        f32 f = f32_from_raw(r);
        test_real_fast(f, &alc, true, true);
    }
}

/// Test some special real number read/write
static void test_special_real(void) {
    char alc_buf[4096];
    yyjson_alc alc;
    yyjson_alc_pool_init(&alc, alc_buf, sizeof(alc_buf));
    
    // short digits
    for (int sig = 1; sig <= 200; sig++) {
        for (int exp = -326; exp <= 308; exp++) {
            char buf[64];
            snprintf(buf, sizeof(buf), "%de%d", sig, exp);
            f64 num;
            f64_read(buf, &num);
            test_real_fast(num, &alc, true, true);
        }
    }
    
    // edge cases
    for (u64 exp = 0; exp <= 2046; exp++) {
        for (u64 sig = 0; sig <= 100; sig++) {
            u64 raw = (exp << 52) | sig;
            f64 num = f64_from_raw(raw);
            test_real_fast(num, &alc, true, true);
        }
        for (u64 sig = 0xFFFFFFFFFFFFFULL; sig >= (0xFFFFFFFFFFFFFULL - 100); sig--) {
            u64 raw = (exp << 52) | sig;
            f64 num = f64_from_raw(raw);
            test_real_fast(num, &alc, true, true);
        }
    }
}

/// Test all float number read/write.
static void test_all_float(void) {
    char alc_buf[4096];
    yyjson_alc alc;
    yyjson_alc_pool_init(&alc, alc_buf, sizeof(alc_buf));
    
    printf("--- begin test all float ---\n");
    f64 begin_time = yy_get_time();
    for (u32 i = 0, max = (u32)1 << 31; i < max; i++) {
        f32 f = f32_from_raw(i);
        test_real_fast((f64)f, &alc, true, false);
        
        // print progress
        if ((i << 8) == 0 && i) {
            f64 progress = (f64)i / max;
            f64 elapsed = yy_get_time() - begin_time;
            f64 expected = elapsed / (i + 1) * max;
            f64 remaining = expected - elapsed;
            printf("progress: %.2f%%, remaining: %.1f minutes\n",
                   progress * 100, remaining / 60);
        }
    }
    printf("--- end test all float ---\n");
}



// =============================================================================
// Test input types and flags
// =============================================================================

/// Test reader with different parameters.
static void test_read_params(void) {
    yyjson_val ival;
    yyjson_mut_val mval;
    const char *ptr;
    
    ptr = yyjson_read_number(NULL, &ival, 0, NULL, NULL);
    yy_assertf(ptr == NULL, "read line NULL should fail\n");
    ptr = yyjson_read_number("123", NULL, 0, NULL, NULL);
    yy_assertf(ptr == NULL, "read val NULL should fail\n");
    ptr = yyjson_read_number(NULL, NULL, 0, NULL, NULL);
    yy_assertf(ptr == NULL, "read line and val NULL should fail\n");
    
    ptr = yyjson_mut_read_number(NULL, &mval, 0, NULL, NULL);
    yy_assertf(ptr == NULL, "read line NULL should fail\n");
    ptr = yyjson_mut_read_number("123", NULL, 0, NULL, NULL);
    yy_assertf(ptr == NULL, "read val NULL should fail\n");
    ptr = yyjson_mut_read_number(NULL, NULL, 0, NULL, NULL);
    yy_assertf(ptr == NULL, "read line and val NULL should fail\n");
}

/// Test all combinations of number types and flags.
static void test_read_flags(void) {
    
    /// all number types
    const char *num_arr[] = {
        "0", // uint
        "-0", // sint
        "0.0", // real
        "-0.0", // real
        
        "123", // uint
        "-123", // sint
        "123.0", // real
        "-123.0", // real
        
        "9223372036854775808", // uint
        "9223372036854775808.0", // real
        
        "18446744073709551615", // uint
        "18446744073709551615.0", // real
        "18446744073709551616", // uint overflow
        "184467440737095516160", // uint overflow
        
        "-9223372036854775808", // sint
        "-9223372036854775808.0", // real
        "-9223372036854775809", // sint overflow
        "-92233720368547758090", // sint overflow
        
        "12345678901234567890123456789012345678901234567890"
        "12345678901234567890123456789012345678901234567890"
        "12345678901234567890123456789012345678901234567890"
        "12345678901234567890123456789012345678901234567890"
        "12345678901234567890123456789012345678901234567890"
        "12345678901234567890123456789012345678901234567890"
        "12345678901234567890123456789012345678901234567890"
        "12345678901234567890123456789012345678901234567890", // uint->real overflow
        "-12345678901234567890123456789012345678901234567890"
        "12345678901234567890123456789012345678901234567890"
        "12345678901234567890123456789012345678901234567890"
        "12345678901234567890123456789012345678901234567890"
        "12345678901234567890123456789012345678901234567890"
        "12345678901234567890123456789012345678901234567890"
        "12345678901234567890123456789012345678901234567890"
        "12345678901234567890123456789012345678901234567890", // sint->real overflow
        
        "123e999", // real overflow
        "-123e999", // real overflow
        
        "NaN", // nan
        "Inf", // inf
        "-Infinity", // -inf
        "001", // fail
    };
    
    /// all number flags
    yyjson_read_flag flag_arr[] = {
        YYJSON_READ_NUMBER_AS_RAW,
        YYJSON_READ_BIGNUM_AS_RAW,
        YYJSON_READ_ALLOW_INF_AND_NAN,
    };
    
    /// test number type
    for (usize i = 0; i < yy_nelems(num_arr); i++) {
        const char *num_str = num_arr[i];
        usize num_len = strlen(num_str);
        num_type type = get_num_type(num_str);
        
        /// test flag combination
        u32 flag_count = (u32)yy_nelems(flag_arr);
        u32 comb_count = 1 << flag_count;
        for (u32 c = 0; c < comb_count; c++) {
            yyjson_read_flag flg = 0;
            for (u32 f = 0; f < flag_count; f++) {
                if (c & (1 << f)) flg |= flag_arr[f];
            }
            
            /// doc read
            yyjson_doc *doc = yyjson_read(num_str, num_len, flg);
            yyjson_val *val = yyjson_doc_get_root(doc);
            
            {   /// val read
                yyjson_val val2;
                const char *end = yyjson_read_number(num_str, &val2, flg, NULL, NULL);
                if (val) {
                    yy_assert(yyjson_equals(val, &val2));
                    yy_assert(end && *end == '\0');
                } else {
                    yy_assert(!end);
                }
            }
            {   /// mut val read
                yyjson_mut_val val2;
                const char *end = yyjson_mut_read_number(num_str, &val2, flg, NULL, NULL);
                if (val) {
                    yy_assert(yyjson_equals(val, (yyjson_val *)&val2));
                    yy_assert(end && *end == '\0');
                } else {
                    yy_assert(!end);
                }
            }
            {   /// minity format read
                usize buf_len = num_len + 2;
                char *buf = calloc(1, buf_len + 1);
                buf[0] = '[';
                memcpy(buf + 1, num_str, num_len);
                buf[num_len + 1] = ']';
                yyjson_doc *doc2 = yyjson_read(buf, buf_len, flg);
                yyjson_val *val2 = yyjson_arr_get_first(yyjson_doc_get_root(doc2));
                yy_assert(val == val2 || yyjson_equals(val, val2));
                yyjson_doc_free(doc2);
                free(buf);
            }
            {   /// pretty format read
                usize buf_len = num_len + 6;
                char *buf = calloc(1, buf_len + 1);
                memcpy(buf, "[\n  ", 4);
                memcpy(buf + 4, num_str, num_len);
                memcpy(buf + 4 + num_len, "\n]", 2);
                yyjson_doc *doc2 = yyjson_read(buf, buf_len, flg);
                yyjson_val *val2 = yyjson_arr_get_first(yyjson_doc_get_root(doc2));
                yy_assert(val == val2 || yyjson_equals(val, val2));
                yyjson_doc_free(doc2);
                free(buf);
            }
            
#if YYJSON_DISABLE_NON_STANDARD
            flg &= ~YYJSON_READ_ALLOW_INF_AND_NAN;
#endif
            if (type == NUM_TYPE_FAIL) {
                /// invalid number format
                yy_assert(!doc);
            } else if (flg & YYJSON_READ_NUMBER_AS_RAW) {
                /// all number should be raw
                if (type == NUM_TYPE_LITERAL &&
                    !(flg & YYJSON_READ_ALLOW_INF_AND_NAN)) {
                    yy_assert(!doc);
                } else {
                    yy_assert(yyjson_is_raw(val));
                    yy_assert(!strcmp(num_str, yyjson_get_raw(val)));
                }
            } else switch (type) {
                case NUM_TYPE_SINT:
                case NUM_TYPE_UINT: {
                    /// integer number format
                    if (!check_int_overflow(num_str, type)) {
                        /// integer number not overflow
                        yy_assert(yyjson_is_int(val));
                    } else if (!check_real_overflow(num_str, type)) {
                        /// integer number overflow, but real number not overflow
                        if (flg & YYJSON_READ_BIGNUM_AS_RAW) {
                            yy_assert(yyjson_is_raw(val));
                            yy_assert(!strcmp(num_str, yyjson_get_raw(val)));
                        } else {
                            yy_assert(yyjson_is_real(val));
                        }
                    } else {
                        /// real number overflow
                        if (flg & YYJSON_READ_BIGNUM_AS_RAW) {
                            yy_assert(yyjson_is_raw(val));
                            yy_assert(!strcmp(num_str, yyjson_get_raw(val)));
                        } else if (flg & YYJSON_READ_ALLOW_INF_AND_NAN) {
                            yy_assert(yyjson_is_real(val));
                        } else {
                            yy_assert(!doc);
                        }
                    }
                    break;
                }
                case NUM_TYPE_REAL: {
                    /// real number
                    if (!check_real_overflow(num_str, type)) {
                        /// real number not overflow
                        yy_assert(yyjson_is_real(val));
                    } else {
                        /// real number overflow
                        if (flg & YYJSON_READ_BIGNUM_AS_RAW) {
                            yy_assert(yyjson_is_raw(val));
                            yy_assert(!strcmp(num_str, yyjson_get_raw(val)));
                        } else if (flg & YYJSON_READ_ALLOW_INF_AND_NAN) {
                            yy_assert(yyjson_is_real(val));
                        } else {
                            yy_assert(!doc);
                        }
                    }
                    break;
                }
                case NUM_TYPE_LITERAL: {
                    if ((flg & YYJSON_READ_ALLOW_INF_AND_NAN)) {
                        yy_assert(yyjson_is_real(val));
                    } else {
                        yy_assert(!doc);
                    }
                    break;
                }
                default: {
                    break;
                }
            }
            
            yyjson_doc_free(doc);
        }
    }
}

/// Test all combinations of number types and flags.
static void test_write_flags(void) {
    const u32 prec = 3;
    yyjson_write_flag flag_arr[] = {
        YYJSON_WRITE_ALLOW_INF_AND_NAN,
        YYJSON_WRITE_INF_AND_NAN_AS_NULL,
        YYJSON_WRITE_FP_TO_FLOAT,
        YYJSON_WRITE_FP_TO_FIXED(3),
    };
    
    /// test flag combination
    u32 flag_count = (u32)yy_nelems(flag_arr);
    u32 comb_count = 1 << flag_count;
    for (u32 c = 0; c < comb_count; c++) {
        yyjson_write_flag flg = 0;
        for (u32 f = 0; f < flag_count; f++) {
            if (c & (1 << f)) flg |= flag_arr[f];
        }
        bool allow_inf_nan = (flg & YYJSON_WRITE_ALLOW_INF_AND_NAN);
        bool inf_nan_as_null = (flg & YYJSON_WRITE_INF_AND_NAN_AS_NULL);
        bool to_float = (flg & YYJSON_WRITE_FP_TO_FLOAT) != 0;
        bool to_fixed = (flg >> (32 - YYJSON_WRITE_FP_PREC_BITS)) != 0;
        
        f64 num64 = 0.12345678901234567;
        f32 num32 = (f32)num64;
        yyjson_val val = { 0 };
        char *str;
        
        /// int
        yyjson_set_int(&val, 321);
        str = yyjson_val_write(&val, flg, NULL);
        yy_assert(get_num_type(str) == NUM_TYPE_UINT);
        free(str);
        
        /// float
        yyjson_set_float(&val, num32);
        str = yyjson_val_write(&val, flg, NULL);
        validate_real_output(str, &val, flg);
        free(str);
        
        /// float to fixed
        yyjson_set_float(&val, num32);
        yyjson_set_fp_to_fixed(&val, prec + 1);
        str = yyjson_val_write(&val, flg, NULL);
        validate_real_output(str, &val, flg);
        free(str);
        
        /// double
        yyjson_set_double(&val, num64);
        str = yyjson_val_write(&val, flg, NULL);
        validate_real_output(str, &val, flg);
        free(str);
        
        /// double to fixed
        yyjson_set_double(&val, num64);
        yyjson_set_fp_to_fixed(&val, prec + 1);
        str = yyjson_val_write(&val, flg, NULL);
        validate_real_output(str, &val, flg);
        free(str);
        
        /// inf
        num64 = INFINITY;
        yyjson_set_double(&val, num64);
        str = yyjson_val_write(&val, flg, NULL);
        validate_real_output(str, &val, flg);
        free(str);
        
        /// inf to fixed
        num64 = INFINITY;
        yyjson_set_double(&val, num64);
        yyjson_set_fp_to_fixed(&val, prec + 1);
        str = yyjson_val_write(&val, flg, NULL);
        validate_real_output(str, &val, flg);
        free(str);
        
        /// float inf
        num64 = 1e100;
        yyjson_set_double(&val, num64);
        str = yyjson_val_write(&val, flg, NULL);
        validate_real_output(str, &val, flg);
        free(str);
        
        /// float inf to fixed
        num64 = 1e100;
        yyjson_set_double(&val, num64);
        yyjson_set_fp_to_fixed(&val, prec + 1);
        str = yyjson_val_write(&val, flg, NULL);
        validate_real_output(str, &val, flg);
        free(str);
    }
    
    {   /// set val format
        yyjson_val val = { 0 };
        
        /// set float/double
        yyjson_set_float(&val, (float)1.25);
        yy_assert(yyjson_is_real(&val));
        yy_assert((float)yyjson_get_real(&val) == (float)1.25);
        yy_assert((val.tag >> 32) == YYJSON_WRITE_FP_TO_FLOAT);
        yyjson_set_double(&val, 1.25);
        yy_assert(yyjson_is_real(&val));
        yy_assert(yyjson_get_real(&val) == 1.25);
        yy_assert((val.tag >> 32) == 0);
        
        /// set to fixed
        yyjson_set_fp_to_fixed(&val, 12);
        yy_assert(yyjson_is_real(&val));
        yy_assert((val.tag >> 32) == YYJSON_WRITE_FP_TO_FIXED(12));
        yyjson_set_fp_to_fixed(&val, 0);
        yy_assert(yyjson_is_real(&val));
        yy_assert((val.tag >> 32) == YYJSON_WRITE_FP_TO_FIXED(0));
        
        /// set to float
        yyjson_set_fp_to_float(&val, true);
        yy_assert(yyjson_is_real(&val));
        yy_assert((val.tag >> 32) == YYJSON_WRITE_FP_TO_FLOAT);
        yyjson_set_fp_to_float(&val, false);
        yy_assert(yyjson_is_real(&val));
        yy_assert((val.tag >> 32) == 0);
    }
    {   /// set mut val format
        yyjson_mut_val val = { 0 };
        
        /// set float/double
        yyjson_mut_set_float(&val, (float)1.25);
        yy_assert(yyjson_mut_is_real(&val));
        yy_assert((float)yyjson_mut_get_real(&val) == (float)1.25);
        yy_assert((val.tag >> 32) == YYJSON_WRITE_FP_TO_FLOAT);
        yyjson_mut_set_double(&val, 1.25);
        yy_assert(yyjson_mut_is_real(&val));
        yy_assert(yyjson_mut_get_real(&val) == 1.25);
        yy_assert((val.tag >> 32) == 0);
        
        /// set to fixed
        yyjson_mut_set_fp_to_fixed(&val, 12);
        yy_assert(yyjson_mut_is_real(&val));
        yy_assert((val.tag >> 32) == YYJSON_WRITE_FP_TO_FIXED(12));
        yyjson_mut_set_fp_to_fixed(&val, 0);
        yy_assert(yyjson_mut_is_real(&val));
        yy_assert((val.tag >> 32) == YYJSON_WRITE_FP_TO_FIXED(0));
        
        /// set to float
        yyjson_mut_set_fp_to_float(&val, true);
        yy_assert(yyjson_mut_is_real(&val));
        yy_assert((val.tag >> 32) == YYJSON_WRITE_FP_TO_FLOAT);
        yyjson_mut_set_fp_to_float(&val, false);
        yy_assert(yyjson_mut_is_real(&val));
        yy_assert((val.tag >> 32) == 0);
    }
}



// =============================================================================
// Test locale
// =============================================================================

static void test_number_locale(void) {
    test_read_params();
    test_read_flags();
    test_write_flags();
    test_all_files();
}

static void test_number_extra(void) {
    test_random_int();
    test_random_real();
    test_special_real();
    
#if YYJSON_TEST_ALL_FLOAT || 0
    test_all_float(); /// costs too much time, disabled for regular testing
#endif
}

yy_test_case(test_number) {
    /// change locale (decimal point is ',')
    setlocale(LC_ALL, "fr_FR");
    update_locale_decimal_point();
    test_number_locale();
    
    /// reset locale (decimal point is '.')
    setlocale(LC_ALL, "C");
    update_locale_decimal_point();
    test_number_locale();
    
    /// test some extra numbers
    test_number_extra();
}

#else
yy_test_case(test_number) {}
#endif