1
2
3
4
5
6
7
8
9
10
11 package org.eclipse.jgit.diff;
12
13 import static java.nio.charset.StandardCharsets.UTF_8;
14 import static org.junit.Assert.assertEquals;
15 import static org.junit.Assert.assertTrue;
16
17 import java.io.ByteArrayInputStream;
18 import java.io.IOException;
19
20 import org.eclipse.jgit.diff.SimilarityIndex.TableFullException;
21 import org.eclipse.jgit.lib.Constants;
22 import org.junit.Test;
23
24 public class SimilarityIndexTest {
25 @Test
26 public void testIndexingSmallObject() throws TableFullException {
27 SimilarityIndex si = hash(""
28 + "A\n"
29 + "B\n"
30 + "D\n"
31 + "B\n"
32 );
33
34 int key_A = keyFor("A\n");
35 int key_B = keyFor("B\n");
36 int key_D = keyFor("D\n");
37 assertTrue(key_A != key_B && key_A != key_D && key_B != key_D);
38
39 assertEquals(3, si.size());
40 assertEquals(2, si.count(si.findIndex(key_A)));
41 assertEquals(4, si.count(si.findIndex(key_B)));
42 assertEquals(2, si.count(si.findIndex(key_D)));
43 }
44
45 @Test
46 public void testIndexingLargeObject() throws IOException,
47 TableFullException {
48 byte[] in = (""
49 + "A\n"
50 + "B\n"
51 + "B\n"
52 + "B\n").getBytes(UTF_8);
53 SimilarityIndex si = new SimilarityIndex();
54 si.hash(new ByteArrayInputStream(in), in.length, false);
55 assertEquals(2, si.size());
56 }
57
58 @Test
59 public void testCommonScore_SameFiles() throws TableFullException {
60 String text = ""
61 + "A\n"
62 + "B\n"
63 + "D\n"
64 + "B\n";
65 SimilarityIndex src = hash(text);
66 SimilarityIndex dst = hash(text);
67 assertEquals(8, src.common(dst));
68 assertEquals(8, dst.common(src));
69
70 assertEquals(100, src.score(dst, 100));
71 assertEquals(100, dst.score(src, 100));
72 }
73
74 @Test
75 public void testCommonScore_SameFiles_CR_canonicalization()
76 throws TableFullException {
77 String text = ""
78 + "A\r\n"
79 + "B\r\n"
80 + "D\r\n"
81 + "B\r\n";
82 SimilarityIndex src = hash(text);
83 SimilarityIndex dst = hash(text.replace("\r", ""));
84 assertEquals(8, src.common(dst));
85 assertEquals(8, dst.common(src));
86
87 assertEquals(100, src.score(dst, 100));
88 assertEquals(100, dst.score(src, 100));
89 }
90
91 @Test
92 public void testCommonScoreLargeObject_SameFiles_CR_canonicalization()
93 throws TableFullException, IOException {
94 String text = ""
95 + "A\r\n"
96 + "B\r\n"
97 + "D\r\n"
98 + "B\r\n";
99 SimilarityIndex src = new SimilarityIndex();
100 byte[] bytes1 = text.getBytes(UTF_8);
101 src.hash(new ByteArrayInputStream(bytes1), bytes1.length, true);
102 src.sort();
103
104 SimilarityIndex dst = new SimilarityIndex();
105 byte[] bytes2 = text.replace("\r", "").getBytes(UTF_8);
106 dst.hash(new ByteArrayInputStream(bytes2), bytes2.length, true);
107 dst.sort();
108
109 assertEquals(8, src.common(dst));
110 assertEquals(8, dst.common(src));
111
112 assertEquals(100, src.score(dst, 100));
113 assertEquals(100, dst.score(src, 100));
114 }
115
116 @Test
117 public void testCommonScore_EmptyFiles() throws TableFullException {
118 SimilarityIndex src = hash("");
119 SimilarityIndex dst = hash("");
120 assertEquals(0, src.common(dst));
121 assertEquals(0, dst.common(src));
122 }
123
124 @Test
125 public void testCommonScore_TotallyDifferentFiles()
126 throws TableFullException {
127 SimilarityIndex src = hash("A\n");
128 SimilarityIndex dst = hash("D\n");
129 assertEquals(0, src.common(dst));
130 assertEquals(0, dst.common(src));
131 }
132
133 @Test
134 public void testCommonScore_SimiliarBy75() throws TableFullException {
135 SimilarityIndex src = hash("A\nB\nC\nD\n");
136 SimilarityIndex dst = hash("A\nB\nC\nQ\n");
137 assertEquals(6, src.common(dst));
138 assertEquals(6, dst.common(src));
139
140 assertEquals(75, src.score(dst, 100));
141 assertEquals(75, dst.score(src, 100));
142 }
143
144 private static SimilarityIndex hash(String text) throws TableFullException {
145 SimilarityIndex src = new SimilarityIndex();
146 byte[] raw = Constants.encode(text);
147 src.hash(raw, 0, raw.length);
148 src.sort();
149 return src;
150 }
151
152 private static int keyFor(String line) throws TableFullException {
153 SimilarityIndex si = hash(line);
154 assertEquals("single line scored", 1, si.size());
155 return si.key(0);
156 }
157 }