#182 - fixes in bson_compare
authorVyacheslav Tyutyunkov <tve@softmotions.com>
Fri, 10 Mar 2017 07:24:40 +0000 (14:24 +0700)
committerVyacheslav Tyutyunkov <tve@softmotions.com>
Fri, 10 Mar 2017 07:24:40 +0000 (14:24 +0700)
src/bson/bson.c
src/ejdb/tests/ejdbtest6.c

index b192f2d..16b6e51 100644 (file)
@@ -2178,6 +2178,13 @@ int bson_compare_fpaths(const void *bsdata1, const void *bsdata2, const char *fp
     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.
@@ -2190,13 +2197,19 @@ int bson_compare_fpaths(const void *bsdata1, const void *bsdata2, const char *fp
 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);
index c9d293c..cdc8e79 100644 (file)
@@ -3,7 +3,7 @@
 #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);
@@ -38,8 +38,6 @@ int testIssue182() {
 
 
     // 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);
@@ -72,7 +70,33 @@ int testIssue182() {
 
     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) {
@@ -100,6 +124,7 @@ int main() {
 
     /* Add the tests to the suite */
     if (
+            (NULL == CU_add_test(pSuite, "subTestIssue182", subTestIssue182)) ||
             (NULL == CU_add_test(pSuite, "testIssue182", testIssue182))
             ) {
         CU_cleanup_registry();