summaryrefslogtreecommitdiff
path: root/tests/t0601-read.c
diff options
context:
space:
mode:
authorVicent Marti <tanoku@gmail.com>2010-07-19 15:35:52 +0200
committerVicent Marti <tanoku@gmail.com>2010-08-12 18:49:04 +0200
commitff17642dc212bd345fda7dc213f08f8815c96841 (patch)
tree30b4dc9f9f1527a1703453d0a742bd75e4e70383 /tests/t0601-read.c
parent1baa25ee9bec1636d827c9bb7e90f646a56990ab (diff)
downloadlibgit2-ff17642dc212bd345fda7dc213f08f8815c96841.tar.gz
Add unit tests for index manipulation
Three new unit tests, t06XX files have been added. t0601-read: tests for loading index files from disk, for creating in-memory indexes and for accessing index entries. t0602-write: tests for writing index files back to disk t0603-sort: tests for properly sorting the entries array of an index Two test indexes have been added in 'tests/resources/': test/resources/index: a sample index from a libgit2 repository test/resources/gitgit.index: a sample index from a git.git repository (includes TREE extension data) Signed-off-by: Vicent Marti <tanoku@gmail.com>
Diffstat (limited to 'tests/t0601-read.c')
-rw-r--r--tests/t0601-read.c116
1 files changed, 116 insertions, 0 deletions
diff --git a/tests/t0601-read.c b/tests/t0601-read.c
new file mode 100644
index 000000000..4c31be9c3
--- /dev/null
+++ b/tests/t0601-read.c
@@ -0,0 +1,116 @@
+#include "test_lib.h"
+#include "test_helpers.h"
+#include "index.h"
+
+#include <git/odb.h>
+#include <git/index.h>
+
+#define TEST_INDEX_PATH "../resources/index"
+#define TEST_INDEX2_PATH "../resources/gitgit.index"
+
+#define TEST_INDEX_ENTRY_COUNT 109
+#define TEST_INDEX2_ENTRY_COUNT 1437
+
+struct test_entry {
+ unsigned int index;
+ char path[128];
+ size_t file_size;
+ uint32_t mtime;
+};
+
+struct test_entry TEST_ENTRIES[] = {
+ {4, "Makefile", 5064, 0x4C3F7F33},
+ {62, "tests/Makefile", 2631, 0x4C3F7F33},
+ {36, "src/index.c", 10014, 0x4C43368D},
+ {6, "git.git-authors", 2709, 0x4C3F7F33},
+ {48, "src/revobject.h", 1448, 0x4C3F7FE2}
+};
+
+BEGIN_TEST(index_loadempty_test)
+ git_index *index;
+
+ index = git_index_alloc("in-memory-index");
+ must_be_true(index != NULL);
+ must_be_true(index->on_disk == 0);
+
+ must_pass(git_index_read(index));
+
+ must_be_true(index->on_disk == 0);
+ must_be_true(index->entry_count == 0);
+ must_be_true(index->sorted);
+
+ git_index_free(index);
+END_TEST
+
+BEGIN_TEST(index_load_test)
+ git_index *index;
+ unsigned int i;
+
+ index = git_index_alloc(TEST_INDEX_PATH);
+ must_be_true(index != NULL);
+ must_be_true(index->on_disk);
+
+ must_pass(git_index_read(index));
+
+ must_be_true(index->on_disk);
+ must_be_true(index->entry_count == TEST_INDEX_ENTRY_COUNT);
+ must_be_true(index->sorted);
+
+ for (i = 0; i < ARRAY_SIZE(TEST_ENTRIES); ++i) {
+ git_index_entry *e = &index->entries[TEST_ENTRIES[i].index];
+
+ must_be_true(strcmp(e->path, TEST_ENTRIES[i].path) == 0);
+ must_be_true(e->mtime.seconds == TEST_ENTRIES[i].mtime);
+ must_be_true(e->file_size == TEST_ENTRIES[i].file_size);
+ }
+
+ git_index_free(index);
+END_TEST
+
+BEGIN_TEST(index2_load_test)
+ git_index *index;
+
+ index = git_index_alloc(TEST_INDEX2_PATH);
+ must_be_true(index != NULL);
+ must_be_true(index->on_disk);
+
+ must_pass(git_index_read(index));
+
+ must_be_true(index->on_disk);
+ must_be_true(index->entry_count == TEST_INDEX2_ENTRY_COUNT);
+ must_be_true(index->sorted);
+ must_be_true(index->tree != NULL);
+
+ git_index_free(index);
+END_TEST
+
+BEGIN_TEST(index_find_test)
+ git_index *index;
+ unsigned int i;
+
+ index = git_index_alloc(TEST_INDEX_PATH);
+ must_be_true(index != NULL);
+ must_pass(git_index_read(index));
+
+ for (i = 0; i < ARRAY_SIZE(TEST_ENTRIES); ++i) {
+ int idx = git_index_find(index, TEST_ENTRIES[i].path);
+ must_be_true((unsigned int)idx == TEST_ENTRIES[i].index);
+ }
+
+ git_index_free(index);
+END_TEST
+
+BEGIN_TEST(index_findempty_test)
+ git_index *index;
+ unsigned int i;
+
+ index = git_index_alloc("fake-index");
+ must_be_true(index != NULL);
+
+ for (i = 0; i < ARRAY_SIZE(TEST_ENTRIES); ++i) {
+ int idx = git_index_find(index, TEST_ENTRIES[i].path);
+ must_be_true(idx == GIT_ENOTFOUND);
+ }
+
+ git_index_free(index);
+END_TEST