Permalink
Browse files

Merge pull request #615 from benstraub/port-all-tests-to-clar

Port all tests to clar
  • Loading branch information...
2 parents 5afe95d + b173121 commit 4cba39ac58b0dca42e9c552343d05758dd32607d @vmg vmg committed Apr 3, 2012
Showing 325 changed files with 3,169 additions and 7,097 deletions.
View
@@ -51,8 +51,7 @@ SET(INSTALL_INC include CACHE PATH "Where to install headers to.")
# Build options
OPTION (BUILD_SHARED_LIBS "Build Shared Library (OFF for Static)" ON)
OPTION (THREADSAFE "Build libgit2 as threadsafe" OFF)
-OPTION (BUILD_TESTS "Build Tests" ON)
-OPTION (BUILD_CLAR "Build Tests using the Clar suite" OFF)
+OPTION (BUILD_CLAR "Build Tests using the Clar suite" ON)
OPTION (TAGS "Generate tags" OFF)
# Platform specific compilation flags
@@ -64,10 +63,11 @@ IF (MSVC)
IF (STDCALL)
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /Gz")
ENDIF ()
- # TODO: bring back /RTC1 /RTCc
- SET(CMAKE_C_FLAGS_DEBUG "/Od /DEBUG /MTd")
+ SET(CMAKE_C_FLAGS_DEBUG "/Od /DEBUG /MTd /RTC1 /RTCs /RTCu")
SET(CMAKE_C_FLAGS_RELEASE "/MT /O2")
SET(WIN_RC "src/win32/git2.rc")
+
+ # Precompiled headers
ELSE ()
SET(CMAKE_C_FLAGS "-O2 -g -D_GNU_SOURCE -Wall -Wextra -Wno-missing-field-initializers -Wstrict-aliasing=2 -Wstrict-prototypes -Wmissing-prototypes ${CMAKE_C_FLAGS}")
SET(CMAKE_C_FLAGS_DEBUG "-O0 -g")
@@ -127,29 +127,10 @@ INSTALL(DIRECTORY include/git2 DESTINATION ${INSTALL_INC} )
INSTALL(FILES include/git2.h DESTINATION ${INSTALL_INC} )
# Tests
-IF (BUILD_TESTS)
- SET(TEST_RESOURCES "${CMAKE_CURRENT_SOURCE_DIR}/tests/resources" CACHE PATH "Path to test resources.")
- ADD_DEFINITIONS(-DTEST_RESOURCES=\"${TEST_RESOURCES}\")
-
- INCLUDE_DIRECTORIES(tests)
- FILE(GLOB SRC_TEST tests/t??-*.c)
-
- ADD_EXECUTABLE(libgit2_test tests/test_main.c tests/test_lib.c tests/test_helpers.c ${SRC} ${SRC_TEST} ${SRC_ZLIB} ${SRC_HTTP} ${SRC_REGEX})
- TARGET_LINK_LIBRARIES(libgit2_test ${CMAKE_THREAD_LIBS_INIT})
- IF (WIN32)
- TARGET_LINK_LIBRARIES(libgit2_test ws2_32)
- ELSEIF (CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)")
- TARGET_LINK_LIBRARIES(libgit2_test socket nsl)
- ENDIF ()
-
- ENABLE_TESTING()
- ADD_TEST(libgit2_test libgit2_test)
-ENDIF ()
-
IF (BUILD_CLAR)
FIND_PACKAGE(PythonInterp REQUIRED)
- SET(CLAR_FIXTURES "${CMAKE_CURRENT_SOURCE_DIR}/tests/resources/")
+ SET(CLAR_FIXTURES "${CMAKE_CURRENT_SOURCE_DIR}/tests-clar/resources/")
SET(CLAR_PATH "${CMAKE_CURRENT_SOURCE_DIR}/tests-clar")
ADD_DEFINITIONS(-DCLAR_FIXTURE_PATH=\"${CLAR_FIXTURES}\")
View
@@ -70,7 +70,7 @@ The following CMake variables are declared:
- `INSTALL_LIB`: Where to install libraries to.
- `INSTALL_INC`: Where to install headers to.
- `BUILD_SHARED_LIBS`: Build libgit2 as a Shared Library (defaults to ON)
-- `BUILD_TESTS`: Build the libgit2 test suite (defaults to ON)
+- `BUILD_CLAR`: Build [Clar](https://github.com/tanoku/clar)-based test suite (defaults to ON)
- `THREADSAFE`: Build libgit2 with threading support (defaults to OFF)
Language Bindings
View
@@ -8,7 +8,7 @@ void test_buf_basic__resize(void)
git_buf buf1 = GIT_BUF_INIT;
git_buf_puts(&buf1, test_string);
cl_assert(git_buf_oom(&buf1) == 0);
- cl_assert(strcmp(git_buf_cstr(&buf1), test_string) == 0);
+ cl_assert_strequal(git_buf_cstr(&buf1), test_string);
git_buf_puts(&buf1, test_string);
cl_assert(strlen(git_buf_cstr(&buf1)) == strlen(test_string) * 2);
@@ -20,10 +20,10 @@ void test_buf_basic__printf(void)
git_buf buf2 = GIT_BUF_INIT;
git_buf_printf(&buf2, "%s %s %d ", "shoop", "da", 23);
cl_assert(git_buf_oom(&buf2) == 0);
- cl_assert(strcmp(git_buf_cstr(&buf2), "shoop da 23 ") == 0);
+ cl_assert_strequal(git_buf_cstr(&buf2), "shoop da 23 ");
git_buf_printf(&buf2, "%s %d", "woop", 42);
cl_assert(git_buf_oom(&buf2) == 0);
- cl_assert(strcmp(git_buf_cstr(&buf2), "shoop da 23 woop 42") == 0);
+ cl_assert_strequal(git_buf_cstr(&buf2), "shoop da 23 woop 42");
git_buf_free(&buf2);
}
@@ -21,7 +21,7 @@ static int mv_read_cb(const char *name, const char *value, void *data)
if (!strcmp(name, _name))
(*n)++;
- return 0;
+ return GIT_SUCCESS;
}
void test_config_multivar__foreach(void)
View
@@ -26,9 +26,9 @@ void test_config_new__write_new_config(void)
cl_git_pass(git_config_add_file(config, file, 0));
cl_git_pass(git_config_get_string(config, "color.ui", &out));
- cl_assert(strcmp(out, "auto") == 0);
+ cl_assert_strequal(out, "auto");
cl_git_pass(git_config_get_string(config, "core.editor", &out));
- cl_assert(strcmp(out, "ed") == 0);
+ cl_assert_strequal(out, "ed");
git_config_free(config);
View
@@ -28,9 +28,9 @@ void test_config_read__case_sensitive(void)
cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config1")));
cl_git_pass(git_config_get_string(cfg, "this.that.other", &str));
- cl_assert(!strcmp(str, "true"));
+ cl_assert_strequal(str, "true");
cl_git_pass(git_config_get_string(cfg, "this.That.other", &str));
- cl_assert(!strcmp(str, "yes"));
+ cl_assert_strequal(str, "yes");
cl_git_pass(git_config_get_bool(cfg, "this.that.other", &i));
cl_assert(i == 1);
@@ -55,7 +55,7 @@ void test_config_read__multiline_value(void)
cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config2")));
cl_git_pass(git_config_get_string(cfg, "this.That.and", &str));
- cl_assert(!strcmp(str, "one one one two two three three"));
+ cl_assert_strequal(str, "one one one two two three three");
git_config_free(cfg);
}
@@ -71,7 +71,7 @@ void test_config_read__subsection_header(void)
cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config3")));
cl_git_pass(git_config_get_string(cfg, "section.subsection.var", &str));
- cl_assert(!strcmp(str, "hello"));
+ cl_assert_strequal(str, "hello");
/* The subsection is transformed to lower-case */
cl_must_fail(git_config_get_string(cfg, "section.subSectIon.var", &str));
@@ -171,10 +171,10 @@ void test_config_read__prefixes(void)
cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config9")));
cl_git_pass(git_config_get_string(cfg, "remote.ab.url", &str));
- cl_assert(strcmp(str, "http://example.com/git/ab") == 0);
+ cl_assert_strequal(str, "http://example.com/git/ab");
cl_git_pass(git_config_get_string(cfg, "remote.abba.url", &str));
- cl_assert(strcmp(str, "http://example.com/git/abba") == 0);
+ cl_assert_strequal(str, "http://example.com/git/abba");
git_config_free(cfg);
}
View
@@ -0,0 +1,162 @@
+#include "clar_libgit2.h"
+
+#include "hashtable.h"
+#include "hash.h"
+
+
+// Helpers
+typedef struct _aux_object {
+ int __bulk;
+ git_oid id;
+ int visited;
+} table_item;
+
+static uint32_t hash_func(const void *key, int hash_id)
+{
+ uint32_t r;
+ const git_oid *id = (const git_oid *)key;
+
+ memcpy(&r, id->id + (hash_id * sizeof(uint32_t)), sizeof(r));
+ return r;
+}
+
+static int hash_cmpkey(const void *a, const void *b)
+{
+ return git_oid_cmp((const git_oid*)a, (const git_oid*)b);
+}
+
+
+void test_hash_table__new(void)
+{
+ // create a new hashtable
+ git_hashtable *table = NULL;
+
+ table = git_hashtable_alloc(55, hash_func, hash_cmpkey);
+ cl_assert(table != NULL);
+ cl_assert(table->size_mask + 1 == 64);
+
+ git_hashtable_free(table);
+}
+
+void test_hash_table__fill(void)
+{
+ // fill the hashtable with random entries
+ const int objects_n = 32;
+ int i;
+ table_item *objects;
+ git_hashtable *table = NULL;
+
+ table = git_hashtable_alloc(objects_n * 2, hash_func, hash_cmpkey);
+ cl_assert(table != NULL);
+
+ objects = (table_item *)git__malloc(objects_n * sizeof(table_item));
+ memset(objects, 0x0, objects_n * sizeof(table_item));
+
+ /* populate the hash table */
+ for (i = 0; i < objects_n; ++i) {
+ git_hash_buf(&(objects[i].id), &i, sizeof(int));
+ cl_git_pass(git_hashtable_insert(table, &(objects[i].id), &(objects[i])));
+ }
+
+ /* make sure all the inserted objects can be found */
+ for (i = 0; i < objects_n; ++i) {
+ git_oid id;
+ table_item *ob;
+
+ git_hash_buf(&id, &i, sizeof(int));
+ ob = (table_item *)git_hashtable_lookup(table, &id);
+
+ cl_assert(ob != NULL);
+ cl_assert(ob == &(objects[i]));
+ }
+
+ /* make sure we cannot find inexisting objects */
+ for (i = 0; i < 50; ++i) {
+ int hash_id;
+ git_oid id;
+
+ hash_id = (rand() % 50000) + objects_n;
+ git_hash_buf(&id, &hash_id, sizeof(int));
+ cl_assert(git_hashtable_lookup(table, &id) == NULL);
+ }
+
+ git_hashtable_free(table);
+ git__free(objects);
+}
+
+
+void test_hash_table__resize(void)
+{
+ // make sure the table resizes automatically
+ const int objects_n = 64;
+ int i;
+ unsigned int old_size;
+ table_item *objects;
+ git_hashtable *table = NULL;
+
+ table = git_hashtable_alloc(objects_n, hash_func, hash_cmpkey);
+ cl_assert(table != NULL);
+
+ objects = (table_item*)git__malloc(objects_n * sizeof(table_item));
+ memset(objects, 0x0, objects_n * sizeof(table_item));
+
+ old_size = table->size_mask + 1;
+
+ /* populate the hash table -- should be automatically resized */
+ for (i = 0; i < objects_n; ++i) {
+ git_hash_buf(&(objects[i].id), &i, sizeof(int));
+ cl_git_pass(git_hashtable_insert(table, &(objects[i].id), &(objects[i])));
+ }
+
+ cl_assert(table->size_mask > old_size);
+
+ /* make sure all the inserted objects can be found */
+ for (i = 0; i < objects_n; ++i) {
+ git_oid id;
+ table_item *ob;
+
+ git_hash_buf(&id, &i, sizeof(int));
+ ob = (table_item *)git_hashtable_lookup(table, &id);
+
+ cl_assert(ob != NULL);
+ cl_assert(ob == &(objects[i]));
+ }
+
+ git_hashtable_free(table);
+ git__free(objects);
+}
+
+
+void test_hash_table__iterate(void)
+{
+ // iterate through all the contents of the table
+
+ const int objects_n = 32;
+ int i;
+ table_item *objects, *ob;
+
+ git_hashtable *table = NULL;
+
+ table = git_hashtable_alloc(objects_n * 2, hash_func, hash_cmpkey);
+ cl_assert(table != NULL);
+
+ objects = git__malloc(objects_n * sizeof(table_item));
+ memset(objects, 0x0, objects_n * sizeof(table_item));
+
+ /* populate the hash table */
+ for (i = 0; i < objects_n; ++i) {
+ git_hash_buf(&(objects[i].id), &i, sizeof(int));
+ cl_git_pass(git_hashtable_insert(table, &(objects[i].id), &(objects[i])));
+ }
+
+ GIT_HASHTABLE_FOREACH_VALUE(table, ob, ob->visited = 1);
+
+ /* make sure all nodes have been visited */
+ for (i = 0; i < objects_n; ++i)
+ {
+ cl_assert(objects[i].visited);
+ }
+
+ git_hashtable_free(table);
+ git__free(objects);
+}
Oops, something went wrong.

0 comments on commit 4cba39a

Please sign in to comment.