1
1
module MxTests {
2
+ "use strict" ;
2
3
3
4
import HashSet = mx . HashSet ;
4
- import EqualityComparer = mx . EqualityComparer ;
5
5
6
+ var HashSet = mx . HashSet ,
7
+ EqualityComparer = mx . EqualityComparer ;
6
8
7
9
8
10
/* Factory methods
9
11
---------------------------------------------------------------------- */
10
12
11
13
interface SimpleObject {
12
14
name : string ;
13
- val : number ;
15
+ value : number ;
14
16
}
15
17
16
18
21
23
22
24
function CreateObjectHashSet ( ) : HashSet < SimpleObject > {
23
25
24
- var _items : SimpleObject [ ] = [ { name : "A" , val : 1 } , { name : "A" , val : 2 } , { name : "B" , val : 3 } , { name : "B" , val : 4 } ] ,
26
+ var _items : SimpleObject [ ] = [ { name : "A" , value : 1 } , { name : "A" , value : 2 } , { name : "B" , value : 3 } , { name : "B" , value : 4 } ] ,
25
27
_comparer = EqualityComparer . create < SimpleObject > ( obj => mx . hash ( obj . name ) , ( a , b ) => a . name === b . name ) ;
26
28
27
29
return new HashSet < SimpleObject > ( _items , _comparer ) ;
28
30
}
29
-
31
+
30
32
31
33
32
34
/* Tests
51
53
52
54
assert . ok ( _hash1 . add ( 6 ) === true , "add item to a HashSet of numbers!" ) ;
53
55
assert . ok ( _hash1 . add ( 1 ) === false , "add existing item to a HashSet of numbers!" ) ;
54
- assert . ok ( _hash2 . add ( { name : "C" , val : 5 } ) === true , "add item to a HashSet of objects!" ) ;
55
- assert . ok ( _hash2 . add ( { name : "A" , val : 5 } ) === false , "add an existing item to a HashSet of objects!" ) ;
56
+ assert . ok ( _hash2 . add ( { name : "C" , value : 5 } ) === true , "add item to a HashSet of objects!" ) ;
57
+ assert . ok ( _hash2 . add ( { name : "A" , value : 5 } ) === false , "add an existing item to a HashSet of objects!" ) ;
56
58
} ) ;
57
59
58
60
72
74
73
75
assert . ok ( _hash1 . contains ( 1 ) === true , "HashSet of numbers contains an item!" ) ;
74
76
assert . ok ( _hash1 . contains ( 6 ) === false , "HashSet of numbers does not contain an item!" ) ;
75
- assert . ok ( _hash2 . contains ( { name : "A" , val : 5 } ) === true , "HashSet of objects contains an item!" ) ;
76
- assert . ok ( _hash2 . contains ( { name : "C" , val : 5 } ) === false , "HashSet of objects does not contain an item!" ) ;
77
+ assert . ok ( _hash2 . contains ( { name : "A" , value : 5 } ) === true , "HashSet of objects contains an item!" ) ;
78
+ assert . ok ( _hash2 . contains ( { name : "C" , value : 5 } ) === false , "HashSet of objects does not contain an item!" ) ;
77
79
} ) ;
78
80
79
81
95
97
_hash2 = CreateObjectHashSet ( ) ;
96
98
97
99
assert . ok ( _hash1 . comparer ( ) === mx . EqualityComparer . defaultComparer , "HashSet default comparer!" ) ;
98
- assert . ok ( _hash2 . comparer ( ) . equals ( { name : "A" , val : 1 } , { name : "A" , val : 2 } ) , "HashSet custom comparer!" ) ;
100
+ assert . ok ( _hash2 . comparer ( ) . equals ( { name : "A" , value : 1 } , { name : "A" , value : 2 } ) , "HashSet custom comparer!" ) ;
99
101
} ) ;
100
102
101
103
106
108
107
109
assert . ok ( _hash1 . remove ( 1 ) === true , "HashSet of numbers remove an item!" ) ;
108
110
assert . ok ( _hash1 . remove ( 1 ) === false , "HashSet of numbers remove non existing item!" ) ;
109
- assert . ok ( _hash2 . remove ( { name : "A" , val : 1 } ) === true , "HashSet of objects remove an item!" ) ;
110
- assert . ok ( _hash2 . remove ( { name : "A" , val : 1 } ) === false , "HashSet of objects remove non existing item!" ) ;
111
+ assert . ok ( _hash2 . remove ( { name : "A" , value : 1 } ) === true , "HashSet of objects remove an item!" ) ;
112
+ assert . ok ( _hash2 . remove ( { name : "A" , value : 1 } ) === false , "HashSet of objects remove non existing item!" ) ;
111
113
} ) ;
112
114
113
115
120
122
assert . ok ( _hash1 . removeWhere ( t => t < 3 ) === 0 , "HashSet of numbers remove with invalid predicate, get number of items removed!" ) ;
121
123
assert . ok ( _hash1 . count ( ) === 3 , "HashSet of numbers remove with predicate, get count!" ) ;
122
124
123
- assert . ok ( _hash2 . removeWhere ( t => t . val < 3 ) === 1 , "HashSet of objects remove with predicate, get number of items removed!" ) ;
124
- assert . ok ( _hash2 . removeWhere ( t => t . val < 3 ) === 0 , "HashSet of objects remove with invalid predicate, get number of items removed!" ) ;
125
+ assert . ok ( _hash2 . removeWhere ( t => t . value < 3 ) === 1 , "HashSet of objects remove with predicate, get number of items removed!" ) ;
126
+ assert . ok ( _hash2 . removeWhere ( t => t . value < 3 ) === 0 , "HashSet of objects remove with invalid predicate, get number of items removed!" ) ;
125
127
assert . ok ( _hash2 . count ( ) === 1 , "HashSet of objects remove with predicate, get count!" ) ;
126
128
} ) ;
127
129
133
135
_hash3 = CreateNumericHashSet ( ) ;
134
136
135
137
_hash1 . exceptWith ( [ 1 , 2 , 3 ] ) ;
136
- _hash2 . exceptWith ( [ { name : "A" , val : 0 } ] ) ;
138
+ _hash2 . exceptWith ( [ { name : "A" , value : 0 } ] ) ;
137
139
_hash3 . exceptWith ( CreateNumericHashSet ( ) ) ;
138
140
139
141
assert . ok ( _hash1 . count ( ) === 2 && _hash1 . contains ( 1 ) === false , "HashSet of numbers except a collection, get count!" ) ;
140
- assert . ok ( _hash2 . count ( ) === 1 && _hash2 . contains ( { name : "A" , val : 0 } ) === false , "HashSet of objects except a collection, get count!" ) ;
142
+ assert . ok ( _hash2 . count ( ) === 1 && _hash2 . contains ( { name : "A" , value : 0 } ) === false , "HashSet of objects except a collection, get count!" ) ;
141
143
assert . ok ( _hash3 . count ( ) === 0 , "HashSet of numbers except an equal set, get count!" ) ;
142
144
} ) ;
143
145
149
151
_hash3 = CreateNumericHashSet ( ) ;
150
152
151
153
_hash1 . intersectWith ( [ 1 , 2 , 3 ] ) ;
152
- _hash2 . intersectWith ( [ { name : "A" , val : 0 } ] ) ;
154
+ _hash2 . intersectWith ( [ { name : "A" , value : 0 } ] ) ;
153
155
_hash3 . intersectWith ( CreateNumericHashSet ( ) ) ;
154
156
155
157
assert . ok ( _hash1 . count ( ) === 3 && _hash1 . contains ( 1 ) === true , "HashSet of numbers intersect with a collection, get count!" ) ;
156
- assert . ok ( _hash2 . count ( ) === 1 && _hash2 . contains ( { name : "A" , val : 0 } ) === true , "HashSet of objects intersect with a collection, get count!" ) ;
158
+ assert . ok ( _hash2 . count ( ) === 1 && _hash2 . contains ( { name : "A" , value : 0 } ) === true , "HashSet of objects intersect with a collection, get count!" ) ;
157
159
assert . ok ( _hash3 . count ( ) === 5 , "HashSet of numbers intersect with an equal set, get count!" ) ;
158
160
} ) ;
159
161
212
214
_hash2 = CreateObjectHashSet ( ) ;
213
215
214
216
assert . ok ( _hash1 . overlaps ( [ 1 , 2 , 3 ] ) === true , "HashSet of numbers overlaps with another collection!" ) ;
215
- assert . ok ( _hash2 . overlaps ( [ { name : "A" , val : 0 } ] ) === true , "HashSet of objects overlaps with another collection!" ) ;
217
+ assert . ok ( _hash2 . overlaps ( [ { name : "A" , value : 0 } ] ) === true , "HashSet of objects overlaps with another collection!" ) ;
216
218
assert . ok ( new HashSet ( ) . overlaps ( [ 1 , 2 , 3 ] ) === false , "an empty HashSet does not overlap with another collection!" ) ;
217
219
} ) ;
218
220
236
238
_hash3 = CreateNumericHashSet ( ) ;
237
239
238
240
_hash1 . symmetricExceptWith ( [ 2 , 3 , 4 ] ) ;
239
- _hash2 . symmetricExceptWith ( [ { name : "A" , val : 0 } ] ) ;
241
+ _hash2 . symmetricExceptWith ( [ { name : "A" , value : 0 } ] ) ;
240
242
_hash3 . exceptWith ( CreateNumericHashSet ( ) ) ;
241
243
242
244
assert . ok ( _hash1 . count ( ) === 2 , "HashSet of numbers symmetric except another collection, get count!" ) ;
243
245
assert . ok ( _hash1 . contains ( 1 ) === true && _hash1 . contains ( 5 ) === true , "HashSet of numbers symmetric except another collection, check contains!" ) ;
244
246
assert . ok ( _hash2 . count ( ) === 1 , "HashSet of objects symmetric except another collection, get count!" ) ;
245
- 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!" ) ;
247
+ assert . ok ( _hash2 . contains ( { name : "A" , value : 0 } ) === false && _hash2 . contains ( { name : "B" , value : 0 } ) === true , "HashSet of objects symmetric except another collection, check contains!" ) ;
246
248
assert . ok ( _hash3 . count ( ) === 0 , "HashSet of numbers symmetric except an equal set, get count!" ) ;
247
249
} ) ;
248
250
254
256
_hash3 = CreateNumericHashSet ( ) ;
255
257
256
258
_hash1 . unionWith ( [ 5 , 6 , 7 , 8 ] ) ;
257
- _hash2 . unionWith ( [ { name : "A" , val : 5 } , { name : "B" , val : 6 } , { name : "C" , val : 7 } , { name : "D" , val : 8 } ] ) ;
259
+ _hash2 . unionWith ( [ { name : "A" , value : 5 } , { name : "B" , value : 6 } , { name : "C" , value : 7 } , { name : "D" , value : 8 } ] ) ;
258
260
_hash3 . unionWith ( CreateNumericHashSet ( ) ) ;
259
261
260
262
assert . ok ( _hash1 . count ( ) === 8 , "HashSet of numbers union with another collection, get count!" ) ;
261
263
assert . ok ( _hash1 . contains ( 1 ) === true && _hash1 . contains ( 8 ) === true , "HashSet of numbers union with another collection, check contains!" ) ;
262
264
assert . ok ( _hash2 . count ( ) === 4 , "HashSet of objects union with another collection, get count!" ) ;
263
- 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!" ) ;
265
+ assert . ok ( _hash2 . contains ( { name : "A" , value : 0 } ) === true && _hash2 . contains ( { name : "D" , value : 0 } ) === true , "HashSet of objects union with another collection, check contains!" ) ;
264
266
assert . ok ( _hash3 . count ( ) === 5 , "HashSet of numbers union with an equal set, get count!" ) ;
265
267
} ) ;
266
268
271
273
_hash2 = CreateObjectHashSet ( ) ;
272
274
273
275
assert . deepEqual ( _hash1 . select ( t => t * 2 ) . where ( t => t > 5 ) . toArray ( ) , [ 6 , 8 , 10 ] , "select-where-toArray over a HashSet of numbers!" ) ;
274
- assert . deepEqual ( _hash2 . select ( t => t . val * 2 ) . where ( t => t > 5 ) . toArray ( ) , [ 6 ] , "select-where-toArray over a HashSet of objects!" ) ;
276
+ assert . deepEqual ( _hash2 . select ( t => t . value * 2 ) . where ( t => t > 5 ) . toArray ( ) , [ 6 ] , "select-where-toArray over a HashSet of objects!" ) ;
275
277
} ) ;
276
278
}
0 commit comments