1
+ module MxTests {
2
+
3
+
4
+ function CreateDictionary ( ) : Dictionary < number , string > {
5
+ var dic = new Dictionary < number , string > ( ) ;
6
+ dic . add ( 1 , "A" ) ;
7
+ dic . add ( 2 , "B" ) ;
8
+ dic . add ( 3 , "C" ) ;
9
+ dic . add ( 4 , "D" ) ;
10
+ dic . add ( 5 , "E" ) ;
11
+
12
+ return dic ;
13
+ }
14
+
15
+
16
+ QUnit . module ( "Dictionary" ) ;
17
+
18
+
19
+ QUnit . test ( "constructor" , function ( assert ) {
20
+
21
+ var _comparer = EqualityComparer . create (
22
+ function ( o ) { return mx . hash ( o ) ; } ,
23
+ function ( a , b ) { return a === b ; }
24
+ ) ,
25
+ _d1 = new Dictionary < number , string > ( ) ,
26
+ _d2 = new Dictionary < number , string > ( CreateDictionary ( ) ) ,
27
+ _d3 = new Dictionary < number , string > ( _comparer ) ,
28
+ _d4 = new Dictionary < number , string > ( 5 ) ,
29
+ _d5 = new Dictionary < number , string > ( 5 , _comparer ) ,
30
+ _d6 = new Dictionary < number , string > ( CreateDictionary ( ) , _comparer ) ;
31
+
32
+
33
+ assert . ok ( _d1 . count ( ) === 0 , "initialize a Dictionary!" ) ;
34
+ assert . ok ( _d2 . count ( ) === 5 , "initialize a Dictionary using specified dictionary!" ) ;
35
+ assert . ok ( _d3 . count ( ) === 0 , "initialize a Dictionary using specified comparer!" ) ;
36
+ assert . ok ( _d4 . count ( ) === 0 , "initialize a Dictionary using initial capacity!" ) ;
37
+ assert . ok ( _d5 . count ( ) === 0 , "initialize a Dictionary using using initial capacity and comparer!" ) ;
38
+ assert . ok ( _d6 . count ( ) === 5 , "initialize a Dictionary using specified dictionary and comparer!" ) ;
39
+ } ) ;
40
+
41
+
42
+ QUnit . test ( "add" , function ( assert ) {
43
+ var _dic = new Dictionary < number , string > ( ) ;
44
+ _dic . add ( 1 , "A" ) ;
45
+
46
+ assert . ok ( _dic . count ( ) === 1 , "ditionary add" ) ;
47
+ assert . throws ( function ( ) {
48
+ _dic . add ( 1 , "B" ) ;
49
+ } , "throws an error adding duplicate key" ) ;
50
+ } ) ;
51
+
52
+
53
+ QUnit . test ( "clear" , function ( assert ) {
54
+ var _dic = CreateDictionary ( ) ;
55
+ _dic . clear ( ) ;
56
+
57
+ assert . ok ( _dic . count ( ) === 0 , "ditionary clear!" ) ;
58
+ } ) ;
59
+
60
+
61
+ QUnit . test ( "containsKey" , function ( assert ) {
62
+
63
+ var _dic = CreateDictionary ( ) ;
64
+
65
+ assert . ok ( _dic . containsKey ( 1 ) === true , "dictionary contains key!" ) ;
66
+ assert . ok ( _dic . containsKey ( 10 ) === false , "dictionary does not contain key!" ) ;
67
+ } ) ;
68
+
69
+
70
+ QUnit . test ( "containsValue" , function ( assert ) {
71
+
72
+ var _dic = CreateDictionary ( ) ;
73
+
74
+ assert . ok ( _dic . containsValue ( "A" ) === true , "dictionary contains value!" ) ;
75
+ assert . ok ( _dic . containsValue ( "Z" ) === false , "dictionary does not contain value!" ) ;
76
+ } ) ;
77
+
78
+
79
+ QUnit . test ( "copyTo" , function ( assert ) {
80
+
81
+ var _dic = CreateDictionary ( ) ,
82
+ _arr = new Array ( _dic . count ( ) ) ;
83
+
84
+ _dic . copyTo ( _arr , 0 ) ;
85
+
86
+ assert . deepEqual ( _arr , [ 1 , 2 , 3 , 4 , 5 ] , "dictionary copy to an array!" ) ;
87
+ assert . throws ( function ( ) {
88
+ _dic . copyTo ( [ ] , 0 ) ;
89
+ } , "throws an error when the number of elements is greater than the number of elements that the destination array can contain!" ) ;
90
+ } ) ;
91
+
92
+
93
+ QUnit . test ( "keys" , function ( assert ) {
94
+
95
+ var _dic = CreateDictionary ( ) ;
96
+
97
+ assert . deepEqual ( _dic . keys ( ) , [ 1 , 2 , 3 , 4 , 5 ] , "dictionary keys!" ) ;
98
+ assert . deepEqual ( new Dictionary ( ) . keys ( ) , [ ] , "empty dictionary keys!" ) ;
99
+ } ) ;
100
+
101
+
102
+ QUnit . test ( "values" , function ( assert ) {
103
+
104
+ var _dic = CreateDictionary ( ) ;
105
+
106
+ assert . deepEqual ( _dic . values ( ) , [ "A" , "B" , "C" , "D" , "E" ] , "dictionary values!" ) ;
107
+ assert . deepEqual ( new Dictionary ( ) . values ( ) , [ ] , "empty dictionary values!" ) ;
108
+ } ) ;
109
+
110
+
111
+ QUnit . test ( "get" , function ( assert ) {
112
+
113
+ var _dic = CreateDictionary ( ) ;
114
+
115
+ assert . ok ( _dic . get ( 1 ) === "A" , "dictionary get value!" ) ;
116
+ assert . throws ( function ( ) {
117
+ _dic . get ( 10 ) ;
118
+ } , "throws an error getting non existing key!" ) ;
119
+ } ) ;
120
+
121
+
122
+ QUnit . test ( "set" , function ( assert ) {
123
+
124
+ var _dic = CreateDictionary ( ) ;
125
+ _dic . set ( 1 , "AA" ) ;
126
+
127
+ assert . ok ( _dic . get ( 1 ) === "AA" , "dictionary set value!" ) ;
128
+
129
+ _dic . set ( 6 , "F" ) ;
130
+ assert . ok ( _dic . count ( ) === 6 && _dic . get ( 6 ) === "F" , "dictionary set new key and value!" ) ;
131
+ } ) ;
132
+
133
+
134
+ QUnit . test ( "tryGetValue" , function ( assert ) {
135
+
136
+ var _dic = CreateDictionary ( ) ;
137
+
138
+ assert . ok ( function ( ) {
139
+ var value : string ;
140
+
141
+ var res = _dic . tryGetValue ( 1 , function ( val ) {
142
+ value = val ;
143
+ } ) ;
144
+
145
+ return res && value === "A" ;
146
+
147
+ } , "dictionary tryGetValue, exisiting key!" ) ;
148
+
149
+
150
+ assert . ok ( function ( ) {
151
+ var value : string ;
152
+
153
+ var res = _dic . tryGetValue ( 10 , function ( val ) {
154
+ value = val ;
155
+ } ) ;
156
+
157
+ return res === false ;
158
+
159
+ } , "dictionary tryGetValue, invalid key!" ) ;
160
+ } ) ;
161
+
162
+
163
+ QUnit . test ( "remove" , function ( assert ) {
164
+
165
+ var _dic = CreateDictionary ( ) ;
166
+
167
+ assert . ok ( _dic . remove ( 1 ) === true && _dic . count ( ) === 4 , "dictionary remove key!" ) ;
168
+ assert . ok ( _dic . remove ( 10 ) === false && _dic . count ( ) === 4 , "dictionary remove non existing key!" ) ;
169
+ } ) ;
170
+
171
+
172
+ QUnit . test ( "key-value pair" , function ( assert ) {
173
+
174
+ var _pair1 = new KeyValuePair ( 1 , "A" ) ,
175
+ _pair2 = new KeyValuePair ( 1 , "A" ) ;
176
+
177
+ assert . ok ( _pair1 . key === 1 && _pair1 . value === "A" , "KeyValuePair get key/value!" ) ;
178
+
179
+ _pair1 . key = 2 ;
180
+ _pair1 . value = "B" ;
181
+ assert . ok ( _pair1 . key === 1 && _pair1 . value === "A" , "KeyValuePair key/value immutable!" ) ;
182
+
183
+ assert . ok ( mx . hash ( _pair1 ) === mx . hash ( _pair2 ) , "KeyValuePair get hash code!" ) ;
184
+ assert . ok ( mx . equals ( _pair1 , _pair2 ) , "KeyValuePair equality check!" ) ;
185
+ } ) ;
186
+
187
+
188
+ QUnit . test ( "dictionary enumerable" , function ( assert ) {
189
+
190
+ var _dic = CreateDictionary ( ) ;
191
+
192
+ assert . deepEqual ( _dic . select ( t => t . key ) . toArray ( ) , [ 1 , 2 , 3 , 4 , 5 ] , "dictionary select keys, to array!" ) ;
193
+ assert . deepEqual ( _dic . select ( t => t . value ) . toArray ( ) , [ "A" , "B" , "C" , "D" , "E" ] , "dictionary select values, to array!" ) ;
194
+ assert . ok ( _dic . toArray ( ) . first ( ) . key === 1 && _dic . toArray ( ) . first ( ) . value === "A" , "dictionary select key-value items!" ) ;
195
+ } ) ;
196
+ }
0 commit comments