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
|
<?php
require_once dirname(__FILE__).'/../phpunit.php';
class MapItem {
public $data='data';
}
Prado::using('System.Collections.TPriorityMap');
/**
* @package System.Collections
*/
class TPriorityMapTest extends PHPUnit_Framework_TestCase {
protected $map;
protected $item1,$item2,$item3,$item4,$item5;
public function setUp() {
// test that TPriorityMap complies with TMap
$this->map=new TPriorityMap;
$this->item1=new MapItem;
$this->item2=new MapItem;
$this->item3=new MapItem;
$this->item4=new MapItem;
$this->item5=new MapItem;
$this->map->add('key1',$this->item1);
$this->map->add('key2',$this->item2);
//Test the priority capabilities
}
public function setUpPriorities() {
$this->map->add('key3', $this->item3, 0);
$this->map->add('key4', $this->item4, 100);
$this->map->add('key5', $this->item5, 1);
}
public function tearDown() {
$this->map=null;
$this->item1=null;
$this->item2=null;
$this->item3=null;
}
public function testConstruct() {
$a=array(1,2,'key3'=>3);
$map=new TPriorityMap($a);
$this->assertEquals(3,$map->getCount());
$map2=new TPriorityMap($this->map);
$this->assertEquals(2,$map2->getCount());
/* Test the priority functionality of TPriorityMap */
$map3=new TPriorityMap($this->map, false, 100, -1);
$this->assertEquals(100,$map3->getDefaultPriority());
$this->assertEquals(-1,$map3->getPrecision());
}
/* Test that TPriorityMap complies with TMap */
public function testGetReadOnly() {
$map = new TPriorityMap(null, true);
self::assertEquals(true, $map->getReadOnly(), 'List is not read-only');
$map = new TList(null, false);
self::assertEquals(false, $map->getReadOnly(), 'List is read-only');
}
public function testGetCount() {
$this->assertEquals(2,$this->map->getCount());
}
public function testGetKeys() {
$keys=$this->map->getKeys();
$this->assertTrue(count($keys)===2 && $keys[0]==='key1' && $keys[1]==='key2');
}
public function testAdd()
{
$this->map->add('key3',$this->item3);
$this->assertTrue($this->map->getCount()==3 && $this->map->contains('key3'));
}
public function testCanNotAddWhenReadOnly() {
$map = new TPriorityMap(array(), true);
try {
$map->add('key', 'value');
} catch(TInvalidOperationException $e) {
return;
}
self::fail('An expected TInvalidOperationException was not raised');
}
public function testRemove()
{
$this->map->remove('key1');
$this->assertTrue($this->map->getCount()==1 && !$this->map->contains('key1'));
$this->assertTrue($this->map->remove('unknown key')===null);
}
public function testCanNotRemoveWhenReadOnly() {
$map = new TPriorityMap(array('key' => 'value'), true);
try {
$map->remove('key');
} catch(TInvalidOperationException $e) {
return;
}
self::fail('An expected TInvalidOperationException was not raised');
}
public function testClear()
{
$this->map->clear();
$this->assertTrue($this->map->getCount()==0 && !$this->map->contains('key1') && !$this->map->contains('key2'));
}
public function testContains()
{
$this->assertTrue($this->map->contains('key1'));
$this->assertTrue($this->map->contains('key2'));
$this->assertFalse($this->map->contains('key3'));
}
public function testCopyFrom()
{
$array=array('key3'=>$this->item3,'key4'=>$this->item1);
$this->map->copyFrom($array);
$this->assertTrue($this->map->getCount()==2 && $this->map['key3']===$this->item3 && $this->map['key4']===$this->item1);
try
{
$this->map->copyFrom($this);
$this->fail('no exception raised when copying a non-traversable object');
}
catch(TInvalidDataTypeException $e)
{
}
}
public function testMergeWith()
{
$array=array('key2'=>$this->item1,'key3'=>$this->item3);
$this->map->mergeWith($array);
$this->assertEquals(3,$this->map->getCount());
$this->assertTrue($this->map['key2']===$this->item1);
$this->assertTrue($this->map['key3']===$this->item3);
try
{
$this->map->mergeWith($this);
$this->fail('no exception raised when copying a non-traversable object');
}
catch(TInvalidDataTypeException $e)
{
}
}
public function testArrayRead()
{
$this->assertTrue($this->map['key1']===$this->item1);
$this->assertTrue($this->map['key2']===$this->item2);
$this->assertEquals(null,$this->map['key3']);
}
public function testArrayWrite()
{
$this->map['key3']=$this->item3;
$this->assertTrue($this->map['key3']===$this->item3);
$this->assertEquals(3,$this->map->getCount());
$this->map['key1']=$this->item3;
$this->assertTrue($this->map['key1']===$this->item3);
$this->assertEquals(3,$this->map->getCount());
unset($this->map['key2']);
$this->assertEquals(2,$this->map->getCount());
$this->assertFalse($this->map->contains('key2'));
try
{
unset($this->map['unknown key']);
}
catch(Exception $e)
{
$this->fail('exception raised when unsetting element with unknown key');
}
}
public function testArrayForeach()
{
$n=0;
$found=0;
foreach($this->map as $index=>$item)
{
$n++;
if($index==='key1' && $item===$this->item1)
$found++;
if($index==='key2' && $item===$this->item2)
$found++;
}
$this->assertTrue($n==2 && $found==2);
}
public function testArrayMisc()
{
$this->assertEquals($this->map->Count,count($this->map));
$this->assertTrue(isset($this->map['key1']));
$this->assertFalse(isset($this->map['unknown key']));
}
public function testToArray() {
$map = new TPriorityMap(array('key' => 'value'));
self::assertEquals(array('key' => 'value'), $map->toArray());
}
/* Test the priority functionality of TPriorityMap */
public function testDefaultPriorityAndPrecision() {
$this->assertEquals(10, $this->map->DefaultPriority);
$this->map->DefaultPriority = 5;
$this->assertEquals(5, $this->map->getDefaultPriority());
$this->assertEquals(8, $this->map->Precision);
$this->map->Precision = 0;
$this->assertEquals(0, $this->map->getPrecision());
;
$this->assertEquals(5, $this->map->add('key3', $this->item3));
$this->assertEquals(10, $this->map->add('key4', $this->item1, 10));
$this->assertTrue(10 == $this->map->add('key4', $this->item1, 10.01));
$this->assertTrue(100 == $this->map->add('key4', $this->item1, 100));
$this->map->Precision = 1;
$this->assertTrue(10.1 == $this->map->add('key5', $this->item1, 10.1));
$this->assertEquals(5, $this->map->getCount());
}
public function testAddWithPriorityAndPriorityOfAt() {
$this->setUpPriorities();
$this->assertEquals(5, $this->map->getCount());
$this->assertEquals(10, $this->map->priorityOf($this->item1));
$this->assertEquals(0, $this->map->priorityOf($this->item3));
$this->assertEquals(100, $this->map->priorityOf($this->item4));
$this->assertEquals(1, $this->map->priorityOf($this->item5));
$this->assertEquals(false, $this->map->priorityOf(null));
$this->assertEquals(false, $this->map->priorityOf('foo'));
$this->assertEquals(10, $this->map->priorityAt('key1'));
$this->assertEquals(0, $this->map->priorityAt('key3'));
$this->assertEquals(100, $this->map->priorityAt('key4'));
$this->assertEquals(1, $this->map->priorityAt('key5'));
$this->assertEquals(false, $this->map->priorityAt(null));
$this->assertEquals(false, $this->map->priorityAt('foo'));
}
public function testRemoveWithPriorityAndItemsAtWithPriority() {
$this->setUpPriorities();
$this->assertEquals(5, $this->map->getCount());
$this->map->remove('key6');
$this->assertEquals(5, $this->map->getCount());
$this->map->remove('key6', null);
$this->assertEquals(5, $this->map->getCount());
// key5 is at priority 1... not the default priority defined by null... nothing should happen here
$this->map->remove('key5', null);
$this->assertEquals(5, $this->map->getCount());
// key5 is at priority 1... not 50... nothing should happen here
$this->map->remove('key5', 50);
$this->assertEquals(5, $this->map->getCount());
$this->assertEquals(array('key3'=>$this->item3), $this->map->itemsAtPriority(0));
$this->assertEquals(array('key1'=>$this->item1, 'key2'=>$this->item2), $this->map->itemsAtPriority($this->map->DefaultPriority));
$this->assertEquals($this->item2, $this->map->itemAt('key2'));
$this->assertEquals($this->item2, $this->map->itemAt('key2', 10));
$this->assertNull($this->map->itemAt('key2', 11)); //'key2' doesn't exist and priority 11... it is only at priority 10
$this->assertNull($this->map->itemAt('key2', 10.1)); //'key2' doesn't exist and priority 10.1... it is only at priority 10
$this->assertEquals($this->item4, $this->map->remove('key4'));
$this->assertEquals(4, $this->map->getCount());
$this->assertEquals($this->item5, $this->map->remove('key5'));
$this->assertEquals(3, $this->map->getCount());
}
public function testIteratorAndArrayWithPriorities() {
$this->setUpPriorities();
// This is the primary reason for a TPriorityMap
$array = $this->map->toArray();
$ordered_keys = array_keys($array);
$this->assertEquals('key3', $ordered_keys[0]);
$this->assertEquals('key5', $ordered_keys[1]);
$this->assertEquals('key1', $ordered_keys[2]);
$this->assertEquals('key2', $ordered_keys[3]);
$this->assertEquals('key4', $ordered_keys[4]);
$ordered_values = array_values($array);
$this->assertEquals($this->item3, $ordered_values[0]);
$this->assertEquals($this->item5, $ordered_values[1]);
$this->assertEquals($this->item1, $ordered_values[2]);
$this->assertEquals($this->item2, $ordered_values[3]);
$this->assertEquals($this->item4, $ordered_values[4]);
$iter = $this->map->getIterator();
$this->assertTrue($iter->valid());
$this->assertEquals('key3', $iter->key());
$this->assertEquals($this->item1, $iter->current());
$iter->next();
$this->assertTrue($iter->valid());
$this->assertEquals('key5', $iter->key());
$this->assertEquals($this->item3, $iter->current());
$iter->next();
$this->assertTrue($iter->valid());
$this->assertEquals('key1', $iter->key());
$this->assertEquals($this->item5, $iter->current());
$iter->next();
$this->assertTrue($iter->valid());
$this->assertEquals('key2', $iter->key());
$this->assertEquals($this->item5, $iter->current());
$iter->next();
$this->assertTrue($iter->valid());
$this->assertEquals('key4', $iter->key());
$this->assertEquals($this->item5, $iter->current());
$iter->next();
$this->assertFalse($iter->valid());
$this->assertEquals(null, $iter->key());
$this->assertEquals(null, $iter->current());
}
public function testGetPriorities() {
$this->setUpPriorities();
$priorities = $this->map->getPriorities();
$this->assertEquals(0, $priorities[0]);
$this->assertEquals(1, $priorities[1]);
$this->assertEquals(10, $priorities[2]);
$this->assertEquals(100, $priorities[3]);
$this->assertEquals(null, $priorities[4]);
}
public function testCopyAndMergeWithPriorities() {
$this->setUpPriorities();
$map1 = new TPriorityMap();
$map1->add('key1', $this->item1);
$map1->add('keyc', 'valuec');
$map1->copyFrom($this->map);
$this->assertEquals(5, $map1->getCount());
$array = $map1->toArray();
$ordered_keys = array_keys($array);
$this->assertEquals('key3', $ordered_keys[0]);
$this->assertEquals('key5', $ordered_keys[1]);
$this->assertEquals('key1', $ordered_keys[2]);
$this->assertEquals('key2', $ordered_keys[3]);
$this->assertEquals('key4', $ordered_keys[4]);
$ordered_values = array_values($array);
$this->assertEquals($this->item3, $ordered_values[0]);
$this->assertEquals($this->item5, $ordered_values[1]);
$this->assertEquals($this->item1, $ordered_values[2]);
$this->assertEquals($this->item2, $ordered_values[3]);
$this->assertEquals($this->item4, $ordered_values[4]);
$map2 = new TPriorityMap();
$map2->add('startkey', 'startvalue', -1000);
$map2->add('key5', 'value5', 40);
$map2->add('endkey', 'endvalue', 1000);
$map2->mergeWith($this->map);
$this->assertEquals(7, $map2->getCount());
$array = $map2->toArray();
$ordered_keys = array_keys($array);
$this->assertEquals('startkey', $ordered_keys[0]);
$this->assertEquals('key3', $ordered_keys[1]);
$this->assertEquals('key5', $ordered_keys[2]);
$this->assertEquals('key1', $ordered_keys[3]);
$this->assertEquals('key2', $ordered_keys[4]);
$this->assertEquals('key4', $ordered_keys[5]);
$this->assertEquals('endkey', $ordered_keys[6]);
$ordered_values = array_values($array);
$this->assertEquals('startvalue', $ordered_values[0]);
$this->assertEquals($this->item3, $ordered_values[1]);
$this->assertEquals($this->item5, $ordered_values[2]);
$this->assertEquals($this->item1, $ordered_values[3]);
$this->assertEquals($this->item2, $ordered_values[4]);
$this->assertEquals($this->item4, $ordered_values[5]);
$this->assertEquals('endvalue', $ordered_values[6]);
$this->assertEquals(1, $map2->priorityAt('key5'));
$this->assertEquals(1, $map2->priorityOf($this->item5));
}
public function testSetPriorityAt() {
$this->assertEquals(10, $this->map->priorityAt('key2'));
$this->assertEquals(10, $this->map->setPriorityAt('key2', 1));
$this->assertEquals(1, $this->map->priorityAt('key2'));
$this->assertEquals(1, $this->map->setPriorityAt('key2'));
$this->assertEquals(10, $this->map->priorityAt('key2'));
}
public function testToArrayBelowPriority() {
$this->setUpPriorities();
$array = $this->map->toArrayBelowPriority(1);
$this->assertEquals(array('key3'=> $this->item3), $array);
$this->assertEquals(1, count($array));
$array = $this->map->toArrayBelowPriority(1, true);
$this->assertEquals(array('key3'=> $this->item3, 'key5'=> $this->item5), $array);
$this->assertEquals(2, count($array));
$array = $this->map->toArrayBelowPriority(2);
$this->assertEquals(array('key3'=> $this->item3, 'key5'=> $this->item5), $array);
$this->assertEquals(2, count($array));
$array = $this->map->toArrayBelowPriority(10);
$this->assertEquals(array('key3'=> $this->item3, 'key5'=> $this->item5), $array);
$this->assertEquals(2, count($array));
$array = $this->map->toArrayBelowPriority(10, true);
$this->assertEquals(array('key3'=> $this->item3, 'key5'=> $this->item5, 'key1' => $this->item1, 'key2' => $this->item2), $array);
$this->assertEquals(4, count($array));
$array = $this->map->toArrayBelowPriority(100);
$this->assertEquals(array('key3'=> $this->item3, 'key5'=> $this->item5, 'key1' => $this->item1, 'key2' => $this->item2), $array);
$this->assertEquals(4, count($array));
$array = $this->map->toArrayBelowPriority(100, true);
$this->assertEquals(array('key3'=> $this->item3, 'key5'=> $this->item5, 'key1' => $this->item1, 'key2' => $this->item2, 'key4' => $this->item4), $array);
$this->assertEquals(5, count($array));
}
public function testToArrayAbovePriority() {
$this->setUpPriorities();
$array = $this->map->toArrayAbovePriority(100, false);
$this->assertEquals(0, count($array));
$array = $this->map->toArrayAbovePriority(100, true);
$this->assertEquals(1, count($array));
$this->assertEquals(array('key4' => $this->item4), $array);
$array = $this->map->toArrayAbovePriority(11);
$this->assertEquals(array('key4' => $this->item4), $array);
$this->assertEquals(1, count($array));
$array = $this->map->toArrayAbovePriority(10, false);
$this->assertEquals(array('key4' => $this->item4), $array);
$this->assertEquals(1, count($array));
$array = $this->map->toArrayAbovePriority(10);
$this->assertEquals(array('key1' => $this->item1, 'key2' => $this->item2, 'key4' => $this->item4), $array);
$this->assertEquals(3, count($array));
$array = $this->map->toArrayAbovePriority(0);
$this->assertEquals(array('key3' => $this->item3, 'key5' => $this->item5, 'key1' => $this->item1, 'key2' => $this->item2, 'key4' => $this->item4), $array);
$this->assertEquals(5, count($array));
}
}
?>
|