1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.util;
20
21 import java.util.ArrayList;
22 import java.util.Arrays;
23 import java.util.Iterator;
24 import java.util.List;
25 import java.util.NoSuchElementException;
26
27 import org.apache.hadoop.hbase.SmallTests;
28 import org.junit.Test;
29 import org.junit.experimental.categories.Category;
30
31 import static org.junit.Assert.*;
32
33 @Category(SmallTests.class)
34 public class TestConcatenatedLists {
35 @Test
36 public void testUnsupportedOps() {
37
38 ConcatenatedLists<Long> c = new ConcatenatedLists<Long>();
39 c.addSublist(Arrays.asList(0L, 1L));
40 try {
41 c.add(2L);
42 fail("Should throw");
43 } catch (UnsupportedOperationException ex) {
44 }
45 try {
46 c.addAll(Arrays.asList(2L, 3L));
47 fail("Should throw");
48 } catch (UnsupportedOperationException ex) {
49 }
50 try {
51 c.remove(0L);
52 fail("Should throw");
53 } catch (UnsupportedOperationException ex) {
54 }
55 try {
56 c.removeAll(Arrays.asList(0L, 1L));
57 fail("Should throw");
58 } catch (UnsupportedOperationException ex) {
59 }
60 try {
61 c.clear();
62 fail("Should throw");
63 } catch (UnsupportedOperationException ex) {
64 }
65 try {
66 c.retainAll(Arrays.asList(0L, 1L));
67 fail("Should throw");
68 } catch (UnsupportedOperationException ex) {
69 }
70 Iterator<Long> iter = c.iterator();
71 iter.next();
72 try {
73 iter.remove();
74 fail("Should throw");
75 } catch (UnsupportedOperationException ex) {
76 }
77 }
78
79 @Test
80 public void testEmpty() {
81 verify(new ConcatenatedLists<Long>(), -1);
82 }
83
84 @Test
85 public void testOneOne() {
86 ConcatenatedLists<Long> c = new ConcatenatedLists<Long>();
87 c.addSublist(Arrays.asList(0L));
88 verify(c, 0);
89 }
90
91 @Test
92 public void testOneMany() {
93 ConcatenatedLists<Long> c = new ConcatenatedLists<Long>();
94 c.addSublist(Arrays.asList(0L, 1L, 2L));
95 verify(c, 2);
96 }
97
98 @Test
99 @SuppressWarnings("unchecked")
100 public void testManyOne() {
101 ConcatenatedLists<Long> c = new ConcatenatedLists<Long>();
102 c.addSublist(Arrays.asList(0L));
103 c.addAllSublists(Arrays.asList(Arrays.asList(1L), Arrays.asList(2L)));
104 verify(c, 2);
105 }
106
107 @Test
108 @SuppressWarnings("unchecked")
109 public void testManyMany() {
110 ConcatenatedLists<Long> c = new ConcatenatedLists<Long>();
111 c.addAllSublists(Arrays.asList(Arrays.asList(0L, 1L)));
112 c.addSublist(Arrays.asList(2L, 3L, 4L));
113 c.addAllSublists(Arrays.asList(Arrays.asList(5L), Arrays.asList(6L, 7L)));
114 verify(c, 7);
115 }
116
117 private void verify(ConcatenatedLists<Long> c, int last) {
118 assertEquals((last == -1), c.isEmpty());
119 assertEquals(last + 1, c.size());
120 assertTrue(c.containsAll(c));
121 Long[] array = c.toArray(new Long[0]);
122 List<Long> all = new ArrayList<Long>();
123 Iterator<Long> iter = c.iterator();
124 for (Long i = 0L; i <= last; ++i) {
125 assertTrue(iter.hasNext());
126 assertEquals(i, iter.next());
127 assertEquals(i, array[i.intValue()]);
128 assertTrue(c.contains(i));
129 assertTrue(c.containsAll(Arrays.asList(i)));
130 all.add(i);
131 }
132 assertTrue(c.containsAll(all));
133 assertFalse(iter.hasNext());
134 try {
135 iter.next();
136 fail("Should have thrown");
137 } catch (NoSuchElementException nsee) {
138 }
139 }
140 }