summaryrefslogtreecommitdiff
path: root/src/test/java/bjc/esodata/NestListTest.java
blob: ff3723e1c8c770a1de72963465d718b0a45a1120 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package bjc.esodata;

import static org.junit.Assert.*;

import java.util.*;

import org.junit.*;

import bjc.*;

@SuppressWarnings("javadoc")
public class NestListTest
{

	@Test
	public void testAddItemElement() {
		NestList<String> nl = new NestList<>();
		
		assertEquals("NestLists are created empty", 0, nl.size());
		
		nl.addItems("hello", "there");
		
		assertEquals("Adding two items to an empty NestList makes it size 2",
				2, nl.size());
	}

	@Test
	public void testAddItemNestListOfElement() {
		NestList<String> nl = new NestList<>();
		
		NestList<String> nl2 = new NestList<>();
		
		nl2.addItem("friend");
		
		nl.addItems("hello", "there");
		nl.addItem(nl2);
		
		assertEquals("Adding a sublist increases the size of NestList by 1",
				3, nl.size());
	}

	@Test
	public void testAddSublist() {
		NestList<String> nl = new NestList<>();
		
		nl.addSublist("here", "is", "a");
		nl.addItem("thing");
		
		assertEquals("addSublist increases size of NestList by 1", 2, nl.size());
	}

	@Test
	public void testFlatIterator() {
		NestList<String> nl1 = new NestList<>();
		NestList<String> nl2 = new NestList<>();
		NestList<String> nl3 = new NestList<>();
		
		nl1.addItems("of", "means");
		
		nl2.addItem(nl1);
		
		nl3.addItem("Hello");
		nl3.addSublist("my", "unfortunate");
		nl3.addItem("friend");
		nl3.addItem(nl2);
		
		TestUtils.assertIteratorEquals(nl3.flatIterator(),
				"Hello", "my", "unfortunate", "friend", "of", "means");
	}

	@Test
	public void testFlatten() {	
		NestList<String> nl1A = new NestList<>();
		NestList<String> nl2A = new NestList<>();
		NestList<String> nl3A = new NestList<>();
		
		nl1A.addItems("of", "means");
		
		nl2A.addItem(nl1A);
		
		nl3A.addItem("Hello");
		nl3A.addSublist("my", "unfortunate");
		nl3A.addItem("friend");
		nl3A.addItem(nl2A);
		
		NestList<String> nl1B = new NestList<>();
		NestList<String> nl2B = new NestList<>();
		
		nl1B.addItems("of", "means");

		nl2B.addItems("Hello", "my", "unfortunate", "friend");
		nl2B.addItem(nl1B);
		
		assertEquals("Flatten removes one level of nesting",
				nl2B, nl3A.flatten());
	}

	@Test
	public void testDeepFlatten() {
		NestList<String> nl1 = new NestList<>();
		NestList<String> nl2 = new NestList<>();
		NestList<String> nl3 = new NestList<>();
		
		nl1.addItems("of", "means");
		
		nl2.addItem(nl1);
		
		nl3.addItem("Hello");
		nl3.addSublist("my", "unfortunate");
		nl3.addItem("friend");
		nl3.addItem(nl2);
		
		List<String> testList = Arrays.asList(
				"Hello", "my", "unfortunate", "friend", "of", "means");
		
		assertEquals("deepFlatten flattens out all sublists",
				testList, nl3.deepFlatten());
	}
}