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.juneau.utils;
18  
19  import static org.apache.juneau.TestUtils.*;
20  import static org.junit.jupiter.api.Assertions.*;
21  
22  import java.util.*;
23  
24  import org.apache.juneau.*;
25  import org.apache.juneau.internal.*;
26  import org.junit.jupiter.api.*;
27  
28  class MultiSetTest extends TestBase {
29  
30  	@Test void doTest() {
31  		List<String> l1, l2;
32  		MultiSet<String> ms;
33  
34  		l1 = Arrays.asList(new String[]{"1","2"});
35  		l2 = Arrays.asList(new String[]{"3","4"});
36  		ms = new MultiSet<>(l1, l2);
37  		var i1 = ms.iterator();
38  		assertTrue(i1.hasNext());
39  		assertEquals("1", i1.next());
40  		assertTrue(i1.hasNext());
41  		assertEquals("2", i1.next());
42  		assertTrue(i1.hasNext());
43  		assertEquals("3", i1.next());
44  		assertTrue(i1.hasNext());
45  		assertEquals("4", i1.next());
46  		assertFalse(i1.hasNext());
47  		assertThrows(NoSuchElementException.class, i1::next);
48  
49  		l1 = Arrays.asList(new String[]{"1","2"});
50  		l2 = Arrays.asList(new String[]{});
51  		ms = new MultiSet<>(l1, l2);
52  		var i2 = ms.iterator();
53  		assertTrue(i2.hasNext());
54  		assertEquals("1", i2.next());
55  		assertTrue(i2.hasNext());
56  		assertEquals("2", i2.next());
57  		assertFalse(i2.hasNext());
58  		assertThrows(NoSuchElementException.class, i2::next);
59  
60  		l1 = Arrays.asList(new String[]{});
61  		l2 = Arrays.asList(new String[]{"3","4"});
62  		ms = new MultiSet<>(l1, l2);
63  		var i3 = ms.iterator();
64  		assertTrue(i3.hasNext());
65  		assertEquals("3", i3.next());
66  		assertTrue(i3.hasNext());
67  		assertEquals("4", i3.next());
68  		assertFalse(i3.hasNext());
69  		assertThrows(NoSuchElementException.class, i3::next);
70  
71  		l1 = Arrays.asList(new String[]{});
72  		l2 = Arrays.asList(new String[]{});
73  		ms = new MultiSet<>(l1, l2);
74  		var i4 = ms.iterator();
75  		assertFalse(i4.hasNext());
76  		assertThrows(NoSuchElementException.class, i4::next);
77  
78  		l1 = Arrays.asList(new String[]{"1","2"});
79  		ms = new MultiSet<>(l1);
80  		var i5 = ms.iterator();
81  		assertTrue(i5.hasNext());
82  		assertEquals("1", i5.next());
83  		assertTrue(i5.hasNext());
84  		assertEquals("2", i5.next());
85  		assertFalse(i5.hasNext());
86  		assertThrows(NoSuchElementException.class, i5::next);
87  
88  		l1 = new LinkedList<>(Arrays.asList(new String[]{"1","2"}));
89  		l2 = new LinkedList<>(Arrays.asList(new String[]{"3","4"}));
90  		ms = new MultiSet<>(l1, l2);
91  		assertList(ms, "1", "2", "3", "4");
92  		assertList(ms.enumerator(), "1", "2", "3", "4");
93  		assertEquals(4, ms.size());
94  
95  		var t = ms.iterator();
96  		t.next();
97  		t.remove();
98  		assertList(ms.enumerator(), "2", "3", "4");
99  
100 		t = ms.iterator();
101 		t.next();
102 		t.remove();
103 		assertList(ms.enumerator(), "3", "4");
104 
105 		t = ms.iterator();
106 		t.next();
107 		t.remove();
108 		assertList(ms.enumerator(), "4");
109 
110 		t = ms.iterator();
111 		t.next();
112 		t.remove();
113 		assertEmpty(ms.enumerator());
114 		assertEquals(0, ms.size());
115 
116 		ms = new MultiSet<>();
117 		assertEmpty(ms);
118 		assertEquals(0, ms.size());
119 
120 		assertThrows(IllegalArgumentException.class, ()->new MultiSet<>((Collection<String>)null));
121 		assertThrows(NoSuchElementException.class, ()->new MultiSet<String>().iterator().next());
122 		assertThrows(NoSuchElementException.class, ()->new MultiSet<String>().iterator().remove());
123 	}
124 }