001 /*
002 * LAPIS lightweight structured text processing system
003 *
004 * Copyright (C) 2003 Massachusetts Institute of Technology.
005 * All rights reserved.
006 *
007 * This library is free software; you can redistribute it
008 * and/or modify it under the terms of the GNU General
009 * Public License as published by the Free Software
010 * Foundation, version 2.
011 *
012 * LAPIS homepage: http://graphics.lcs.mit.edu/lapis/
013 */
014
015 package lapisx.iterator;
016
017 import junit.framework.*;
018 import java.util.*;
019
020 /**
021 * Test case for ConcatIterator.
022 */
023 public class ConcatIteratorTest extends TestCase {
024
025 public ConcatIteratorTest (String name) {
026 super(name);
027 }
028
029 protected void setUp () {
030 }
031
032 public static Test suite () {
033 return new TestSuite (ConcatIteratorTest.class);
034 }
035
036 public void testConcat () {
037 List l1 = Arrays.asList (new String[] {"a", "b", "c"});
038 List l2 = Arrays.asList (new String[] {"d", "e", "f"});
039 List l3 = Arrays.asList (new String[] {"g", "h"});
040 Iterator EMPTY = Collections.EMPTY_LIST.iterator ();
041
042 // concatenate two iterators
043 Iterator g1 = new ConcatIterator (l1.iterator (), l2.iterator ());
044 assertEquals (Arrays.asList (new String[] {"a", "b", "c", "d", "e", "f"}), accumulate (g1));
045
046 // append empty iterator
047 Iterator g2 = new ConcatIterator (l1.iterator (), EMPTY);
048 assertEquals (Arrays.asList (new String[] {"a", "b", "c"}), accumulate (g2));
049
050 // prefix empty iterator
051 Iterator g3 = new ConcatIterator (EMPTY, l2.iterator ());
052 assertEquals (Arrays.asList (new String[] {"d", "e", "f"}), accumulate (g3));
053
054 // concatenate three iterators
055 Iterator g4 = new ConcatIterator (Arrays.asList (new Iterator[] {l2.iterator (), l3.iterator (), l1.iterator ()}));
056 assertEquals (Arrays.asList (new String[] {"d", "e", "f", "g", "h", "a", "b", "c", }), accumulate (g4));
057
058 // concatenate one iterator
059 Iterator g5 = new ConcatIterator (Collections.singleton (l3.iterator ()));
060 assertEquals (Arrays.asList (new String[] {"g", "h"}), accumulate (g5));
061
062 // concatenate no iterators
063 Iterator g6 = new ConcatIterator (Collections.EMPTY_LIST);
064 assertEquals (Collections.EMPTY_LIST, accumulate (g6));
065
066 // concatenate three empty iterators
067 Iterator g7 = new ConcatIterator (Collections.nCopies (3, EMPTY));
068 assertEquals (Collections.EMPTY_LIST, accumulate (g7));
069
070 // remove from concatenated iterator
071 List ml1 = new ArrayList (); ml1.add ("x"); ml1.add ("y");
072 List ml2 = new ArrayList (); ml2.add ("z");
073 Iterator g8 = new ConcatIterator (ml1.iterator (), ml2.iterator ());
074 while (g8.hasNext ()) {
075 g8.next ();
076 g8.remove();
077 }
078 assertEquals (Collections.EMPTY_LIST, ml1);
079 assertEquals (Collections.EMPTY_LIST, ml2);
080 }
081
082 private List accumulate (Iterator g) {
083 List s = new ArrayList ();
084 while (g.hasNext ())
085 s.add (g.next ());
086 return s;
087 }
088 }