Order of items in an array is preserved after removing an item.

Issue #81
pull/85/head
Krzysztof Gabis 7 years ago
parent d485b068c7
commit 343fe13f17
  1. 15
      parson.c
  2. 14
      tests.c

@ -1547,20 +1547,13 @@ void json_free_serialized_string(char *string) {
}
JSON_Status json_array_remove(JSON_Array *array, size_t ix) {
JSON_Value *temp_value = NULL;
size_t last_element_ix = 0;
size_t to_move_bytes = 0;
if (array == NULL || ix >= json_array_get_count(array)) {
return JSONFailure;
}
last_element_ix = json_array_get_count(array) - 1;
json_value_free(json_array_get_value(array, ix));
if (ix != last_element_ix) { /* Replace value with one from the end of array */
temp_value = json_array_get_value(array, last_element_ix);
if (temp_value == NULL) {
return JSONFailure;
}
array->items[ix] = temp_value;
}
to_move_bytes = (json_array_get_count(array) - 1 - ix) * sizeof(JSON_Value**);
memmove(array->items + ix, array->items + ix + 1, to_move_bytes);
array->count -= 1;
return JSONSuccess;
}
@ -1918,7 +1911,7 @@ JSON_Status json_validate(const JSON_Value *schema, const JSON_Value *value) {
}
}
JSON_Status json_value_equals(const JSON_Value *a, const JSON_Value *b) {
int json_value_equals(const JSON_Value *a, const JSON_Value *b) {
JSON_Object *a_object = NULL, *b_object = NULL;
JSON_Array *a_array = NULL, *b_array = NULL;
const char *a_string = NULL, *b_string = NULL;

@ -263,6 +263,7 @@ void test_suite_3(void) {
TEST(json_parse_string("x") == NULL);
TEST(json_parse_string("{:\"no name\"}") == NULL);
TEST(json_parse_string("[,\"no first value\"]") == NULL);
TEST(json_parse_string("{\"key\"\"value\"}") == NULL);
TEST(json_parse_string("[\"\\u00zz\"]") == NULL); /* invalid utf value */
TEST(json_parse_string("[\"\\u00\"]") == NULL); /* invalid utf value */
TEST(json_parse_string("[\"\\u\"]") == NULL); /* invalid utf value */
@ -306,6 +307,9 @@ void test_suite_5(void) {
JSON_Object *obj = NULL;
JSON_Array *interests_arr = NULL;
JSON_Value *remove_test_val = NULL;
JSON_Array *remove_test_arr = NULL;
val = json_value_init_object();
TEST(val != NULL);
@ -407,6 +411,16 @@ void test_suite_5(void) {
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);
/* Testing removing values from array, order of the elements should be preserved */
remove_test_val = json_parse_string("[1, 2, 3, 4, 5]");
remove_test_arr = json_array(remove_test_val);
json_array_remove(remove_test_arr, 2);
TEST(json_value_equals(remove_test_val, json_parse_string("[1, 2, 4, 5]")));
json_array_remove(remove_test_arr, 0);
TEST(json_value_equals(remove_test_val, json_parse_string("[2, 4, 5]")));
json_array_remove(remove_test_arr, 2);
TEST(json_value_equals(remove_test_val, json_parse_string("[2, 4]")));
}
void test_suite_6(void) {

Loading…
Cancel
Save