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  
48  import org.junit.Test;
49  
50  
51  public class HistogramDiffTest extends AbstractDiffTestCase {
52  	@Override
53  	protected HistogramDiff algorithm() {
54  		HistogramDiff hd = new HistogramDiff();
55  		hd.setFallbackAlgorithm(null);
56  		return hd;
57  	}
58  
59  	@Test
60  	public void testEdit_NoUniqueMiddleSide_FlipBlocks() {
61  		EditList r = diff(t("aRRSSz"), t("aSSRRz"));
62  		assertEquals(2, r.size());
63  		assertEquals(new Edit(1, 3, 1, 1), r.get(0)); // DELETE "RR"
64  		assertEquals(new Edit(5, 5, 3, 5), r.get(1)); // INSERT "RR
65  	}
66  
67  	@Test
68  	public void testEdit_NoUniqueMiddleSide_Insert2() {
69  		EditList r = diff(t("aRSz"), t("aRRSSz"));
70  		assertEquals(1, r.size());
71  		assertEquals(new Edit(2, 2, 2, 4), r.get(0));
72  	}
73  
74  	@Test
75  	public void testEdit_NoUniqueMiddleSide_FlipAndExpand() {
76  		EditList r = diff(t("aRSz"), t("aSSRRz"));
77  		assertEquals(2, r.size());
78  		assertEquals(new Edit(1, 2, 1, 1), r.get(0)); // DELETE "R"
79  		assertEquals(new Edit(3, 3, 2, 5), r.get(1)); // INSERT "SRR"
80  	}
81  
82  	@Test
83  	public void testEdit_LcsContainsUnique() {
84  		EditList r = diff(t("nqnjrnjsnm"), t("AnqnjrnjsnjTnmZ"));
85  		assertEquals(new Edit(0, 0, 0, 1), r.get(0)); // INSERT "A";
86  		assertEquals(new Edit(9, 9, 10, 13), r.get(1)); // INSERT "jTn";
87  		assertEquals(new Edit(10, 10, 14, 15), r.get(2)); // INSERT "Z";
88  		assertEquals(3, r.size());
89  	}
90  
91  	@Test
92  	public void testExceedsChainLength_DuringScanOfA() {
93  		HistogramDiff hd = new HistogramDiff();
94  		hd.setFallbackAlgorithm(null);
95  		hd.setMaxChainLength(3);
96  
97  		SequenceComparator<RawText> cmp = new SequenceComparator<RawText>() {
98  			@Override
99  			public boolean equals(RawText a, int ai, RawText b, int bi) {
100 				return RawTextComparator.DEFAULT.equals(a, ai, b, bi);
101 			}
102 
103 			@Override
104 			public int hash(RawText a, int ai) {
105 				return 1;
106 			}
107 		};
108 
109 		EditList r = hd.diff(cmp, t("RabS"), t("QabT"));
110 		assertEquals(1, r.size());
111 		assertEquals(new Edit(0, 4, 0, 4), r.get(0));
112 	}
113 
114 	@Test
115 	public void testExceedsChainLength_DuringScanOfB() {
116 		HistogramDiff hd = new HistogramDiff();
117 		hd.setFallbackAlgorithm(null);
118 		hd.setMaxChainLength(1);
119 
120 		EditList r = hd.diff(RawTextComparator.DEFAULT, t("RaaS"), t("QaaT"));
121 		assertEquals(1, r.size());
122 		assertEquals(new Edit(0, 4, 0, 4), r.get(0));
123 	}
124 
125 	@Test
126 	public void testFallbackToMyersDiff() {
127 		HistogramDiff hd = new HistogramDiff();
128 		hd.setMaxChainLength(4);
129 
130 		RawTextComparator cmp = RawTextComparator.DEFAULT;
131 		RawText ac = t("bbbbb");
132 		RawText bc = t("AbCbDbEFbZ");
133 		EditList r;
134 
135 		// Without fallback our results are limited due to collisions.
136 		hd.setFallbackAlgorithm(null);
137 		r = hd.diff(cmp, ac, bc);
138 		assertEquals(1, r.size());
139 
140 		// Results go up when we add a fallback for the high collision regions.
141 		hd.setFallbackAlgorithm(MyersDiff.INSTANCE);
142 		r = hd.diff(cmp, ac, bc);
143 		assertEquals(5, r.size());
144 	}
145 }