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.testclassification.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, 2L));
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 @SuppressWarnings("ModifyingCollectionWithItself")
118 @edu.umd.cs.findbugs.annotations.SuppressWarnings(value="DMI_VACUOUS_SELF_COLLECTION_CALL",
119 justification="Intended vacuous containsAll call on 'c'")
120 private void verify(ConcatenatedLists<Long> c, int last) {
121 assertEquals((last == -1), c.isEmpty());
122 assertEquals(last + 1, c.size());
123
124 assertTrue(c.containsAll(c));
125 Long[] array = c.toArray(new Long[c.size()]);
126 List<Long> all = new ArrayList<Long>();
127 Iterator<Long> iter = c.iterator();
128 for (Long i = 0L; i <= last; ++i) {
129 assertTrue(iter.hasNext());
130 assertEquals(i, iter.next());
131 assertEquals(i, array[i.intValue()]);
132 assertTrue(c.contains(i));
133 assertTrue(c.containsAll(Arrays.asList(i)));
134 all.add(i);
135 }
136 assertTrue(c.containsAll(all));
137 assertFalse(iter.hasNext());
138 try {
139 iter.next();
140 fail("Should have thrown");
141 } catch (NoSuchElementException nsee) {
142 }
143 }
144 }