VectorTest.php 5.9 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
{
Qiang Xue committed
14 15 16
	/**
	 * @var Vector
	 */
17 18 19 20 21
	protected $vector;
	protected $item1, $item2, $item3;

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

47 48 49 50 51 52 53 54 55 56
	public function testItemAt()
	{
		$a=array(1, 2, null, 4);
		$vector=new Vector($a);
		$this->assertEquals(1, $vector->itemAt(0));
		$this->assertEquals(2, $vector->itemAt(1));
		$this->assertNull($vector->itemAt(2));
		$this->assertEquals(4, $vector->itemAt(3));
	}

57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
	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));
Qiang Xue committed
78
		$this->setExpectedException('yii\base\InvalidParamException');
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
		$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));
Qiang Xue committed
100
		$this->setExpectedException('yii\base\InvalidParamException');
101 102 103
		$this->vector->removeAt(2);
	}

Qiang Xue committed
104
	public function testRemoveAll()
105
	{
106
		$this->vector->add($this->item3);
Qiang Xue committed
107
		$this->vector->removeAll();
108 109 110
		$this->assertEquals(0,$this->vector->getCount());
		$this->assertEquals(-1,$this->vector->indexOf($this->item1));
		$this->assertEquals(-1,$this->vector->indexOf($this->item2));
111 112

		$this->vector->add($this->item3);
Qiang Xue committed
113
		$this->vector->removeAll(true);
114 115 116
		$this->assertEquals(0,$this->vector->getCount());
		$this->assertEquals(-1,$this->vector->indexOf($this->item1));
		$this->assertEquals(-1,$this->vector->indexOf($this->item2));
117 118
	}

Qiang Xue committed
119
	public function testHas()
120
	{
Qiang Xue committed
121 122 123
		$this->assertTrue($this->vector->has($this->item1));
		$this->assertTrue($this->vector->has($this->item2));
		$this->assertFalse($this->vector->has($this->item3));
124 125 126 127 128 129 130 131 132
	}

	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
133
	public function testFromArray()
134 135
	{
		$array=array($this->item3,$this->item1);
Qiang Xue committed
136
		$this->vector->copyFrom($array);
137
		$this->assertTrue(count($array)==2 && $this->vector[0]===$this->item3 && $this->vector[1]===$this->item1);
Qiang Xue committed
138
		$this->setExpectedException('yii\base\InvalidParamException');
Qiang Xue committed
139
		$this->vector->copyFrom($this);
140 141 142 143 144 145 146
	}

	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);
147 148 149 150 151 152

		$a=array(1);
		$vector=new Vector($a);
		$this->vector->mergeWith($vector);
		$this->assertTrue($this->vector->getCount()==5 && $this->vector[0]===$this->item1 && $this->vector[3]===$this->item1 && $this->vector[4]===1);

Qiang Xue committed
153
		$this->setExpectedException('yii\base\InvalidParamException');
154 155 156 157 158 159 160 161 162 163 164 165 166
		$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);
Qiang Xue committed
167
		$this->setExpectedException('yii\base\InvalidParamException');
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
		$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
196
		$vector = new Vector(array(1, 2, 3));
197 198 199 200 201 202
		$vector->offsetSet(null, 4);
		$this->assertEquals(array(1, 2, 3, 4), $vector->toArray());
	}

	public function testOffsetSetReplace()
	{
w  
Qiang Xue committed
203
		$vector = new Vector(array(1, 2, 3));
204 205 206 207 208 209
		$vector->offsetSet(1, 4);
		$this->assertEquals(array(1, 4, 3), $vector->toArray());
	}

	public function testOffsetUnset()
	{
w  
Qiang Xue committed
210
		$vector = new Vector(array(1, 2, 3));
211 212 213 214 215 216
		$vector->offsetUnset(1);
		$this->assertEquals(array(1, 3), $vector->toArray());
	}

	public function testIteratorCurrent()
	{
w  
Qiang Xue committed
217
		$vector = new Vector(array('value1', 'value2'));
218 219 220 221
		$val = $vector->getIterator()->current();
		$this->assertEquals('value1', $val);
	}
}