1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
|
<?php
require_once dirname(__FILE__).'/../phpunit.php';
class PriorityListItem {
public $data='data';
}
Prado::using('System.Collections.TPriorityList');
/**
* All Test cases for the TList are here. The TPriorityList should act just like a TList when used exactly like a TList
*
* The TPriority List should start behaving differently when using the class outside of the standard TList Function calls
*
* @package System.Collections
*/
class TPriorityListTest extends PHPUnit_Framework_TestCase {
protected $list;
protected $item1, $item2, $item3, $item4;
protected $plist;
protected $pfirst, $pitem1, $pitem2, $pitem3, $pitem4, $pitem5;
public function setUp() {
$this->list=new TPriorityList;
$this->item1=new PriorityListItem;
$this->item2=new PriorityListItem;
$this->item3=new PriorityListItem;
$this->item4=new PriorityListItem;
$this->list->add($this->item1);
$this->list->add($this->item2);
// **** start the setup for non-TList things
$this->plist=new TPriorityList;
$this->pfirst=new PriorityListItem;
$this->pitem1=new PriorityListItem;
$this->pitem2=new PriorityListItem;
$this->pitem3=new PriorityListItem;
$this->pitem4=new PriorityListItem;
$this->pitem5=new PriorityListItem;
$this->plist->add($this->pitem1);
$this->plist->add($this->pitem2);
$this->plist->insertAtPriority($this->pfirst, -10000000);
$this->plist->insertAtPriority($this->pitem3, 100);
// 4 and 5 are not inserted
// ending setup: pfirst @ -10000000[0], pitem1 @ 10[0], pitem2 @ 10[1], pitem3 @ 100[0]
}
public function tearDown() {
$this->list=null;
$this->item1=null;
$this->item2=null;
$this->item3=null;
$this->item4=null;
// **** start the setup for non-TList things
$this->list=null;
$this->item1=null;
$this->item2=null;
$this->item3=null;
$this->item4=null;
$this->item5=null;
}
//*****************************************************************
//******* start test cases for TList operations
//******* TPriorityList should act exactly like a TList if no special functions are used
public function testConstructTList() {
$a=array(1,2,3);
$list=new TPriorityList($a);
$this->assertEquals(3,$list->getCount());
$list2=new TPriorityList($this->list);
$this->assertEquals(2,$list2->getCount());
}
public function testGetReadOnlyTList() {
$list = new TPriorityList(null, true);
self::assertEquals(true, $list->getReadOnly(), 'List is not read-only');
$list = new TPriorityList(null, false);
self::assertEquals(false, $list->getReadOnly(), 'List is read-only');
}
public function testGetCountTList() {
$this->assertEquals(2,$this->list->getCount());
$this->assertEquals(2,$this->list->Count);
}
public function testAddTList() {
$this->assertEquals(2,$this->list->add(null));
$this->assertEquals(3,$this->list->add($this->item3));
$this->assertEquals(4,$this->list->getCount());
$this->assertEquals(3,$this->list->indexOf($this->item3));
}
public function testInsertAtTList() {
$this->assertNull($this->list->insertAt(0,$this->item3));
$this->assertEquals(3,$this->list->getCount());
$this->assertEquals(2,$this->list->indexOf($this->item2));
$this->assertEquals(0,$this->list->indexOf($this->item3));
$this->assertEquals(1,$this->list->indexOf($this->item1));
try {
$this->list->insertAt(4,$this->item3);
$this->fail('exception not raised when adding item at an out-of-range index');
} catch(TInvalidDataValueException $e) {
}
}
public function testInsertBeforeTList() {
try {
$this->list->insertBefore($this->item4,$this->item3);
$this->fail('exception not raised when adding item before a non-existant base item');
} catch(TInvalidDataValueException $e) {
}
$this->assertEquals(2,$this->list->getCount());
$this->assertEquals(0,$this->list->insertBefore($this->item1,$this->item3));
$this->assertEquals(3,$this->list->getCount());
$this->assertEquals(0,$this->list->indexOf($this->item3));
$this->assertEquals(1,$this->list->indexOf($this->item1));
$this->assertEquals(2,$this->list->indexOf($this->item2));
}
public function testInsertAfterTList() {
try {
$this->list->insertAfter($this->item4,$this->item3);
$this->fail('exception not raised when adding item after a non-existant base item');
} catch(TInvalidDataValueException $e) {
}
$this->assertEquals(2,$this->list->getCount());
$this->assertEquals(2,$this->list->insertAfter($this->item2,$this->item3));
$this->assertEquals(3,$this->list->getCount());
$this->assertEquals(0,$this->list->indexOf($this->item1));
$this->assertEquals(1,$this->list->indexOf($this->item2));
$this->assertEquals(2,$this->list->indexOf($this->item3));
}
public function testCanNotInsertWhenReadOnlyTList() {
$list = new TPriorityList(array(), true);
try {
$list->insertAt(1, 2);
self::fail('An expected TInvalidOperationException was not raised');
} catch(TInvalidOperationException $e) {
}
try {
$list->insertAt(0, 2);
self::fail('An expected TInvalidOperationException was not raised');
} catch(TInvalidOperationException $e) {
}
}
public function testRemoveTList() {
$this->assertEquals(0,$this->list->remove($this->item1));
$this->assertEquals(1,$this->list->getCount());
$this->assertEquals(-1,$this->list->indexOf($this->item1));
$this->assertEquals(0,$this->list->indexOf($this->item2));
try {
$this->list->remove($this->item1);
$this->fail('exception not raised when removing nonexisting item');
} catch(Exception $e) {
}
}
public function testRemoveAtTList() {
$this->list->add($this->item3);
$this->assertEquals($this->item2, $this->list->removeAt(1));
$this->assertEquals(-1,$this->list->indexOf($this->item2));
$this->assertEquals(1,$this->list->indexOf($this->item3));
$this->assertEquals(0,$this->list->indexOf($this->item1));
try {
$this->list->removeAt(2);
$this->fail('exception not raised when removing item with invalid index');
} catch(TInvalidDataValueException $e) {
}
}
public function testCanNotRemoveWhenReadOnlyTList() {
$list = new TPriorityList(array(1, 2, 3), true);
try {
$list->removeAt(2);
} catch(TInvalidOperationException $e) {
return;
}
self::fail('An expected TInvalidOperationException was not raised');
}
public function testClearTList() {
$this->list->clear();
$this->assertEquals(0,$this->list->getCount());
$this->assertEquals(-1,$this->list->indexOf($this->item1));
$this->assertEquals(-1,$this->list->indexOf($this->item2));
}
public function testContainTLists() {
$this->assertTrue($this->list->contains($this->item1));
$this->assertTrue($this->list->contains($this->item2));
$this->assertFalse($this->list->contains($this->item3));
}
public function testIndexOfTList() {
$this->assertEquals(0,$this->list->indexOf($this->item1));
$this->assertEquals(1,$this->list->indexOf($this->item2));
$this->assertEquals(-1,$this->list->indexOf($this->item3));
}
public function testCopyFromTList() {
$array=array($this->item3,$this->item1);
$this->list->copyFrom($array);
$this->assertTrue(count($array)==2 && $this->list[0]===$this->item3 && $this->list[1]===$this->item1);
try {
$this->list->copyFrom($this);
$this->fail('exception not raised when copying from non-traversable object');
} catch(TInvalidDataTypeException $e) {
}
}
public function testMergeWithTList() {
$array=array($this->item3,$this->item1);
$this->list->mergeWith($array);
$this->assertTrue($this->list->getCount()==4 && $this->list[0]===$this->item1 && $this->list[3]===$this->item1);
try {
$this->list->mergeWith($this);
$this->fail('exception not raised when copying from non-traversable object');
} catch(TInvalidDataTypeException $e) {
}
}
public function testToArrayTList() {
$array=$this->list->toArray();
$this->assertTrue(count($array)==2 && $array[0]===$this->item1 && $array[1]===$this->item2);
}
public function testArrayReadTList() {
$this->assertTrue($this->list[0]===$this->item1);
$this->assertTrue($this->list[1]===$this->item2);
try {
$a=$this->list[2];
$this->fail('exception not raised when accessing item with out-of-range index');
} catch(TInvalidDataValueException $e) {
}
}
public function testGetIteratorTList() {
$n=0;
$found=0;
foreach($this->list as $index=>$item) {
foreach($this->list as $a=>$b); // test of iterator
$n++;
if($index===0 && $item===$this->item1)
$found++;
if($index===1 && $item===$this->item2)
$found++;
}
$this->assertTrue($n==2 && $found==2);
}
public function testArrayMiscTList() {
$this->assertEquals($this->list->Count,count($this->list));
$this->assertTrue(isset($this->list[1]));
$this->assertFalse(isset($this->list[2]));
}
public function testOffsetSetAddTList() {
$list = new TPriorityList(array(1, 2, 3));
$list->offsetSet(null, 4);
self::assertEquals(array(1, 2, 3, 4), $list->toArray());
}
public function testOffsetSetReplaceTList() {
$list = new TPriorityList(array(1, 2, 3));
$list->offsetSet(1, 4);
self::assertEquals(array(1, 4, 3), $list->toArray());
}
public function testOffsetUnsetTList() {
$list = new TPriorityList(array(1, 2, 3));
$list->offsetUnset(1);
self::assertEquals(array(1, 3), $list->toArray());
}
//******* end test cases for TList operations
//*****************************************************************
public function testConstructPriorities() {
$a=array('a' => 1, '0.5' => 2, 9 => 8);
$list=new TPriorityList($a);
$this->assertEquals(3,$list->getCount());
$list2=new TPriorityList($this->plist);
$this->assertEquals(4,$list2->getCount());
$this->assertEquals(-10000000,$list2->priorityOf($this->pfirst));
$this->assertEquals(100,$list2->priorityOf($this->pitem3));
$this->assertEquals(-10000000,$list2->priorityAt(0));
$this->assertEquals($this->plist->DefaultPriority,$list2->priorityAt(2));
$this->assertEquals(100,$list2->priorityAt(3));
}
public function testGetCountPriorities() {
$this->assertEquals(4,$this->plist->getCount());
$this->assertEquals(4,$this->plist->Count);
}
public function testAddPriorities() {
$this->assertEquals(3,$this->plist->add(null));
$this->assertEquals(4,$this->plist->add($this->item3));
$this->assertEquals(4,$this->plist->getCount());
$this->assertEquals(3,$this->plist->indexOf($this->item3));
}
public function testInsertAtPriorities() {
$this->assertNull($this->plist->insertAt(0,$this->item3));
$this->assertEquals(3,$this->plist->getCount());
$this->assertEquals(2,$this->plist->indexOf($this->item2));
$this->assertEquals(0,$this->plist->indexOf($this->item3));
$this->assertEquals(1,$this->plist->indexOf($this->item1));
try {
$this->plist->insertAt(4,$this->item3);
$this->fail('exception not raised when adding item at an out-of-range index');
} catch(TInvalidDataValueException $e) {
}
}
public function testInsertBeforePriorities() {
try {
$this->plist->insertBefore($this->item4,$this->item3);
$this->fail('exception not raised when adding item before a non-existant base item');
} catch(TInvalidDataValueException $e) {
}
$this->assertEquals(2,$this->plist->getCount());
$this->plist->insertBefore($this->item1,$this->item3);
$this->assertEquals(3,$this->plist->getCount());
$this->assertEquals(0,$this->plist->indexOf($this->item3));
$this->assertEquals(1,$this->plist->indexOf($this->item1));
$this->assertEquals(2,$this->plist->indexOf($this->item2));
}
public function testInsertAfterPriorities() {
try {
$this->plist->insertAfter($this->item4,$this->item3);
$this->fail('exception not raised when adding item after a non-existant base item');
} catch(TInvalidDataValueException $e) {
}
$this->assertEquals(2,$this->plist->getCount());
$this->plist->insertAfter($this->item2,$this->item3);
$this->assertEquals(3,$this->plist->getCount());
$this->assertEquals(0,$this->plist->indexOf($this->item1));
$this->assertEquals(1,$this->plist->indexOf($this->item2));
$this->assertEquals(2,$this->plist->indexOf($this->item3));
}
public function testCanNotInsertWhenReadOnlyPriorities() {
$list = new TPriorityList(array(), true);
try {
$list->insertAt(1, 2);
} catch(TInvalidOperationException $e) {
return;
}
self::fail('An expected TInvalidOperationException was not raised');
}
public function testRemovePriorities() {
$this->assertEquals(0,$this->plist->remove($this->item1));
$this->assertEquals(1,$this->plist->getCount());
$this->assertEquals(-1,$this->plist->indexOf($this->item1));
$this->assertEquals(0,$this->plist->indexOf($this->item2));
try {
$this->plist->remove($this->item1);
$this->fail('exception not raised when removing nonexisting item');
} catch(Exception $e) {
}
}
public function testRemoveAtPriorities() {
$this->plist->add($this->item3);
$this->assertEquals($this->item2, $this->plist->removeAt(1));
$this->assertEquals(-1,$this->plist->indexOf($this->item2));
$this->assertEquals(1,$this->plist->indexOf($this->item3));
$this->assertEquals(0,$this->plist->indexOf($this->item1));
try {
$this->plist->removeAt(2);
$this->fail('exception not raised when removing item with invalid index');
} catch(TInvalidDataValueException $e) {
}
}
public function testCanNotRemoveWhenReadOnlyPriorities() {
$list = new TPriorityList(array(1, 2, 3), true);
try {
$list->removeAt(2);
} catch(TInvalidOperationException $e) {
return;
}
self::fail('An expected TInvalidOperationException was not raised');
}
public function testClearPriorities() {
$this->plist->clear();
$this->assertEquals(0,$this->plist->getCount());
$this->assertEquals(-1,$this->plist->indexOf($this->item1));
$this->assertEquals(-1,$this->plist->indexOf($this->item2));
}
public function testContainsPriorities() {
$this->assertTrue($this->plist->contains($this->item1));
$this->assertTrue($this->plist->contains($this->item2));
$this->assertFalse($this->plist->contains($this->item3));
}
public function testIndexOfPriorities() {
$this->assertEquals(0,$this->plist->indexOf($this->item1));
$this->assertEquals(1,$this->plist->indexOf($this->item2));
$this->assertEquals(-1,$this->plist->indexOf($this->item3));
}
public function testCopyFromPriorities() {
$array=array($this->item3,$this->item1);
$this->plist->copyFrom($array);
$this->assertTrue(count($array)==2 && $this->plist[0]===$this->item3 && $this->plist[1]===$this->item1);
try {
$this->plist->copyFrom($this);
$this->fail('exception not raised when copying from non-traversable object');
} catch(TInvalidDataTypeException $e) {
}
}
public function testMergeWithPriorities() {
$array=array($this->item3,$this->item1);
$this->plist->mergeWith($array);
$this->assertTrue($this->plist->getCount()==4 && $this->plist[0]===$this->item1 && $this->plist[3]===$this->item1);
try {
$this->plist->mergeWith($this);
$this->fail('exception not raised when copying from non-traversable object');
} catch(TInvalidDataTypeException $e) {
}
}
public function testToArrayPriorities() {
$array=$this->plist->toArray();
$this->assertTrue(count($array)==2 && $array[0]===$this->item1 && $array[1]===$this->item2);
}
public function testArrayReadPriorities() {
$this->assertTrue($this->plist[0]===$this->item1);
$this->assertTrue($this->plist[1]===$this->item2);
try {
$a=$this->plist[2];
$this->fail('exception not raised when accessing item with out-of-range index');
} catch(TInvalidDataValueException $e) {
}
}
public function testGetIteratorPriorities() {
$n=0;
$found=0;
foreach($this->plist as $index=>$item) {
foreach($this->plist as $a=>$b); // test of iterator
$n++;
if($index===0 && $item===$this->item1)
$found++;
if($index===1 && $item===$this->item2)
$found++;
}
$this->assertTrue($n==2 && $found==2);
}
public function testArrayMiscPriorities() {
$this->assertEquals($this->plist->Count,count($this->plist));
$this->assertTrue(isset($this->plist[1]));
$this->assertFalse(isset($this->plist[2]));
}
public function testOffsetSetAddPriorities() {
$list = new TPriorityList(array(1, 2, 3));
$list->offsetSet(null, 4);
self::assertEquals(array(1, 2, 3, 4), $list->toArray());
}
public function testOffsetSetReplacePriorities() {
$list = new TPriorityList(array(1, 2, 3));
$list->offsetSet(1, 4);
self::assertEquals(array(1, 4, 3), $list->toArray());
}
public function testOffsetUnsetPriorities() {
$list = new TPriorityList(array(1, 2, 3));
$list->offsetUnset(1);
self::assertEquals(array(1, 3), $list->toArray());
}
}
|