/** * @license * Copyright Larry Diamond 2019 All Rights Reserved. * * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://github.com/larrydiamond/typescriptcollectionsframework/LICENSE */ import {AllFieldCollectable} from "../src/AllFieldCollectable"; import {AllFieldHashable} from "../src/AllFieldHashable"; import {ArrayList} from "../src/ArrayList"; import {Collection} from "../src/Collection"; import {Collections} from "../src/Collections"; import {Comparator} from "../src/Comparator"; import {CompositeCollection} from "../src/CompositeCollection"; import {Consumer} from "../src/Consumer"; import {HashSet} from "../src/HashSet"; import {ImmutableCollection} from "../src/ImmutableCollection"; import {JIterator} from "../src/JIterator"; import {LinkedList} from "../src/LinkedList"; import {HashMultiSet} from "../src/HashClasses"; import {NavigableHashSet} from "../src/NavigableHash"; import {PriorityQueue} from "../src/PriorityQueue"; import {SkipListSet} from "../src/SkipList"; import {TreeSet} from "../src/TreeSet"; import {Test, TestBoolean, TestNumber, TestString} from 'jasts'; import {EvictingDeque} from "../src/EvictingDeque"; describe("Test Collection", function() { it("Test empty string Collections ArrayList", function() { testEmptyStringCollection(new ArrayList (), "ArrayList"); testEmptyStringCollection(new ArrayList (new AllFieldCollectable()), "ArrayList AllFieldCollectable"); }); it("Test empty string Collections LinkedList", function() { testEmptyStringCollection(new LinkedList (), "LinkedList"); testEmptyStringCollection(new LinkedList (new AllFieldCollectable()), "LinkedList AllFieldCollectable"); }); it("Test empty string Collections HashSet", function() { testEmptyStringCollection(new HashSet (), "HashSet"); testEmptyStringCollection(new HashSet (new AllFieldHashable()), "HashSet AllFieldHashable"); }); it("Test empty string Collections Empty", function() { testEmptyStringCollection(Collections.emptyList(), "EmptyList"); testEmptyStringCollection(Collections.emptySet(), "EmptySet"); }); it("Test empty string Collections NavigableHashSet", function() { testEmptyStringCollection(new NavigableHashSet (Collections.getStringComparator()), "NavigableHashSet"); }); it("Test empty string Collections SkipListSet", function() { testEmptyStringCollection(new SkipListSet (Collections.getStringComparator()), "SkipListSet"); }); it("Test empty string Collections TreeSet", function() { testEmptyStringCollection(new TreeSet (Collections.getStringComparator()), "TreeSet"); }); it("Test empty string Collections PriorityQueue", function() { testEmptyStringCollection(new PriorityQueue (Collections.getStringComparator()), "PriorityQueue"); }); it("Test empty string Collections HashMultiSet", function() { testEmptyStringCollection(new HashMultiSet (), "HashMultiSet"); testEmptyStringCollection(new HashMultiSet (new AllFieldHashable()), "HashMultiSet"); }); it("Test empty string Collections EvictingDeque", function() { testEmptyStringCollection(new EvictingDeque (1000, ), "EvictingDeque"); testEmptyStringCollection(new EvictingDeque (1000, new AllFieldHashable()), "EvictingDeque"); }); it("Test empty string Collections CompositeCollections", function() { testEmptyStringCollection(new CompositeCollection (undefined), "CompositeCollection undefined"); testEmptyStringCollection(new CompositeCollection (null), "CompositeCollection null"); testEmptyStringCollection(new CompositeCollection (new ArrayList()), "CompositeCollection empty"); }); it("Test empty number Collections", function() { testEmptyNumberCollection(new ArrayList ()); testEmptyNumberCollection(new ArrayList (new AllFieldCollectable())); testEmptyNumberCollection(new LinkedList ()); testEmptyNumberCollection(new LinkedList (new AllFieldCollectable())); testEmptyNumberCollection(new HashSet ()); testEmptyNumberCollection(new HashSet (new AllFieldHashable())); testEmptyNumberCollection(Collections.emptyList()); testEmptyNumberCollection(Collections.emptySet()); testEmptyNumberCollection(new NavigableHashSet (Collections.getNumberComparator())); testEmptyNumberCollection(new SkipListSet (Collections.getNumberComparator())); testEmptyNumberCollection(new TreeSet (Collections.getNumberComparator())); testEmptyNumberCollection(new PriorityQueue (Collections.getNumberComparator())); testEmptyNumberCollection(new HashMultiSet ()); testEmptyNumberCollection(new HashMultiSet (new AllFieldHashable())); testEmptyNumberCollection(new CompositeCollection (undefined)); testEmptyNumberCollection(new CompositeCollection (null)); testEmptyNumberCollection(new CompositeCollection (new ArrayList())); testEmptyNumberCollection(new EvictingDeque (1000)); testEmptyNumberCollection(new EvictingDeque (1000, new AllFieldHashable())); }); it("Test add one item to string Collections", function() { const al:ArrayList = new ArrayList (); const ll:LinkedList = new LinkedList (); const hs:HashSet = new HashSet (); const alc:ArrayList = new ArrayList (new AllFieldCollectable()); const llc:LinkedList = new LinkedList (new AllFieldCollectable()); const hsc:HashSet = new HashSet (new AllFieldHashable()); testAddOneItemToStringCollection(al, "ArrayList"); testAddOneItemToStringCollection(ll, "LinkedList"); testAddOneItemToStringCollection(hs, "HashSet"); testAddOneItemToStringCollection(alc, "ArrayList AllFieldCollectable"); testAddOneItemToStringCollection(llc, "LinkedList AllFieldCollectable"); testAddOneItemToStringCollection(hsc, "HashSet AllFieldHashable"); testAddOneItemToStringCollection(new NavigableHashSet (Collections.getStringComparator()), "NavigableHashSet StringComparator"); testAddOneItemToStringCollection(new SkipListSet (Collections.getStringComparator()), "SkipListSet StringComparator"); testAddOneItemToStringCollection(new TreeSet (Collections.getStringComparator()), "TreeSet StringComparator"); testAddOneItemToStringCollection(new PriorityQueue (Collections.getStringComparator()), "PriorityQueue StringComparator"); testAddOneItemToStringCollection(new EvictingDeque (1000), "EvictingDeque"); testAddOneItemToStringCollection(new EvictingDeque (1000, new AllFieldHashable()), "EvictingDeque"); }); it("Test add one item to string Collection Hash MultiSet", function() { testAddOneItemToStringCollection(new HashMultiSet (), "HashMultiSet"); testAddOneItemToStringCollection(new HashMultiSet (new AllFieldHashable()), "HashMultiSet AllFieldHashable"); }); it("Test add one item to number Collections", function() { const al:ArrayList = new ArrayList (); const ll:LinkedList = new LinkedList (); const hs:HashSet = new HashSet (); const alc:ArrayList = new ArrayList (new AllFieldCollectable()); const llc:LinkedList = new LinkedList (new AllFieldCollectable()); const hsc:HashSet = new HashSet (new AllFieldHashable()); testAddOneItemToNumberCollection(al); testAddOneItemToNumberCollection(ll); testAddOneItemToNumberCollection(hs); testAddOneItemToNumberCollection(alc); testAddOneItemToNumberCollection(llc); testAddOneItemToNumberCollection(hsc); testAddOneItemToNumberCollection(new NavigableHashSet (Collections.getNumberComparator())); testAddOneItemToNumberCollection(new SkipListSet (Collections.getNumberComparator())); testAddOneItemToNumberCollection(new TreeSet (Collections.getNumberComparator())); testAddOneItemToNumberCollection(new PriorityQueue (Collections.getNumberComparator())); testAddOneItemToNumberCollection(new HashMultiSet (new AllFieldHashable())); testAddOneItemToNumberCollection(new EvictingDeque (1000)); testAddOneItemToNumberCollection(new EvictingDeque (1000, new AllFieldHashable())); }); it("Test add two items to string Collections", function() { const al:ArrayList = new ArrayList (); const ll:LinkedList = new LinkedList (); const hs:HashSet = new HashSet (); const alc:ArrayList = new ArrayList (new AllFieldCollectable()); const llc:LinkedList = new LinkedList (new AllFieldCollectable()); const hsc:HashSet = new HashSet (new AllFieldHashable()); testAddTwoItemsToStringCollection(al); testAddTwoItemsToStringCollection(ll); testAddTwoItemsToStringCollection(hs); testAddTwoItemsToStringCollection(alc); testAddTwoItemsToStringCollection(llc); testAddTwoItemsToStringCollection(hsc); testAddTwoItemsToStringCollection(new NavigableHashSet (Collections.getStringComparator())); testAddTwoItemsToStringCollection(new SkipListSet (Collections.getStringComparator())); testAddTwoItemsToStringCollection(new TreeSet (Collections.getStringComparator())); testAddTwoItemsToStringCollection(new PriorityQueue (Collections.getStringComparator())); testAddTwoItemsToStringCollection(new HashMultiSet (new AllFieldHashable())); }); it("Test add two items to number Collections", function() { const al:ArrayList = new ArrayList (); const ll:LinkedList = new LinkedList (); const hs:HashSet = new HashSet (); const alc:ArrayList = new ArrayList (new AllFieldCollectable()); const llc:LinkedList = new LinkedList (new AllFieldCollectable()); const hsc:HashSet = new HashSet (new AllFieldHashable()); testAddTwoItemsToNumberCollection(al); testAddTwoItemsToNumberCollection(ll); testAddTwoItemsToNumberCollection(hs); testAddTwoItemsToNumberCollection(alc); testAddTwoItemsToNumberCollection(llc); testAddTwoItemsToNumberCollection(hsc); testAddTwoItemsToNumberCollection(new NavigableHashSet (Collections.getNumberComparator())); testAddTwoItemsToNumberCollection(new SkipListSet (Collections.getNumberComparator())); testAddTwoItemsToNumberCollection(new TreeSet (Collections.getNumberComparator())); testAddTwoItemsToNumberCollection(new PriorityQueue (Collections.getNumberComparator())); testAddTwoItemsToNumberCollection(new HashMultiSet (new AllFieldHashable())); }); it("Test add items to string Collections", function() { const al:ArrayList = new ArrayList (); const ll:LinkedList = new LinkedList (); const hs:HashSet = new HashSet (); const alc:ArrayList = new ArrayList (new AllFieldCollectable()); const llc:LinkedList = new LinkedList (new AllFieldCollectable()); const hsc:HashSet = new HashSet (new AllFieldHashable()); testAddItemsToStringCollection(al); testAddItemsToStringCollection(ll); testAddItemsToStringCollection(hs); testAddItemsToStringCollection(alc); testAddItemsToStringCollection(llc); testAddItemsToStringCollection(hsc); testAddItemsToStringCollection(new NavigableHashSet (Collections.getStringComparator())); testAddItemsToStringCollection(new SkipListSet (Collections.getStringComparator())); testAddItemsToStringCollection(new TreeSet (Collections.getStringComparator())); testAddItemsToStringCollection(new PriorityQueue (Collections.getStringComparator())); testAddItemsToStringCollection(new HashMultiSet (new AllFieldHashable())); }); it("Test add items to number Collections", function() { const al:ArrayList = new ArrayList (); const ll:LinkedList = new LinkedList (); const hs:HashSet = new HashSet (); const alc:ArrayList = new ArrayList (new AllFieldCollectable()); const llc:LinkedList = new LinkedList (new AllFieldCollectable()); const hsc:HashSet = new HashSet (new AllFieldHashable()); testAddItemsToNumberCollection(al); testAddItemsToNumberCollection(ll); testAddItemsToNumberCollection(hs); testAddItemsToNumberCollection(alc); testAddItemsToNumberCollection(llc); testAddItemsToNumberCollection(hsc); testAddItemsToNumberCollection(new NavigableHashSet (Collections.getNumberComparator())); testAddItemsToNumberCollection(new SkipListSet (Collections.getNumberComparator())); testAddItemsToNumberCollection(new TreeSet (Collections.getNumberComparator())); testAddItemsToNumberCollection(new PriorityQueue (Collections.getNumberComparator())); testAddItemsToNumberCollection(new HashMultiSet (new AllFieldHashable())); }); }); function addTestNumbers (coll:Collection) { expect (coll.add (100)).toEqual(true); expect (coll.add (200)).toEqual(true); expect (coll.add (300)).toEqual(true); expect (coll.add (400)).toEqual(true); expect (coll.add (500)).toEqual(true); expect (coll.add (600)).toEqual(true); expect (coll.add (700)).toEqual(true); expect (coll.add (800)).toEqual(true); expect (coll.add (900)).toEqual(true); expect (coll.add (1000)).toEqual(true); } function addTestStrings (coll:Collection) { expect (coll.add ("first")).toEqual(true); expect (coll.add ("second")).toEqual(true); expect (coll.add ("third")).toEqual(true); expect (coll.add ("fourth")).toEqual(true); expect (coll.add ("fifth")).toEqual(true); expect (coll.add ("sixth")).toEqual(true); expect (coll.add ("seventh")).toEqual(true); expect (coll.add ("eighth")).toEqual(true); expect (coll.add ("ninth")).toEqual(true); expect (coll.add ("tenth")).toEqual(true); } const failActionString:Consumer = { accept(element:string) { fail ('Unwanted code branch in testEmptyStringCollection'); throw new Error('Unwanted code branch in testEmptyStringCollection'); } } function testEmptyStringCollection (coll:ImmutableCollection, typestring:string) : void { TestBoolean.true ("Expect empty string collection isEmpty true " + typestring, coll.isEmpty()); expect (coll.isEmpty ()).toEqual(true); expect (coll.size ()).toEqual(0); TestBoolean.false ("Empty string collection will not contain blah " + typestring, coll.contains("blah")); for (const iter:JIterator = coll.iterator(); iter.hasNext(); ) { fail ('Unwanted code branch in testEmptyStringCollection'); throw new Error('Unwanted code branch in testEmptyStringCollection'); } const i:Iterator = coll[Symbol.iterator](); const ir:IteratorResult = i.next(); expect (ir.done).toEqual(true); coll.forEach(failActionString); TestString.equals ("Empty array should stringify to []", JSON.stringify (coll), "[]"); } const failActionNumber:Consumer = { accept(element:number) { fail ('Unwanted code branch in testEmptyNumberCollection'); throw new Error('Unwanted code branch in testEmptyNumberCollection'); } } function testEmptyNumberCollection (coll:ImmutableCollection) : void { expect (coll.isEmpty ()).toEqual(true); expect (coll.size ()).toEqual(0); expect (coll.contains(500)).toEqual(false); for (const iter:JIterator = coll.iterator(); iter.hasNext(); ) { fail ('Unwanted code branch in testEmptyNumberCollection'); throw new Error('Unwanted code branch in testEmptyNumberCollection'); } const i:Iterator = coll[Symbol.iterator](); const ir:IteratorResult = i.next(); expect (ir.done).toEqual(true); coll.forEach(failActionNumber); TestString.equals ("Empty array should stringify to []", JSON.stringify (coll), "[]"); } function testAddOneItemToStringCollection (coll:Collection, typestring:string) : void { expect (coll.isEmpty ()).toEqual(true); expect (coll.size ()).toEqual(0); coll.clear(); expect (coll.isEmpty ()).toEqual(true); expect (coll.size ()).toEqual(0); expect (coll.add ("blah")).toEqual (true); expect (coll.isEmpty ()).toEqual(false); expect (coll.size ()).toEqual(1); let testCount : number = 0; coll.forEach ({ accept(element:string) { testCount = testCount + 1; } }); expect (testCount).toEqual (1); // console.log ("About to run test for " + typestring); TestString.equals ("One element array should stringify to [blah]", JSON.stringify (coll), "[\"blah\"]"); } function testAddOneItemToNumberCollection (coll:Collection) : void { expect (coll.isEmpty ()).toEqual(true); expect (coll.size ()).toEqual(0); coll.clear(); expect (coll.isEmpty ()).toEqual(true); expect (coll.size ()).toEqual(0); expect (coll.add (100)).toEqual (true); expect (coll.isEmpty ()).toEqual(false); expect (coll.size ()).toEqual(1); let testCount : number = 0; coll.forEach ({ accept(element:number) { testCount = testCount + 1; } }); expect (testCount).toEqual (1); TestString.equals ("One element array should stringify to [100]", JSON.stringify (coll), "[100]"); } function testAddTwoItemsToStringCollection (coll:Collection) : void { expect (coll.isEmpty ()).toEqual(true); expect (coll.size ()).toEqual(0); expect (coll.add ("blah")).toEqual (true); expect (coll.isEmpty ()).toEqual(false); expect (coll.size ()).toEqual(1); expect (coll.add ("more")).toEqual (true); expect (coll.isEmpty ()).toEqual(false); expect (coll.size ()).toEqual(2); let testCount : number = 0; coll.forEach ({ accept(element:string) { testCount = testCount + 1; } }); expect (testCount).toEqual (2); coll.clear(); expect (coll.isEmpty ()).toEqual(true); expect (coll.size ()).toEqual(0); } function testAddTwoItemsToNumberCollection (coll:Collection) : void { expect (coll.isEmpty ()).toEqual(true); expect (coll.size ()).toEqual(0); expect (coll.add (100)).toEqual (true); expect (coll.isEmpty ()).toEqual(false); expect (coll.size ()).toEqual(1); expect (coll.add (200)).toEqual (true); expect (coll.isEmpty ()).toEqual(false); expect (coll.size ()).toEqual(2); let testCount : number = 0; coll.forEach ({ accept(element:number) { testCount = testCount + 1; } }); expect (testCount).toEqual (2); coll.clear(); expect (coll.isEmpty ()).toEqual(true); expect (coll.size ()).toEqual(0); } function testAddItemsToStringCollection (coll:Collection) : void { expect (coll.isEmpty ()).toEqual(true); expect (coll.size ()).toEqual(0); addTestStrings (coll); expect (coll.isEmpty ()).toEqual(false); expect (coll.size ()).toEqual(10); expect (coll.contains ("notfound")).toEqual (false); expect (coll.contains ("sixth")).toEqual (true); let testCount : number = 0; coll.forEach ({ accept(element:string) { testCount = testCount + 1; } }); expect (testCount).toEqual (10); } function testAddItemsToNumberCollection (coll:Collection) : void { expect (coll.isEmpty ()).toEqual(true); expect (coll.size ()).toEqual(0); addTestNumbers (coll); expect (coll.isEmpty ()).toEqual(false); expect (coll.size ()).toEqual(10); expect (coll.contains (31415926553)).toEqual (false); expect (coll.contains (500)).toEqual (true); let testCount : number = 0; coll.forEach ({ accept(element:number) { testCount = testCount + 1; } }); expect (testCount).toEqual (10); }