View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.collections4.set;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertSame;
21  import static org.junit.jupiter.api.Assertions.assertTrue;
22  
23  import java.util.ArrayList;
24  import java.util.Iterator;
25  import java.util.List;
26  
27  import org.junit.jupiter.api.Test;
28  
29  /**
30   * Extension of {@link AbstractSetTest} for exercising the {@link ListOrderedSet}
31   * implementation.
32   */
33  public class ListOrderedSet2Test<E> extends AbstractSetTest<E> {
34  
35      private static final Integer ZERO = Integer.valueOf(0);
36      private static final Integer ONE = Integer.valueOf(1);
37      private static final Integer TWO = Integer.valueOf(2);
38      private static final Integer THREE = Integer.valueOf(3);
39  
40      @Override
41      public String getCompatibilityVersion() {
42          return "4";
43      }
44  
45      @Override
46      public ListOrderedSet<E> makeObject() {
47          return new ListOrderedSet<>();
48      }
49  
50      @SuppressWarnings("unchecked")
51      protected ListOrderedSet<E> setupSet() {
52          final ListOrderedSet<E> set = makeObject();
53  
54          for (int i = 0; i < 10; i++) {
55              set.add((E) Integer.toString(i));
56          }
57          return set;
58      }
59  
60      @Test
61      @SuppressWarnings("unchecked")
62      public void testListAddIndexed() {
63          final ListOrderedSet<E> set = makeObject();
64          set.add((E) ZERO);
65          set.add((E) TWO);
66  
67          set.add(1, (E) ONE);
68          assertEquals(3, set.size());
69          assertSame(ZERO, set.get(0));
70          assertSame(ONE, set.get(1));
71          assertSame(TWO, set.get(2));
72  
73          set.add(0, (E) ONE);
74          assertEquals(3, set.size());
75          assertSame(ZERO, set.get(0));
76          assertSame(ONE, set.get(1));
77          assertSame(TWO, set.get(2));
78  
79          final List<E> list = new ArrayList<>();
80          list.add((E) ZERO);
81          list.add((E) TWO);
82  
83          set.addAll(0, list);
84          assertEquals(3, set.size());
85          assertSame(ZERO, set.get(0));
86          assertSame(ONE, set.get(1));
87          assertSame(TWO, set.get(2));
88  
89          list.add(0, (E) THREE); // list = [3,0,2]
90          set.remove(TWO);    //  set = [0,1]
91          set.addAll(1, list);
92          assertEquals(4, set.size());
93          assertSame(ZERO, set.get(0));
94          assertSame(THREE, set.get(1));
95          assertSame(TWO, set.get(2));
96          assertSame(ONE, set.get(3));
97      }
98  
99      @Test
100     @SuppressWarnings("unchecked")
101     public void testListAddRemove() {
102         final ListOrderedSet<E> set = makeObject();
103         final List<E> view = set.asList();
104         set.add((E) ZERO);
105         set.add((E) ONE);
106         set.add((E) TWO);
107 
108         assertEquals(3, set.size());
109         assertSame(ZERO, set.get(0));
110         assertSame(ONE, set.get(1));
111         assertSame(TWO, set.get(2));
112         assertEquals(3, view.size());
113         assertSame(ZERO, view.get(0));
114         assertSame(ONE, view.get(1));
115         assertSame(TWO, view.get(2));
116 
117         assertEquals(0, set.indexOf(ZERO));
118         assertEquals(1, set.indexOf(ONE));
119         assertEquals(2, set.indexOf(TWO));
120 
121         set.remove(1);
122         assertEquals(2, set.size());
123         assertSame(ZERO, set.get(0));
124         assertSame(TWO, set.get(1));
125         assertEquals(2, view.size());
126         assertSame(ZERO, view.get(0));
127         assertSame(TWO, view.get(1));
128     }
129 
130     @Test
131     @SuppressWarnings("unchecked")
132     public void testOrdering() {
133         final ListOrderedSet<E> set = setupSet();
134         Iterator<E> it = set.iterator();
135 
136         for (int i = 0; i < 10; i++) {
137             assertEquals(Integer.toString(i), it.next(), "Sequence is wrong");
138         }
139 
140         for (int i = 0; i < 10; i += 2) {
141             assertTrue(set.remove(Integer.toString(i)), "Must be able to remove int");
142         }
143 
144         it = set.iterator();
145         for (int i = 1; i < 10; i += 2) {
146             assertEquals(Integer.toString(i), it.next(), "Sequence is wrong after remove ");
147         }
148 
149         for (int i = 0; i < 10; i++) {
150             set.add((E) Integer.toString(i));
151         }
152 
153         assertEquals(10, set.size(), "Size of set is wrong!");
154 
155         it = set.iterator();
156         for (int i = 1; i < 10; i += 2) {
157             assertEquals(Integer.toString(i), it.next(), "Sequence is wrong");
158         }
159         for (int i = 0; i < 10; i += 2) {
160             assertEquals(Integer.toString(i), it.next(), "Sequence is wrong");
161         }
162     }
163 
164 //    public void testCreate() throws Exception {
165 //        resetEmpty();
166 //        writeExternalFormToDisk((java.io.Serializable) collection, "D:/dev/collections/data/test/ListOrderedSet.emptyCollection.version3.1.obj");
167 //        resetFull();
168 //        writeExternalFormToDisk((java.io.Serializable) collection, "D:/dev/collections/data/test/ListOrderedSet.fullCollection.version3.1.obj");
169 //    }
170 
171 }