1/*
2 *  RopeTest.java
3 *  Copyright (C) 2007 Amin Ahmad.
4 *
5 *  This file is part of Java Ropes.
6 *
7 *  Java Ropes is free software: you can redistribute it and/or modify
8 *  it under the terms of the GNU General Public License as published by
9 *  the Free Software Foundation, either version 3 of the License, or
10 *  (at your option) any later version.
11 *
12 *  Java Ropes is distributed in the hope that it will be useful,
13 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 *  GNU General Public License for more details.
16 *
17 *  You should have received a copy of the GNU General Public License
18 *  along with Java Ropes.  If not, see <http://www.gnu.org/licenses/>.
19 *
20 *  Amin Ahmad can be contacted at amin.ahmad@gmail.com or on the web at
21 *  www.ahmadsoft.org.
22 */
23package org.ahmadsoft.ropes.test;
24
25import java.io.ByteArrayInputStream;
26import java.io.ObjectInputStream;
27import java.io.ObjectOutputStream;
28import java.util.Iterator;
29import java.util.regex.Pattern;
30
31import junit.framework.Assert;
32import junit.framework.TestCase;
33
34import org.ahmadsoft.ropes.Rope;
35import org.ahmadsoft.ropes.impl.ConcatenationRope;
36import org.ahmadsoft.ropes.impl.FlatCharSequenceRope;
37import org.ahmadsoft.ropes.impl.ReverseRope;
38import org.ahmadsoft.ropes.impl.SubstringRope;
39
40import com.sun.xml.internal.messaging.saaj.util.ByteOutputStream;
41
42public class RopeTest extends TestCase {
43
44	public void testTemp() {
45		// insert temporary code here.
46	}
47
48	public void testMatches() {
49		Rope x1 = new FlatCharSequenceRope("0123456789");
50		Rope x2 = new ConcatenationRope(x1, x1);
51
52		assertTrue(x2.matches("0.*9"));
53		assertTrue(x2.matches(Pattern.compile("0.*9")));
54
55		assertTrue(x2.matches("0.*90.*9"));
56		assertTrue(x2.matches(Pattern.compile("0.*90.*9")));
57	}
58
59	public void testConcatenationFlatFlat() {
60		Rope r1 = Rope.BUILDER.build("alpha");
61		final Rope r2 = Rope.BUILDER.build("beta");
62		Rope r3 = r1.append(r2);
63		Assert.assertEquals("alphabeta", r3.toString());
64
65		r1 = Rope.BUILDER.build("The quick brown fox jumped over");
66		r3 = r1.append(r1);
67		Assert.assertEquals("The quick brown fox jumped overThe quick brown fox jumped over", r3.toString());
68	}
69
70	public void testIterator() {
71		Rope x1 = new FlatCharSequenceRope("0123456789");
72		Rope x2 = new FlatCharSequenceRope("0123456789");
73		Rope x3 = new FlatCharSequenceRope("0123456789");
74		ConcatenationRope c1 = new ConcatenationRope(x1, x2);
75		ConcatenationRope c2 = new ConcatenationRope(c1, x3);
76
77		Iterator<Character> i = c2.iterator();
78		for (int j = 0; j < c2.length(); ++j) {
79			assertTrue("Has next (" + j + "/" + c2.length() + ")", i.hasNext());
80			i.next();
81		}
82		assertTrue(!i.hasNext());
83
84		FlatCharSequenceRope z1 = new FlatCharSequenceRope("0123456789");
85		Rope z2 = new SubstringRope(z1, 2, 0);
86		Rope z3 = new SubstringRope(z1, 2, 2);
87		Rope z4 = new ConcatenationRope(z3, new SubstringRope(z1, 6, 2)); // 2367
88
89		i = z2.iterator();
90		assertTrue(!i.hasNext());
91		i = z3.iterator();
92		assertTrue(i.hasNext());
93		assertEquals((char) '2',(char) i.next());
94		assertTrue(i.hasNext());
95		assertEquals((char) '3', (char) i.next());
96		assertTrue(!i.hasNext());
97		for (int j=0; j<=z3.length(); ++j) {
98			try {
99				z3.iterator(j);
100			} catch (Exception e) {
101				fail(j + " " + e.toString());
102			}
103		}
104		assertTrue(4 == z4.length());
105		for (int j=0; j<=z4.length(); ++j) {
106			try {
107				z4.iterator(j);
108			} catch (Exception e) {
109				fail(j + " " + e.toString());
110			}
111		}
112		i=z4.iterator(4);
113		assertTrue(!i.hasNext());
114		i=z4.iterator(2);
115		assertTrue(i.hasNext());
116		assertEquals((char) '6',(char) i.next());
117		assertTrue(i.hasNext());
118		assertEquals((char) '7',(char) i.next());
119		assertTrue(!i.hasNext());
120
121
122	}
123
124	public void testReverse() {
125		Rope x1 = new FlatCharSequenceRope("012345");
126		Rope x2 = new FlatCharSequenceRope("67");
127		Rope x3 = new ConcatenationRope(x1, x2);
128		System.out.println(x1.reverse());
129		assertEquals("543210", x1.reverse().toString());
130		assertEquals("76543210", x3.reverse().toString());
131		assertEquals(x3.reverse(), x3.reverse().reverse().reverse());
132		assertEquals("654321", x3.reverse().subSequence(1, 7).toString());
133	}
134
135
136	public void testTrim() {
137		Rope x1 = new FlatCharSequenceRope("\u0012  012345");
138		Rope x2 = new FlatCharSequenceRope("\u0002 67	       \u0007");
139		Rope x3 = new ConcatenationRope(x1, x2);
140
141		assertEquals("012345", x1.ltrim().toString());
142		assertEquals("67	       \u0007", x2.ltrim().toString());
143		assertEquals("012345\u0002 67	       \u0007", x3.ltrim().toString());
144
145		assertEquals("\u0012  012345", x1.rtrim().toString());
146		assertEquals("\u0002 67", x2.rtrim().toString());
147		assertEquals("\u0012  012345\u0002 67", x3.rtrim().toString());
148		assertEquals("012345\u0002 67", x3.rtrim().reverse().rtrim().reverse().toString());
149
150		assertEquals(x3.ltrim().rtrim(), x3.rtrim().ltrim());
151		assertEquals(x3.ltrim().rtrim(), x3.ltrim().reverse().ltrim().reverse());
152		assertEquals(x3.ltrim().rtrim(), x3.trim());
153	}
154
155	public void testCreation() {
156		try {
157			Rope.BUILDER.build("The quick brown fox jumped over");
158		} catch (final Exception e) {
159			Assert.fail("Nonempty string: " + e.getMessage());
160		}
161		try {
162			Rope.BUILDER.build("");
163		} catch (final Exception e) {
164			Assert.fail("Empty string: " + e.getMessage());
165		}
166	}
167
168	public void testEquals() {
169		final Rope r1 = Rope.BUILDER.build("alpha");
170		final Rope r2 = Rope.BUILDER.build("beta");
171		final Rope r3 = Rope.BUILDER.build("alpha");
172
173		Assert.assertEquals(r1, r3);
174		Assert.assertFalse(r1.equals(r2));
175	}
176
177	public void testHashCode() {
178		final Rope r1 = Rope.BUILDER.build("alpha");
179		final Rope r2 = Rope.BUILDER.build("beta");
180		final Rope r3 = Rope.BUILDER.build("alpha");
181
182		Assert.assertEquals(r1.hashCode(), r3.hashCode());
183		Assert.assertFalse(r1.hashCode() == r2.hashCode());
184	}
185
186	public void testHashCode2() {
187		Rope r1 = new FlatCharSequenceRope(new StringBuffer("The quick brown fox."));
188		Rope r2 = new ConcatenationRope(new FlatCharSequenceRope(""), new FlatCharSequenceRope("The quick brown fox."));
189
190		assertTrue(r1.equals(r2));
191		assertTrue(r1.equals(r2));
192	}
193
194	public void testIndexOf() {
195		final Rope r1 = Rope.BUILDER.build("alpha");
196		final Rope r2 = Rope.BUILDER.build("beta");
197		final Rope r3 = r1.append(r2);
198		Assert.assertEquals(1, r3.indexOf('l'));
199		Assert.assertEquals(6, r3.indexOf('e'));
200
201
202		Rope r = Rope.BUILDER.build("abcdef");
203		assertEquals(-1, r.indexOf('z'));
204		assertEquals(0, r.indexOf('a'));
205		assertEquals(1, r.indexOf('b'));
206		assertEquals(5, r.indexOf('f'));
207
208
209		assertEquals(1, r.indexOf('b', 0));
210		assertEquals(0, r.indexOf('a', 0));
211		assertEquals(-1, r.indexOf('z', 0));
212		assertEquals(-1, r.indexOf('b',2));
213		assertEquals(5, r.indexOf('f',5));
214
215		assertEquals(2, r.indexOf("cd", 1));
216
217		r = Rope.BUILDER.build("The quick brown fox jumped over the jumpy brown dog.");
218		assertEquals(0, r.indexOf("The"));
219		assertEquals(10, r.indexOf("brown"));
220		assertEquals(10, r.indexOf("brown", 10));
221		assertEquals(42, r.indexOf("brown",11));
222		assertEquals(-1, r.indexOf("brown",43));
223		assertEquals(-1, r.indexOf("hhe"));
224
225		r = Rope.BUILDER.build("zbbzzz");
226		assertEquals(-1, r.indexOf("ab",1));
227	}
228
229	public void testInsert() {
230		final Rope r1 = Rope.BUILDER.build("alpha");
231		Assert.assertEquals("betaalpha", r1.insert(0, "beta").toString());
232		Assert.assertEquals("alphabeta", r1.insert(r1.length(), "beta").toString());
233		Assert.assertEquals("abetalpha", r1.insert(1, "beta").toString());
234	}
235
236	public void testPrepend() {
237		Rope r1 = Rope.BUILDER.build("alphabeta");
238		for (int j=0;j<2;++j)
239			r1 = r1.subSequence(0, 5).append(r1);
240		Assert.assertEquals("alphaalphaalphabeta", r1.toString());
241		r1 = r1.append(r1.subSequence(5, 15));
242		Assert.assertEquals("alphaalphaalphabetaalphaalpha", r1.toString());
243	}
244
245	public void testCompareTo() {
246		final Rope r1 = Rope.BUILDER.build("alpha");
247		final Rope r2 = Rope.BUILDER.build("beta");
248		final Rope r3 = Rope.BUILDER.build("alpha");
249		final Rope r4 = Rope.BUILDER.build("alpha1");
250		final String s2 = "beta";
251
252		assertTrue(r1.compareTo(r3) == 0);
253		assertTrue(r1.compareTo(r2) < 0);
254		assertTrue(r2.compareTo(r1) > 0);
255		assertTrue(r1.compareTo(r4) < 0);
256		assertTrue(r4.compareTo(r1) > 0);
257		assertTrue(r1.compareTo(s2) < 0);
258		assertTrue(r2.compareTo(s2) == 0);
259	}
260
261	public void testToString() {
262		String phrase = "The quick brown fox jumped over the lazy brown dog. Boy am I glad the dog was asleep.";
263		final Rope r1 = Rope.BUILDER.build(phrase);
264		assertTrue(phrase.equals(r1.toString()));
265		assertTrue(phrase.subSequence(7, 27).equals(r1.subSequence(7, 27).toString()));
266	}
267
268	public void testReverseIterator() {
269		FlatCharSequenceRope r1 = new FlatCharSequenceRope("01234");
270		ReverseRope r2 = new ReverseRope(r1);
271		SubstringRope r3 = new SubstringRope(r1, 0, 3);
272		ConcatenationRope r4 = new ConcatenationRope(new ConcatenationRope(r1,r2),r3);	//0123443210012
273
274		Iterator<Character> x = r1.reverseIterator();
275		assertTrue(x.hasNext());
276		assertEquals((char) '4',(char)  x.next());
277		assertTrue(x.hasNext());
278		assertEquals((char) '3',(char)  x.next());
279		assertTrue(x.hasNext());
280		assertEquals((char) '2',(char)  x.next());
281		assertTrue(x.hasNext());
282		assertEquals((char) '1',(char)  x.next());
283		assertTrue(x.hasNext());
284		assertEquals((char) '0',(char)  x.next());
285		assertFalse(x.hasNext());
286
287		x = r1.reverseIterator(4);
288		assertTrue(x.hasNext());
289		assertEquals((char) '0',(char)  x.next());
290		assertFalse(x.hasNext());
291
292		x = r2.reverseIterator();
293		assertTrue(x.hasNext());
294		assertEquals((char) '0',(char)  x.next());
295		assertTrue(x.hasNext());
296		assertEquals((char) '1',(char)  x.next());
297		assertTrue(x.hasNext());
298		assertEquals((char) '2',(char)  x.next());
299		assertTrue(x.hasNext());
300		assertEquals((char) '3',(char)  x.next());
301		assertTrue(x.hasNext());
302		assertEquals((char) '4',(char)  x.next());
303		assertFalse(x.hasNext());
304
305		x = r2.reverseIterator(4);
306		assertTrue(x.hasNext());
307		assertEquals((char) '4',(char)  x.next());
308		assertFalse(x.hasNext());
309
310		x = r3.reverseIterator();
311		assertTrue(x.hasNext());
312		assertEquals((char) '2',(char)  x.next());
313		assertTrue(x.hasNext());
314		assertEquals((char) '1',(char)  x.next());
315		assertTrue(x.hasNext());
316		assertEquals((char) '0',(char)  x.next());
317		assertFalse(x.hasNext());
318
319		x = r3.reverseIterator(1);
320		assertTrue(x.hasNext());
321		assertEquals((char) '1',(char)  x.next());
322		assertTrue(x.hasNext());
323		assertEquals((char) '0',(char)  x.next());
324		assertFalse(x.hasNext());
325
326		x = r4.reverseIterator(); //0123443210012
327		assertTrue(x.hasNext());
328		assertEquals((char) '2',(char)  x.next());
329		assertTrue(x.hasNext());
330		assertEquals((char) '1',(char)  x.next());
331		assertTrue(x.hasNext());
332		assertEquals((char) '0',(char)  x.next());
333		assertTrue(x.hasNext());
334		assertEquals((char) '0',(char)  x.next());
335		assertTrue(x.hasNext());
336		assertEquals((char) '1',(char)  x.next());
337		assertTrue(x.hasNext());
338		assertEquals((char) '2',(char)  x.next());
339		assertTrue(x.hasNext());
340		assertEquals((char) '3',(char)  x.next());
341		assertTrue(x.hasNext());
342		assertEquals((char) '4',(char)  x.next());
343		assertTrue(x.hasNext());
344		assertEquals((char) '4',(char)  x.next());
345		assertTrue(x.hasNext());
346		assertEquals((char) '3',(char)  x.next());
347		assertTrue(x.hasNext());
348		assertEquals((char) '2',(char)  x.next());
349		assertTrue(x.hasNext());
350		assertEquals((char) '1',(char)  x.next());
351		assertTrue(x.hasNext());
352		assertEquals((char) '0',(char)  x.next());
353		assertFalse(x.hasNext());
354
355		x = r4.reverseIterator(7);
356		assertEquals((char) '4',(char)  x.next());
357		assertTrue(x.hasNext());
358		assertEquals((char) '4',(char)  x.next());
359		assertTrue(x.hasNext());
360		assertEquals((char) '3',(char)  x.next());
361		assertTrue(x.hasNext());
362		assertEquals((char) '2',(char)  x.next());
363		assertTrue(x.hasNext());
364		assertEquals((char) '1',(char)  x.next());
365		assertTrue(x.hasNext());
366		assertEquals((char) '0',(char)  x.next());
367		assertFalse(x.hasNext());
368
369		x = r4.reverseIterator(12);
370		assertTrue(x.hasNext());
371		assertEquals((char) '0',(char)  x.next());
372		assertFalse(x.hasNext());
373
374		x = r4.reverseIterator(13);
375		assertFalse(x.hasNext());
376
377	}
378
379	public void testSerialize() {
380		FlatCharSequenceRope r1 = new FlatCharSequenceRope("01234");
381		ReverseRope r2 = new ReverseRope(r1);
382		SubstringRope r3 = new SubstringRope(r1, 0, 1);
383		ConcatenationRope r4 = new ConcatenationRope(new ConcatenationRope(r1,r2),r3);	//01234432100
384
385		ByteOutputStream out = new ByteOutputStream();
386		try {
387			ObjectOutputStream oos = new ObjectOutputStream(out);
388			oos.writeObject(r4);
389			oos.close();
390			ByteArrayInputStream in = new ByteArrayInputStream(out.getBytes());
391			ObjectInputStream ois = new ObjectInputStream(in);
392			Rope r = (Rope) ois.readObject();
393			assertTrue(r instanceof FlatCharSequenceRope);
394		} catch (Exception e) {
395			fail(e.toString());
396		}
397
398
399	}
400
401	public void testAppend() {
402		Rope r = Rope.BUILDER.build("");
403		r=r.append('a');
404		assertEquals("a", r.toString());
405		r=r.append("boy");
406		assertEquals("aboy", r.toString());
407		r=r.append("test", 0, 4);
408		assertEquals("aboytest", r.toString());
409	}
410
411	public void testCharAt() {
412		FlatCharSequenceRope r1 = new FlatCharSequenceRope("0123456789");
413		SubstringRope r2 = new SubstringRope(r1,0,1);
414		SubstringRope r3 = new SubstringRope(r1,9,1);
415		ConcatenationRope r4 = new ConcatenationRope(r1, r3);
416
417		assertEquals('0', r1.charAt(0));
418		assertEquals('9', r1.charAt(9));
419		assertEquals('0', r2.charAt(0));
420		assertEquals('9', r3.charAt(0));
421		assertEquals('0', r4.charAt(0));
422		assertEquals('9', r4.charAt(9));
423		assertEquals('9', r4.charAt(10));
424	}
425
426	public void testRegexp() {
427		ConcatenationRope r = new ConcatenationRope(new FlatCharSequenceRope("012345"), new FlatCharSequenceRope("6789"));
428		CharSequence c = r.getForSequentialAccess();
429		for (int j=0; j<10; ++j) {
430			assertEquals(r.charAt(j), c.charAt(j));
431		}
432		c = r.getForSequentialAccess();
433
434		int[] indices={1,2,1,3,5,0,6,7,8,1,7,7,7};
435		for (int i: indices) {
436			assertEquals("Index: " + i, r.charAt(i), c.charAt(i));
437		}
438	}
439}
440