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.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 }