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 package org.eclipse.jgit.diff;
45
46 import static org.junit.Assert.assertEquals;
47 import static org.junit.Assert.assertTrue;
48
49 import java.io.ByteArrayInputStream;
50 import java.io.IOException;
51
52 import org.eclipse.jgit.diff.SimilarityIndex.TableFullException;
53 import org.eclipse.jgit.lib.Constants;
54 import org.junit.Test;
55
56 public class SimilarityIndexTest {
57 @Test
58 public void testIndexingSmallObject() throws TableFullException {
59 SimilarityIndex si = hash(""
60 + "A\n"
61 + "B\n"
62 + "D\n"
63 + "B\n"
64 );
65
66 int key_A = keyFor("A\n");
67 int key_B = keyFor("B\n");
68 int key_D = keyFor("D\n");
69 assertTrue(key_A != key_B && key_A != key_D && key_B != key_D);
70
71 assertEquals(3, si.size());
72 assertEquals(2, si.count(si.findIndex(key_A)));
73 assertEquals(4, si.count(si.findIndex(key_B)));
74 assertEquals(2, si.count(si.findIndex(key_D)));
75 }
76
77 @Test
78 public void testIndexingLargeObject() throws IOException,
79 TableFullException {
80 byte[] in = (""
81 + "A\n"
82 + "B\n"
83 + "B\n"
84 + "B\n").getBytes("UTF-8");
85 SimilarityIndex si = new SimilarityIndex();
86 si.hash(new ByteArrayInputStream(in), in.length, false);
87 assertEquals(2, si.size());
88 }
89
90 @Test
91 public void testCommonScore_SameFiles() throws TableFullException {
92 String text = ""
93 + "A\n"
94 + "B\n"
95 + "D\n"
96 + "B\n";
97 SimilarityIndex src = hash(text);
98 SimilarityIndex dst = hash(text);
99 assertEquals(8, src.common(dst));
100 assertEquals(8, dst.common(src));
101
102 assertEquals(100, src.score(dst, 100));
103 assertEquals(100, dst.score(src, 100));
104 }
105
106 @Test
107 public void testCommonScore_SameFiles_CR_canonicalization()
108 throws TableFullException {
109 String text = ""
110 + "A\r\n"
111 + "B\r\n"
112 + "D\r\n"
113 + "B\r\n";
114 SimilarityIndex src = hash(text);
115 SimilarityIndex dst = hash(text.replace("\r", ""));
116 assertEquals(8, src.common(dst));
117 assertEquals(8, dst.common(src));
118
119 assertEquals(100, src.score(dst, 100));
120 assertEquals(100, dst.score(src, 100));
121 }
122
123 @Test
124 public void testCommonScoreLargeObject_SameFiles_CR_canonicalization()
125 throws TableFullException, IOException {
126 String text = ""
127 + "A\r\n"
128 + "B\r\n"
129 + "D\r\n"
130 + "B\r\n";
131 SimilarityIndex src = new SimilarityIndex();
132 byte[] bytes1 = text.getBytes("UTF-8");
133 src.hash(new ByteArrayInputStream(bytes1), bytes1.length, true);
134 src.sort();
135
136 SimilarityIndex dst = new SimilarityIndex();
137 byte[] bytes2 = text.replace("\r", "").getBytes("UTF-8");
138 dst.hash(new ByteArrayInputStream(bytes2), bytes2.length, true);
139 dst.sort();
140
141 assertEquals(8, src.common(dst));
142 assertEquals(8, dst.common(src));
143
144 assertEquals(100, src.score(dst, 100));
145 assertEquals(100, dst.score(src, 100));
146 }
147
148 @Test
149 public void testCommonScore_EmptyFiles() throws TableFullException {
150 SimilarityIndex src = hash("");
151 SimilarityIndex dst = hash("");
152 assertEquals(0, src.common(dst));
153 assertEquals(0, dst.common(src));
154 }
155
156 @Test
157 public void testCommonScore_TotallyDifferentFiles()
158 throws TableFullException {
159 SimilarityIndex src = hash("A\n");
160 SimilarityIndex dst = hash("D\n");
161 assertEquals(0, src.common(dst));
162 assertEquals(0, dst.common(src));
163 }
164
165 @Test
166 public void testCommonScore_SimiliarBy75() throws TableFullException {
167 SimilarityIndex src = hash("A\nB\nC\nD\n");
168 SimilarityIndex dst = hash("A\nB\nC\nQ\n");
169 assertEquals(6, src.common(dst));
170 assertEquals(6, dst.common(src));
171
172 assertEquals(75, src.score(dst, 100));
173 assertEquals(75, dst.score(src, 100));
174 }
175
176 private static SimilarityIndex hash(String text) throws TableFullException {
177 SimilarityIndex src = new SimilarityIndex();
178 byte[] raw = Constants.encode(text);
179 src.hash(raw, 0, raw.length);
180 src.sort();
181 return src;
182 }
183
184 private static int keyFor(String line) throws TableFullException {
185 SimilarityIndex si = hash(line);
186 assertEquals("single line scored", 1, si.size());
187 return si.key(0);
188 }
189 }