Skip to content

Commit cc89138

Browse files
committed
update the test case: arrayInsertAtRandomIndex
1 parent 14ac99d commit cc89138

File tree

1 file changed

+49
-16
lines changed

1 file changed

+49
-16
lines changed

src/test_lib_json/main.cpp

Lines changed: 49 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -312,36 +312,69 @@ JSONTEST_FIXTURE(ValueTest, arrayIssue252) {
312312
}
313313
JSONTEST_FIXTURE(ValueTest, arrayInsertAtRandomIndex) {
314314
Json::Value array;
315-
JSONCPP_STRING str0 = "index2";
316-
JSONCPP_STRING str1 = "index4";
317-
array.append("index0");
315+
Json::Value str0("index2");
316+
Json::Value str1("index3");
317+
array.append("index0"); // append rvalue
318318
array.append("index1");
319-
array.append(str0);
320-
JSONTEST_ASSERT_EQUAL(Json::Value("index0"), array[0]);
319+
array.append(str0); // append lvalue
320+
321+
std::vector<Json::Value *> vec; // storage value address for checking
322+
for(int i = 0 ; i < 3 ; i++) {
323+
vec.push_back(&array[i]);
324+
}
325+
JSONTEST_ASSERT_EQUAL(Json::Value("index0"), array[0]); // check append
321326
JSONTEST_ASSERT_EQUAL(Json::Value("index1"), array[1]);
322327
JSONTEST_ASSERT_EQUAL(Json::Value("index2"), array[2]);
323328

324-
array.insert(0, "index3"); // rvalue
329+
// insert rvalue at the head
330+
array.insert(0, str1);
325331
JSONTEST_ASSERT_EQUAL(Json::Value("index3"), array[0]);
326332
JSONTEST_ASSERT_EQUAL(Json::Value("index0"), array[1]);
327333
JSONTEST_ASSERT_EQUAL(Json::Value("index1"), array[2]);
328334
JSONTEST_ASSERT_EQUAL(Json::Value("index2"), array[3]);
329-
330-
array.insert(3, str1); // lvalue
335+
// checking address
336+
for(int i = 0 ; i < 3 ; i++) {
337+
JSONTEST_ASSERT_EQUAL(vec[i], &array[i]);
338+
}
339+
vec.push_back(&array[3]);
340+
// insert lvalue at middle
341+
array.insert(2, "index4");
331342
JSONTEST_ASSERT_EQUAL(Json::Value("index3"), array[0]);
332343
JSONTEST_ASSERT_EQUAL(Json::Value("index0"), array[1]);
333-
JSONTEST_ASSERT_EQUAL(Json::Value("index1"), array[2]);
334-
JSONTEST_ASSERT_EQUAL(Json::Value("index4"), array[3]);
344+
JSONTEST_ASSERT_EQUAL(Json::Value("index4"), array[2]);
345+
JSONTEST_ASSERT_EQUAL(Json::Value("index1"), array[3]);
335346
JSONTEST_ASSERT_EQUAL(Json::Value("index2"), array[4]);
336-
337-
array.insert(9, "index6");
338-
// beyond size(). it should be allowed to insert into its tail.
347+
// checking address
348+
for(int i = 0 ; i < 4 ; i++) {
349+
JSONTEST_ASSERT_EQUAL(vec[i], &array[i]);
350+
}
351+
vec.push_back(&array[4]);
352+
// insert rvalue at the tail
353+
array.insert(5, "index5");
339354
JSONTEST_ASSERT_EQUAL(Json::Value("index3"), array[0]);
340355
JSONTEST_ASSERT_EQUAL(Json::Value("index0"), array[1]);
341-
JSONTEST_ASSERT_EQUAL(Json::Value("index1"), array[2]);
342-
JSONTEST_ASSERT_EQUAL(Json::Value("index4"), array[3]);
356+
JSONTEST_ASSERT_EQUAL(Json::Value("index4"), array[2]);
357+
JSONTEST_ASSERT_EQUAL(Json::Value("index1"), array[3]);
358+
JSONTEST_ASSERT_EQUAL(Json::Value("index2"), array[4]);
359+
JSONTEST_ASSERT_EQUAL(Json::Value("index5"), array[5]);
360+
// checking address
361+
for(int i = 0 ; i < 5 ; i++) {
362+
JSONTEST_ASSERT_EQUAL(vec[i], &array[i]);
363+
}
364+
vec.push_back(&array[5]);
365+
// beyond max array size, it should be allowed to insert into its tail
366+
array.insert(10, "index10");
367+
JSONTEST_ASSERT_EQUAL(Json::Value("index3"), array[0]);
368+
JSONTEST_ASSERT_EQUAL(Json::Value("index0"), array[1]);
369+
JSONTEST_ASSERT_EQUAL(Json::Value("index4"), array[2]);
370+
JSONTEST_ASSERT_EQUAL(Json::Value("index1"), array[3]);
343371
JSONTEST_ASSERT_EQUAL(Json::Value("index2"), array[4]);
344-
JSONTEST_ASSERT_EQUAL(Json::Value("index6"), array[5]);
372+
JSONTEST_ASSERT_EQUAL(Json::Value("index5"), array[5]);
373+
JSONTEST_ASSERT_EQUAL(Json::Value("index10"), array[6]);
374+
// checking address
375+
for(int i = 0 ; i < 6 ; i++) {
376+
JSONTEST_ASSERT_EQUAL(vec[i], &array[i]);
377+
}
345378
}
346379
JSONTEST_FIXTURE(ValueTest, null) {
347380
JSONTEST_ASSERT_EQUAL(Json::nullValue, null_.type());

0 commit comments

Comments
 (0)