VectorTest.php 5.06 KB
Newer Older
1 2
<?php

Qiang Xue committed
3 4
namespace yiiunit\framework\base;

w  
Qiang Xue committed
5 6
use yii\base\Vector;

7 8 9 10 11
class ListItem
{
	public $data='data';
}

Qiang Xue committed
12
class VectorTest extends \yiiunit\TestCase
13 14 15 16 17 18
{
	protected $vector;
	protected $item1, $item2, $item3;

	public function setUp()
	{
w  
Qiang Xue committed
19
		$this->vector=new Vector;
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
		$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);
w  
Qiang Xue committed
38
		$vector=new Vector($a);
39
		$this->assertEquals(3,$vector->getCount());
w  
Qiang Xue committed
40
		$vector2=new Vector($this->vector);
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
		$this->assertEquals(2,$vector2->getCount());
	}

	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 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 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));
	}

Qiang Xue committed
113
	public function testFromArray()
114 115
	{
		$array=array($this->item3,$this->item1);
Qiang Xue committed
116
		$this->vector->copyFrom($array);
117 118
		$this->assertTrue(count($array)==2 && $this->vector[0]===$this->item3 && $this->vector[1]===$this->item1);
		$this->setExpectedException('yii\base\Exception');
Qiang Xue committed
119
		$this->vector->copyFrom($this);
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
	}

	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()
	{
w  
Qiang Xue committed
170
		$vector = new Vector(array(1, 2, 3));
171 172 173 174 175 176
		$vector->offsetSet(null, 4);
		$this->assertEquals(array(1, 2, 3, 4), $vector->toArray());
	}

	public function testOffsetSetReplace()
	{
w  
Qiang Xue committed
177
		$vector = new Vector(array(1, 2, 3));
178 179 180 181 182 183
		$vector->offsetSet(1, 4);
		$this->assertEquals(array(1, 4, 3), $vector->toArray());
	}

	public function testOffsetUnset()
	{
w  
Qiang Xue committed
184
		$vector = new Vector(array(1, 2, 3));
185 186 187 188 189 190
		$vector->offsetUnset(1);
		$this->assertEquals(array(1, 3), $vector->toArray());
	}

	public function testIteratorCurrent()
	{
w  
Qiang Xue committed
191
		$vector = new Vector(array('value1', 'value2'));
192 193 194 195
		$val = $vector->getIterator()->current();
		$this->assertEquals('value1', $val);
	}
}