Skip to content

Commit 45b8d27

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

File tree

2 files changed

+59
-20
lines changed

2 files changed

+59
-20
lines changed

src/lib_json/json_value.cpp

+9-4
Original file line numberDiff line numberDiff line change
@@ -1169,17 +1169,22 @@ Value const& Value::operator[](CppTL::ConstString const& key) const {
11691169
}
11701170
#endif
11711171
Value& Value::append(const Value& value) { return (*this)[size()] = value; }
1172+
11721173
Value& Value::append(Value&& value) {
11731174
return (*this)[size()] = std::move(value);
11741175
}
1176+
11751177
/// \brief Insert value in array at specific index
11761178
bool Value::insert(ArrayIndex index, const Value& newValue) {
1177-
return insert(index,Value(newValue));
1179+
JSON_ASSERT_MESSAGE(
1180+
type() == arrayValue,
1181+
"in Json::Value::insert: requires arrayValue");
1182+
return insert(index,Value(newValue));
11781183
}
11791184
bool Value::insert(ArrayIndex index, Value&& newValue) {
1180-
if (type() != arrayValue) {
1181-
return false;
1182-
}
1185+
JSON_ASSERT_MESSAGE(
1186+
type() == arrayValue,
1187+
"in Json::Value::insert: requires arrayValue");
11831188
ArrayIndex length = size();
11841189
if (index > length) {
11851190
append(std::move(newValue));

src/test_lib_json/main.cpp

+50-16
Original file line numberDiff line numberDiff line change
@@ -310,38 +310,72 @@ JSONTEST_FIXTURE(ValueTest, arrayIssue252) {
310310
}
311311
// JSONTEST_ASSERT_EQUAL(5, root["array"].size());
312312
}
313+
313314
JSONTEST_FIXTURE(ValueTest, arrayInsertAtRandomIndex) {
314315
Json::Value array;
315-
JSONCPP_STRING str0 = "index2";
316-
JSONCPP_STRING str1 = "index4";
317-
array.append("index0");
316+
Json::Value str0("index2");
317+
Json::Value str1("index3");
318+
array.append("index0"); // append rvalue
318319
array.append("index1");
319-
array.append(str0);
320-
JSONTEST_ASSERT_EQUAL(Json::Value("index0"), array[0]);
320+
array.append(str0); // append lvalue
321+
322+
std::vector<Json::Value *> vec; // storage value address for checking
323+
for(int i = 0 ; i < 3 ; i++) {
324+
vec.push_back(&array[i]);
325+
}
326+
JSONTEST_ASSERT_EQUAL(Json::Value("index0"), array[0]); // check append
321327
JSONTEST_ASSERT_EQUAL(Json::Value("index1"), array[1]);
322328
JSONTEST_ASSERT_EQUAL(Json::Value("index2"), array[2]);
323329

324-
array.insert(0, "index3"); // rvalue
330+
// insert lvalue at the head
331+
array.insert(0, str1);
325332
JSONTEST_ASSERT_EQUAL(Json::Value("index3"), array[0]);
326333
JSONTEST_ASSERT_EQUAL(Json::Value("index0"), array[1]);
327334
JSONTEST_ASSERT_EQUAL(Json::Value("index1"), array[2]);
328335
JSONTEST_ASSERT_EQUAL(Json::Value("index2"), array[3]);
329-
330-
array.insert(3, str1); // lvalue
336+
// checking address
337+
for(int i = 0 ; i < 3 ; i++) {
338+
JSONTEST_ASSERT_EQUAL(vec[i], &array[i]);
339+
}
340+
vec.push_back(&array[3]);
341+
// insert rvalue at middle
342+
array.insert(2, "index4");
331343
JSONTEST_ASSERT_EQUAL(Json::Value("index3"), array[0]);
332344
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]);
345+
JSONTEST_ASSERT_EQUAL(Json::Value("index4"), array[2]);
346+
JSONTEST_ASSERT_EQUAL(Json::Value("index1"), array[3]);
335347
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.
348+
// checking address
349+
for(int i = 0 ; i < 4 ; i++) {
350+
JSONTEST_ASSERT_EQUAL(vec[i], &array[i]);
351+
}
352+
vec.push_back(&array[4]);
353+
// insert rvalue at the tail
354+
array.insert(5, "index5");
339355
JSONTEST_ASSERT_EQUAL(Json::Value("index3"), array[0]);
340356
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]);
357+
JSONTEST_ASSERT_EQUAL(Json::Value("index4"), array[2]);
358+
JSONTEST_ASSERT_EQUAL(Json::Value("index1"), array[3]);
343359
JSONTEST_ASSERT_EQUAL(Json::Value("index2"), array[4]);
344-
JSONTEST_ASSERT_EQUAL(Json::Value("index6"), array[5]);
360+
JSONTEST_ASSERT_EQUAL(Json::Value("index5"), array[5]);
361+
// checking address
362+
for(int i = 0 ; i < 5 ; i++) {
363+
JSONTEST_ASSERT_EQUAL(vec[i], &array[i]);
364+
}
365+
vec.push_back(&array[5]);
366+
// beyond max array size, it should be allowed to insert into its tail
367+
array.insert(10, "index10");
368+
JSONTEST_ASSERT_EQUAL(Json::Value("index3"), array[0]);
369+
JSONTEST_ASSERT_EQUAL(Json::Value("index0"), array[1]);
370+
JSONTEST_ASSERT_EQUAL(Json::Value("index4"), array[2]);
371+
JSONTEST_ASSERT_EQUAL(Json::Value("index1"), array[3]);
372+
JSONTEST_ASSERT_EQUAL(Json::Value("index2"), array[4]);
373+
JSONTEST_ASSERT_EQUAL(Json::Value("index5"), array[5]);
374+
JSONTEST_ASSERT_EQUAL(Json::Value("index10"), array[6]);
375+
// checking address
376+
for(int i = 0 ; i < 6 ; i++) {
377+
JSONTEST_ASSERT_EQUAL(vec[i], &array[i]);
378+
}
345379
}
346380
JSONTEST_FIXTURE(ValueTest, null) {
347381
JSONTEST_ASSERT_EQUAL(Json::nullValue, null_.type());

0 commit comments

Comments
 (0)