Qiang Xue
14 years ago
6 changed files with 465 additions and 41 deletions
@ -0,0 +1,209 @@
|
||||
<?php |
||||
|
||||
class MapItem |
||||
{ |
||||
public $data='data'; |
||||
} |
||||
|
||||
class DictionaryTest extends \yii\test\TestCase |
||||
{ |
||||
protected $dictionary; |
||||
protected $item1,$item2,$item3; |
||||
|
||||
public function setUp() |
||||
{ |
||||
$this->dictionary=new \yii\base\Dictionary; |
||||
$this->item1=new MapItem; |
||||
$this->item2=new MapItem; |
||||
$this->item3=new MapItem; |
||||
$this->dictionary->add('key1',$this->item1); |
||||
$this->dictionary->add('key2',$this->item2); |
||||
} |
||||
|
||||
public function tearDown() |
||||
{ |
||||
$this->dictionary=null; |
||||
$this->item1=null; |
||||
$this->item2=null; |
||||
$this->item3=null; |
||||
} |
||||
|
||||
public function testConstruct() |
||||
{ |
||||
$a=array(1,2,'key3'=>3); |
||||
$dictionary=new \yii\base\Dictionary($a); |
||||
$this->assertEquals(3,$dictionary->getCount()); |
||||
$dictionary2=new \yii\base\Dictionary($this->dictionary); |
||||
$this->assertEquals(2,$dictionary2->getCount()); |
||||
} |
||||
|
||||
public function testReadOnly() |
||||
{ |
||||
$dictionary = new \yii\base\Dictionary(array(), true); |
||||
self::assertEquals(true, $dictionary->readOnly, 'List is not read-only'); |
||||
} |
||||
|
||||
public function testGetCount() |
||||
{ |
||||
$this->assertEquals(2,$this->dictionary->getCount()); |
||||
} |
||||
|
||||
public function testGetKeys() |
||||
{ |
||||
$keys=$this->dictionary->getKeys(); |
||||
$this->assertEquals(2,count($keys)); |
||||
$this->assertEquals('key1',$keys[0]); |
||||
$this->assertEquals('key2',$keys[1]); |
||||
} |
||||
|
||||
public function testAdd() |
||||
{ |
||||
$this->dictionary->add('key3',$this->item3); |
||||
$this->assertEquals(3,$this->dictionary->getCount()); |
||||
$this->assertTrue($this->dictionary->contains('key3')); |
||||
} |
||||
|
||||
public function testCanNotAddWhenReadOnly() |
||||
{ |
||||
$dictionary = new \yii\base\Dictionary(array(), true); |
||||
$this->setExpectedException('yii\base\Exception'); |
||||
$dictionary->add('key', 'value'); |
||||
} |
||||
|
||||
public function testRemove() |
||||
{ |
||||
$this->dictionary->remove('key1'); |
||||
$this->assertEquals(1,$this->dictionary->getCount()); |
||||
$this->assertTrue(!$this->dictionary->contains('key1')); |
||||
$this->assertTrue($this->dictionary->remove('unknown key')===null); |
||||
} |
||||
|
||||
public function testCanNotRemoveWhenReadOnly() |
||||
{ |
||||
$dictionary = new \yii\base\Dictionary(array('key' => 'value'), true); |
||||
$this->setExpectedException('yii\base\Exception'); |
||||
$dictionary->remove('key'); |
||||
} |
||||
|
||||
public function testClear() |
||||
{ |
||||
$this->dictionary->clear(); |
||||
$this->assertEquals(0,$this->dictionary->getCount()); |
||||
$this->assertTrue(!$this->dictionary->contains('key1') && !$this->dictionary->contains('key2')); |
||||
} |
||||
|
||||
public function testContains() |
||||
{ |
||||
$this->assertTrue($this->dictionary->contains('key1')); |
||||
$this->assertTrue($this->dictionary->contains('key2')); |
||||
$this->assertFalse($this->dictionary->contains('key3')); |
||||
} |
||||
|
||||
public function testCopyFrom() |
||||
{ |
||||
$array=array('key3'=>$this->item3,'key4'=>$this->item1); |
||||
$this->dictionary->copyFrom($array); |
||||
|
||||
$this->assertEquals(2, $this->dictionary->getCount()); |
||||
$this->assertEquals($this->item3, $this->dictionary['key3']); |
||||
$this->assertEquals($this->item1, $this->dictionary['key4']); |
||||
|
||||
$this->setExpectedException('yii\base\Exception'); |
||||
$this->dictionary->copyFrom($this); |
||||
} |
||||
|
||||
public function testMergeWith() |
||||
{ |
||||
$a=array('a'=>'v1','v2',array('2'),'c'=>array('3','c'=>'a')); |
||||
$b=array('v22','a'=>'v11',array('2'),'c'=>array('c'=>'3','a')); |
||||
$c=array('a'=>'v11','v2',array('2'),'c'=>array('3','c'=>'3','a'),'v22',array('2')); |
||||
$dictionary=new \yii\base\Dictionary($a); |
||||
$dictionary2=new \yii\base\Dictionary($b); |
||||
$dictionary->mergeWith($dictionary2); |
||||
$this->assertTrue($dictionary->toArray()===$c); |
||||
|
||||
$array=array('key2'=>$this->item1,'key3'=>$this->item3); |
||||
$this->dictionary->mergeWith($array,false); |
||||
$this->assertEquals(3,$this->dictionary->getCount()); |
||||
$this->assertEquals($this->item1,$this->dictionary['key2']); |
||||
$this->assertEquals($this->item3,$this->dictionary['key3']); |
||||
$this->setExpectedException('yii\base\Exception'); |
||||
$this->dictionary->mergeWith($this,false); |
||||
} |
||||
|
||||
public function testRecursiveMergeWithTraversable(){ |
||||
$dictionary = new \yii\base\Dictionary(); |
||||
$obj = new ArrayObject(array( |
||||
'k1' => $this->item1, |
||||
'k2' => $this->item2, |
||||
'k3' => new ArrayObject(array( |
||||
'k4' => $this->item3, |
||||
)) |
||||
)); |
||||
$dictionary->mergeWith($obj,true); |
||||
|
||||
$this->assertEquals(3, $dictionary->getCount()); |
||||
$this->assertEquals($this->item1, $dictionary['k1']); |
||||
$this->assertEquals($this->item2, $dictionary['k2']); |
||||
$this->assertEquals($this->item3, $dictionary['k3']['k4']); |
||||
} |
||||
|
||||
public function testArrayRead() |
||||
{ |
||||
$this->assertEquals($this->item1,$this->dictionary['key1']); |
||||
$this->assertEquals($this->item2,$this->dictionary['key2']); |
||||
$this->assertEquals(null,$this->dictionary['key3']); |
||||
} |
||||
|
||||
public function testArrayWrite() |
||||
{ |
||||
$this->dictionary['key3']=$this->item3; |
||||
$this->assertEquals(3,$this->dictionary->getCount()); |
||||
$this->assertEquals($this->item3,$this->dictionary['key3']); |
||||
|
||||
$this->dictionary['key1']=$this->item3; |
||||
$this->assertEquals(3,$this->dictionary->getCount()); |
||||
$this->assertEquals($this->item3,$this->dictionary['key1']); |
||||
|
||||
unset($this->dictionary['key2']); |
||||
$this->assertEquals(2,$this->dictionary->getCount()); |
||||
$this->assertTrue(!$this->dictionary->contains('key2')); |
||||
|
||||
unset($this->dictionary['unknown key']); |
||||
} |
||||
|
||||
public function testArrayForeach() |
||||
{ |
||||
$n=0; |
||||
$found=0; |
||||
foreach($this->dictionary 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->dictionary->Count,count($this->dictionary)); |
||||
$this->assertTrue(isset($this->dictionary['key1'])); |
||||
$this->assertFalse(isset($this->dictionary['unknown key'])); |
||||
} |
||||
|
||||
public function testToArray() |
||||
{ |
||||
$dictionary = new \yii\base\Dictionary(array('key' => 'value')); |
||||
$this->assertEquals(array('key' => 'value'), $dictionary->toArray()); |
||||
} |
||||
|
||||
public function testIteratorCurrent() |
||||
{ |
||||
$dictionary = new \yii\base\Dictionary(array('key1' => 'value1', 'key2' => 'value2')); |
||||
$val = $dictionary->getIterator()->current(); |
||||
$this->assertEquals('value1', $val); |
||||
} |
||||
} |
@ -0,0 +1,213 @@
|
||||
<?php |
||||
|
||||
class ListItem |
||||
{ |
||||
public $data='data'; |
||||
} |
||||
|
||||
class VectorTest extends \yii\test\TestCase |
||||
{ |
||||
protected $vector; |
||||
protected $item1, $item2, $item3; |
||||
|
||||
public function setUp() |
||||
{ |
||||
$this->vector=new \yii\base\Vector; |
||||
$this->item1=new ListItem; |
||||
$this->item2=new ListItem; |
||||
$this->item3=new ListItem; |
||||
$this->vector->add($this->item1); |
||||
$this->vector->add($this->item2); |
||||
} |
||||
|
||||
public function tearDown() |
||||
{ |
||||
$this->vector=null; |
||||
$this->item1=null; |
||||
$this->item2=null; |
||||
$this->item3=null; |
||||
} |
||||
|
||||
public function testConstruct() |
||||
{ |
||||
$a=array(1,2,3); |
||||
$vector=new \yii\base\Vector($a); |
||||
$this->assertEquals(3,$vector->getCount()); |
||||
$vector2=new \yii\base\Vector($this->vector); |
||||
$this->assertEquals(2,$vector2->getCount()); |
||||
} |
||||
|
||||
public function testReadOnly() |
||||
{ |
||||
$vector = new \yii\base\Vector(array(), true); |
||||
$this->assertEquals(true, $vector->readOnly, 'List is not read-only'); |
||||
$vector = new \yii\base\Vector(array(), false); |
||||
$this->assertEquals(false, $vector->readOnly, 'List is read-only'); |
||||
} |
||||
|
||||
public function testGetCount() |
||||
{ |
||||
$this->assertEquals(2,$this->vector->getCount()); |
||||
$this->assertEquals(2,$this->vector->Count); |
||||
} |
||||
|
||||
public function testAdd() |
||||
{ |
||||
$this->vector->add(null); |
||||
$this->vector->add($this->item3); |
||||
$this->assertEquals(4,$this->vector->getCount()); |
||||
$this->assertEquals(3,$this->vector->indexOf($this->item3)); |
||||
} |
||||
|
||||
public function testInsertAt() |
||||
{ |
||||
$this->vector->insertAt(0,$this->item3); |
||||
$this->assertEquals(3,$this->vector->getCount()); |
||||
$this->assertEquals(2,$this->vector->indexOf($this->item2)); |
||||
$this->assertEquals(0,$this->vector->indexOf($this->item3)); |
||||
$this->assertEquals(1,$this->vector->indexOf($this->item1)); |
||||
$this->setExpectedException('yii\base\Exception'); |
||||
$this->vector->insertAt(4,$this->item3); |
||||
} |
||||
|
||||
public function testCanNotInsertWhenReadOnly() |
||||
{ |
||||
$vector = new \yii\base\Vector(array(), true); |
||||
$this->setExpectedException('yii\base\Exception'); |
||||
$vector->insertAt(1, 2); |
||||
} |
||||
|
||||
public function testRemove() |
||||
{ |
||||
$this->vector->remove($this->item1); |
||||
$this->assertEquals(1,$this->vector->getCount()); |
||||
$this->assertEquals(-1,$this->vector->indexOf($this->item1)); |
||||
$this->assertEquals(0,$this->vector->indexOf($this->item2)); |
||||
|
||||
$this->assertEquals(false,$this->vector->remove($this->item1)); |
||||
|
||||
} |
||||
|
||||
public function testRemoveAt() |
||||
{ |
||||
$this->vector->add($this->item3); |
||||
$this->vector->removeAt(1); |
||||
$this->assertEquals(-1,$this->vector->indexOf($this->item2)); |
||||
$this->assertEquals(1,$this->vector->indexOf($this->item3)); |
||||
$this->assertEquals(0,$this->vector->indexOf($this->item1)); |
||||
$this->setExpectedException('yii\base\Exception'); |
||||
$this->vector->removeAt(2); |
||||
} |
||||
|
||||
public function testCanNotRemoveWhenReadOnly() |
||||
{ |
||||
$vector = new \yii\base\Vector(array(1, 2, 3), true); |
||||
$this->setExpectedException('yii\base\Exception'); |
||||
$vector->removeAt(2); |
||||
} |
||||
|
||||
public function testClear() |
||||
{ |
||||
$this->vector->clear(); |
||||
$this->assertEquals(0,$this->vector->getCount()); |
||||
$this->assertEquals(-1,$this->vector->indexOf($this->item1)); |
||||
$this->assertEquals(-1,$this->vector->indexOf($this->item2)); |
||||
} |
||||
|
||||
public function testContains() |
||||
{ |
||||
$this->assertTrue($this->vector->contains($this->item1)); |
||||
$this->assertTrue($this->vector->contains($this->item2)); |
||||
$this->assertFalse($this->vector->contains($this->item3)); |
||||
} |
||||
|
||||
public function testIndexOf() |
||||
{ |
||||
$this->assertEquals(0,$this->vector->indexOf($this->item1)); |
||||
$this->assertEquals(1,$this->vector->indexOf($this->item2)); |
||||
$this->assertEquals(-1,$this->vector->indexOf($this->item3)); |
||||
} |
||||
|
||||
public function testCopyFrom() |
||||
{ |
||||
$array=array($this->item3,$this->item1); |
||||
$this->vector->copyFrom($array); |
||||
$this->assertTrue(count($array)==2 && $this->vector[0]===$this->item3 && $this->vector[1]===$this->item1); |
||||
$this->setExpectedException('yii\base\Exception'); |
||||
$this->vector->copyFrom($this); |
||||
} |
||||
|
||||
public function testMergeWith() |
||||
{ |
||||
$array=array($this->item3,$this->item1); |
||||
$this->vector->mergeWith($array); |
||||
$this->assertTrue($this->vector->getCount()==4 && $this->vector[0]===$this->item1 && $this->vector[3]===$this->item1); |
||||
$this->setExpectedException('yii\base\Exception'); |
||||
$this->vector->mergeWith($this); |
||||
} |
||||
|
||||
public function testToArray() |
||||
{ |
||||
$array=$this->vector->toArray(); |
||||
$this->assertTrue(count($array)==2 && $array[0]===$this->item1 && $array[1]===$this->item2); |
||||
} |
||||
|
||||
public function testArrayRead() |
||||
{ |
||||
$this->assertTrue($this->vector[0]===$this->item1); |
||||
$this->assertTrue($this->vector[1]===$this->item2); |
||||
$this->setExpectedException('yii\base\Exception'); |
||||
$a=$this->vector[2]; |
||||
} |
||||
|
||||
public function testGetIterator() |
||||
{ |
||||
$n=0; |
||||
$found=0; |
||||
foreach($this->vector as $index=>$item) |
||||
{ |
||||
foreach($this->vector 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 testArrayMisc() |
||||
{ |
||||
$this->assertEquals($this->vector->Count,count($this->vector)); |
||||
$this->assertTrue(isset($this->vector[1])); |
||||
$this->assertFalse(isset($this->vector[2])); |
||||
} |
||||
|
||||
public function testOffsetSetAdd() |
||||
{ |
||||
$vector = new \yii\base\Vector(array(1, 2, 3)); |
||||
$vector->offsetSet(null, 4); |
||||
$this->assertEquals(array(1, 2, 3, 4), $vector->toArray()); |
||||
} |
||||
|
||||
public function testOffsetSetReplace() |
||||
{ |
||||
$vector = new \yii\base\Vector(array(1, 2, 3)); |
||||
$vector->offsetSet(1, 4); |
||||
$this->assertEquals(array(1, 4, 3), $vector->toArray()); |
||||
} |
||||
|
||||
public function testOffsetUnset() |
||||
{ |
||||
$vector = new \yii\base\Vector(array(1, 2, 3)); |
||||
$vector->offsetUnset(1); |
||||
$this->assertEquals(array(1, 3), $vector->toArray()); |
||||
} |
||||
|
||||
public function testIteratorCurrent() |
||||
{ |
||||
$vector = new \yii\base\Vector(array('value1', 'value2')); |
||||
$val = $vector->getIterator()->current(); |
||||
$this->assertEquals('value1', $val); |
||||
} |
||||
} |
Loading…
Reference in new issue