/** * @license * Copyright Larry Diamond 2018 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 {AllFieldHashable} from "../src/AllFieldHashable"; import {Collections} from "../src/Collections"; import {Comparator} from "../src/Comparator"; import {HashSet} from "../src/HashSet"; import {ImmutableSet} from "../src/ImmutableSet"; import {JSet} from "../src/JSet"; import {LinkedHashSet} from "../src/LinkedHashSet"; import {NavigableHashSet} from "../src/NavigableHash"; import {SkipListSet} from "../src/SkipList"; import {TreeSet} from "../src/TreeSet"; // PetStoreProduct will be used in testing class PetStoreProduct { private productName:string; private price:number; public constructor (iName:string, iPrice:number) { this.productName = iName; this.price = iPrice; } public getProductName ():string { return this.productName; } public getPrice():number { return this.price; } } const alphabeticalSortPetStoreProduct:Comparator = { compare(o1:PetStoreProduct, o2:PetStoreProduct) : number { if (o1 === o2) return 0; if (o1 === undefined) return -1; if (o1 === null) return -1; if (o2 === undefined) return 1; if (o2 === null) return 1; if (o1.getProductName() === o2.getProductName()) return 0; if (o1.getProductName() === undefined) return -1; if (o1.getProductName() === null) return -1; if (o2.getProductName() === undefined) return 1; if (o2.getProductName() === null) return 1; if (o1.getProductName() < o2.getProductName()) return -1; return 1; } }; const product2:PetStoreProduct = new PetStoreProduct("ChewToy", 14.99); const product1:PetStoreProduct = new PetStoreProduct("Catnip", 4.99); const product3:PetStoreProduct = new PetStoreProduct("Goldfish", 9.99); const productNotAvailable:PetStoreProduct = new PetStoreProduct("Bananas", 1.99); describe("Test generic Set functionality", function() { it("Test empty Sets", function() { testEmptyStringSet (Collections.emptySet()); testEmptyPetStoreProductAndValueClassSet (Collections.emptySet()); testEmptyStringSet (new HashSet ()); testEmptyStringSet (new HashSet (new AllFieldHashable())); testEmptyStringSet (new LinkedHashSet (new AllFieldHashable())); testEmptyPetStoreProductAndValueClassSet (new HashSet ()); testEmptyPetStoreProductAndValueClassSet (new HashSet (new AllFieldHashable())); testEmptyPetStoreProductAndValueClassSet (new LinkedHashSet ()); testEmptyPetStoreProductAndValueClassSet (new LinkedHashSet (new AllFieldHashable())); testEmptyStringSet (new TreeSet (Collections.getStringComparator())); testEmptyPetStoreProductAndValueClassSet (new TreeSet (alphabeticalSortPetStoreProduct)); testEmptyStringSet (new SkipListSet (Collections.getStringComparator())); testEmptyPetStoreProductAndValueClassSet (new SkipListSet (alphabeticalSortPetStoreProduct)); testEmptyStringSet (new NavigableHashSet (Collections.getStringComparator())); testEmptyPetStoreProductAndValueClassSet (new NavigableHashSet (alphabeticalSortPetStoreProduct)); }); it("Test adding to empty Sets", function() { testAddingOneEntryStringSet (new HashSet ()); testAddingOneEntryStringSet (new HashSet (new AllFieldHashable())); testAddingOneEntryStringSet (new LinkedHashSet (new AllFieldHashable())); testAddingOneEntryPetStoreProductAndValueClassSet (new HashSet ()); testAddingOneEntryPetStoreProductAndValueClassSet (new HashSet (new AllFieldHashable())); testAddingOneEntryPetStoreProductAndValueClassSet (new LinkedHashSet ()); testAddingOneEntryPetStoreProductAndValueClassSet (new LinkedHashSet (new AllFieldHashable())); testAddingOneEntryStringSet (new TreeSet (Collections.getStringComparator())); testAddingOneEntryPetStoreProductAndValueClassSet (new TreeSet (alphabeticalSortPetStoreProduct)); testAddingOneEntryStringSet (new SkipListSet (Collections.getStringComparator())); testAddingOneEntryPetStoreProductAndValueClassSet (new SkipListSet (alphabeticalSortPetStoreProduct)); testAddingOneEntryStringSet (new NavigableHashSet (Collections.getStringComparator())); testAddingOneEntryPetStoreProductAndValueClassSet (new NavigableHashSet (alphabeticalSortPetStoreProduct)); }); it("Test adding two items to empty Sets", function() { testAddingTwoEntriesStringSet (new HashSet ()); testAddingTwoEntriesStringSet (new HashSet (new AllFieldHashable())); testAddingTwoEntriesStringSet (new LinkedHashSet (new AllFieldHashable())); testAddingTwoEntriesPetStoreProductAndValueClassSet (new HashSet ()); testAddingTwoEntriesPetStoreProductAndValueClassSet (new HashSet (new AllFieldHashable())); testAddingTwoEntriesPetStoreProductAndValueClassSet (new LinkedHashSet ()); testAddingTwoEntriesPetStoreProductAndValueClassSet (new LinkedHashSet (new AllFieldHashable())); testAddingTwoEntriesStringSet (new TreeSet (Collections.getStringComparator())); testAddingTwoEntriesPetStoreProductAndValueClassSet (new TreeSet (alphabeticalSortPetStoreProduct)); testAddingTwoEntriesStringSet (new SkipListSet (Collections.getStringComparator())); testAddingTwoEntriesPetStoreProductAndValueClassSet (new SkipListSet (alphabeticalSortPetStoreProduct)); testAddingTwoEntriesStringSet (new NavigableHashSet (Collections.getStringComparator())); testAddingTwoEntriesPetStoreProductAndValueClassSet (new NavigableHashSet (alphabeticalSortPetStoreProduct)); }); it("Test clearing Sets", function() { testClearingStringSet (new HashSet ()); testClearingStringSet (new HashSet (new AllFieldHashable())); testClearingStringSet (new LinkedHashSet (new AllFieldHashable())); testClearingPetStoreProductAndValueClassSet (new HashSet ()); testClearingPetStoreProductAndValueClassSet (new HashSet (new AllFieldHashable())); testClearingPetStoreProductAndValueClassSet (new LinkedHashSet ()); testClearingPetStoreProductAndValueClassSet (new LinkedHashSet (new AllFieldHashable())); testClearingStringSet (new TreeSet (Collections.getStringComparator())); testClearingPetStoreProductAndValueClassSet (new TreeSet (alphabeticalSortPetStoreProduct)); testClearingStringSet (new SkipListSet (Collections.getStringComparator())); testClearingPetStoreProductAndValueClassSet (new SkipListSet (alphabeticalSortPetStoreProduct)); testClearingStringSet (new NavigableHashSet (Collections.getStringComparator())); testClearingPetStoreProductAndValueClassSet (new NavigableHashSet (alphabeticalSortPetStoreProduct)); }); it ("Test attempting to add a duplicate is ignored", function () { testDuplicatingStringSet (new HashSet ()); testDuplicatingStringSet (new HashSet (new AllFieldHashable())); testDuplicatingStringSet (new LinkedHashSet (new AllFieldHashable())); testDuplicatingPetStoreProductAndValueClassSet (new HashSet ()); testDuplicatingPetStoreProductAndValueClassSet (new HashSet (new AllFieldHashable())); testDuplicatingPetStoreProductAndValueClassSet (new LinkedHashSet ()); testDuplicatingPetStoreProductAndValueClassSet (new LinkedHashSet (new AllFieldHashable())); testDuplicatingStringSet (new TreeSet (Collections.getStringComparator())); testDuplicatingPetStoreProductAndValueClassSet (new TreeSet (alphabeticalSortPetStoreProduct)); testDuplicatingStringSet (new SkipListSet (Collections.getStringComparator())); testDuplicatingPetStoreProductAndValueClassSet (new SkipListSet (alphabeticalSortPetStoreProduct)); testDuplicatingStringSet (new NavigableHashSet (Collections.getStringComparator())); testDuplicatingPetStoreProductAndValueClassSet (new NavigableHashSet (alphabeticalSortPetStoreProduct)); }); it ("Test remove", function () { testRemoveStringSet (new HashSet ()); testRemoveStringSet (new HashSet (new AllFieldHashable())); testRemoveStringSet (new LinkedHashSet (new AllFieldHashable())); testRemovePetStoreProductAndValueClassSet (new HashSet ()); testRemovePetStoreProductAndValueClassSet (new HashSet (new AllFieldHashable())); testRemovePetStoreProductAndValueClassSet (new LinkedHashSet ()); testRemovePetStoreProductAndValueClassSet (new LinkedHashSet (new AllFieldHashable())); testRemoveStringSet (new TreeSet (Collections.getStringComparator())); testRemovePetStoreProductAndValueClassSet (new TreeSet (alphabeticalSortPetStoreProduct)); testRemoveStringSet (new SkipListSet (Collections.getStringComparator())); testRemovePetStoreProductAndValueClassSet (new SkipListSet (alphabeticalSortPetStoreProduct)); testRemoveStringSet (new NavigableHashSet (Collections.getStringComparator())); testRemovePetStoreProductAndValueClassSet (new NavigableHashSet (alphabeticalSortPetStoreProduct)); }); }); function testEmptyStringSet (Set:ImmutableSet) : void { expect (Set.isEmpty ()).toEqual(true); expect (Set.size ()).toEqual(0); } function testEmptyPetStoreProductAndValueClassSet (Set:ImmutableSet) : void { expect (Set.isEmpty ()).toEqual(true); expect (Set.size ()).toEqual(0); } function testAddingOneEntryStringSet (Set:JSet) : void { expect (Set.size ()).toEqual(0); expect (Set.isEmpty ()).toEqual(true); expect (true).toEqual(Set.add("testkey")); expect (Set.size ()).toEqual(1); expect (Set.isEmpty ()).toEqual(false); expect (true).toEqual (Set.contains ("testkey")); expect (false).toEqual (Set.contains ("key not found")); } function testAddingOneEntryPetStoreProductAndValueClassSet (Set:JSet) : void { expect (Set.size ()).toEqual(0); expect (Set.isEmpty ()).toEqual(true); expect (true).toEqual(Set.add(product1)); expect (Set.size ()).toEqual(1); expect (Set.isEmpty ()).toEqual(false); } function testAddingTwoEntriesStringSet (Set:JSet) : void { expect (Set.size ()).toEqual(0); expect (Set.isEmpty ()).toEqual(true); expect (true).toEqual(Set.add("testkey")); expect (Set.size ()).toEqual(1); expect (Set.isEmpty ()).toEqual(false); expect (true).toEqual(Set.add("secondkey")); expect (Set.size ()).toEqual(2); expect (Set.isEmpty ()).toEqual(false); expect (true).toEqual (Set.contains ("testkey")); expect (false).toEqual (Set.contains ("key not found")); } function testAddingTwoEntriesPetStoreProductAndValueClassSet (Set:JSet) : void { expect (Set.size ()).toEqual(0); expect (Set.isEmpty ()).toEqual(true); expect (true).toEqual(Set.add(product1)); expect (Set.size ()).toEqual(1); expect (Set.isEmpty ()).toEqual(false); expect (true).toEqual(Set.add(product2)); expect (Set.size ()).toEqual(2); expect (Set.isEmpty ()).toEqual(false); } function testClearingStringSet (Set:JSet) : void { expect (Set.size ()).toEqual(0); expect (Set.isEmpty ()).toEqual(true); expect (true).toEqual(Set.add("testkey")); expect (Set.size ()).toEqual(1); expect (Set.isEmpty ()).toEqual(false); expect (true).toEqual(Set.add("secondkey")); expect (Set.size ()).toEqual(2); expect (Set.isEmpty ()).toEqual(false); expect (true).toEqual (Set.contains ("testkey")); expect (false).toEqual (Set.contains ("key not found")); expect (undefined).toEqual(Set.clear()); expect (Set.size ()).toEqual(0); expect (Set.isEmpty ()).toEqual(true); expect (false).toEqual (Set.contains ("testkey")); expect (false).toEqual (Set.contains ("key not found")); } function testClearingPetStoreProductAndValueClassSet (Set:JSet) : void { expect (Set.size ()).toEqual(0); expect (Set.isEmpty ()).toEqual(true); expect (true).toEqual(Set.add(product1)); expect (Set.size ()).toEqual(1); expect (Set.isEmpty ()).toEqual(false); expect (true).toEqual(Set.add(product2)); expect (Set.size ()).toEqual(2); expect (Set.isEmpty ()).toEqual(false); expect (undefined).toEqual(Set.clear()); expect (Set.size ()).toEqual(0); expect (Set.isEmpty ()).toEqual(true); } function testDuplicatingStringSet (Set:JSet) : void { expect (Set.size ()).toEqual(0); expect (Set.isEmpty ()).toEqual(true); expect (true).toEqual(Set.add("testkey")); expect (Set.size ()).toEqual(1); expect (Set.isEmpty ()).toEqual(false); expect (false).toEqual(Set.add("testkey")); expect (Set.size ()).toEqual(1); expect (Set.isEmpty ()).toEqual(false); } function testDuplicatingPetStoreProductAndValueClassSet (Set:JSet) : void { expect (Set.size ()).toEqual(0); expect (Set.isEmpty ()).toEqual(true); expect (true).toEqual(Set.add(product1)); expect (Set.size ()).toEqual(1); expect (Set.isEmpty ()).toEqual(false); expect (false).toEqual(Set.add(product1)); expect (Set.size ()).toEqual(1); expect (Set.isEmpty ()).toEqual(false); } function testRemoveStringSet (Set:JSet) : void { expect (Set.contains ("testkey")).toEqual (false); expect (Set.size ()).toEqual(0); expect (Set.isEmpty ()).toEqual(true); expect (true).toEqual(Set.add("testkey")); expect (Set.size ()).toEqual(1); expect (Set.isEmpty ()).toEqual(false); expect (Set.contains ("testkey")).toEqual (true); expect (false).toEqual (Set.remove ("NoSuchString")); expect (Set.contains ("testkey")).toEqual (true); expect (Set.size ()).toEqual(1); expect (Set.isEmpty ()).toEqual(false); expect (true).toEqual (Set.remove ("testkey")); expect (Set.size ()).toEqual(0); expect (Set.isEmpty ()).toEqual(true); expect (Set.contains ("testkey")).toEqual (false); } function testRemovePetStoreProductAndValueClassSet (Set:JSet) : void { expect (Set.contains (product1)).toEqual (false); expect (Set.size ()).toEqual(0); expect (Set.isEmpty ()).toEqual(true); expect (true).toEqual(Set.add(product1)); expect (Set.size ()).toEqual(1); expect (Set.isEmpty ()).toEqual(false); expect (Set.contains (product1)).toEqual (true); expect (false).toEqual (Set.remove (product2)); expect (Set.size ()).toEqual(1); expect (Set.isEmpty ()).toEqual(false); expect (Set.contains (product1)).toEqual (true); expect (true).toEqual (Set.remove (product1)); expect (Set.contains (product1)).toEqual (false); expect (Set.size ()).toEqual(0); expect (Set.isEmpty ()).toEqual(true); }