|
11 | 11 | import java.util.ListIterator;
|
12 | 12 | import java.util.Locale;
|
13 | 13 | import java.util.Map;
|
| 14 | +import java.util.NavigableSet; |
14 | 15 | import java.util.NoSuchElementException;
|
15 | 16 | import java.util.Random;
|
16 | 17 | import java.util.Set;
|
| 18 | +import java.util.concurrent.ConcurrentSkipListSet; |
17 | 19 |
|
18 | 20 | import com.jwetherell.algorithms.data_structures.AVLTree;
|
19 | 21 | import com.jwetherell.algorithms.data_structures.BTree;
|
|
28 | 30 | import com.jwetherell.algorithms.data_structures.IList;
|
29 | 31 | import com.jwetherell.algorithms.data_structures.IMap;
|
30 | 32 | import com.jwetherell.algorithms.data_structures.IQueue;
|
| 33 | +import com.jwetherell.algorithms.data_structures.ISet; |
31 | 34 | import com.jwetherell.algorithms.data_structures.IStack;
|
32 | 35 | import com.jwetherell.algorithms.data_structures.ITree;
|
33 | 36 | import com.jwetherell.algorithms.data_structures.IntervalTree;
|
@@ -80,7 +83,7 @@ public class DataStructures {
|
80 | 83 | private static boolean validateContents = true; // Was the item added/removed really added/removed from the structure
|
81 | 84 | private static boolean validateIterator = true; // Does the iterator(s) work
|
82 | 85 |
|
83 |
| - private static final int TESTS = 34; // Max number of dynamic data structures to test |
| 86 | + private static final int TESTS = 35; // Max number of dynamic data structures to test |
84 | 87 | private static final String[] testNames = new String[TESTS]; // Array to hold the test names
|
85 | 88 | private static final long[][] testResults = new long[TESTS][]; // Array to hold the test results
|
86 | 89 | private static int testIndex = 0; // Index into the tests
|
@@ -254,6 +257,12 @@ private static boolean runTests() {
|
254 | 257 | return false;
|
255 | 258 | }
|
256 | 259 |
|
| 260 | + passed = testJavaSkipList(); |
| 261 | + if (!passed) { |
| 262 | + System.err.println("Java's Skip List failed."); |
| 263 | + return false; |
| 264 | + } |
| 265 | + |
257 | 266 | passed = testSkipList();
|
258 | 267 | if (!passed) {
|
259 | 268 | System.err.println("Skip List failed.");
|
@@ -1568,12 +1577,20 @@ private static boolean testSegmentTree() {
|
1568 | 1577 | return true;
|
1569 | 1578 | }
|
1570 | 1579 |
|
| 1580 | + private static boolean testJavaSkipList() { |
| 1581 | + String sName = "Java's SkipList"; |
| 1582 | + NavigableSet<Integer> sList = new ConcurrentSkipListSet<Integer>(); |
| 1583 | + Collection<Integer> lCollection = sList; |
| 1584 | + if(!testJavaCollection(lCollection,Type.Integer,sName)) return false; |
| 1585 | + return true; |
| 1586 | + } |
| 1587 | + |
1571 | 1588 | private static boolean testSkipList() {
|
1572 | 1589 | String sName = "SkipList";
|
1573 | 1590 | SkipList<Integer> sList = new SkipList<Integer>();
|
1574 | 1591 | Collection<Integer> lCollection = sList.toCollection();
|
1575 | 1592 |
|
1576 |
| - if((validateStructure||validateContents) && !testList(sList,sName)) return false; |
| 1593 | + if((validateStructure||validateContents) && !testSet(sList,sName)) return false; |
1577 | 1594 | if(!testJavaCollection(lCollection,Type.Integer,sName)) return false;
|
1578 | 1595 | return true;
|
1579 | 1596 | }
|
@@ -2586,6 +2603,120 @@ private static <T extends Comparable<T>> boolean testList(IList<T> list, String
|
2586 | 2603 | return true;
|
2587 | 2604 | }
|
2588 | 2605 |
|
| 2606 | + /** |
| 2607 | + * Tests the actual list operations |
| 2608 | + * |
| 2609 | + * @param list to test. |
| 2610 | + * @return True is works as a list structure. |
| 2611 | + */ |
| 2612 | + private static <T extends Comparable<T>> boolean testSet(ISet<T> list, String name) { |
| 2613 | + for (int i = 0; i < unsorted.length; i++) { |
| 2614 | + T item = (T)unsorted[i]; |
| 2615 | + list.add(item); |
| 2616 | + if (validateStructure && !list.validate() && !(list.size() == i + 1)) { |
| 2617 | + System.err.println(name+" YIKES!! " + item + " caused a size mismatch."); |
| 2618 | + handleError(list); |
| 2619 | + return false; |
| 2620 | + } |
| 2621 | + if (validateContents && !list.contains(item)) { |
| 2622 | + System.err.println(name+" YIKES!! " + item + " doesn't exists but has been added."); |
| 2623 | + handleError(list); |
| 2624 | + return false; |
| 2625 | + } |
| 2626 | + } |
| 2627 | + |
| 2628 | + boolean contains = list.contains((T)INVALID); |
| 2629 | + if (contains) { |
| 2630 | + System.err.println(name+" invalidity check. contains=" + contains); |
| 2631 | + handleError(list); |
| 2632 | + return false; |
| 2633 | + } |
| 2634 | + |
| 2635 | + int size = list.size(); |
| 2636 | + for (int i = 0; i < size; i++) { |
| 2637 | + T item = (T)unsorted[i]; |
| 2638 | + boolean removed = list.remove(item); |
| 2639 | + if (validateStructure && !list.validate() && !(list.size() == unsorted.length - (i + 1))) { |
| 2640 | + System.err.println(name+" YIKES!! " + item + " caused a size mismatch."); |
| 2641 | + handleError(list); |
| 2642 | + return false; |
| 2643 | + } |
| 2644 | + if (validateContents && removed && list.contains(item)) { |
| 2645 | + System.err.println(name+" YIKES!! " + item + " still exists but it has been remove."); |
| 2646 | + handleError(list); |
| 2647 | + return false; |
| 2648 | + } |
| 2649 | + } |
| 2650 | + |
| 2651 | + // Add half, remove a quarter, add three-quarters, remove all |
| 2652 | + int quarter = unsorted.length/4; |
| 2653 | + int half = unsorted.length/2; |
| 2654 | + for (int i = 0; i < half; i++) { |
| 2655 | + T item = (T)unsorted[i]; |
| 2656 | + list.add(item); |
| 2657 | + if (validateStructure && !list.validate() && !(list.size() == i + 1)) { |
| 2658 | + System.err.println(name+" YIKES!! " + item + " caused a size mismatch."); |
| 2659 | + handleError(list); |
| 2660 | + return false; |
| 2661 | + } |
| 2662 | + if (validateContents && !list.contains(item)) { |
| 2663 | + System.err.println(name+" YIKES!! " + item + " doesn't exists but has been added."); |
| 2664 | + handleError(list); |
| 2665 | + return false; |
| 2666 | + } |
| 2667 | + } |
| 2668 | + for (int i = (half-1); i >= quarter; i--) { |
| 2669 | + T item = (T)unsorted[i]; |
| 2670 | + boolean removed = list.remove(item); |
| 2671 | + if (validateStructure && !list.validate() && !(list.size() == i)) { |
| 2672 | + System.err.println(name+" YIKES!! " + item + " caused a size mismatch."); |
| 2673 | + handleError(list); |
| 2674 | + return false; |
| 2675 | + } |
| 2676 | + if (validateContents && removed && list.contains(item)) { |
| 2677 | + System.err.println(name+" YIKES!! " + item + " still exists but it has been remove."); |
| 2678 | + handleError(list); |
| 2679 | + return false; |
| 2680 | + } |
| 2681 | + } |
| 2682 | + for (int i = quarter; i < unsorted.length; i++) { |
| 2683 | + T item = (T)unsorted[i]; |
| 2684 | + list.add(item); |
| 2685 | + if (validateStructure && !list.validate() && !(list.size() == i + 1)) { |
| 2686 | + System.err.println(name+" YIKES!! " + item + " caused a size mismatch."); |
| 2687 | + handleError(list); |
| 2688 | + return false; |
| 2689 | + } |
| 2690 | + if (validateContents && !list.contains(item)) { |
| 2691 | + System.err.println(name+" YIKES!! " + item + " doesn't exists but has been added."); |
| 2692 | + handleError(list); |
| 2693 | + return false; |
| 2694 | + } |
| 2695 | + } |
| 2696 | + for (int i = unsorted.length-1; i >= 0; i--) { |
| 2697 | + T item = (T)unsorted[i]; |
| 2698 | + boolean removed = list.remove(item); |
| 2699 | + if (validateStructure && !list.validate() && !(list.size() == i)) { |
| 2700 | + System.err.println(name+" YIKES!! " + item + " caused a size mismatch."); |
| 2701 | + handleError(list); |
| 2702 | + return false; |
| 2703 | + } |
| 2704 | + if (validateContents && removed && list.contains(item)) { |
| 2705 | + System.err.println(name+" YIKES!! " + item + " still exists but it has been remove."); |
| 2706 | + handleError(list); |
| 2707 | + return false; |
| 2708 | + } |
| 2709 | + } |
| 2710 | + |
| 2711 | + if (validateStructure && (list.size() != 0)) { |
| 2712 | + System.err.println(name+" YIKES!! a size mismatch."); |
| 2713 | + handleError(list); |
| 2714 | + return false; |
| 2715 | + } |
| 2716 | + |
| 2717 | + return true; |
| 2718 | + } |
| 2719 | + |
2589 | 2720 | private static <T extends Comparable<T>> boolean testJavaCollection(Collection<T> collection, Type type, String name) {
|
2590 | 2721 | // Make sure the collection is empty
|
2591 | 2722 | if (!collection.isEmpty()) {
|
|
0 commit comments