Skip to content

Commit b760777

Browse files
committed
typescript tests
1 parent 326b18f commit b760777

File tree

3 files changed

+458
-9
lines changed

3 files changed

+458
-9
lines changed

test/typescript/hashset.ts

Lines changed: 260 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,264 @@
11
module MxTests {
22

3+
interface SimpleObject {
4+
name: string;
5+
val: number;
6+
}
37

8+
9+
function CreateNumericHashSet(): HashSet<number> {
10+
return new HashSet<number>(mx.range(1, 5));
11+
}
12+
13+
14+
function CreateObjectHashSet(): HashSet<SimpleObject> {
15+
16+
var _items: SimpleObject[] = [{ name: "A", val: 1 }, { name: "A", val: 2 }, { name: "B", val: 3 }, { name: "B", val: 4 }],
17+
_comparer = EqualityComparer.create<SimpleObject>(obj => mx.hash(obj.name), (a, b) => a.name === b.name);
18+
19+
return new HashSet<SimpleObject>(_items, _comparer);
20+
}
21+
22+
23+
QUnit.module("Hashset");
24+
25+
26+
QUnit.test("constructor", function (assert) {
27+
28+
assert.ok(new HashSet<number>().count() === 0, "initialize an empty HashSet!");
29+
assert.ok(new HashSet<number>(mx.range(1, 5)).count() === 5, "initialize a HashSet using specified collection!");
30+
assert.ok(new HashSet<number>(mx.EqualityComparer.defaultComparer).count() === 0, "initialize a HashSet using specified equality comparer!");
31+
assert.ok(CreateObjectHashSet().count() === 2, "initialize a HashSet using specified collection and equality comparer!");
32+
});
33+
34+
35+
QUnit.test("add", function (assert) {
36+
37+
var _hash1 = CreateNumericHashSet(),
38+
_hash2 = CreateObjectHashSet();
39+
40+
assert.ok(_hash1.add(6) === true, "add item to a HashSet of numbers!");
41+
assert.ok(_hash1.add(1) === false, "add existing item to a HashSet of numbers!");
42+
assert.ok(_hash2.add({ name: "C", val: 5 }) === true, "add item to a HashSet of objects!");
43+
assert.ok(_hash2.add({ name: "A", val: 5 }) === false, "add an existing item to a HashSet of objects!");
44+
});
45+
46+
47+
QUnit.test("clear", function (assert) {
48+
49+
var _hash = CreateNumericHashSet();
50+
51+
_hash.clear();
52+
assert.ok(_hash.count() === 0, "clear a HashSet!");
53+
});
54+
55+
56+
QUnit.test("contains", function (assert) {
57+
58+
var _hash1 = CreateNumericHashSet(),
59+
_hash2 = CreateObjectHashSet();
60+
61+
assert.ok(_hash1.contains(1) === true, "HashSet of numbers contains an item!");
62+
assert.ok(_hash1.contains(6) === false, "HashSet of numbers does not contain an item!");
63+
assert.ok(_hash2.contains({ name: "A", val: 5 }) === true, "HashSet of objects contains an item!");
64+
assert.ok(_hash2.contains({ name: "C", val: 5 }) === false, "HashSet of objects does not contain an item!");
65+
});
66+
67+
68+
QUnit.test("copyTo", function (assert) {
69+
70+
var _hash = CreateNumericHashSet(),
71+
_arr = new Array(_hash.count());
72+
73+
_hash.copyTo(_arr, 0);
74+
75+
assert.deepEqual(_arr, [1, 2, 3, 4, 5], "HashSet copy to an array!");
76+
assert.throws(() => _hash.copyTo([], 0), "throws an error when the number of elements is greater than the number of elements that the destination array can contain!");
77+
});
78+
79+
80+
QUnit.test("comparer", function (assert) {
81+
82+
var _hash1 = CreateNumericHashSet(),
83+
_hash2 = CreateObjectHashSet();
84+
85+
assert.ok(_hash1.comparer() === mx.EqualityComparer.defaultComparer, "HashSet default comparer!");
86+
assert.ok(_hash2.comparer().equals({ name: "A", val: 1 }, { name: "A", val: 2 }), "HashSet custom comparer!");
87+
});
88+
89+
90+
QUnit.test("remove", function (assert) {
91+
92+
var _hash1 = CreateNumericHashSet(),
93+
_hash2 = CreateObjectHashSet();
94+
95+
assert.ok(_hash1.remove(1) === true, "HashSet of numbers remove an item!");
96+
assert.ok(_hash1.remove(1) === false, "HashSet of numbers remove non existing item!");
97+
assert.ok(_hash2.remove({ name: "A", val: 1 }) === true, "HashSet of objects remove an item!");
98+
assert.ok(_hash2.remove({ name: "A", val: 1 }) === false, "HashSet of objects remove non existing item!");
99+
});
100+
101+
102+
QUnit.test("removeWhere", function (assert) {
103+
104+
var _hash1 = CreateNumericHashSet(),
105+
_hash2 = CreateObjectHashSet();
106+
107+
assert.ok(_hash1.removeWhere(t => t < 3) === 2, "HashSet of numbers remove with predicate, get number of items removed!");
108+
assert.ok(_hash1.removeWhere(t => t < 3) === 0, "HashSet of numbers remove with invalid predicate, get number of items removed!");
109+
assert.ok(_hash1.count() === 3, "HashSet of numbers remove with predicate, get count!");
110+
111+
assert.ok(_hash2.removeWhere(t => t.val < 3) === 1, "HashSet of objects remove with predicate, get number of items removed!");
112+
assert.ok(_hash2.removeWhere(t => t.val < 3) === 0, "HashSet of objects remove with invalid predicate, get number of items removed!");
113+
assert.ok(_hash2.count() === 1, "HashSet of objects remove with predicate, get count!");
114+
});
115+
116+
117+
QUnit.test("exceptWith", function (assert) {
118+
119+
var _hash1 = CreateNumericHashSet(),
120+
_hash2 = CreateObjectHashSet(),
121+
_hash3 = CreateNumericHashSet();
122+
123+
_hash1.exceptWith([1, 2, 3]);
124+
_hash2.exceptWith([{ name: "A", val: 0 }]);
125+
_hash3.exceptWith(CreateNumericHashSet());
126+
127+
assert.ok(_hash1.count() === 2 && _hash1.contains(1) === false, "HashSet of numbers except a collection, get count!");
128+
assert.ok(_hash2.count() === 1 && _hash2.contains({ name: "A", val: 0 }) === false, "HashSet of objects except a collection, get count!");
129+
assert.ok(_hash3.count() === 0, "HashSet of numbers except an equal set, get count!");
130+
});
131+
132+
133+
QUnit.test("intersectWith", function (assert) {
134+
135+
var _hash1 = CreateNumericHashSet(),
136+
_hash2 = CreateObjectHashSet(),
137+
_hash3 = CreateNumericHashSet();
138+
139+
_hash1.intersectWith([1, 2, 3]);
140+
_hash2.intersectWith([{ name: "A", val: 0 }]);
141+
_hash3.intersectWith(CreateNumericHashSet());
142+
143+
assert.ok(_hash1.count() === 3 && _hash1.contains(1) === true, "HashSet of numbers intersect with a collection, get count!");
144+
assert.ok(_hash2.count() === 1 && _hash2.contains({ name: "A", val: 0 }) === true, "HashSet of objects intersect with a collection, get count!");
145+
assert.ok(_hash3.count() === 5, "HashSet of numbers intersect with an equal set, get count!");
146+
});
147+
148+
149+
QUnit.test("isProperSubsetOf", function (assert) {
150+
151+
var _hash = CreateNumericHashSet();
152+
153+
assert.ok(new HashSet<number>().isProperSubsetOf([1, 2, 3]) === true, "an empty set is a proper subset of any other collection!");
154+
assert.ok(new HashSet<number>().isProperSubsetOf([]) === false, "an empty set is not a proper subset of another empty collection!");
155+
assert.ok(_hash.isProperSubsetOf([1, 2, 3, 4]) === false, "a hash set is not a proper subset of another collection when count is greater than the number of elements in other!");
156+
assert.ok(_hash.isProperSubsetOf([1, 2, 3, 4, 5]) === false, "a hash set is not a proper subset of another collection when count is equal to the number of elements in other!");
157+
assert.ok(_hash.isProperSubsetOf([1, 2, 3, 4, 5, 6]) === true, "hash set proper subset!");
158+
});
159+
160+
161+
QUnit.test("isProperSupersetOf", function (assert) {
162+
163+
var _hash = CreateNumericHashSet();
164+
165+
assert.ok(new HashSet<number>().isProperSupersetOf([1, 2, 3]) === false, "an empty set is a not superset of any other collection!");
166+
assert.ok(new HashSet<number>().isProperSupersetOf([]) === false, "an empty set is not a proper superset of another empty collection!");
167+
assert.ok(_hash.isProperSupersetOf([1, 2, 3, 4, 5, 6]) === false, "a hash set is not a proper superset of another collection when count is less than the number of elements in other!");
168+
assert.ok(_hash.isProperSupersetOf([1, 2, 3, 4, 5]) === false, "a hash set is not a proper superset of another collection when count is equal to the number of elements in other!");
169+
assert.ok(_hash.isProperSupersetOf([1, 2, 3]) === true, "hash set proper superset!");
170+
});
171+
172+
173+
QUnit.test("isSubsetOf", function (assert) {
174+
175+
var _hash = CreateNumericHashSet();
176+
177+
assert.ok(new HashSet<number>().isSubsetOf([1, 2, 3]) === true, "an empty set is a subset of any other collection!");
178+
assert.ok(new HashSet<number>().isSubsetOf([]) === true, "an empty set is a subset of another empty collection!");
179+
assert.ok(_hash.isSubsetOf([1, 2, 3, 4]) === false, "a hash set is not a subset of another collection when count is greater than the number of elements in other!");
180+
assert.ok(_hash.isSubsetOf([1, 2, 3, 4, 5]) === true, "a hash set is a proper subset of another collection when count is equal to the number of elements in other!");
181+
assert.ok(_hash.isSubsetOf([1, 2, 3, 4, 5, 6]) === true, "hash set subset!");
182+
});
183+
184+
185+
QUnit.test("isSupersetOf", function (assert) {
186+
187+
var _hash = CreateNumericHashSet();
188+
189+
assert.ok(new HashSet<number>().isSupersetOf([1, 2, 3]) === false, "an empty set is a not superset of any other collection!");
190+
assert.ok(new HashSet<number>().isSupersetOf([]) === true, "an empty set is superset of another empty collection!");
191+
assert.ok(_hash.isSupersetOf([1, 2, 3, 4, 5, 6]) === false, "a hash set is not a superset of another collection when count is less than the number of elements in other!");
192+
assert.ok(_hash.isSupersetOf([1, 2, 3, 4, 5]) === true, "a hash set is a proper superset of another collection when count is equal to the number of elements in other!");
193+
assert.ok(_hash.isSupersetOf([1, 2, 3]) === true, "hash set superset!");
194+
});
195+
196+
197+
QUnit.test("overlaps", function (assert) {
198+
199+
var _hash1 = CreateNumericHashSet(),
200+
_hash2 = CreateObjectHashSet();
201+
202+
assert.ok(_hash1.overlaps([1, 2, 3]) === true, "HashSet of numbers overlaps with another collection!");
203+
assert.ok(_hash2.overlaps([{ name: "A", val: 0 }]) === true, "HashSet of objects overlaps with another collection!");
204+
assert.ok(new HashSet().overlaps([1, 2, 3]) === false, "an empty HashSet does not overlap with another collection!");
205+
});
206+
207+
208+
QUnit.test("setEquals", function (assert) {
209+
210+
var _hash1 = CreateNumericHashSet(),
211+
_hash2 = CreateNumericHashSet();
212+
213+
assert.ok(_hash1.setEquals(_hash2) === true, "HashSet of numbers equals with another HashSet!");
214+
assert.ok(_hash1.setEquals([1, 2, 3, 4, 5]) === true, "HashSet of numbers equals with another collection!");
215+
assert.ok(new HashSet<number>().setEquals([]) === true, "an empty HashSet equals with an empty collection!");
216+
assert.ok(new HashSet<number>().setEquals([1, 2, 3]) === false, "an empty HashSet does not equals with another collection!");
217+
});
218+
219+
220+
QUnit.test("symmetricExceptWith", function (assert) {
221+
222+
var _hash1 = CreateNumericHashSet(),
223+
_hash2 = CreateObjectHashSet(),
224+
_hash3 = CreateNumericHashSet();
225+
226+
_hash1.symmetricExceptWith([2, 3, 4]);
227+
_hash2.symmetricExceptWith([{ name: "A", val: 0 }]);
228+
_hash3.exceptWith(CreateNumericHashSet());
229+
230+
assert.ok(_hash1.count() === 2, "HashSet of numbers symmetric except another collection, get count!");
231+
assert.ok(_hash1.contains(1) === true && _hash1.contains(5) === true, "HashSet of numbers symmetric except another collection, check contains!");
232+
assert.ok(_hash2.count() === 1, "HashSet of objects symmetric except another collection, get count!");
233+
assert.ok(_hash2.contains({ name: "A", val: 0 }) === false && _hash2.contains({ name: "B", val: 0 }) === true, "HashSet of objects symmetric except another collection, check contains!");
234+
assert.ok(_hash3.count() === 0, "HashSet of numbers symmetric except an equal set, get count!");
235+
});
236+
237+
238+
QUnit.test("unionWith", function (assert) {
239+
240+
var _hash1 = CreateNumericHashSet(),
241+
_hash2 = CreateObjectHashSet(),
242+
_hash3 = CreateNumericHashSet();
243+
244+
_hash1.unionWith([5, 6, 7, 8]);
245+
_hash2.unionWith([{ name: "A", val: 5 }, { name: "B", val: 6 }, { name: "C", val: 7 }, { name: "D", val: 8 }]);
246+
_hash3.unionWith(CreateNumericHashSet());
247+
248+
assert.ok(_hash1.count() === 8, "HashSet of numbers union with another collection, get count!");
249+
assert.ok(_hash1.contains(1) === true && _hash1.contains(8) === true, "HashSet of numbers union with another collection, check contains!");
250+
assert.ok(_hash2.count() === 4, "HashSet of objects union with another collection, get count!");
251+
assert.ok(_hash2.contains({ name: "A", val: 0 }) === true && _hash2.contains({ name: "D", val: 0 }) === true, "HashSet of objects union with another collection, check contains!");
252+
assert.ok(_hash3.count() === 5, "HashSet of numbers union with an equal set, get count!");
253+
});
254+
255+
256+
QUnit.test("set enumerable", function (assert) {
257+
258+
var _hash1 = CreateNumericHashSet(),
259+
_hash2 = CreateObjectHashSet();
260+
261+
assert.deepEqual(_hash1.select(t => t * 2).where(t => t > 5).toArray(), [6, 8, 10], "select-where-toArray over a HashSet of numbers!");
262+
assert.deepEqual(_hash2.select(t => t.val * 2).where(t => t > 5).toArray(), [6], "select-where-toArray over a HashSet of objects!");
263+
});
4264
}

0 commit comments

Comments
 (0)