Exemplo n.º 1
0
void test_suite_2_with_comments(void) {
	const char *filename = "tests/test_2_comments.txt";
	JSON_Value *root_value = NULL;
	root_value = json_parse_file_with_comments(filename);
	test_suite_2(root_value);
	TEST(json_value_equals(root_value, json_parse_string(json_serialize_to_string(root_value))));
	TEST(json_value_equals(root_value, json_parse_string(json_serialize_to_string_pretty(root_value))));
	json_value_free(root_value);
}
Exemplo n.º 2
0
Arquivo: tests.c Projeto: Kutoc/parson
void test_suite_4() {
    const char *filename = "tests/test_2.txt";
    JSON_Value *a = NULL, *a_copy = NULL;
    printf("Testing %s:\n", filename);
    a = json_parse_file(filename);
    TEST(json_value_equals(a, a)); /* test equality test */
    a_copy = json_value_deep_copy(a);
    TEST(a_copy != NULL);
    TEST(json_value_equals(a, a_copy));
}
Exemplo n.º 3
0
Arquivo: tests.c Projeto: Kutoc/parson
void test_suite_6(void) {
    const char *filename = "tests/test_2.txt";
    JSON_Value *a = NULL;
    JSON_Value *b = NULL;
    a = json_parse_file(filename);
    b = json_parse_file(filename);
    TEST(json_value_equals(a, b));
    json_object_set_string(json_object(a), "string", "eki");
    TEST(!json_value_equals(a, b));
    a = json_value_deep_copy(b);
    TEST(json_value_equals(a, b));
    json_array_append_number(json_object_get_array(json_object(b), "string array"), 1337);
    TEST(!json_value_equals(a, b));
}
Exemplo n.º 4
0
Arquivo: tests.c Projeto: Kutoc/parson
void test_suite_8(void) {
    const char *filename = "tests/test_2.txt";
    const char *temp_filename = "tests/test_2_serialized.txt";
    JSON_Value *a = NULL;
    JSON_Value *b = NULL;
    a = json_parse_file(filename);
    TEST(json_serialize_to_file(a, temp_filename) == JSONSuccess);
    b = json_parse_file(temp_filename);
    TEST(json_value_equals(a, b));
    remove(temp_filename);
}
Exemplo n.º 5
0
void test_suite_8(void) {
	const char *filename = "tests/test_2.txt";
	const char *temp_filename = "tests/test_2_serialized.txt";
	JSON_Value *a = NULL;
	JSON_Value *b = NULL;
	char *buf = NULL;
	size_t serialization_size = 0;
	a = json_parse_file(filename);
	TEST(json_serialize_to_file(a, temp_filename) == JSONSuccess);
	b = json_parse_file(temp_filename);
	TEST(json_value_equals(a, b));
	remove(temp_filename);
	serialization_size = json_serialization_size(a);
	buf = json_serialize_to_string(a);
	TEST((strlen(buf) + 1) == serialization_size);
}
Exemplo n.º 6
0
void test_suite_9(void) {
	const char *filename = "tests/test_2_pretty.txt";
	const char *temp_filename = "tests/test_2_serialized_pretty.txt";
	char *file_contents = NULL;
	char *serialized = NULL;
	JSON_Value *a = NULL;
	JSON_Value *b = NULL;
	size_t serialization_size = 0;
	a = json_parse_file(filename);
	TEST(json_serialize_to_file_pretty(a, temp_filename) == JSONSuccess);
	b = json_parse_file(temp_filename);
	TEST(json_value_equals(a, b));
	remove(temp_filename);
	serialization_size = json_serialization_size_pretty(a);
	serialized = json_serialize_to_string_pretty(a);
	TEST((strlen(serialized) + 1) == serialization_size);

	file_contents = read_file(filename);
	TEST(STREQ(file_contents, serialized));
}
Exemplo n.º 7
0
Arquivo: tests.c Projeto: Kutoc/parson
void test_suite_5(void) {
    JSON_Value *val_from_file = json_parse_file("tests/test_5.txt");
    
    JSON_Value *val = json_value_init_object();
    JSON_Object *obj = json_value_get_object(val);
    TEST(json_object_set_string(obj, "first", "John") == JSONSuccess);
    TEST(json_object_set_string(obj, "last", "Doe") == JSONSuccess);
    TEST(json_object_set_number(obj, "age", 25) == JSONSuccess);
    TEST(json_object_set_boolean(obj, "registered", 1) == JSONSuccess);
    TEST(json_object_set_value(obj, "interests", json_value_init_array()) == JSONSuccess);
    TEST(json_array_append_string(json_object_get_array(obj, "interests"), "Writing") == JSONSuccess);
    TEST(json_array_append_string(json_object_get_array(obj, "interests"), "Mountain Biking") == JSONSuccess);
    TEST(json_array_replace_string(json_object_get_array(obj, "interests"), 0, "Reading") == JSONSuccess);
    TEST(json_object_dotset_string(obj, "favorites.color", "blue") == JSONSuccess);
    TEST(json_object_dotset_string(obj, "favorites.sport", "running") == JSONSuccess);
    TEST(json_object_dotset_string(obj, "favorites.fruit", "apple") == JSONSuccess);
    TEST(json_object_dotremove(obj, "favorites.fruit") == JSONSuccess);
    TEST(json_object_set_string(obj, "utf string", "\\u006corem\\u0020ipsum") == JSONSuccess);
    TEST(json_object_set_string(obj, "utf-8 string", "あいうえお") == JSONSuccess);
    TEST(json_object_set_string(obj, "surrogate string", "lorem\\uD834\\uDD1Eipsum\\uD834\\uDF67lorem") == JSONSuccess);
    TEST(json_value_equals(val_from_file, val));
}
Exemplo n.º 8
0
/*typically "left" is the output of SERIALIZE... and right is hand-coded JSON*/
static bool areTwoJsonsEqual(const unsigned char* left, size_t leftSize, const char* right)
{
    bool result;

    char* cloneOfLeft = (char*)malloc(leftSize + 1); /*because of out SERIALIZE... there is a byte array that is NOT '\0' terminated*/
    ASSERT_IS_NOT_NULL(cloneOfLeft);

    (void)memcpy(cloneOfLeft, left, leftSize);
    cloneOfLeft[leftSize] = '\0';

    JSON_Value* actualJson = json_parse_string((char*)cloneOfLeft);
    ASSERT_IS_NOT_NULL(actualJson);
    JSON_Value* expectedJson = json_parse_string(right);
    ASSERT_IS_NOT_NULL(expectedJson);

    result = (json_value_equals(expectedJson, actualJson) != 0);

    json_value_free(expectedJson);
    json_value_free(actualJson);
    free(cloneOfLeft);

    return result;
}
Exemplo n.º 9
0
Arquivo: tests.c Projeto: Kutoc/parson
void test_suite_1(void) {
    JSON_Value *val;
    TEST((val = json_parse_file("tests/test_1_1.txt")) != NULL);
    TEST(json_value_equals(json_parse_string(json_serialize_to_string(val)), val));
    if (val) { json_value_free(val); }
    TEST((val = json_parse_file("tests/test_1_2.txt")) != NULL);
    TEST(json_value_equals(json_parse_string(json_serialize_to_string(val)), val));
    if (val) { json_value_free(val); }
    TEST((val = json_parse_file("tests/test_1_3.txt")) != NULL);
    TEST(json_value_equals(json_parse_string(json_serialize_to_string(val)), val));
    if (val) { json_value_free(val); }
    
    TEST((val = json_parse_file_with_comments("tests/test_1_1.txt")) != NULL);
    TEST(json_value_equals(json_parse_string(json_serialize_to_string(val)), val));
    if (val) { json_value_free(val); }
    TEST((val = json_parse_file_with_comments("tests/test_1_2.txt")) != NULL);
    TEST(json_value_equals(json_parse_string(json_serialize_to_string(val)), val));
    if (val) { json_value_free(val); }
    TEST((val = json_parse_file_with_comments("tests/test_1_3.txt")) != NULL);
    TEST(json_value_equals(json_parse_string(json_serialize_to_string(val)), val));
    if (val) { json_value_free(val); }
}
Exemplo n.º 10
0
void test_suite_5(void) {
	JSON_Value *val_from_file = json_parse_file("tests/test_5.txt");

	JSON_Value *val = NULL;
	JSON_Object *obj = NULL;
	JSON_Array *interests_arr = NULL;

	val = json_value_init_object();
	TEST(val != NULL);

	obj = json_value_get_object(val);
	TEST(obj != NULL);

	TEST(json_object_set_string(obj, "first", "John") == JSONSuccess);
	TEST(json_object_set_string(obj, "last", "Doe") == JSONSuccess);
	TEST(json_object_set_number(obj, "age", 25) == JSONSuccess);
	TEST(json_object_set_boolean(obj, "registered", 1) == JSONSuccess);

	TEST(json_object_set_value(obj, "interests", json_value_init_array()) == JSONSuccess);
	interests_arr = json_object_get_array(obj, "interests");
	TEST(interests_arr != NULL);
	TEST(json_array_append_string(interests_arr, "Writing") == JSONSuccess);
	TEST(json_array_append_string(interests_arr, "Mountain Biking") == JSONSuccess);
	TEST(json_array_replace_string(interests_arr, 0, "Reading") == JSONSuccess);

	TEST(json_object_dotset_string(obj, "favorites.color", "blue") == JSONSuccess);
	TEST(json_object_dotset_string(obj, "favorites.sport", "running") == JSONSuccess);
	TEST(json_object_dotset_string(obj, "favorites.fruit", "apple") == JSONSuccess);
	TEST(json_object_dotremove(obj, "favorites.fruit") == JSONSuccess);
	TEST(json_object_set_string(obj, "utf string", "lorem ipsum") == JSONSuccess);
	TEST(json_object_set_string(obj, "utf-8 string", "あいうえお") == JSONSuccess);
	TEST(json_object_set_string(obj, "surrogate string", "lorem𝄞ipsum𝍧lorem") == JSONSuccess);
	TEST(json_object_set_string(obj, "windows path", "C:\\Windows\\Path") == JSONSuccess);
	TEST(json_value_equals(val_from_file, val));

	TEST(json_object_set_string(obj, NULL, "") == JSONFailure);
	TEST(json_object_set_string(obj, "last", NULL) == JSONFailure);
	TEST(json_object_set_string(obj, NULL, NULL) == JSONFailure);
	TEST(json_object_set_value(obj, NULL, NULL) == JSONFailure);

	TEST(json_object_dotset_string(obj, NULL, "") == JSONFailure);
	TEST(json_object_dotset_string(obj, "favorites.color", NULL) == JSONFailure);
	TEST(json_object_dotset_string(obj, NULL, NULL) == JSONFailure);
	TEST(json_object_dotset_value(obj, NULL, NULL) == JSONFailure);

	TEST(json_array_append_string(NULL, "lorem") == JSONFailure);
	TEST(json_array_append_value(interests_arr, NULL) == JSONFailure);
	TEST(json_array_append_value(NULL, NULL) == JSONFailure);

	TEST(json_array_remove(NULL, 0) == JSONFailure);
	TEST(json_array_replace_value(interests_arr, 0, NULL) == JSONFailure);
	TEST(json_array_replace_string(NULL, 0, "lorem") == JSONFailure);
	TEST(json_array_replace_string(interests_arr, 100, "not existing") == JSONFailure);

	TEST(json_array_append_string(json_object_get_array(obj, "interests"), NULL) == JSONFailure);


	/* UTF-8 tests */
	TEST(json_object_set_string(obj, "correct string", "κόσμε") == JSONSuccess);

	TEST(json_object_set_string(obj, "boundary 1", "\xed\x9f\xbf") == JSONSuccess);
	TEST(json_object_set_string(obj, "boundary 2", "\xee\x80\x80") == JSONSuccess);
	TEST(json_object_set_string(obj, "boundary 3", "\xef\xbf\xbd") == JSONSuccess);
	TEST(json_object_set_string(obj, "boundary 4", "\xf4\x8f\xbf\xbf") == JSONSuccess);

	TEST(json_object_set_string(obj, "first continuation byte", "\x80") == JSONFailure);
	TEST(json_object_set_string(obj, "last continuation byte", "\xbf") == JSONFailure);

	TEST(json_object_set_string(obj, "impossible sequence 1", "\xfe") == JSONFailure);
	TEST(json_object_set_string(obj, "impossible sequence 2", "\xff") == JSONFailure);
	TEST(json_object_set_string(obj, "impossible sequence 3", "\xfe\xfe\xff\xff") == JSONFailure);

	TEST(json_object_set_string(obj, "overlong 1", "\xc0\xaf") == JSONFailure);
	TEST(json_object_set_string(obj, "overlong 2", "\xc1\xbf") == JSONFailure);
	TEST(json_object_set_string(obj, "overlong 3", "\xe0\x80\xaf") == JSONFailure);
	TEST(json_object_set_string(obj, "overlong 4", "\xe0\x9f\xbf") == JSONFailure);
	TEST(json_object_set_string(obj, "overlong 5", "\xf0\x80\x80\xaf") == JSONFailure);
	TEST(json_object_set_string(obj, "overlong 6", "\xf0\x8f\xbf\xbf") == JSONFailure);
	TEST(json_object_set_string(obj, "overlong 7", "\xf0\x8f\xbf\xbf") == JSONFailure);

	TEST(json_object_set_string(obj, "overlong null 1", "\xc0\x80") == JSONFailure);
	TEST(json_object_set_string(obj, "overlong null 2", "\xe0\x80\x80") == JSONFailure);
	TEST(json_object_set_string(obj, "overlong null 3", "\xf0\x80\x80\x80") == JSONFailure);
	TEST(json_object_set_string(obj, "overlong null 4", "\xf8\x80\x80\x80\x80") == JSONFailure);
	TEST(json_object_set_string(obj, "overlong null 5", "\xfc\x80\x80\x80\x80\x80") == JSONFailure);

	TEST(json_object_set_string(obj, "single surrogate 1", "\xed\xa0\x80") == JSONFailure);
	TEST(json_object_set_string(obj, "single surrogate 2", "\xed\xaf\xbf") == JSONFailure);
	TEST(json_object_set_string(obj, "single surrogate 3", "\xed\xbf\xbf") == JSONFailure);
}