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>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
diff --git a/tests/t0601-read.c b/tests/t0601-read.c
new file mode 100644
index 0000000..4c31be9
--- /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
diff --git a/tests/t0602-write.c b/tests/t0602-write.c
new file mode 100644
index 0000000..c5e5c7d
--- /dev/null
+++ b/tests/t0602-write.c
@@ -0,0 +1,49 @@
+#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"
+
+int filecmp(const char *filename1, const char *filename2)
+{
+ git_file file1, file2;
+ struct stat stat1, stat2;
+
+ /* char buffer1[1024], buffer2[1024]; */
+
+ file1 = gitfo_open(filename1, O_RDONLY);
+ file2 = gitfo_open(filename2, O_RDONLY);
+
+ if (file1 < 0 || file2 < 0)
+ return GIT_ERROR;
+
+ gitfo_fstat(file1, &stat1);
+ gitfo_fstat(file2, &stat2);
+
+ if (stat1.st_size != stat2.st_size)
+ return GIT_ERROR;
+
+ /* TODO: byte-per-byte comparison */
+
+ return 0;
+}
+
+BEGIN_TEST(index_load_test)
+ git_index *index;
+ git_filelock out_file;
+
+ index = git_index_alloc(TEST_INDEX_PATH);
+ must_be_true(index != NULL);
+ must_pass(git_index_read(index));
+ must_be_true(index->on_disk);
+
+ must_pass(git_filelock_init(&out_file, "index_rewrite"));
+ must_pass(git_filelock_lock(&out_file, 0));
+ must_pass(git_index__write(index, &out_file));
+ must_pass(git_filelock_commit(&out_file));
+
+ git_index_free(index);
+END_TEST
diff --git a/tests/t0603-sort.c b/tests/t0603-sort.c
new file mode 100644
index 0000000..0735f98
--- /dev/null
+++ b/tests/t0603-sort.c
@@ -0,0 +1,64 @@
+#include "test_lib.h"
+#include "test_helpers.h"
+#include "index.h"
+
+#include <git/odb.h>
+#include <git/index.h>
+
+#define TEST_INDEX_PATH "../t0600-objects/index"
+
+void print_entries(git_index *index)
+{
+ unsigned int i;
+
+ for (i = 0; i < index->entry_count; ++i)
+ printf("%d: %s\n", i, index->entries[i].path);
+}
+
+void randomize_entries(git_index *index)
+{
+ unsigned int i, j;
+ git_index_entry tmp;
+
+ srand(time(NULL));
+
+ for (i = 0; i < index->entry_count; ++i) {
+ j = rand() % index->entry_count;
+ memcpy(&tmp, &index->entries[j], sizeof(git_index_entry));
+ memcpy(&index->entries[j], &index->entries[i], sizeof(git_index_entry));
+ memcpy(&index->entries[i], &tmp, sizeof(git_index_entry));
+ }
+
+ index->sorted = 0;
+}
+
+BEGIN_TEST(index_sort_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));
+
+ randomize_entries(index);
+
+ git_index__sort(index);
+ must_be_true(index->sorted);
+
+ for (i = 1; i < index->entry_count; ++i)
+ must_be_true(strcmp(index->entries[i - 1].path,
+ index->entries[i].path) < 0);
+
+ git_index_free(index);
+END_TEST
+
+BEGIN_TEST(index_sort_empty_test)
+ git_index *index;
+ index = git_index_alloc("fake-index");
+ must_be_true(index != NULL);
+
+ git_index__sort(index);
+ must_be_true(index->sorted);
+
+ git_index_free(index);
+END_TEST