1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.grouplens.grapht.util;
21
22 import org.junit.Test;
23
24 import java.util.Iterator;
25 import java.util.NoSuchElementException;
26
27 import static org.hamcrest.Matchers.*;
28 import static org.junit.Assert.assertThat;
29 import static org.junit.Assert.fail;
30
31
32
33
34 public class AbstractChainTest {
35 static class TestChain extends AbstractChain<String> {
36 TestChain(AbstractChain<String> prev, String tv) {
37 super(prev, tv);
38 }
39 public TestChain extend(String tok) {
40 return new TestChain(this, tok);
41 }
42 }
43
44 static TestChain singleton(String tok) {
45 return new TestChain(null, tok);
46 }
47 static TestChain chain(String t1, String... toks) {
48 TestChain chain = singleton(t1);
49 for (String tok: toks) {
50 chain = chain.extend(tok);
51 }
52 return chain;
53 }
54
55 @Test
56 public void testSingletonBasicProps() throws Exception {
57 TestChain chain = singleton("foo");
58 assertThat(chain.size(), equalTo(1));
59 assertThat(chain.isEmpty(), equalTo(false));
60 assertThat(chain.contains("foo"), equalTo(true));
61 assertThat(chain, contains("foo"));
62 }
63
64 @Test
65 public void testSingletonsEqual() {
66 TestChain foo1 = singleton("foo");
67 TestChain foo2 = singleton("foo");
68 TestChain bar = singleton("bar");
69
70 assertThat(foo1.equals(foo1), equalTo(true));
71 assertThat(foo1.equals(foo2), equalTo(true));
72 assertThat(foo1.equals(bar), equalTo(false));
73 }
74
75 @Test
76 public void testSingletonGet() {
77 TestChain foo = singleton("foo");
78
79 assertThat(foo.get(0), equalTo("foo"));
80 }
81
82 @Test
83 public void testExtend() {
84 TestChain foo = singleton("foo");
85 TestChain foobar = foo.extend("bar");
86
87 assertThat(foobar.size(), equalTo(2));
88
89 Iterator<String> iter = foobar.iterator();
90 assertThat(iter.next(), equalTo("foo"));
91 assertThat(iter.next(), equalTo("bar"));
92 assertThat(iter.hasNext(), equalTo(false));
93
94 assertThat(foobar.get(1), equalTo("bar"));
95 assertThat(foobar.get(0), equalTo("foo"));
96 }
97
98 @Test
99 public void testReverseIteration() {
100 TestChain chain = chain("foobie", "bletch", "forever");
101 assertThat(chain, hasSize(3));
102 assertThat(chain.reverse(), contains("forever", "bletch", "foobie"));
103
104 Iterator<String> iter = chain.reverseIterator();
105 iter.next();
106 iter.next();
107 iter.next();
108 try {
109 iter.next();
110 fail("must throw NoSuchElement");
111 } catch (Throwable th) {
112 assertThat(th, instanceOf(NoSuchElementException.class));
113 }
114 }
115
116 @Test
117 public void testChainsEqual() {
118 TestChain base = chain("foo", "bar", "blatz");
119 TestChain copy = chain("foo", "bar", "blatz");
120 TestChain chend = chain("foo", "bar", "blam");
121 TestChain chstart = chain("f00", "bar", "blatz");
122
123 assertThat(base.equals(base), equalTo(true));
124 assertThat(base.equals(copy), equalTo(true));
125 assertThat(base.equals(chend), equalTo(false));
126 assertThat(base.equals(chstart), equalTo(false));
127 }
128 }