summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorVyacheslav Tyutyunkov <tve@softmotions.com>2017-03-24 15:56:06 +0700
committerƁukasz Stelmach <l.stelmach@samsung.com>2017-04-20 12:45:35 +0200
commit5ffbf0182884ea5c1f5632f102b07168bcb30b43 (patch)
treefda15b8110a3b9c424d99f2b3f879d1c358c9c2b /src
parente9a62d9693c1b42d44e277febf8de58cf2643db0 (diff)
downloadejdb-5ffbf0182884ea5c1f5632f102b07168bcb30b43.tar.gz
ejdb-5ffbf0182884ea5c1f5632f102b07168bcb30b43.tar.bz2
ejdb-5ffbf0182884ea5c1f5632f102b07168bcb30b43.zip
#170
Diffstat (limited to 'src')
-rw-r--r--src/bson/bson.c2
-rw-r--r--src/bson/tests/bsontest.c54
2 files changed, 54 insertions, 2 deletions
diff --git a/src/bson/bson.c b/src/bson/bson.c
index 16b6e51..6ecde34 100644
--- a/src/bson/bson.c
+++ b/src/bson/bson.c
@@ -1829,7 +1829,7 @@ static void _bson_fix_duplicate_keys(bson_iterator *it, bson *bso) {
int ind = 0;
int j = -1;
while(++j < TCLISTNUM(dups)) {
- buf = tclistval(dups, TCLISTNUM(dups) - 1, &bufsz);
+ buf = tclistval(dups, j, &bufsz);
memcpy(&itoff, buf, sizeof(itoff));
it2.cur = it->cur + itoff;
it2.first = itoff == 0 ? it->first : 0;
diff --git a/src/bson/tests/bsontest.c b/src/bson/tests/bsontest.c
index 935c3da..b31ab0d 100644
--- a/src/bson/tests/bsontest.c
+++ b/src/bson/tests/bsontest.c
@@ -152,6 +152,57 @@ void testCheckDuplicates(void) {
CU_ASSERT_EQUAL(bson_iterator_int(&it), 1);
}
+void testCheckDuplicates2(void) {
+ bson b, b2;
+ bson_iterator it, sit;
+ bson_type bt;
+
+ bson_init(&b);
+ bson_append_start_array(&b, "array");
+ bson_append_int(&b, "0", 1);
+ bson_append_finish_array(&b);
+ bson_append_start_array(&b, "array");
+ bson_append_int(&b, "0", 3);
+ bson_append_int(&b, "1", 4);
+ bson_append_finish_array(&b);
+ bson_finish(&b);
+ CU_ASSERT_FALSE_FATAL(b.err);
+
+ CU_ASSERT_TRUE_FATAL(bson_check_duplicate_keys(&b));
+
+ bson_init(&b2);
+ bson_fix_duplicate_keys(&b, &b2);
+ bson_finish(&b2);
+ CU_ASSERT_FALSE_FATAL(b2.err);
+
+ CU_ASSERT_FALSE_FATAL(bson_check_duplicate_keys(&b2));
+
+ BSON_ITERATOR_INIT(&it, &b2);
+ bt = bson_iterator_next(&it);
+ CU_ASSERT_EQUAL_FATAL(bt, BSON_ARRAY);
+ CU_ASSERT_STRING_EQUAL_FATAL(BSON_ITERATOR_KEY(&it), "array");
+ BSON_ITERATOR_SUBITERATOR(&it, &sit);
+
+ bt = bson_iterator_next(&sit);
+ CU_ASSERT_STRING_EQUAL_FATAL(BSON_ITERATOR_KEY(&sit), "0");
+ CU_ASSERT_TRUE_FATAL(BSON_IS_NUM_TYPE(bt));
+ CU_ASSERT_EQUAL_FATAL(bson_iterator_int(&sit), 1);
+ bt = bson_iterator_next(&sit);
+ CU_ASSERT_STRING_EQUAL_FATAL(BSON_ITERATOR_KEY(&sit), "1");
+ CU_ASSERT_TRUE_FATAL(BSON_IS_NUM_TYPE(bt));
+ CU_ASSERT_EQUAL_FATAL(bson_iterator_int(&sit), 3);
+ bt = bson_iterator_next(&sit);
+ CU_ASSERT_STRING_EQUAL_FATAL(BSON_ITERATOR_KEY(&sit), "2");
+ CU_ASSERT_TRUE_FATAL(BSON_IS_NUM_TYPE(bt));
+ CU_ASSERT_EQUAL_FATAL(bson_iterator_int(&sit), 4);
+
+ bt = bson_iterator_next(&sit);
+ CU_ASSERT_EQUAL_FATAL(bt, BSON_EOO);
+
+ bson_destroy(&b2);
+ bson_destroy(&b);
+}
+
int main() {
setlocale(LC_ALL, "en_US.UTF-8");
CU_pSuite pSuite = NULL;
@@ -168,7 +219,8 @@ int main() {
}
/* Add the tests to the suite */
- if ((NULL == CU_add_test(pSuite, "testCheckDuplicates", testCheckDuplicates))
+ if ((NULL == CU_add_test(pSuite, "testCheckDuplicates", testCheckDuplicates)) ||
+ (NULL == CU_add_test(pSuite, "testCheckDuplicates2", testCheckDuplicates2))
) {
CU_cleanup_registry();
return CU_get_error();