This commit is contained in:
2024-11-30 19:03:49 +08:00
commit 1e6763c160
3806 changed files with 737676 additions and 0 deletions

View File

@@ -0,0 +1,215 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.tomcat.util.collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.junit.Assert;
import org.junit.Test;
public class TestCaseInsensitiveKeyMap {
@Test
public void testPut() {
Object o1 = new Object();
Object o2 = new Object();
CaseInsensitiveKeyMap<Object> map = new CaseInsensitiveKeyMap<>();
map.put("a", o1);
Object o = map.put("A", o2);
Assert.assertEquals(o1, o);
Assert.assertEquals(o2, map.get("a"));
Assert.assertEquals(o2, map.get("A"));
}
@Test(expected=NullPointerException.class)
public void testPutNullKey() {
Object o1 = new Object();
CaseInsensitiveKeyMap<Object> map = new CaseInsensitiveKeyMap<>();
map.put(null, o1);
}
@Test
public void testGet() {
Object o1 = new Object();
CaseInsensitiveKeyMap<Object> map = new CaseInsensitiveKeyMap<>();
map.put("a", o1);
Assert.assertEquals(o1, map.get("a"));
Assert.assertEquals(o1, map.get("A"));
}
@Test
public void testGetNullKey() {
Object o1 = new Object();
CaseInsensitiveKeyMap<Object> map = new CaseInsensitiveKeyMap<>();
map.put("a", o1);
Assert.assertNull(map.get(null));
}
@Test
public void testContainsKey() {
Object o1 = new Object();
CaseInsensitiveKeyMap<Object> map = new CaseInsensitiveKeyMap<>();
map.put("a", o1);
Assert.assertTrue(map.containsKey("a"));
Assert.assertTrue(map.containsKey("A"));
}
@Test
public void testContainsKeyNonString() {
Object o1 = new Object();
CaseInsensitiveKeyMap<Object> map = new CaseInsensitiveKeyMap<>();
map.put("a", o1);
Assert.assertFalse(map.containsKey(o1));
}
@Test
public void testContainsKeyNull() {
Object o1 = new Object();
CaseInsensitiveKeyMap<Object> map = new CaseInsensitiveKeyMap<>();
map.put("a", o1);
Assert.assertFalse(map.containsKey(null));
}
@Test
public void testContainsValue() {
Object o1 = new Object();
CaseInsensitiveKeyMap<Object> map = new CaseInsensitiveKeyMap<>();
map.put("a", o1);
Assert.assertTrue(map.containsValue(o1));
}
@Test
public void testRemove() {
Object o1 = new Object();
CaseInsensitiveKeyMap<Object> map = new CaseInsensitiveKeyMap<>();
map.put("a", o1);
Assert.assertFalse(map.isEmpty());
map.remove("A");
Assert.assertTrue(map.isEmpty());
map.put("A", o1);
Assert.assertFalse(map.isEmpty());
map.remove("a");
Assert.assertTrue(map.isEmpty());
}
@Test
public void testClear() {
Object o1 = new Object();
CaseInsensitiveKeyMap<Object> map = new CaseInsensitiveKeyMap<>();
for (int i = 0; i < 10; i++) {
map.put(Integer.toString(i), o1);
}
Assert.assertEquals(10, map.size());
map.clear();
Assert.assertEquals(0, map.size());
}
@Test
public void testPutAll() {
Object o1 = new Object();
Object o2 = new Object();
Map<String,Object> source = new HashMap<>();
source.put("a", o1);
source.put("A", o2);
CaseInsensitiveKeyMap<Object> map = new CaseInsensitiveKeyMap<>();
map.putAll(source);
Assert.assertEquals(1, map.size());
Assert.assertTrue(map.containsValue(o1) != map.containsValue(o2));
}
@Test
public void testKeySetContains() {
Object o1 = new Object();
CaseInsensitiveKeyMap<Object> map = new CaseInsensitiveKeyMap<>();
map.put("a", o1);
Set<String> keys = map.keySet();
Assert.assertTrue(keys.contains("a"));
Assert.assertTrue(keys.contains("A"));
}
@Test
public void testKeySetRemove() {
Object o1 = new Object();
CaseInsensitiveKeyMap<Object> map = new CaseInsensitiveKeyMap<>();
map.put("a", o1);
Iterator<String> iter = map.keySet().iterator();
Assert.assertTrue(iter.hasNext());
iter.next();
iter.remove();
Assert.assertTrue(map.isEmpty());
}
@Test
public void testEntrySetRemove() {
Object o1 = new Object();
CaseInsensitiveKeyMap<Object> map = new CaseInsensitiveKeyMap<>();
map.put("a", o1);
Iterator<Entry<String,Object>> iter = map.entrySet().iterator();
Assert.assertTrue(iter.hasNext());
Entry<String,Object> entry = iter.next();
Assert.assertEquals("a", entry.getKey());
Assert.assertEquals(o1, entry.getValue());
iter.remove();
Assert.assertTrue(map.isEmpty());
}
}

View File

@@ -0,0 +1,117 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.tomcat.util.collections;
import org.junit.Assert;
import org.junit.Test;
public class TestSynchronizedQueue {
public void testPollEmpty() {
SynchronizedQueue<Object> queue = new SynchronizedQueue<>();
Assert.assertNull(queue.poll());
}
@Test
public void testOfferPollOrder() {
SynchronizedQueue<Object> queue = new SynchronizedQueue<>();
Object o1 = new Object();
Object o2 = new Object();
Object o3 = new Object();
Object o4 = new Object();
queue.offer(o1);
queue.offer(o2);
queue.offer(o3);
queue.offer(o4);
Assert.assertSame(queue.poll(), o1);
Assert.assertSame(queue.poll(), o2);
Assert.assertSame(queue.poll(), o3);
Assert.assertSame(queue.poll(), o4);
Assert.assertNull(queue.poll());
}
@Test
public void testExpandOfferPollOrder() {
SynchronizedQueue<Object> queue = new SynchronizedQueue<>();
Object o1 = new Object();
Object o2 = new Object();
Object o3 = new Object();
Object o4 = new Object();
for (int i = 0; i < 300; i++) {
queue.offer(o1);
queue.offer(o2);
queue.offer(o3);
queue.offer(o4);
}
for (int i = 0; i < 300; i++) {
Assert.assertSame(queue.poll(), o1);
Assert.assertSame(queue.poll(), o2);
Assert.assertSame(queue.poll(), o3);
Assert.assertSame(queue.poll(), o4);
}
Assert.assertNull(queue.poll());
}
@Test
public void testExpandOfferPollOrder2() {
SynchronizedQueue<Object> queue = new SynchronizedQueue<>();
Object o1 = new Object();
Object o2 = new Object();
Object o3 = new Object();
Object o4 = new Object();
for (int i = 0; i < 100; i++) {
queue.offer(o1);
queue.offer(o2);
queue.offer(o3);
queue.offer(o4);
}
for (int i = 0; i < 50; i++) {
Assert.assertSame(queue.poll(), o1);
Assert.assertSame(queue.poll(), o2);
Assert.assertSame(queue.poll(), o3);
Assert.assertSame(queue.poll(), o4);
}
for (int i = 0; i < 200; i++) {
queue.offer(o1);
queue.offer(o2);
queue.offer(o3);
queue.offer(o4);
}
for (int i = 0; i < 250; i++) {
Assert.assertSame(queue.poll(), o1);
Assert.assertSame(queue.poll(), o2);
Assert.assertSame(queue.poll(), o3);
Assert.assertSame(queue.poll(), o4);
}
Assert.assertNull(queue.poll());
}
}

View File

@@ -0,0 +1,119 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.tomcat.util.collections;
import org.junit.Assert;
import org.junit.Test;
public class TestSynchronizedStack {
@Test
public void testPopEmpty() {
SynchronizedStack<Object> stack = new SynchronizedStack<>();
Assert.assertNull(stack.pop());
}
@Test
public void testPushPopOrder() {
SynchronizedStack<Object> stack = new SynchronizedStack<>();
Object o1 = new Object();
Object o2 = new Object();
Object o3 = new Object();
Object o4 = new Object();
stack.push(o1);
stack.push(o2);
stack.push(o3);
stack.push(o4);
Assert.assertSame(stack.pop(), o4);
Assert.assertSame(stack.pop(), o3);
Assert.assertSame(stack.pop(), o2);
Assert.assertSame(stack.pop(), o1);
Assert.assertNull(stack.pop());
}
@Test
public void testExpandPushPopOrder() {
SynchronizedStack<Object> stack = new SynchronizedStack<>();
Object o1 = new Object();
Object o2 = new Object();
Object o3 = new Object();
Object o4 = new Object();
for (int i = 0; i < 300; i++) {
stack.push(o1);
stack.push(o2);
stack.push(o3);
stack.push(o4);
}
for (int i = 0; i < 300; i++) {
Assert.assertSame(stack.pop(), o4);
Assert.assertSame(stack.pop(), o3);
Assert.assertSame(stack.pop(), o2);
Assert.assertSame(stack.pop(), o1);
}
Assert.assertNull(stack.pop());
}
@Test
public void testLimit() {
SynchronizedStack<Object> stack = new SynchronizedStack<>(2,2);
Object o1 = new Object();
Object o2 = new Object();
Object o3 = new Object();
Object o4 = new Object();
stack.push(o1);
stack.push(o2);
stack.push(o3);
stack.push(o4);
Assert.assertSame(stack.pop(), o2);
Assert.assertSame(stack.pop(), o1);
Assert.assertNull(stack.pop());
}
@Test
public void testLimitExpand() {
SynchronizedStack<Object> stack = new SynchronizedStack<>(1,3);
Object o1 = new Object();
Object o2 = new Object();
Object o3 = new Object();
Object o4 = new Object();
stack.push(o1);
stack.push(o2);
stack.push(o3);
stack.push(o4);
Assert.assertSame(stack.pop(), o3);
Assert.assertSame(stack.pop(), o2);
Assert.assertSame(stack.pop(), o1);
Assert.assertNull(stack.pop());
}
}

View File

@@ -0,0 +1,107 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.tomcat.util.collections;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import org.junit.Test;
public class TesterPerformanceSynchronizedQueue {
private static final int THREAD_COUNT = 4;
private static final int ITERATIONS = 1000000;
private static final SynchronizedQueue<Object> S_QUEUE =
new SynchronizedQueue<>();
private static final Queue<Object> QUEUE = new ConcurrentLinkedQueue<>();
@Test
public void testSynchronizedQueue() throws InterruptedException {
Thread[] threads = new Thread[THREAD_COUNT];
for (int i = 0; i < THREAD_COUNT; i++) {
threads[i] = new StackThread();
}
long start = System.currentTimeMillis();
for (int i = 0; i < THREAD_COUNT; i++) {
threads[i].start();
}
for (int i = 0; i < THREAD_COUNT; i++) {
threads[i].join();
}
long end = System.currentTimeMillis();
System.out.println("SynchronizedQueue: " + (end - start) + "ms");
}
public static class StackThread extends Thread {
@Override
public void run() {
for(int i = 0; i < ITERATIONS; i++) {
Object obj = S_QUEUE.poll();
if (obj == null) {
obj = new Object();
}
S_QUEUE.offer(obj);
}
super.run();
}
}
@Test
public void testConcurrentQueue() throws InterruptedException {
Thread[] threads = new Thread[THREAD_COUNT];
for (int i = 0; i < THREAD_COUNT; i++) {
threads[i] = new QueueThread();
}
long start = System.currentTimeMillis();
for (int i = 0; i < THREAD_COUNT; i++) {
threads[i].start();
}
for (int i = 0; i < THREAD_COUNT; i++) {
threads[i].join();
}
long end = System.currentTimeMillis();
System.out.println("ConcurrentLinkedQueue: " + (end - start) + "ms");
}
public static class QueueThread extends Thread {
@Override
public void run() {
for(int i = 0; i < ITERATIONS; i++) {
Object obj = QUEUE.poll();
if (obj == null) {
obj = new Object();
}
QUEUE.offer(obj);
}
super.run();
}
}
}

View File

@@ -0,0 +1,107 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.tomcat.util.collections;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import org.junit.Test;
public class TesterPerformanceSynchronizedStack {
private static final int THREAD_COUNT = 4;
private static final int ITERATIONS = 1000000;
private static final SynchronizedStack<Object> STACK =
new SynchronizedStack<>();
private static final Queue<Object> QUEUE = new ConcurrentLinkedQueue<>();
@Test
public void testSynchronizedStack() throws InterruptedException {
Thread[] threads = new Thread[THREAD_COUNT];
for (int i = 0; i < THREAD_COUNT; i++) {
threads[i] = new StackThread();
}
long start = System.currentTimeMillis();
for (int i = 0; i < THREAD_COUNT; i++) {
threads[i].start();
}
for (int i = 0; i < THREAD_COUNT; i++) {
threads[i].join();
}
long end = System.currentTimeMillis();
System.out.println("SynchronizedStack: " + (end - start) + "ms");
}
public static class StackThread extends Thread {
@Override
public void run() {
for(int i = 0; i < ITERATIONS; i++) {
Object obj = STACK.pop();
if (obj == null) {
obj = new Object();
}
STACK.push(obj);
}
super.run();
}
}
@Test
public void testConcurrentQueue() throws InterruptedException {
Thread[] threads = new Thread[THREAD_COUNT];
for (int i = 0; i < THREAD_COUNT; i++) {
threads[i] = new QueueThread();
}
long start = System.currentTimeMillis();
for (int i = 0; i < THREAD_COUNT; i++) {
threads[i].start();
}
for (int i = 0; i < THREAD_COUNT; i++) {
threads[i].join();
}
long end = System.currentTimeMillis();
System.out.println("ConcurrentLinkedQueue: " + (end - start) + "ms");
}
public static class QueueThread extends Thread {
@Override
public void run() {
for(int i = 0; i < ITERATIONS; i++) {
Object obj = QUEUE.poll();
if (obj == null) {
obj = new Object();
}
QUEUE.offer(obj);
}
super.run();
}
}
}