]> git.proxmox.com Git - libgit2.git/blame - tests-clar/stress/diff.c
Drop support for THREADSAFE on Windows XP
[libgit2.git] / tests-clar / stress / diff.c
CommitLineData
e8242022
RB
1#include "clar_libgit2.h"
2#include "../diff/diff_helpers.h"
3
4static git_repository *g_repo = NULL;
5
6void test_stress_diff__initialize(void)
7{
8}
9
10void test_stress_diff__cleanup(void)
11{
12 cl_git_sandbox_cleanup();
13}
14
15#define ANOTHER_POEM \
16"OH, glorious are the guarded heights\nWhere guardian souls abide—\nSelf-exiled from our gross delights—\nAbove, beyond, outside:\nAn ampler arc their spirit swings—\nCommands a juster view—\nWe have their word for all these things,\nNo doubt their words are true.\n\nYet we, the bond slaves of our day,\nWhom dirt and danger press—\nCo-heirs of insolence, delay,\nAnd leagued unfaithfulness—\nSuch is our need must seek indeed\nAnd, having found, engage\nThe men who merely do the work\nFor which they draw the wage.\n\nFrom forge and farm and mine and bench,\nDeck, altar, outpost lone—\nMill, school, battalion, counter, trench,\nRail, senate, sheepfold, throne—\nCreation's cry goes up on high\nFrom age to cheated age:\n\"Send us the men who do the work\n\"For which they draw the wage!\"\n"
17
18static void test_with_many(size_t expected_new)
19{
20 git_index *index;
21 git_tree *tree, *new_tree;
22 git_diff_list *diff = NULL;
23 diff_expects exp;
24 git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT;
25 git_diff_find_options opts = GIT_DIFF_FIND_OPTIONS_INIT;
26
27 cl_git_pass(git_repository_index(&index, g_repo));
28 cl_git_pass(
29 git_revparse_single((git_object **)&tree, g_repo, "HEAD^{tree}"));
30
31 cl_git_pass(p_rename("renames/ikeepsix.txt", "renames/ikeepsix2.txt"));
32 cl_git_pass(git_index_remove_bypath(index, "ikeepsix.txt"));
33 cl_git_pass(git_index_add_bypath(index, "ikeepsix2.txt"));
34 cl_git_pass(git_index_write(index));
35
36 cl_git_pass(git_diff_tree_to_index(&diff, g_repo, tree, index, &diffopts));
37
38 memset(&exp, 0, sizeof(exp));
39 cl_git_pass(git_diff_foreach(
40 diff, diff_file_cb, NULL, NULL, &exp));
41 cl_assert_equal_i(1, exp.file_status[GIT_DELTA_DELETED]);
42 cl_assert_equal_i(expected_new + 1, exp.file_status[GIT_DELTA_ADDED]);
43 cl_assert_equal_i(expected_new + 2, exp.files);
44
45 opts.flags = GIT_DIFF_FIND_ALL;
46 cl_git_pass(git_diff_find_similar(diff, &opts));
47
48 memset(&exp, 0, sizeof(exp));
49 cl_git_pass(git_diff_foreach(
50 diff, diff_file_cb, NULL, NULL, &exp));
51 cl_assert_equal_i(1, exp.file_status[GIT_DELTA_RENAMED]);
52 cl_assert_equal_i(expected_new, exp.file_status[GIT_DELTA_ADDED]);
53 cl_assert_equal_i(expected_new + 1, exp.files);
54
55 git_diff_list_free(diff);
56
57 {
58 git_object *parent;
59 git_signature *sig;
60 git_oid tree_id, commit_id;
61 git_reference *ref;
62
63 cl_git_pass(git_index_write_tree(&tree_id, index));
64 cl_git_pass(git_tree_lookup(&new_tree, g_repo, &tree_id));
65
66 cl_git_pass(git_revparse_ext(&parent, &ref, g_repo, "HEAD"));
67 cl_git_pass(git_signature_new(
68 &sig, "Sm Test", "sm@tester.test", 1372350000, 480));
69
70 cl_git_pass(git_commit_create_v(
71 &commit_id, g_repo, git_reference_name(ref), sig, sig,
72 NULL, "yoyoyo", new_tree, 1, parent));
73
74 git_object_free(parent);
75 git_reference_free(ref);
76 git_signature_free(sig);
77 }
78
79 cl_git_pass(git_diff_tree_to_tree(
80 &diff, g_repo, tree, new_tree, &diffopts));
81
82 memset(&exp, 0, sizeof(exp));
83 cl_git_pass(git_diff_foreach(
84 diff, diff_file_cb, NULL, NULL, &exp));
85 cl_assert_equal_i(1, exp.file_status[GIT_DELTA_DELETED]);
86 cl_assert_equal_i(expected_new + 1, exp.file_status[GIT_DELTA_ADDED]);
87 cl_assert_equal_i(expected_new + 2, exp.files);
88
89 opts.flags = GIT_DIFF_FIND_ALL;
90 cl_git_pass(git_diff_find_similar(diff, &opts));
91
92 memset(&exp, 0, sizeof(exp));
93 cl_git_pass(git_diff_foreach(
94 diff, diff_file_cb, NULL, NULL, &exp));
95 cl_assert_equal_i(1, exp.file_status[GIT_DELTA_RENAMED]);
96 cl_assert_equal_i(expected_new, exp.file_status[GIT_DELTA_ADDED]);
97 cl_assert_equal_i(expected_new + 1, exp.files);
98
99 git_diff_list_free(diff);
100
101 git_tree_free(new_tree);
102 git_tree_free(tree);
103 git_index_free(index);
104}
105
106void test_stress_diff__rename_big_files(void)
107{
108 git_index *index;
109 char tmp[64];
110 int i, j;
111 git_buf b = GIT_BUF_INIT;
112
113 g_repo = cl_git_sandbox_init("renames");
114
115 cl_git_pass(git_repository_index(&index, g_repo));
116
117 for (i = 0; i < 100; i += 1) {
118 snprintf(tmp, sizeof(tmp), "renames/newfile%03d", i);
119 for (j = i * 256; j > 0; --j)
120 git_buf_printf(&b, "more content %d\n", i);
121 cl_git_mkfile(tmp, b.ptr);
122 }
123
124 for (i = 0; i < 100; i += 1) {
125 snprintf(tmp, sizeof(tmp), "renames/newfile%03d", i);
126 cl_git_pass(git_index_add_bypath(index, tmp + strlen("renames/")));
127 }
128
129 git_buf_free(&b);
130 git_index_free(index);
131
132 test_with_many(100);
133}
134
135void test_stress_diff__rename_many_files(void)
136{
137 git_index *index;
138 char tmp[64];
139 int i;
140 git_buf b = GIT_BUF_INIT;
141
142 g_repo = cl_git_sandbox_init("renames");
143
144 cl_git_pass(git_repository_index(&index, g_repo));
145
146 git_buf_printf(&b, "%08d\n" ANOTHER_POEM "%08d\n" ANOTHER_POEM ANOTHER_POEM, 0, 0);
147
148 for (i = 0; i < 2500; i += 1) {
149 snprintf(tmp, sizeof(tmp), "renames/newfile%03d", i);
150 snprintf(b.ptr, 9, "%08d", i);
151 b.ptr[8] = '\n';
152 cl_git_mkfile(tmp, b.ptr);
153 }
154 git_buf_free(&b);
155
156 for (i = 0; i < 2500; i += 1) {
157 snprintf(tmp, sizeof(tmp), "renames/newfile%03d", i);
158 cl_git_pass(git_index_add_bypath(index, tmp + strlen("renames/")));
159 }
160
161 git_index_free(index);
162
163 test_with_many(2500);
164}