return bson_compare_it_current(&it1, &it2);
}
+static inline int bson_compare_type(bson_type t1, bson_type t2) {
+ if (t1 == BSON_EOO || BSON_IS_NULL_TYPE(t1)) {
+ return t2 == BSON_EOO || BSON_IS_NULL_TYPE(t2) ? 0 : -1;
+ }
+ return t1 - t2;
+}
+
/**
*
* Return <0 if value pointing by it1 lesser than from it2.
int bson_compare_it_current(const bson_iterator *it1, const bson_iterator *it2) {
bson_type t1 = BSON_ITERATOR_TYPE(it1);
bson_type t2 = BSON_ITERATOR_TYPE(it2);
+
+ if (bson_compare_type(t1, t2) > 0) {
+ return - bson_compare_it_current(it2, it1);
+ }
if ((BSON_IS_STRING_TYPE(t1) && !BSON_IS_STRING_TYPE(t2)) ||
(BSON_IS_STRING_TYPE(t2) && !BSON_IS_STRING_TYPE(t1))) {
return (t1 - t2);
}
-
- if (t1 == BSON_BOOL || t1 == BSON_EOO || t1 == BSON_NULL || t1 == BSON_UNDEFINED) {
+
+ if (BSON_IS_NULL_TYPE(t1)) {
+ return BSON_IS_NULL_TYPE(t2) ? 0 : -1;
+ } else if (t1 == BSON_BOOL || t1 == BSON_EOO) {
int v1 = bson_iterator_bool(it1);
int v2 = bson_iterator_bool(it2);
return (v1 > v2) ? 1 : ((v1 < v2) ? -1 : 0);
#include "ejdb_private.h"
#include "CUnit/Basic.h"
-int testIssue182() {
+void testIssue182() {
EJDB *jb = ejdbnew();
CU_ASSERT_TRUE_FATAL(ejdbopen(jb, "dbt6", JBOWRITER | JBOCREAT | JBOTRUNC));
EJCOLL *coll = ejdbcreatecoll(jb, "issue182", NULL);
// this query now crashes the db (using -1 sort order does not)
- // db.find("testcoll", {}, { "$orderby": { "test": 1 } }, function(err, cursor, count) {
-
bson bsq1;
bson_init_as_query(&bsq1);
bson_finish(&bsq1);
ejdbclose(jb);
ejdbdel(jb);
- return 0;
+}
+
+void subTestIssue182() {
+ bson b1, b2, b3;
+
+ bson_init(&b1);
+ bson_append_int(&b1, "test", 1);
+ bson_finish(&b1);
+
+ bson_init(&b2);
+ bson_append_int(&b2, "test", -1);
+ bson_finish(&b2);
+
+ bson_init(&b3);
+ bson_append_string(&b3, "other", "");
+ bson_finish(&b3);
+
+ CU_ASSERT_TRUE(bson_compare(bson_data(&b1), bson_data(&b2), "test", 4) > 0);
+ CU_ASSERT_TRUE(bson_compare(bson_data(&b1), bson_data(&b3), "test", 4) > 0);
+ CU_ASSERT_TRUE(bson_compare(bson_data(&b2), bson_data(&b1), "test", 4) < 0);
+ CU_ASSERT_TRUE(bson_compare(bson_data(&b2), bson_data(&b3), "test", 4) > 0);
+ CU_ASSERT_TRUE(bson_compare(bson_data(&b3), bson_data(&b1), "test", 4) < 0);
+ CU_ASSERT_TRUE(bson_compare(bson_data(&b3), bson_data(&b2), "test", 4) < 0);
+
+ bson_destroy(&b1);
+ bson_destroy(&b2);
+ bson_destroy(&b3);
}
int init_suite(void) {
/* Add the tests to the suite */
if (
+ (NULL == CU_add_test(pSuite, "subTestIssue182", subTestIssue182)) ||
(NULL == CU_add_test(pSuite, "testIssue182", testIssue182))
) {
CU_cleanup_registry();