diff options
author | Vyacheslav Tyutyunkov <tve@softmotions.com> | 2017-03-10 11:57:47 +0700 |
---|---|---|
committer | Vyacheslav Tyutyunkov <tve@softmotions.com> | 2017-03-10 11:57:47 +0700 |
commit | 06b2b7b83c8a9a62b16b1841c5852e7a992b38d4 (patch) | |
tree | 203c1855c8f3bb0d159688b90aaa5b3fcddc154f | |
parent | 36c5ce4ae7b0689affb954251e8f006e70ef3655 (diff) | |
download | ejdb-06b2b7b83c8a9a62b16b1841c5852e7a992b38d4.tar.gz ejdb-06b2b7b83c8a9a62b16b1841c5852e7a992b38d4.tar.bz2 ejdb-06b2b7b83c8a9a62b16b1841c5852e7a992b38d4.zip |
#182 test
-rw-r--r-- | src/ejdb/tests/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/ejdb/tests/ejdbtest6.c | 115 |
2 files changed, 116 insertions, 1 deletions
diff --git a/src/ejdb/tests/CMakeLists.txt b/src/ejdb/tests/CMakeLists.txt index c17fc92..594ed71 100644 --- a/src/ejdb/tests/CMakeLists.txt +++ b/src/ejdb/tests/CMakeLists.txt @@ -4,7 +4,7 @@ include_directories(${CUNIT_INCLUDE_DIRS}) set(TEST_DATA_DIR ${CMAKE_CURRENT_BINARY_DIR}) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${TEST_DATA_DIR}) -foreach(TC RANGE 1 5) +foreach(TC RANGE 1 6) add_executable(ejdbtest${TC} ejdbtest${TC}.c) set_target_properties(ejdbtest${TC} PROPERTIES COMPILE_FLAGS "-DEJDB_STATIC") diff --git a/src/ejdb/tests/ejdbtest6.c b/src/ejdb/tests/ejdbtest6.c new file mode 100644 index 0000000..c9d293c --- /dev/null +++ b/src/ejdb/tests/ejdbtest6.c @@ -0,0 +1,115 @@ + +#include "myconf.h" +#include "ejdb_private.h" +#include "CUnit/Basic.h" + +int testIssue182() { + EJDB *jb = ejdbnew(); + CU_ASSERT_TRUE_FATAL(ejdbopen(jb, "dbt6", JBOWRITER | JBOCREAT | JBOTRUNC)); + EJCOLL *coll = ejdbcreatecoll(jb, "issue182", NULL); + CU_ASSERT_PTR_NOT_NULL_FATAL(coll); + + bson_oid_t oid; + bson bv1; + + // add one odd record (has to be the first to provoke the segmentation fault) + bson_init(&bv1); + bson_append_string(&bv1, "test2", "dd"); + bson_finish(&bv1); + CU_ASSERT_FALSE_FATAL(bv1.err); + ejdbsavebson(coll, &bv1, &oid); + bson_destroy(&bv1); + + // add one record with positive value + bson_init(&bv1); + bson_append_int(&bv1, "test", 1); + bson_finish(&bv1); + CU_ASSERT_FALSE_FATAL(bv1.err); + ejdbsavebson(coll, &bv1, &oid); + bson_destroy(&bv1); + + // add one record with negative value + bson_init(&bv1); + bson_append_int(&bv1, "test", -1); + bson_finish(&bv1); + CU_ASSERT_FALSE_FATAL(bv1.err); + ejdbsavebson(coll, &bv1, &oid); + bson_destroy(&bv1); + + + // 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); + CU_ASSERT_FALSE_FATAL(bsq1.err); + + bson bshints; + bson_init_as_query(&bshints); + bson_append_start_object(&bshints, "$orderby"); + bson_append_int(&bshints, "test", 1); + bson_append_finish_object(&bshints); + bson_finish(&bshints); + CU_ASSERT_FALSE_FATAL(bshints.err); + + EJQ *q1 = ejdbcreatequery(jb, &bsq1, NULL, 0, &bshints); + CU_ASSERT_PTR_NOT_NULL_FATAL(q1); + + // execute query + uint32_t count = 0; + TCXSTR *log = tcxstrnew(); + TCLIST *q1res = ejdbqryexecute(coll, q1, &count, 0, log); + + CU_ASSERT_EQUAL(count, 3); + CU_ASSERT_TRUE(TCLISTNUM(q1res) == 3); + + bson_destroy(&bsq1); + bson_destroy(&bshints); + tclistdel(q1res); + ejdbquerydel(q1); + tcxstrdel(log); + + ejdbclose(jb); + ejdbdel(jb); + return 0; +} + +int init_suite(void) { + return 0; +} + +int clean_suite(void) { + return 0; +} + +int main() { + setlocale(LC_ALL, "en_US.UTF-8"); + CU_pSuite pSuite = NULL; + + /* Initialize the CUnit test registry */ + if (CUE_SUCCESS != CU_initialize_registry()) + return CU_get_error(); + + /* Add a suite to the registry */ + pSuite = CU_add_suite("ejdbtest6", init_suite, clean_suite); + if (NULL == pSuite) { + CU_cleanup_registry(); + return CU_get_error(); + } + + /* Add the tests to the suite */ + if ( + (NULL == CU_add_test(pSuite, "testIssue182", testIssue182)) + ) { + CU_cleanup_registry(); + return CU_get_error(); + } + + /* Run all tests using the CUnit Basic interface */ + CU_basic_set_mode(CU_BRM_VERBOSE); + CU_basic_run_tests(); + int ret = CU_get_error() || CU_get_number_of_failures(); + CU_cleanup_registry(); + return ret; +} |