static int test_enum(void) { enum avro_languages { AVRO_C, AVRO_CPP, AVRO_PYTHON, AVRO_RUBY, AVRO_JAVA }; avro_schema_t schema = avro_schema_enum("language"); avro_datum_t datum = avro_enum(schema, AVRO_C); avro_schema_enum_symbol_append(schema, "C"); avro_schema_enum_symbol_append(schema, "C++"); avro_schema_enum_symbol_append(schema, "Python"); avro_schema_enum_symbol_append(schema, "Ruby"); avro_schema_enum_symbol_append(schema, "Java"); if (avro_enum_get(datum) != AVRO_C) { fprintf(stderr, "Unexpected enum value AVRO_C\n"); exit(EXIT_FAILURE); } if (strcmp(avro_enum_get_name(datum), "C") != 0) { fprintf(stderr, "Unexpected enum value name C\n"); exit(EXIT_FAILURE); } write_read_check(schema, datum, NULL, NULL, "enum"); test_json(datum, "\"C\""); avro_enum_set(datum, AVRO_CPP); if (strcmp(avro_enum_get_name(datum), "C++") != 0) { fprintf(stderr, "Unexpected enum value name C++\n"); exit(EXIT_FAILURE); } write_read_check(schema, datum, NULL, NULL, "enum"); test_json(datum, "\"C++\""); avro_enum_set_name(datum, "Python"); if (avro_enum_get(datum) != AVRO_PYTHON) { fprintf(stderr, "Unexpected enum value AVRO_PYTHON\n"); exit(EXIT_FAILURE); } write_read_check(schema, datum, NULL, NULL, "enum"); test_json(datum, "\"Python\""); avro_datum_decref(datum); avro_schema_decref(schema); return 0; }
static int test_record(void) { avro_schema_t schema = avro_schema_record("person", NULL); avro_schema_record_field_append(schema, "name", avro_schema_string()); avro_schema_record_field_append(schema, "age", avro_schema_int()); avro_datum_t datum = avro_record(schema); avro_datum_t name_datum, age_datum; name_datum = avro_givestring("Joseph Campbell", NULL); age_datum = avro_int32(83); avro_record_set(datum, "name", name_datum); avro_record_set(datum, "age", age_datum); write_read_check(schema, datum, NULL, NULL, "record"); test_json(datum, "{\"name\": \"Joseph Campbell\", \"age\": 83}"); int rc; avro_record_set_field_value(rc, datum, int32, "age", 104); int32_t age = 0; avro_record_get_field_value(rc, datum, int32, "age", &age); if (age != 104) { fprintf(stderr, "Incorrect age value\n"); exit(EXIT_FAILURE); } avro_datum_decref(name_datum); avro_datum_decref(age_datum); avro_datum_decref(datum); avro_schema_decref(schema); return 0; }
static int test_array(void) { int i, rval; avro_schema_t schema = avro_schema_array(avro_schema_int()); avro_datum_t datum = avro_array(schema); for (i = 0; i < 10; i++) { avro_datum_t i32_datum = avro_int32(i); rval = avro_array_append_datum(datum, i32_datum); avro_datum_decref(i32_datum); if (rval) { exit(EXIT_FAILURE); } } if (avro_array_size(datum) != 10) { fprintf(stderr, "Unexpected array size"); exit(EXIT_FAILURE); } write_read_check(schema, datum, NULL, NULL, "array"); test_json(datum, "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]"); avro_datum_decref(datum); avro_schema_decref(schema); return 0; }
static int test_int64(void) { int i; avro_schema_t writer_schema = avro_schema_long(); avro_schema_t float_schema = avro_schema_float(); avro_schema_t double_schema = avro_schema_double(); for (i = 0; i < 100; i++) { int64_t value = rand_int64(); avro_datum_t datum = avro_int64(value); avro_datum_t float_datum = avro_float(value); avro_datum_t double_datum = avro_double(value); write_read_check(writer_schema, datum, NULL, NULL, "long"); write_read_check(writer_schema, datum, float_schema, float_datum, "long->float"); write_read_check(writer_schema, datum, double_schema, double_datum, "long->double"); avro_datum_decref(datum); avro_datum_decref(float_datum); avro_datum_decref(double_datum); } avro_datum_t datum = avro_int64(10000); test_json(datum, "10000"); avro_datum_decref(datum); avro_schema_decref(writer_schema); avro_schema_decref(float_schema); avro_schema_decref(double_schema); return 0; }
static int test_bytes(void) { char bytes[] = { 0xDE, 0xAD, 0xBE, 0xEF }; avro_schema_t writer_schema = avro_schema_bytes(); avro_datum_t datum; avro_datum_t expected_datum; datum = avro_givebytes(bytes, sizeof(bytes), NULL); write_read_check(writer_schema, datum, NULL, NULL, "bytes"); test_json(datum, "\"\\u00de\\u00ad\\u00be\\u00ef\""); avro_datum_decref(datum); avro_schema_decref(writer_schema); datum = avro_givebytes(NULL, 0, NULL); avro_givebytes_set(datum, bytes, sizeof(bytes), NULL); expected_datum = avro_givebytes(bytes, sizeof(bytes), NULL); if (!avro_datum_equal(datum, expected_datum)) { fprintf(stderr, "Expected equal bytes instances.\n"); exit(EXIT_FAILURE); } avro_datum_decref(datum); avro_datum_decref(expected_datum); // The following should bork if we don't copy the bytes value // correctly (since we'll try to free a static string). datum = avro_bytes("original", 8); avro_bytes_set(datum, "alsothis", 8); avro_datum_decref(datum); avro_schema_decref(writer_schema); return 0; }
static int test_string(void) { unsigned int i; const char *strings[] = { "Four score and seven years ago", "our father brought forth on this continent", "a new nation", "conceived in Liberty", "and dedicated to the proposition that all men are created equal." }; avro_schema_t writer_schema = avro_schema_string(); for (i = 0; i < sizeof(strings) / sizeof(strings[0]); i++) { avro_datum_t datum = avro_givestring(strings[i], NULL); write_read_check(writer_schema, datum, NULL, NULL, "string"); avro_datum_decref(datum); } avro_datum_t datum = avro_givestring(strings[0], NULL); test_json(datum, "\"Four score and seven years ago\""); avro_datum_decref(datum); // The following should bork if we don't copy the string value // correctly (since we'll try to free a static string). datum = avro_string("this should be copied"); avro_string_set(datum, "also this"); avro_datum_decref(datum); avro_schema_decref(writer_schema); return 0; }
static int test_fixed(void) { char bytes[] = { 0xD, 0xA, 0xD, 0xA, 0xB, 0xA, 0xB, 0xA }; avro_schema_t schema = avro_schema_fixed("msg", sizeof(bytes)); avro_datum_t datum; avro_datum_t expected_datum; datum = avro_givefixed(schema, bytes, sizeof(bytes), NULL); write_read_check(schema, datum, NULL, NULL, "fixed"); test_json(datum, "\"\\r\\n\\r\\n\\u000b\\n\\u000b\\n\""); avro_datum_decref(datum); datum = avro_givefixed(schema, NULL, sizeof(bytes), NULL); avro_givefixed_set(datum, bytes, sizeof(bytes), NULL); expected_datum = avro_givefixed(schema, bytes, sizeof(bytes), NULL); if (!avro_datum_equal(datum, expected_datum)) { fprintf(stderr, "Expected equal fixed instances.\n"); exit(EXIT_FAILURE); } avro_datum_decref(datum); avro_datum_decref(expected_datum); // The following should bork if we don't copy the fixed value // correctly (since we'll try to free a static string). datum = avro_fixed(schema, "original", 8); avro_fixed_set(datum, "alsothis", 8); avro_datum_decref(datum); avro_schema_decref(schema); return 0; }
static int test_union(void) { avro_schema_t schema = avro_schema_union(); avro_datum_t union_datum; avro_datum_t datum; avro_datum_t union_datum1; avro_datum_t datum1; avro_schema_union_append(schema, avro_schema_string()); avro_schema_union_append(schema, avro_schema_int()); avro_schema_union_append(schema, avro_schema_null()); datum = avro_givestring("Follow your bliss.", NULL); union_datum = avro_union(schema, 0, datum); if (avro_union_discriminant(union_datum) != 0) { fprintf(stderr, "Unexpected union discriminant\n"); exit(EXIT_FAILURE); } if (avro_union_current_branch(union_datum) != datum) { fprintf(stderr, "Unexpected union branch datum\n"); exit(EXIT_FAILURE); } union_datum1 = avro_datum_from_schema(schema); avro_union_set_discriminant(union_datum1, 0, &datum1); avro_givestring_set(datum1, "Follow your bliss.", NULL); if (!avro_datum_equal(datum, datum1)) { fprintf(stderr, "Union values should be equal\n"); exit(EXIT_FAILURE); } write_read_check(schema, union_datum, NULL, NULL, "union"); test_json(union_datum, "{\"string\": \"Follow your bliss.\"}"); avro_datum_decref(datum); avro_union_set_discriminant(union_datum, 2, &datum); test_json(union_datum, "null"); avro_datum_decref(union_datum); avro_datum_decref(datum); avro_datum_decref(union_datum1); avro_schema_decref(schema); return 0; }
int main (int argc, char ** argv) { printf ("YAJL TESTS\n"); printf ("==================\n\n"); modules = ksNew (0, KS_END); elektraModulesInit (modules, 0); init (argc, argv); test_nextNotBelow (); test_reverseLevel (); test_countLevel (); test_writing (); test_json ("yajl/testdata_null.json", getNullKeys (), ksNew (0, KS_END)); test_json ("yajl/testdata_boolean.json", getBooleanKeys (), ksNew (0, KS_END)); test_json ("yajl/testdata_number.json", getNumberKeys (), ksNew (0, KS_END)); test_json ("yajl/testdata_string.json", getStringKeys (), ksNew (0, KS_END)); test_json ("yajl/testdata_maps.json", getMapKeys (), ksNew (0, KS_END)); test_json ("yajl/testdata_array.json", getArrayKeys (), ksNew (0, KS_END)); test_json ("yajl/testdata_below.json", getBelowKeys (), ksNew (0, KS_END)); test_json ("yajl/OpenICC_device_config_DB.json", getOpenICCKeys (), ksNew (0, KS_END)); // TODO currently do not have a KeySet, wait for C-plugin to make // it easy to generate it.. test_readWrite ("yajl/empty_object.json", ksNew (0, KS_END)); test_readWrite ("yajl/empty_array.json", ksNew (0, KS_END)); test_readWrite ("yajl/top_level_string.json", ksNew (0, KS_END)); test_readWrite ("yajl/top_level_integer.json", ksNew (0, KS_END)); test_readWrite ("yajl/rfc_object.json", ksNew (0, KS_END)); test_readWrite ("yajl/rfc_array.json", ksNew (0, KS_END)); test_readWrite ("yajl/testdata_array_mixed.json", ksNew (0, KS_END)); test_readWrite ("yajl/testdata_array_in_array.json", ksNew (0, KS_END)); test_readWrite ("yajl/testdata_array_in_array_anon_map.json", ksNew (0, KS_END)); test_readWrite ("yajl/testdata_array_nested.json", ksNew (0, KS_END)); test_readWrite ("yajl/testdata_array_broken.json", ksNew (0, KS_END)); test_readWrite ("yajl/testdata_array_special_ending.json", ksNew (0, KS_END)); test_readWrite ("yajl/testdata_array_outside.json", ksNew (0, KS_END)); test_readWrite ("yajl/keyframes_complex.json", ksNew (0, KS_END)); test_readWrite ("yajl/testdata_array_mixed2.json", ksNew (0, KS_END)); test_readWrite ("yajl/testdata_array_special_start.json", ksNew (0, KS_END)); test_readWrite ("yajl/testdata_array_mixed3.json", ksNew (0, KS_END)); test_readWrite ("yajl/testdata_empty_in_array.json", ksNew (0, KS_END)); test_readWrite ("yajl/testdata_empty_in_map.json", ksNew (0, KS_END)); test_readWrite ("yajl/testdata_empty_in_array1.json", ksNew (0, KS_END)); test_readWrite ("yajl/testdata_empty_in_map2.json", ksNew (0, KS_END)); test_readWrite ("yajl/testdata_empty_in_map1.json", ksNew (0, KS_END)); elektraModulesClose (modules, 0); ksDel (modules); print_result ("test_yajl"); return nbError; }
static int test_null(void) { avro_schema_t schema = avro_schema_null(); avro_datum_t datum = avro_null(); write_read_check(schema, datum, NULL, NULL, "null"); test_json(datum, "null"); avro_datum_decref(datum); return 0; }
int main () { test_json("Array", "[ 0, 1, 2, 4]"); test_json("Empty list", " []"); test_json("Empty list", "{\"label\": [], \"label2\": []}"); test_json("Empty object", "{\"l1\": {}, \"l2\": {}}"); printf("Basic test\n"); test_basic (); printf("Basic test\n"); test_lines (); return 0; }
int main(void) { test_json(); #ifdef WIN32 getchar(); #endif return (0); }
static int test_map(void) { avro_schema_t schema = avro_schema_map(avro_schema_long()); avro_datum_t datum = avro_map(schema); int64_t i = 0; char *nums[] = { "zero", "one", "two", "three", "four", "five", "six", NULL }; while (nums[i]) { avro_datum_t i_datum = avro_int64(i); avro_map_set(datum, nums[i], i_datum); avro_datum_decref(i_datum); i++; } if (avro_array_size(datum) != 7) { fprintf(stderr, "Unexpected map size\n"); exit(EXIT_FAILURE); } avro_datum_t value; const char *key; avro_map_get_key(datum, 2, &key); avro_map_get(datum, key, &value); int64_t val; avro_int64_get(value, &val); if (val != 2) { fprintf(stderr, "Unexpected map value 2\n"); exit(EXIT_FAILURE); } int index; if (avro_map_get_index(datum, "two", &index)) { fprintf(stderr, "Can't get index for key \"two\": %s\n", avro_strerror()); exit(EXIT_FAILURE); } if (index != 2) { fprintf(stderr, "Unexpected index for key \"two\"\n"); exit(EXIT_FAILURE); } if (!avro_map_get_index(datum, "foobar", &index)) { fprintf(stderr, "Unexpected index for key \"foobar\"\n"); exit(EXIT_FAILURE); } write_read_check(schema, datum, NULL, NULL, "map"); test_json(datum, "{\"zero\": 0, \"one\": 1, \"two\": 2, \"three\": 3, " "\"four\": 4, \"five\": 5, \"six\": 6}"); avro_datum_decref(datum); avro_schema_decref(schema); return 0; }
static int test_boolean(void) { int i; const char *expected_json[] = { "false", "true" }; avro_schema_t schema = avro_schema_boolean(); for (i = 0; i <= 1; i++) { avro_datum_t datum = avro_boolean(i); write_read_check(schema, datum, NULL, NULL, "boolean"); test_json(datum, expected_json[i]); avro_datum_decref(datum); } avro_schema_decref(schema); return 0; }
static int execute_test(const char *dir_name) { DIR *d; struct dirent *dent; char path[PATH_MAX]; int ret = 0, exit_code = 0; struct nftnl_parse_err *err; d = opendir(dir_name); if (d == NULL) { perror("opendir"); exit(EXIT_FAILURE); } err = nftnl_parse_err_alloc(); if (err == NULL) { perror("error"); exit(EXIT_FAILURE); } while ((dent = readdir(d)) != NULL) { int len = strlen(dent->d_name); if (strcmp(dent->d_name, ".") == 0 || strcmp(dent->d_name, "..") == 0) continue; snprintf(path, sizeof(path), "%s/%s", dir_name, dent->d_name); if (strcmp(&dent->d_name[len-4], ".xml") == 0) { if ((ret = test_xml(path, err)) == 0) { if (!update) { printf("parsing and validating %s: ", path); printf("\033[32mOK\e[0m\n"); } } exit_code += ret; } if (strcmp(&dent->d_name[len-5], ".json") == 0) { if ((ret = test_json(path, err)) == 0) { if (!update) { printf("parsing and validating %s: ", path); printf("\033[32mOK\e[0m\n"); } } exit_code += ret; }
static int test_double(void) { int i; avro_schema_t schema = avro_schema_double(); for (i = 0; i < 100; i++) { avro_datum_t datum = avro_double(rand_number(-1.0E10, 1.0E10)); write_read_check(schema, datum, NULL, NULL, "double"); avro_datum_decref(datum); } avro_datum_t datum = avro_double(2000.0); test_json(datum, "2000.0"); avro_datum_decref(datum); avro_schema_decref(schema); return 0; }
static int test_float(void) { int i; avro_schema_t schema = avro_schema_float(); avro_schema_t double_schema = avro_schema_double(); for (i = 0; i < 100; i++) { float value = rand_number(-1.0E10, 1.0E10); avro_datum_t datum = avro_float(value); avro_datum_t double_datum = avro_double(value); write_read_check(schema, datum, NULL, NULL, "float"); write_read_check(schema, datum, double_schema, double_datum, "float->double"); avro_datum_decref(datum); avro_datum_decref(double_datum); } avro_datum_t datum = avro_float(2000.0); test_json(datum, "2000.0"); avro_datum_decref(datum); avro_schema_decref(schema); avro_schema_decref(double_schema); return 0; }
TEST(JSON, simple_packed_oom) { test_json("[\"a\",1,true,false,null]"); }
TEST(JSON, simple_mixed_oom) { test_json("{\"a\":1,\"b\":2.3,\"3\":\"test\"}"); }
int Test::all() { clock_t begin = clock(); exeTime = 0; test_general(); test_types(); test_booleans(); test_numbers(); test_strings(); test_arrays(); test_intervals(); test_map(); test_set(); test_objects(); test_functions(); test_classes(); test_loops(); test_operators(); test_references(); test_exceptions(); test_operations(); test_system(); test_json(); test_files(); test_doc(); test_utils(); double elapsed_secs = double(clock() - begin) / CLOCKS_PER_SEC; int errors = (total - success_count); int leaks = (obj_created - obj_deleted); int mpz_leaks = (mpz_obj_created - mpz_obj_deleted); std::ostringstream line1, line2, line3, line4; line1 << "Total : " << total << ", success : " << success_count << ", errors : " << errors; line2 << "Total time : " << elapsed_secs * 1000 << " ms"; line3 << "Objects destroyed : " << obj_deleted << " / " << obj_created << " (" << leaks << " leaked)"; line4 << "MPZ objects destroyed : " << mpz_obj_deleted << " / " << mpz_obj_created << " (" << mpz_leaks << " leaked)"; unsigned w = std::max(line1.str().size(), std::max(line2.str().size(), std::max(line3.str().size(), line4.str().size()))); auto pad = [](std::string s, int l) { l -= s.size(); while (l-- > 0) s += " "; return s; }; std::cout << "┌"; for (unsigned i = 0; i < w + 2; ++i) std::cout << "─"; std::cout << "┐" << std::endl; std::cout << "│ " << pad(line1.str(), w) << " │" << std::endl; std::cout << "│ " << pad(line2.str(), w) << " │" << std::endl; std::cout << "│ " << pad(line3.str(), w) << " │" << std::endl; std::cout << "│ " << pad(line4.str(), w) << " │" << std::endl; std::cout << "├"; for (unsigned i = 0; i < w + 2; ++i) std::cout << "─"; std::cout << "┤"; std::cout << std::endl; int result = abs(errors) + abs(leaks) + abs(mpz_leaks); if (result == 0) { std::cout << "│ " << pad("GOOD! ✔", w + 2) << " │" << std::endl; } else { std::cout << "│ " << pad("BAD! : " + std::to_string(result) + " error(s) ✘", w + 2) << " │" << std::endl; } std::cout << "└"; for (unsigned i = 0; i < w + 2; ++i) std::cout << "─"; std::cout << "┘" << std::endl; for (const auto& error : failed_tests) { std::cout << " " << error << std::endl; } if (failed_tests.size()) { std::cout << std::endl; } return result; }