View Javadoc
1   /*
2    * Copyright (C) 2010, Google Inc.
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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 }