/* This software is licensed by the MIT License, see LICENSE file */ /* Copyright © 2022 Gregory Lirent */ #include #include #include #include #include "modules/libcdsb/include/string.h" #include "include/jsonp.h" #define rfloat() (((vtype_float)rand() / (vtype_float)rand()) * ((rand()&1) ? -1 : 1)) #define rdouble() (((vtype_double)rand() / (vtype_double)rand()) * ((rand()&1) ? -1 : 1)) #define rldouble() (((vtype_ldouble)rand() / (vtype_ldouble)rand()) * ((rand()&1) ? -1 : 1)) #define rs8() ((vtype_int8)rand()) #define rs16() ((vtype_int16)rand()) #define rs32() ((vtype_int32)rand()) #define rs64() (((vtype_int64)rs32()) * ((vtype_int64)rs32())) #define ru8() ((vtype_uint8)rand()) #define ru16() ((vtype_uint16)rand()) #define ru32() ((vtype_uint32)rand()) #define ru64() ((vtype_uint64)rs64()) static void rand_json_object(vtype_string* x, int rec); static void rand_json_array(vtype_string* x, int rec); static void rand_json_spaces(vtype_string* x) { unsigned char ec = rand()%10; for (int i = 0; i < ec; ++i) { switch(rand()%4) { case 0: string_concat(x, ' '); break; case 1: string_concat(x, '\t'); break; case 2: string_concat(x, '\r'); break; case 3: string_concat(x, '\n'); break; } } } static void rand_json_number(vtype_string* x) { union { long long _lld; long double _Lf; char ret[128]; } val; if (rand()%2) { val._lld = rs64(); sprintf(val.ret, "%lld", val._lld); } else { val._Lf = rldouble(); sprintf(val.ret, "%Lg", val._Lf); } string_concat(x, val.ret); } static void rand_json_string(vtype_string* x, size_t minn, size_t maxn) { int n = (rand()%(maxn-minn))+minn; string_concat(x, '"'); while(n--) { int c = (rand()%95)+32; if (c == '"' || c == '\\') c = ' '; string_concat(x, c); } string_concat(x, '"'); } static void rand_json_value(vtype_string* x, int rec) { rand_json_spaces(x); switch(rand()%((rec < 5) ? 7 : 5)) { default: case 0: string_concat(x, "null"); break; case 1: string_concat(x, "false"); break; case 2: string_concat(x, "true"); break; case 3: rand_json_number(x); break; case 4: rand_json_string(x, 0, 4096>>rec); break; case 5: rand_json_object(x, ++rec); break; case 6: rand_json_array(x, ++rec); break; }; rand_json_spaces(x); } static void rand_json_object(vtype_string* x, int rec) { unsigned char ec = ru8()>>rec; string_concat(x, '{'); rand_json_spaces(x); for (int i = 0; i < ec; ++i) { rand_json_string(x, 1, 256>>rec); rand_json_spaces(x); string_concat(x, ':'); rand_json_value(x, rec); if (i + 1 < ec) { string_concat(x, ','); } rand_json_spaces(x); } string_concat(x, '}'); } static void rand_json_array(vtype_string* x, int rec) { unsigned char ec = ru8()>>rec; string_concat(x, '['); rand_json_spaces(x); for (int i = 0; i < ec; ++i) { rand_json_value(x, rec); if (i + 1 < ec) { string_concat(x, ','); } rand_json_spaces(x); } string_concat(x, ']'); } static void rand_json(vtype_string* x, int rec) { switch(rand()%10) { case 0: case 1: case 2: case 3: case 4: case 5: rand_json_object(x, rec); break; case 6: case 7: case 8: rand_json_array(x, rec); break; default: case 9: rand_json_value(x, rec); break; } } int main(int argc, char** argv) { json_t json; vtype_string x; time_t seed = time(0); srand(seed); printf("SEED: %ld\n\n", seed); string_init(&x, 0); rand_json (&x, 3); puts("\n#####################################################################################################################\n"); puts(x.buffer); json_parse(&json, &x); string_free(&x); puts("\n#####################################################################################################################\n"); x = libcjsonp_json_stringify(json.data, json.type, 4, 0); puts(x.buffer); puts("\n#####################################################################################################################\n"); libcjsonp_json_dump(stdout, json.data, json.type, 0, 0); puts("\n\n#####################################################################################################################\n"); string_free(&x); json_free(&json); return 0; }