001: package org.drools.util;
002:
003: /*
004: * Copyright 2005 JBoss Inc
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: import java.util.Iterator;
020: import java.util.LinkedList;
021: import java.util.List;
022: import java.util.Random;
023:
024: import junit.framework.TestCase;
025:
026: public class BinaryHeapPriorityQueueTest extends TestCase {
027: public void testOptimised() {
028: final Random random = new Random();
029: final List items = new LinkedList();
030:
031: final Queue queue = new BinaryHeapQueue(
032: NaturalComparator.INSTANCE, 100000);
033:
034: for (int i = 0; i < 100000; ++i) {
035: items.add(new LongQueueable(random.nextLong()));
036: }
037:
038: final long startEnqueue = System.currentTimeMillis();
039:
040: for (final Iterator i = items.iterator(); i.hasNext();) {
041: queue.enqueue((Queueable) i.next());
042: }
043:
044: final long elapsedEnqueue = System.currentTimeMillis()
045: - startEnqueue;
046:
047: final long startDequeue = System.currentTimeMillis();
048:
049: for (final Iterator i = items.iterator(); i.hasNext();) {
050: ((Queueable) i.next()).dequeue();
051: }
052:
053: // while (!queue.isEmpty()) {
054: // queue.dequeue();
055: // }
056:
057: final long elapsedDequeue = System.currentTimeMillis()
058: - startDequeue;
059:
060: // System.out.println( "elapsedEnqueue = " + elapsedEnqueue );
061: // System.out.println( "elapsedDequeue = " + elapsedDequeue );
062: }
063:
064: public void testBasic() {
065: final Random random = new Random();
066: final List items = new LinkedList();
067:
068: final BinaryHeapQueue queue = new BinaryHeapQueue(
069: NaturalComparator.INSTANCE, 100000);
070:
071: for (int i = 0; i < 100000; ++i) {
072: items.add(new LongQueueable(random.nextLong()));
073: }
074:
075: final long startEnqueue = System.currentTimeMillis();
076:
077: for (final Iterator i = items.iterator(); i.hasNext();) {
078: queue.enqueue((Queueable) i.next());
079: }
080:
081: final long elapsedEnqueue = System.currentTimeMillis()
082: - startEnqueue;
083:
084: final long startDequeue = System.currentTimeMillis();
085:
086: for (final Iterator i = items.iterator(); i.hasNext();) {
087: queue.enqueue((Queueable) i.next());
088: }
089:
090: // while (!queue.isEmpty()) {
091: // queue.pop();
092: // }
093:
094: final long elapsedDequeue = System.currentTimeMillis()
095: - startDequeue;
096:
097: // System.out.println( "elapsedEnqueue = " + elapsedEnqueue );
098: // System.out.println( "elapsedDequeue = " + elapsedDequeue );
099: }
100: }
|