t06-index.c 4.95 KB
Newer Older
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
/*
 * This file is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License, version 2,
 * as published by the Free Software Foundation.
 *
 * In addition to the permissions in the GNU General Public License,
 * the authors give you unlimited permission to link the compiled
 * version of this file into combinations with other programs,
 * and to distribute those combinations without any restriction
 * coming from the use of this file.  (The General Public License
 * restrictions do apply in other respects; for example, they cover
 * modification of the file, and distribution when not linked into
 * a combined executable.)
 *
 * This file is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; see the file COPYING.  If not, write to
 * the Free Software Foundation, 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */
#include "test_lib.h"
#include "test_helpers.h"

#include "index.h"

#define TEST_INDEX_ENTRY_COUNT 109
#define TEST_INDEX2_ENTRY_COUNT 1437

struct test_entry {
	unsigned int index;
	char path[128];
	git_off_t file_size;
37
	git_time_t mtime;
38 39 40 41 42 43 44 45 46 47
};

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}
};

48
BEGIN_TEST(read0, "load an empty index")
49 50
	git_index *index;

51
	must_pass(git_index_open(&index, "in-memory-index"));
52 53 54 55 56 57
	must_be_true(index->on_disk == 0);

	must_pass(git_index_read(index));

	must_be_true(index->on_disk == 0);
	must_be_true(git_index_entrycount(index) == 0);
58
	must_be_true(index->entries.sorted);
59 60 61 62

	git_index_free(index);
END_TEST

63
BEGIN_TEST(read1, "load a standard index (default test index)")
64 65 66 67
	git_index *index;
	unsigned int i;
	git_index_entry **entries;

68
	must_pass(git_index_open(&index, TEST_INDEX_PATH));
69 70 71 72 73 74
	must_be_true(index->on_disk);

	must_pass(git_index_read(index));

	must_be_true(index->on_disk);
	must_be_true(git_index_entrycount(index) == TEST_INDEX_ENTRY_COUNT);
75
	must_be_true(index->entries.sorted);
76 77 78 79 80 81 82 83 84 85 86 87 88 89

	entries = (git_index_entry **)index->entries.contents;

	for (i = 0; i < ARRAY_SIZE(TEST_ENTRIES); ++i) {
		git_index_entry *e = 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

90
BEGIN_TEST(read2, "load a standard index (git.git index)")
91 92
	git_index *index;

93
	must_pass(git_index_open(&index, TEST_INDEX2_PATH));
94 95 96 97 98 99
	must_be_true(index->on_disk);

	must_pass(git_index_read(index));

	must_be_true(index->on_disk);
	must_be_true(git_index_entrycount(index) == TEST_INDEX2_ENTRY_COUNT);
100
	must_be_true(index->entries.sorted);
101 102 103 104 105
	must_be_true(index->tree != NULL);

	git_index_free(index);
END_TEST

106
BEGIN_TEST(find0, "find an entry on an index")
107 108 109
	git_index *index;
	unsigned int i;

110
	must_pass(git_index_open(&index, TEST_INDEX_PATH));
111 112 113 114 115 116 117 118 119 120
	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

121
BEGIN_TEST(find1, "find an entry in an empty index")
122 123 124
	git_index *index;
	unsigned int i;

125
	must_pass(git_index_open(&index, "fake-index"));
126 127 128 129 130 131 132 133 134

	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

135
BEGIN_TEST(write0, "write an index back to disk")
136 137
	git_index *index;

138 139
	must_pass(copy_file(TEST_INDEXBIG_PATH, "index_rewrite"));

140
	must_pass(git_index_open(&index, "index_rewrite"));
141 142 143
	must_pass(git_index_read(index));
	must_be_true(index->on_disk);

144 145
	must_pass(git_index_write(index));
	must_pass(cmp_files(TEST_INDEXBIG_PATH, "index_rewrite"));
146 147

	git_index_free(index);
148 149
	
	gitfo_unlink("index_rewrite");
150 151
END_TEST

152
BEGIN_TEST(sort0, "sort the entires in an index")
153 154 155 156
	/*
	 * TODO: This no longer applies:
	 * index sorting in Git uses some specific changes to the way
	 * directories are sorted.
157 158 159 160
	 *
	 * We need to specificially check for this by creating a new
	 * index, adding entries in random order and then
	 * checking for consistency
161
	 */
162 163
END_TEST

164

165
BEGIN_TEST(sort1, "sort the entires in an empty index")
166 167
	git_index *index;

168
	must_pass(git_index_open(&index, "fake-index"));
169

170
	/* FIXME: this test is slightly dumb */
171
	must_be_true(index->entries.sorted);
172 173 174 175

	git_index_free(index);
END_TEST

176 177 178 179
BEGIN_SUITE(index)
	ADD_TEST(read0);
	ADD_TEST(read1);
	ADD_TEST(read2);
180

181 182
	ADD_TEST(find0);
	ADD_TEST(find1);
183

184
	ADD_TEST(write0);
185

186 187 188
	ADD_TEST(sort0);
	ADD_TEST(sort1);
END_SUITE