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