Skip to content

Commit 51b6da7

Browse files
committed
Testing MultiHashSet
1 parent 243e817 commit 51b6da7

11 files changed

+301
-101
lines changed

definitions/src/Collectable.d.ts

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,35 @@
11
/**
22
* @license
3-
* Copyright Larry Diamond 2017 All Rights Reserved.
3+
* Copyright Larry Diamond 2018 All Rights Reserved.
44
*
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://github.com/larrydiamond/typescriptcollectionsframework/LICENSE
77
*/
8+
/**
9+
* The Collectable interface provides a mechanism to compare objects to see if they are equal.
10+
* This is a replacement for the equals method in Java
11+
*/
812
export interface Collectable<T> {
913
/**
1014
* Compares its two arguments for equality
15+
*
1116
* The equals method implements an equivalence relation on non-null object references:
12-
* It is reflexive: for any non-null reference value x, x.equals(x) should return true.
13-
* It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
14-
* It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
15-
* It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
17+
*
18+
* It is reflexive: for any reference value x, x.equals(x) should return true.
19+
*
20+
* It is symmetric: for any reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
21+
*
22+
* It is transitive: for any reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
23+
*
24+
* It is consistent: for any reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
25+
*
1626
* For any non-null reference value x, x.equals(null) should return false.
17-
* The equals method implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).
27+
*
28+
* For any non-undefined reference value x, x.equals(undefined) should return false.
29+
*
30+
* The equals method implements the most discriminating possible equivalence relation on objects;
31+
* that is, for any non-null and non-undefined reference values x and y,
32+
* this method returns true if and only if x and y refer to the same object (x == y has the value true).
1833
* @param {T} t element to compare
1934
* @return {boolean} true if the other element is "equal" to this one
2035
*/

definitions/src/HashClasses.d.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,10 @@ export declare class HashMultiSet<K> implements MultiSet<K> {
8282
add(element: K): boolean;
8383
/**
8484
* Removes a single occurrence of the specified element from this MultiSet, if present.
85-
* @param {K} element element to be removed from this MultiSet
85+
*
86+
* The element removed will be equal to the element as per the Hashable used in this MultiSet
87+
* and will not necessarily be the element passed in.
88+
* @param {K} element element equal to this element to be removed from this MultiSet
8689
* @return {boolean} true if the set contained the specified element
8790
*/
8891
remove(element: K): boolean;

definitions/src/JSet.d.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,37 @@
11
/**
22
* @license
3-
* Copyright Larry Diamond 2017 All Rights Reserved.
3+
* Copyright Larry Diamond 2018 All Rights Reserved.
44
*
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://github.com/larrydiamond/typescriptcollectionsframework/LICENSE
77
*/
88
import { Collection } from "./Collection";
99
import { ImmutableSet } from "./ImmutableSet";
10+
/**
11+
* A collection that contains no duplicate elements.
12+
*
13+
* More formally, sets contain no pair of elements e1 and e2 such that e1.equals(e2),
14+
* and at most one null element and at most one undefined element.
15+
*
16+
* As implied by its name, this interface models the mathematical set abstraction.
17+
*
18+
* The Set interface places additional stipulations, beyond those inherited from the Collection interface,
19+
* on the contracts of all constructors and on the contracts of the add, equals and hashCode methods.
20+
*
21+
* Declarations for other inherited methods are also included here for convenience.
22+
* (The specifications accompanying these declarations have been tailored to the Set interface,
23+
* but they do not contain any additional stipulations.)
24+
*
25+
* The additional stipulation on constructors is, not surprisingly,
26+
* that all constructors must create a set that contains no duplicate elements (as defined above).
27+
*
28+
* Note: Great care must be exercised if mutable objects are used as set elements.
29+
*
30+
* The behavior of a set is not specified if the value of an object is changed in a manner
31+
* that affects equals comparisons while the object is an element in the set.
32+
*
33+
* A special case of this prohibition is that it is not permissible for a set to contain itself as an element.
34+
*/
1035
export interface JSet<K> extends ImmutableSet<K>, Collection<K> {
1136
/**
1237
* Adds the specified element to this set if it is not already present.

dist/spec/TestMultiSet.spec.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,17 @@ describe("Test generic MultiSet functionality", function () {
121121
expect(tmp.count(productNotAvailable)).toEqual(0);
122122
expect(tmp.count(product2)).toEqual(4);
123123
});
124+
it("Test remove string", function () {
125+
testRemoveString(new HashClasses_1.HashMultiSet());
126+
testRemoveString(new HashClasses_1.HashMultiSet(new AllFieldHashable_1.AllFieldHashable()));
127+
});
128+
it("Test remove number", function () {
129+
testRemoveNumber(new HashClasses_1.HashMultiSet());
130+
testRemoveNumber(new HashClasses_1.HashMultiSet(new AllFieldHashable_1.AllFieldHashable()));
131+
});
132+
it("Test remove number", function () {
133+
testRemovePetStoreProductAndValueClass(new HashClasses_1.HashMultiSet(petStoreProductHashable));
134+
});
124135
});
125136
function testEmptyStringMultiSet(tmp) {
126137
expect(tmp.isEmpty()).toEqual(true);
@@ -357,3 +368,58 @@ function addPetStoreProducts(tmp) {
357368
tmp.add(priceDuplicate2);
358369
tmp.add(priceDuplicate3);
359370
}
371+
function testRemoveString(mset) {
372+
expect(mset.contains("testkey")).toEqual(false);
373+
expect(mset.size()).toEqual(0);
374+
expect(mset.isEmpty()).toEqual(true);
375+
expect(true).toEqual(mset.add("testkey"));
376+
expect(mset.size()).toEqual(1);
377+
expect(mset.isEmpty()).toEqual(false);
378+
expect(mset.contains("testkey")).toEqual(true);
379+
expect(mset.contains("NoSuchString")).toEqual(false);
380+
expect(false).toEqual(mset.remove("NoSuchString"));
381+
expect(mset.contains("NoSuchString")).toEqual(false);
382+
expect(mset.contains("testkey")).toEqual(true);
383+
expect(mset.size()).toEqual(1);
384+
expect(mset.isEmpty()).toEqual(false);
385+
expect(true).toEqual(mset.remove("testkey"));
386+
expect(mset.size()).toEqual(0);
387+
expect(mset.isEmpty()).toEqual(true);
388+
expect(mset.contains("testkey")).toEqual(false);
389+
}
390+
function testRemoveNumber(mset) {
391+
expect(mset.contains(1000)).toEqual(false);
392+
expect(mset.size()).toEqual(0);
393+
expect(mset.isEmpty()).toEqual(true);
394+
expect(true).toEqual(mset.add(1000));
395+
expect(mset.size()).toEqual(1);
396+
expect(mset.isEmpty()).toEqual(false);
397+
expect(mset.contains(1000)).toEqual(true);
398+
expect(mset.contains(2000)).toEqual(false);
399+
expect(false).toEqual(mset.remove(2000));
400+
expect(mset.contains(2000)).toEqual(false);
401+
expect(mset.contains(1000)).toEqual(true);
402+
expect(mset.size()).toEqual(1);
403+
expect(mset.isEmpty()).toEqual(false);
404+
expect(true).toEqual(mset.remove(1000));
405+
expect(mset.size()).toEqual(0);
406+
expect(mset.isEmpty()).toEqual(true);
407+
expect(mset.contains(1000)).toEqual(false);
408+
}
409+
function testRemovePetStoreProductAndValueClass(mset) {
410+
expect(mset.contains(product1)).toEqual(false);
411+
expect(mset.size()).toEqual(0);
412+
expect(mset.isEmpty()).toEqual(true);
413+
expect(true).toEqual(mset.add(product1));
414+
expect(mset.size()).toEqual(1);
415+
expect(mset.isEmpty()).toEqual(false);
416+
expect(mset.contains(product1)).toEqual(true);
417+
expect(false).toEqual(mset.remove(product2));
418+
expect(mset.size()).toEqual(1);
419+
expect(mset.isEmpty()).toEqual(false);
420+
expect(mset.contains(product1)).toEqual(true);
421+
expect(true).toEqual(mset.remove(product1));
422+
expect(mset.contains(product1)).toEqual(false);
423+
expect(mset.size()).toEqual(0);
424+
expect(mset.isEmpty()).toEqual(true);
425+
}

0 commit comments

Comments
 (0)