11module MxTests {
22
33
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+ function CreateSortedList ( ) : SortedList < number , string > {
17+ var _list = new SortedList < number , string > ( ) ;
18+ _list . add ( 5 , "E" ) ;
19+ _list . add ( 3 , "C" ) ;
20+ _list . add ( 2 , "B" ) ;
21+ _list . add ( 4 , "D" ) ;
22+ _list . add ( 1 , "A" ) ;
23+
24+ return _list ;
25+ }
26+
27+
28+ QUnit . module ( "SortedList" ) ;
29+
30+
31+ QUnit . test ( "constructor" , function ( assert ) {
32+
33+ var _comparer = Comparer . create ( function ( a : number , b : number ) {
34+ return a - b ;
35+ } ) ,
36+ _dic = CreateDictionary ( ) ,
37+ _s1 = new SortedList ( ) ,
38+ _s2 = new SortedList ( 5 ) ,
39+ _s3 = new SortedList ( _dic ) ,
40+ _s4 = new SortedList ( _comparer ) ,
41+ _s5 = new SortedList ( 5 , _comparer ) ,
42+ _s6 = new SortedList ( _dic , _comparer ) ;
43+
44+
45+ assert . ok ( _s1 . count ( ) === 0 && _s1 . capacity ( ) === 0 , "initialize a SortedList!" ) ;
46+ assert . ok ( _s2 . count ( ) === 0 && _s2 . capacity ( ) === 5 , "initialize a SortedList using initial capacity!" ) ;
47+ assert . ok ( _s3 . count ( ) === 5 && _s3 . capacity ( ) === 5 , "initialize a SortedList using specified dictionary!" ) ;
48+ assert . ok ( _s4 . count ( ) === 0 && _s4 . capacity ( ) === 0 , "initialize a SortedList using specified comparer!" ) ;
49+ assert . ok ( _s5 . count ( ) === 0 && _s5 . capacity ( ) === 5 , "initialize a SortedList using using initial capacity and comparer!" ) ;
50+ assert . ok ( _s6 . count ( ) === 5 && _s6 . capacity ( ) === 5 , "initialize a SortedList using specified dictionary and comparer!" ) ;
51+ } ) ;
52+
53+
54+ QUnit . test ( "add" , function ( assert ) {
55+
56+ assert . ok ( CreateSortedList ( ) . count ( ) == 5 , "sorted-list add!" ) ;
57+ assert . throws ( function ( ) {
58+ var _list = CreateSortedList ( ) ;
59+ _list . add ( 1 , "AA" ) ;
60+ } , "throws an error adding existing key to the list!" ) ;
61+ } ) ;
62+
63+
64+ QUnit . test ( "get" , function ( assert ) {
65+
66+ var _list = CreateSortedList ( ) ;
67+
68+ assert . ok ( _list . get ( 1 ) === "A" , "sorted-list get!" ) ;
69+ assert . throws ( function ( ) {
70+ _list . get ( 10 ) ;
71+ } , "throws an error getting invalid key!" ) ;
72+ } ) ;
73+
74+
75+ QUnit . test ( "capacity" , function ( assert ) {
76+
77+ var _list = CreateSortedList ( ) ;
78+
79+ assert . ok ( _list . capacity ( ) > 0 , "get sorted-list capacity!" ) ;
80+
81+ _list . capacity ( 10 ) ;
82+ assert . ok ( _list . capacity ( ) === 10 , "set sorted-list capacity!" ) ;
83+ } ) ;
84+
85+
86+ QUnit . test ( "clear" , function ( assert ) {
87+
88+ var _list = CreateSortedList ( ) ;
89+ _list . clear ( ) ;
90+
91+ assert . ok ( _list . count ( ) === 0 && _list . capacity ( ) === 0 , "clear sorted-list!" ) ;
92+ } ) ;
93+
94+
95+ QUnit . test ( "comparer" , function ( assert ) {
96+
97+ var comparer = CreateSortedList ( ) . comparer ( ) ;
98+
99+ assert . ok ( comparer . compare ( 5 , 1 ) > 0 && comparer . compare ( 1 , 5 ) < 0 && comparer . compare ( 1 , 1 ) === 0 , "sorted-list comparer!" ) ;
100+ } ) ;
101+
102+
103+ QUnit . test ( "containsKey" , function ( assert ) {
104+
105+ var _list1 = CreateSortedList ( ) ,
106+ _list2 = new SortedList < { id : number ; name : string } , number > ( {
107+ compare : function ( a , b ) { return a . name . localeCompare ( b . name ) ; }
108+ } ) ;
109+
110+ assert . ok ( _list1 . containsKey ( 1 ) === true , "sorted-list contains key!" ) ;
111+ assert . ok ( _list1 . containsKey ( 10 ) === false , "sorted-list does not contain key!" ) ;
112+
113+
114+ _list2 . add ( { id : 2 , name : "B" } , 2 ) ;
115+ _list2 . add ( { id : 5 , name : "E" } , 5 ) ;
116+ _list2 . add ( { id : 4 , name : "D" } , 4 ) ;
117+ _list2 . add ( { id : 3 , name : "C" } , 3 ) ;
118+ _list2 . add ( { id : 1 , name : "A" } , 1 ) ;
119+
120+ assert . ok ( _list2 . containsKey ( { id : 3 , name : "C" } ) , "sorted-list contains key using specified comparer" ) ;
121+ } ) ;
122+
123+
124+ QUnit . test ( "containsValue" , function ( assert ) {
125+
126+ var _list = CreateSortedList ( ) ;
127+
128+ assert . ok ( _list . containsValue ( "A" ) === true , "sorted-list contains value!" ) ;
129+ assert . ok ( _list . containsValue ( "Z" ) === false , "sorted-list does not contain value!" ) ;
130+ } ) ;
131+
132+
133+ QUnit . test ( "keys" , function ( assert ) {
134+
135+ var _list = CreateSortedList ( ) ;
136+
137+ assert . deepEqual ( _list . keys ( ) , [ 1 , 2 , 3 , 4 , 5 ] , "sorted-list keys!" ) ;
138+ assert . deepEqual ( new SortedList ( ) . keys ( ) , [ ] , "empty sorted-list keys!" ) ;
139+ } ) ;
140+
141+
142+ QUnit . test ( "values" , function ( assert ) {
143+
144+ var _list = CreateSortedList ( ) ;
145+
146+ assert . deepEqual ( _list . values ( ) , [ "A" , "B" , "C" , "D" , "E" ] , "sorted-list values!" ) ;
147+ assert . deepEqual ( new SortedList ( ) . values ( ) , [ ] , "empty sorted-list values!" ) ;
148+ } ) ;
149+
150+
151+ QUnit . test ( "indexOfKey" , function ( assert ) {
152+
153+ var _list = CreateSortedList ( ) ;
154+
155+ assert . ok ( _list . indexOfKey ( 1 ) === 0 , "sorted-list index of key!" ) ;
156+ assert . ok ( _list . indexOfKey ( 10 ) < 0 , "sorted-list index of invalid key!" ) ;
157+ } ) ;
158+
159+
160+ QUnit . test ( "indexOfValue" , function ( assert ) {
161+
162+ var _list = CreateSortedList ( ) ;
163+
164+ assert . ok ( _list . indexOfValue ( "A" ) === 0 , "sorted-list index of value!" ) ;
165+ assert . ok ( _list . indexOfValue ( "Z" ) < 0 , "sorted-list index of invalid value!" ) ;
166+ } ) ;
167+
168+
169+ QUnit . test ( "remove" , function ( assert ) {
170+
171+ var _list = CreateSortedList ( ) ;
172+
173+ assert . ok ( _list . remove ( 1 ) === true && _list . count ( ) === 4 && _list . indexOfKey ( 1 ) < 0 , "sorted-list remove key!" ) ;
174+ assert . ok ( _list . remove ( 1 ) === false && _list . count ( ) === 4 , "sorted-list remove invalid key!" ) ;
175+ } ) ;
176+
177+
178+ QUnit . test ( "removeAt" , function ( assert ) {
179+
180+ var _list = CreateSortedList ( ) ;
181+ _list . removeAt ( 0 ) ;
182+
183+ assert . ok ( _list . count ( ) === 4 && _list . indexOfKey ( 1 ) < 0 , "sorted-list remove at index!" ) ;
184+ assert . throws ( function ( ) {
185+ _list . removeAt ( 10 ) ;
186+ } , "throws an error removing item at invalid index" ) ;
187+ } ) ;
188+
189+
190+ QUnit . test ( "set" , function ( assert ) {
191+
192+ var _list = CreateSortedList ( ) ;
193+
194+ _list . set ( 1 , "AA" ) ;
195+ assert . ok ( _list . count ( ) === 5 && _list . get ( 1 ) === "AA" , "sorted-list set exisiting key's value!" ) ;
196+
197+ _list . set ( 6 , "F" ) ;
198+ assert . ok ( _list . count ( ) === 6 && _list . get ( 6 ) === "F" , "sorted-list set new key and value!" ) ;
199+ } ) ;
200+
201+
202+ QUnit . test ( "tryGetValue" , function ( assert ) {
203+
204+ var _list = CreateSortedList ( ) ;
205+
206+ assert . ok ( function ( ) {
207+ var value : string ;
208+
209+ var res = _list . tryGetValue ( 1 , function ( val ) {
210+ value = val ;
211+ } ) ;
212+
213+ return res && value === "A" ;
214+
215+ } , "sorted-list tryGetValue, exisiting key!" ) ;
216+
217+
218+ assert . ok ( function ( ) {
219+ var value : string ;
220+
221+ var res = _list . tryGetValue ( 10 , function ( val ) {
222+ value = val ;
223+ } ) ;
224+
225+ return res === false ;
226+
227+ } , "sorted-list tryGetValue, invalid key!" ) ;
228+ } ) ;
229+
230+
231+ QUnit . test ( "sorted-list enumerable" , function ( assert ) {
232+
233+ var _list = CreateSortedList ( ) ;
234+
235+ assert . deepEqual ( _list . select ( t => t . key * 2 ) . where ( t => t > 5 ) . toArray ( ) , [ 6 , 8 , 10 ] , "select-where-toArray over a sorted-list!" ) ;
236+ assert . deepEqual ( _list . where ( t => t . key > 2 ) . select ( t => t . value ) . toArray ( ) , [ "C" , "D" , "E" ] , "where-select-toArray over a sorted-list!" ) ;
237+ } ) ;
238+
239+
240+ QUnit . test ( "evaluate sorting" , function ( assert ) {
241+
242+ var _list1 = CreateSortedList ( ) ,
243+ _list2 = new SortedList < { id : number ; name : string } , number > ( {
244+ compare : function ( a , b ) { return a . name . localeCompare ( b . name ) ; }
245+ } ) ;
246+
247+ _list1 . remove ( 5 ) ;
248+ _list1 . add ( 6 , "F" ) ;
249+ _list1 . remove ( 4 ) ;
250+ _list1 . add ( 7 , "G" ) ;
251+ _list1 . remove ( 3 ) ;
252+ _list1 . add ( 8 , "H" ) ;
253+ _list1 . remove ( 2 ) ;
254+ _list1 . add ( 9 , "I" ) ;
255+ _list1 . remove ( 1 ) ;
256+ _list1 . add ( 10 , "J" ) ;
257+
258+ assert . deepEqual ( _list1 . keys ( ) , [ 6 , 7 , 8 , 9 , 10 ] , "evaluate sorted keys after multiple add/remove" ) ;
259+ assert . deepEqual ( _list1 . values ( ) , [ "F" , "G" , "H" , "I" , "J" ] , "evaluate sorted values after multiple add/remove" ) ;
260+
261+
262+
263+ _list2 . add ( { id : 2 , name : "B" } , 2 ) ;
264+ _list2 . add ( { id : 5 , name : "E" } , 5 ) ;
265+ _list2 . add ( { id : 4 , name : "D" } , 4 ) ;
266+ _list2 . add ( { id : 3 , name : "C" } , 3 ) ;
267+ _list2 . add ( { id : 1 , name : "A" } , 1 ) ;
268+
269+ assert . deepEqual ( _list2 . keys ( ) . select ( t => t . id ) . toArray ( ) , [ 1 , 2 , 3 , 4 , 5 ] , "evaluate sorted keys after multiple add/remove using specified comparer!" ) ;
270+ } ) ;
4271}
0 commit comments