summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--framework/Collections/TPriorityList.php493
-rw-r--r--tests/unit/Collections/TListTest.php10
-rw-r--r--tests/unit/Collections/TPriorityListTest.php1441
3 files changed, 1288 insertions, 656 deletions
diff --git a/framework/Collections/TPriorityList.php b/framework/Collections/TPriorityList.php
index 2566aeea..6ce93d49 100644
--- a/framework/Collections/TPriorityList.php
+++ b/framework/Collections/TPriorityList.php
@@ -14,28 +14,33 @@
* TPriorityList class
*
* TPriorityList implements a priority ordered list collection class. It allows you to specify
- * floating numbers for priorities up to a specific precision. There is also a default priority if
- * no priority is specified. If you replace TList with this class it will work exactly the same with
- * priorities set to the default.
+ * any numeric for priorities down to a specific precision. The lower the numeric, the high the priority of the item in the
+ * list. Thus -10 has a higher priority than -5, 0, 10 (the default), 18, 10005, etc. Per {@link round}, precision may be negative and
+ * thus rounding can go by 10, 100, 1000, etc, instead of just .1, .01, .001, etc. The default precision allows for 8 decimal
+ * places. There is also a default priority of 10, if no different default priority is specified or no item specific priority is indicated.
+ * If you replace TList with this class it will work exactly the same with items inserted set to the default priority, until you start
+ * using different priorities than the default priority.
*
- * As you access the array features of this class it flattens and caches the results. It flushes the
- * cache when elements are changed.
+ * As you access the PHP array features of this class, it flattens and caches the results. If at all possible, this
+ * will keep the cache fresh even when manipulated. If this is not possible the cache is cleared.
+ * When an array of items are needed and the cache is outdated, the cache is recreated from the items and their priorities
*
* You can access, append, insert, remove an item by using
- * {@link itemAt}, {@link add}, {@link insertAt}, {@link insertAtPriority}, and {@link remove}.
+ * {@link itemAt}, {@link add}, {@link insertAt}, and {@link remove}.
* To get the number of the items in the list, use {@link getCount}.
* TPriorityList can also be used like a regular array as follows,
* <code>
- * $list[]=$item; // append with the default priority
- * $list[$index]=$item; // $index must be between 0 and $list->Count. This sets the element regardless of priority. Priority stays the same.
+ * $list[]=$item; // append with the default priority. It may not be the last item if other items in the list are prioritized after the default priority
+ * $list[$index]=$item; // $index must be between 0 and $list->Count-1. This sets the element regardless of priority. Priority stays the same.
+ * $list[$index]=$item; // $index is $list->Count. This appends the item to the end of the list with the same priority as the last item in the list.
* unset($list[$index]); // remove the item at $index
* if(isset($list[$index])) // if the list has an item at $index
* foreach($list as $index=>$item) // traverse each item in the list in proper priority order and add/insert order
* $n=count($list); // returns the number of items in the list
* </code>
*
- ** To extend TPriorityList by doing additional operations with each addition or removal
- ** operation, override {@link insertAtIndexInPriority()} and {@link removeAtIndexInPriority()}.
+ * To extend TPriorityList for doing your own operations with each addition or removal,
+ * override {@link insertAtIndexInPriority()} and {@link removeAtIndexInPriority()} and then call the parent.
*
* @author Brad Anderson <javalizard@gmail.com>
* @version $Id: TPriorityList.php 2541 2008-10-21 15:05:13Z javalizard $
@@ -49,34 +54,34 @@ class TPriorityList extends TList
*/
private $_d=array();
/**
- * @var boolean tells if the _d is currently ordered.
+ * @var boolean indicates if the _d is currently ordered.
*/
- private $_o=0;
+ private $_o=false;
/**
* @var array cached flattened internal data storage
*/
private $_fd=null;
/**
- * @var array cached flattened internal data storage
+ * @var integer number of items contain within the list
*/
private $_c=0;
/**
- * @var numeric the default priority of items added if not specified
+ * @var numeric the default priority of items without specified priorities
*/
private $_dp=10;
/**
- * @var numeric the precision of the floating point priorities within this priority list
+ * @var integer the precision of the numeric priorities within this priority list.
*/
- private $_p=10;
-
+ private $_p=8;
+
/**
* Constructor.
* Initializes the list with an array or an iterable object.
- * @param array|Iterator the intial data. Default is null, meaning no initialization.
+ * @param array|Iterator the intial data. Default is null, meaning no initial data.
* @param boolean whether the list is read-only
- * @param float the default priority of items without priorities.
- * @param numeric the precision of the floating priorities
- * @throws TInvalidDataTypeException If data is not null and neither an array nor an iterator.
+ * @param numeric the default priority of items without specified priorities.
+ * @param integer the precision of the numeric priorities
+ * @throws TInvalidDataTypeException If data is not null and is neither an array nor an iterator.
*/
public function __construct($data=null,$readOnly=false,$defaultPriority=10,$precision=8)
{
@@ -84,10 +89,10 @@ class TPriorityList extends TList
if($data!==null)
$this->copyFrom($data);
$this->setReadOnly($readOnly);
- $this->setDefaultPriority($defaultPriority);
$this->setPrecision($precision);
+ $this->setDefaultPriority($defaultPriority);
}
-
+
/**
* Returns the number of items in the list.
* This method is required by Countable interface.
@@ -97,62 +102,66 @@ class TPriorityList extends TList
{
return $this->getCount();
}
-
+
/**
+ * Returns the total number of items in the list
* @return integer the number of items in the list
*/
public function getCount()
{
return $this->_c;
}
-
+
/**
- * @param numeric optional priority at which to count items. if no parameter, it takes the default {@link getDefaultPriority}
- * @return int the number of items in the list at the
+ * Gets the number of items at a priority within the list
+ * @param numeric optional priority at which to count items. if no parameter, it will be set to the default {@link getDefaultPriority}
+ * @return integer the number of items in the list at the specified priority
*/
public function getPriorityCount($priority=null)
{
if($priority === null)
- $priority = $this->DefaultPriority;
+ $priority = $this->getDefaultPriority();
$priority = (string)round(TPropertyValue::ensureFloat($priority), $this->_p);
- if(!isset($this->_d[$priority]) || !is_array($this->_d[$priority])) return false;
+ if(!isset($this->_d[$priority]) || !is_array($this->_d[$priority]))
+ return false;
return count($this->_d[$priority]);
}
-
+
/**
- * @return boolean whether this map is read-only or not. Defaults to false.
+ * @return numeric gets the default priority of inserted items without a specified priority
*/
public function getDefaultPriority()
{
return $this->_dp;
}
-
+
/**
- * @param boolean whether this list is read-only or not
+ * This must be called internally or when instantiated.
+ * @param numeric sets the default priority of inserted items without a specified priority
*/
protected function setDefaultPriority($value)
{
- $this->_dp=TPropertyValue::ensureFloat($value);
+ $this->_dp = (string)round(TPropertyValue::ensureFloat($value), $this->_p);
}
-
+
/**
- * @return int the precision of the floating priorities, defaults with 10
+ * @return integer The precision of numeric priorities, defaults to 8
*/
public function getPrecision()
{
return $this->_p;
}
-
+
/**
- * TPriorityList uses php function {@link round} on its priorities and thus it uses precision.
- * @param int this sets the precision of the floating point priorities.
+ * This must be called internally or when instantiated.
+ * @param integer The precision of numeric priorities.
*/
protected function setPrecision($value)
{
$this->_p=TPropertyValue::ensureInteger($value);
}
-
+
/**
* Returns an iterator for traversing the items in the list.
* This method is required by the interface IteratorAggregate.
@@ -162,21 +171,20 @@ class TPriorityList extends TList
{
return new TPriorityListIterator($this->flattenPriorities());
}
-
+
/**
- * This is ordered lowest to highest.
- * @return array the array of priorities
+ * This returns a list of the priorities within this list, ordered lowest to highest.
+ * @return array the array of priority numerics in decreasing priority order
*/
public function getPriorities()
{
- $this->flattenPriorities();
+ $this->sortPriorities();
return array_keys($this->_d);
}
-
+
/**
- * this orders the priority list and flattens it into an array [0,...,n-1]
- * @return array of the values in the list in priority order
+ * This orders the priority list internally.
*/
protected function sortPriorities() {
if(!$this->_o) {
@@ -184,11 +192,10 @@ class TPriorityList extends TList
$this->_o = true;
}
}
-
/**
- * this orders the priority list and flattens it into an array [0,...,n-1]
- * @return array of the values in the list in priority order
+ * This flattens the priority list into a flat array [0,...,n-1]
+ * @return array array of items in the list in priority and index order
*/
protected function flattenPriorities() {
if(is_array($this->_fd))
@@ -200,16 +207,18 @@ class TPriorityList extends TList
$this->_fd = array_merge($this->_fd, $itemsatpriority);
return $this->_fd;
}
+
/**
- * Returns the item with the specified key.
- * This method is exactly the same as {@link offsetGet}.
- * @param mixed the key
- * @return mixed the element at the offset, null if no element is found at the offset
+ * Returns the item at the index of a flattened priority list.
+ * {@link offsetGet} calls this method.
+ * @param integer the index of the item to get
+ * @return mixed the element at the offset
+ * @error TInvalidDataValueException Issued when the index is invalid
*/
public function itemAt($index)
{
- if($index>=0 && $index<$this->_c) {
+ if($index>=0 && $index<$this->getCount()) {
$arr = $this->flattenPriorities();
return $arr[$index];
} else
@@ -217,27 +226,30 @@ class TPriorityList extends TList
}
/**
- * @return array the key list
+ * Gets all the items at a specific priority.
+ * @param numeric priority of the items to get. Defaults to null, filled in with the default priority, if left blank.
+ * @return array all items at priority in index order, null if there are no items at that priority
*/
public function itemsAtPriority($priority=null)
{
if($priority === null)
- $priority = $this->DefaultPriority;
+ $priority = $this->getDefaultPriority();
$priority = (string)round(TPropertyValue::ensureFloat($priority), $this->_p);
- return isset($this->_d[$priority]) ? $this->_d[$priority] : false;
+ return isset($this->_d[$priority]) ? $this->_d[$priority] : null;
}
/**
- * Returns the item with the specified key.
- * This method is exactly the same as {@link offsetGet}.
- * @param mixed the key
- * @return mixed the element at the offset, null if no element is found at the offset
+ * Returns the item at an index within a priority
+ * @param integer the index into the list of items at priority
+ * @param numeric the priority which to index. no parameter or null will result in the default priority
+ * @return mixed the element at the offset, false if no element is found at the offset
*/
- public function itemAtIndexPriority($index,$priority=null)
+ public function itemAtIndexInPriority($index,$priority=null)
{
if($priority === null)
- $priority = $this->DefaultPriority;
+ $priority = $this->getDefaultPriority();
+ $priority = (string)round(TPropertyValue::ensureFloat($priority), $this->_p);
return !isset($this->_d[$priority]) ? false : (
isset($this->_d[$priority][$index]) ? $this->_d[$priority][$index] : false
@@ -245,30 +257,32 @@ class TPriorityList extends TList
}
/**
- * Adds an item into the map.
- * Note, if the specified key already exists, the old value will be overwritten.
- * @param mixed key
- * @param mixed value
+ * Appends an item into the list at the end of the specified priority. The position of the added item may
+ * not be at the end of the list.
+ * @param mixed item to add into the list at priority
+ * @param numeric priority blank or null for the default priority
* @return int the index within the flattened array
* @throws TInvalidOperationException if the map is read-only
*/
public function add($item, $priority=null)
{
+ if($this->getReadOnly())
+ throw new TInvalidOperationException('list_readonly',get_class($this));
+
return $this->insertAtIndexInPriority($item,false,$priority,true);
}
/**
- * Inserts an item at the specified position.
- * Original item at the position and the next items
- * will be moved one step towards the end.
- * @param integer the specified position.
- * @param mixed new item
+ * Inserts an item at an index. It reads the priority of the item at index within the flattened list
+ * and then inserts the item at that priority-index.
+ * @param integer the specified position in the flattened list.
+ * @param mixed new item to add
* @throws TInvalidDataValueException If the index specified exceeds the bound
* @throws TInvalidOperationException if the list is read-only
*/
public function insertAt($index, $item)
{
- if($this->ReadOnly)
+ if($this->getReadOnly())
throw new TInvalidOperationException('list_readonly',get_class($this));
if(($priority=$this->priorityAt($index,true))!==false)
@@ -278,93 +292,91 @@ class TPriorityList extends TList
}
/**
- * Inserts an item at the specified position.
- * Original item at the position and the next items
- * will be moved one step towards the end.
- * @param integer the specified position.
- * @param mixed new item
+ * Inserts an item at the specified index within a priority. Override and call this method to
+ * insert your own functionality.
+ * @param mixed item to add within the list.
+ * @param integer index within the priority to add the item, defaults to false which appends the item at the priority
+ * @param numeric priority priority of the item. defaults to null, which sets it to the default priority
+ * @param boolean preserveCache specifies if this is a special quick function or not. This defaults to false.
* @throws TInvalidDataValueException If the index specified exceeds the bound
* @throws TInvalidOperationException if the list is read-only
*/
- public function insertAtIndexInPriority($item,$index=false,$priority=null,$indexItem=false)
+ public function insertAtIndexInPriority($item,$index=false,$priority=null,$preserveCache=false)
{
+ if($this->getReadOnly())
+ throw new TInvalidOperationException('list_readonly',get_class($this));
- if(!$this->ReadOnly)
- {
- if($priority===null)
- $priority=$this->DefaultPriority;
-
- $priority=(string)round(TPropertyValue::ensureFloat($priority), $this->_p);
+ if($priority===null)
+ $priority=$this->getDefaultPriority();
+ $priority=(string)round(TPropertyValue::ensureFloat($priority), $this->_p);
+
+ if($preserveCache) {
+ $this->sortPriorities();
+ $cc=0;
+ foreach($this->_d as $prioritykey => $items)
+ if($prioritykey >= $priority)
+ break;
+ else
+ $cc+=count($items);
- if($indexItem) {
- $this->sortPriorities();
- $cc=0;
- foreach($this->_d as $prioritykey => $items)
- if($prioritykey >= $priority)
- break;
- else
- $cc+=count($items);
-
- if($index === false) {
- //This string conversion allows floats as keys
- $c = count($this->_d[$priority]);
- if(!$c && $this->_c)
- $this->_o = false;
- $c += $cc;
- $this->_d[$priority][]=$item;
- } else if(isset($this->_d[$priority])) {
- $c = $index + $cc;
- array_splice($this->_d[$priority],$index,0,array($item));
- } else {
- $c = $cc;
+ if($index === false) {
+ $c = count($this->_d[$priority]);
+ if(!$c && $this->_c) // no items at this priority, but there exist items in this list
$this->_o = false;
- $this->_d[$priority]=array($item);
- }
-
- if($this->_fd && is_array($this->_fd))
- array_splice($this->_fd,$c,0,array($item));
+ $c += $cc;
+ $this->_d[$priority][]=$item;
+ } else if(isset($this->_d[$priority])) {
+ $c = $index + $cc;
+ array_splice($this->_d[$priority],$index,0,array($item));
} else {
- $c = null;
- if($index === false) {
- $cc = count($this->_d[$priority]);
- if(!$cc && $this->_c)
- $this->_o = false;
- $this->_d[$priority][]=$item;
- } else if(isset($this->_d[$priority])) {
- $cc = $index;
- array_splice($this->_d[$priority],$index,0,array($item));
- } else {
- $cc = 0;
- $this->_o = false;
- $this->_d[$priority]=array($item);
- }
- if($this->_fd && is_array($this->_fd) && count($this->_d) == 1)
- array_splice($this->_fd,$cc,0,array($item));
- else
- $this->_fd = null;
+ $c = $cc;
+ $this->_o = false;
+ $this->_d[$priority]=array($item);
}
- $this->_c++;
-
- return $c;
+ if($this->_fd && is_array($this->_fd)) // if there is a flattened array cache
+ array_splice($this->_fd,$c,0,array($item));
+ } else {
+ $c = null;
+ if($index === false) {
+ $cc = count($this->_d[$priority]);
+ if(!$cc && $this->_c)
+ $this->_o = false;
+ $this->_d[$priority][]=$item;
+ } else if(isset($this->_d[$priority])) {
+ $cc = $index;
+ array_splice($this->_d[$priority],$index,0,array($item));
+ } else {
+ $cc = 0;
+ $this->_o = false;
+ $this->_d[$priority]=array($item);
+ }
+ if($this->_fd && is_array($this->_fd) && count($this->_d) == 1)
+ array_splice($this->_fd,$cc,0,array($item));
+ else
+ $this->_fd = null;
}
- else
- throw new TInvalidOperationException('list_readonly',get_class($this));
+
+ $this->_c++;
+
+ return $c;
}
/**
- * Removes an item from the list.
- * The list will first search for the item.
- * The first item found will be removed from the list.
- * @param mixed the item to be removed.
- * @return integer the index at which the item is being removed
+ * Removes an item from the priority list.
+ * The list will search for the item. The first matching item found will be removed from the list.
+ * @param mixed item the item to be removed.
+ * @return integer index within the flattened list at which the item is being removed
* @throws TInvalidDataValueException If the item does not exist
*/
public function remove($item)
{
- if(($priority=$this->priorityOf($item, true)) !== false)
+ if($this->getReadOnly())
+ throw new TInvalidOperationException('list_readonly',get_class($this));
+
+ if(($priority=$this->priorityOf($item, true))!==false)
{
$this->removeAtIndexInPriority($priority[1], $priority[0]);
return $priority[2];
@@ -374,80 +386,88 @@ class TPriorityList extends TList
}
/**
- * Removes an item at the specified position.
- * @param integer the index of the item to be removed.
+ * Removes an item at the specified index in the flattened list.
+ * @param integer index of the item to be removed.
* @return mixed the removed item.
* @throws TInvalidDataValueException If the index specified exceeds the bound
* @throws TInvalidOperationException if the list is read-only
*/
public function removeAt($index)
{
- if($this->ReadOnly)
+ if($this->getReadOnly())
throw new TInvalidOperationException('list_readonly',get_class($this));
- if(($priority = $this->priorityAt($index, true)) !== false)
+ if(($priority = $this->priorityAt($index, true))!==false)
return $this->removeAtIndexInPriority($priority[1], $priority[0]);
throw new TInvalidDataValueException('list_index_invalid',$index);
}
/**
- * Removes an item from the list.
- * The list will first search for the item.
- * The first item found will be removed from the list.
- * @param mixed the item to be removed.
- * @return integer the index at which the item is being removed
+ * Removes an item from a specific priority. The list will first search for the item
+ * at the priority. The first item found will be removed from the priority.
+ * @param mixed item to be removed.
+ * @param numeric priority
+ * @return integer the index within the priority at which the item is being removed
* @throws TInvalidDataValueException If the item does not exist
*/
- public function removeAtPriority($item, $priority=null)
+ public function removeFromPriority($item, $priority=null)
{
- if($priority === null)
- $priority = $this->DefaultPriority;
- if(isset($this->_d[$priority]) && ($index=array_search($item,$this->_d[$priority],true))!==false)
- return $this->removeAtIndexInPriority($index, $priority);
+ if($this->getReadOnly())
+ throw new TInvalidOperationException('list_readonly',get_class($this));
+
+ if($priority===null)
+ $priority = $this->getDefaultPriority();
+ $priority = (string)round(TPropertyValue::ensureFloat($priority), $this->_p);
+
+ if(isset($this->_d[$priority]) && ($index=array_search($item,$this->_d[$priority],true))!==false) {
+ $this->removeAtIndexInPriority($index, $priority);
+ return $index;
+ }
throw new TInvalidDataValueException('list_item_inexistent');
}
/**
- * Removes an item from the list.
- * The list will first search for the item.
- * The first item found will be removed from the list.
- * @param mixed the item to be removed.
- * @return integer the index at which the item is being removed
+ * Removes the item at a specific index within a priority. Override
+ * and call this method to insert your own functionality.
+ * @param integer index of item to remove within the priority.
+ * @param numeric priority of the item to remove, defaults to null, or left blank, it is then set to the default priority
+ * @return mixed the removed item.
* @throws TInvalidDataValueException If the item does not exist
*/
public function removeAtIndexInPriority($index, $priority=null)
{
- if(!$this->ReadOnly)
- {
- if($priority === null)
- $priority = $this->DefaultPriority;
-
- $priority = (string)round(TPropertyValue::ensureFloat($priority), $this->_p);
- if(!isset($this->_d[$priority]) || !isset($this->_d[$priority][$index]))
- throw new TInvalidDataValueException('list_item_inexistent');
-
- // $value is an array of elements removed, only one
- $value = array_splice($this->_d[$priority],$index,1);
- $value = $value[0];
-
- if(!count($this->_d[$priority]))
- unset($this->_d[$priority]);
-
- $this->_c--;
- $this->_fd = null;
- return $value;
- }
- else
+ if($this->getReadOnly())
throw new TInvalidOperationException('list_readonly',get_class($this));
+
+ if($priority === null)
+ $priority = $this->getDefaultPriority();
+ $priority = (string)round(TPropertyValue::ensureFloat($priority), $this->_p);
+
+ if(!isset($this->_d[$priority]) || $index < 0 || $index >= count($this->_d[$priority]))
+ throw new TInvalidDataValueException('list_item_inexistent');
+
+ // $value is an array of elements removed, only one
+ $value = array_splice($this->_d[$priority],$index,1);
+ $value = $value[0];
+
+ if(!count($this->_d[$priority]))
+ unset($this->_d[$priority]);
+
+ $this->_c--;
+ $this->_fd = null;
+ return $value;
}
/**
- * Removes all items in the priority list.
+ * Removes all items in the priority list by calling removeAtIndexInPriority from the last item to the first.
*/
public function clear()
{
+ if($this->getReadOnly())
+ throw new TInvalidOperationException('list_readonly',get_class($this));
+
+ $d = array_reverse($this->_d, true);
foreach($this->_d as $priority => $items) {
- $items = array_reverse($items);
for($index = count($items) - 1; $index >= 0; $index--)
$this->removeAtIndexInPriority($index, $priority);
unset($this->_d[$priority]);
@@ -455,7 +475,7 @@ class TPriorityList extends TList
}
/**
- * @param mixed the item
+ * @param mixed item
* @return boolean whether the list contains the item
*/
public function contains($item)
@@ -464,8 +484,8 @@ class TPriorityList extends TList
}
/**
- * @param mixed the item
- * @return integer the index of the item in the list (0 based), -1 if not found.
+ * @param mixed item
+ * @return integer the index of the item in the flattened list (0 based), -1 if not found.
*/
public function indexOf($item)
{
@@ -476,13 +496,18 @@ class TPriorityList extends TList
}
/**
- * @param mixed the item
- * @return integer the index of the item in the list (0 based), false if not found.
+ * Returns the priority of a particular item
+ * @param mixed the item to look for within the list
+ * @param boolean withindex this specifies if the full positional data of the item within the list is returned.
+ * This defaults to false, if no parameter is provided, so only provides the priority number of the item by default.
+ * @return numeric|array the priority of the item in the list, false if not found.
+ * if withindex is true, an array is returned of [0 => $priority, 1 => $priorityIndex, 2 => flattenedIndex,
+ * 'priority' => $priority, 'index' => $priorityIndex, 'absindex' => flattenedIndex]
*/
public function priorityOf($item, $withindex = false)
{
// this is to ensure priority order
- $this->flattenPriorities();
+ $this->sortPriorities();
$absindex = 0;
foreach($this->_d as $priority => $items) {
@@ -499,34 +524,46 @@ class TPriorityList extends TList
}
/**
- * @param mixed the item
- * @return integer the index of the item in the list (0 based), false if not found.
+ * Retutrns the priority of an item at a particular flattened index.
+ * @param integer index of the item within the list
+ * @param boolean withindex this specifies if the full positional data of the item within the list is returned.
+ * This defaults to false, if no parameter is provided, so only provides the priority number of the item by default.
+ * @return numeric|array the priority of the item in the list, false if not found.
+ * if withindex is true, an array is returned of [0 => $priority, 1 => $priorityIndex, 2 => flattenedIndex,
+ * 'priority' => $priority, 'index' => $priorityIndex, 'absindex' => flattenedIndex]
*/
public function priorityAt($index, $withindex = false)
{
- if($index < 0 || $index >= $this->Count)
+ if($index < 0 || $index >= $this->getCount())
throw new TInvalidDataValueException('list_index_invalid',$index);
// this is to ensure priority order
$absindex = $index;
- $this->flattenPriorities();
+ $this->sortPriorities();
foreach($this->_d as $priority => $items) {
if($index >= ($c = count($items)))
$index -= $c;
else
return $withindex ?
array($priority, $index, $absindex,
- 'priority' => $priority, 'index' => $inde, 'absindex' => $absindexx) : $priority;
+ 'priority' => $priority, 'index' => $index, 'absindex' => $absindex) : $priority;
}
return false;
}
/**
- * @param mixed the item to index
- * @param mixed the item
+ * This inserts an item before another item within the list. It uses the same priority as the
+ * found index item and places the new item before it.
+ * @param mixed indexitem the item to index
+ * @param mixed the item to add before indexitem
+ * @return integer where the item has been inserted in the flattened list
+ * @throws TInvalidDataValueException If the item does not exist
*/
public function insertBefore($indexitem, $item)
{
+ if($this->getReadOnly())
+ throw new TInvalidOperationException('list_readonly',get_class($this));
+
if(($priority = $this->priorityOf($indexitem, true)) === false)
throw new TInvalidDataValueException('list_item_inexistent');
@@ -536,21 +573,28 @@ class TPriorityList extends TList
}
/**
- * @param mixed the item to index
- * @param mixed the item
+ * This inserts an item after another item within the list. It uses the same priority as the
+ * found index item and places the new item after it.
+ * @param mixed indexitem the item to index
+ * @param mixed the item to add after indexitem
+ * @return integer where the item has been inserted in the flattened list
+ * @throws TInvalidDataValueException If the item does not exist
*/
public function insertAfter($indexitem, $item)
{
- if(($priority = $this->priorityOf($indexitem, true)) === false)
+ if($this->getReadOnly())
+ throw new TInvalidOperationException('list_readonly',get_class($this));
+
+ if(($priority=$this->priorityOf($indexitem,true))===false)
throw new TInvalidDataValueException('list_item_inexistent');
- $this->insertAtIndexInPriority($item, $priority[1]+1, $priority[0]);
+ $this->insertAtIndexInPriority($item,$priority[1]+1,$priority[0]);
return $priority[2]+1;
}
/**
- * @return array the list of items in array
+ * @return array the priority list of items in array
*/
public function toArray()
{
@@ -558,17 +602,17 @@ class TPriorityList extends TList
}
/**
- * @return array the list of items in array with array keys as priorities and items as arrays of items
+ * @return array the array of priorities keys with values of arrays of items. The priorities are sorted so important priorities, lower numerics, are first.
*/
public function toPriorityArray()
{
- $this->flattenPriorities();
+ $this->sortPriorities();
return $this->_d;
}
/**
- * Copies iterable data into the map.
+ * Copies iterable data into the priority list.
* Note, existing data in the map will be cleared first.
* @param mixed the data to be copied from, must be an array or object implementing Traversable
* @throws TInvalidDataTypeException If data is neither an array nor an iterator.
@@ -578,7 +622,7 @@ class TPriorityList extends TList
if($data instanceof TPriorityList) {
if($this->getCount()>0)
$this->clear();
- foreach($data->Priorities as $priority) {
+ foreach($data->getPriorities() as $priority) {
foreach($data->itemsAtPriority($priority) as $index => $item)
$this->insertAtIndexInPriority($item, $index, $priority);
}
@@ -594,17 +638,19 @@ class TPriorityList extends TList
}
/**
- * Merges iterable data into the map.
- * Existing data in the map will be kept and overwritten if the keys are the same.
+ * Merges iterable data into the priority list.
+ * New data will be appended to the end of the existing data. If another TPriorityList is merged,
+ * the incoming parameter items will be appended at the priorities they are present. These items will be added
+ * to the end of the existing items with equal priorities, if there are any.
* @param mixed the data to be merged with, must be an array or object implementing Traversable
* @throws TInvalidDataTypeException If data is neither an array nor an iterator.
*/
public function mergeWith($data)
{
if($data instanceof TPriorityList) {
- foreach($data->Priorities as $priority) {
+ foreach($data->getPriorities() as $priority) {
foreach($data->itemsAtPriority($priority) as $index => $item)
- $this->insertAtIndexInPriority($item, $index, $priority);
+ $this->insertAtIndexInPriority($item, false, $priority);
}
} else if(is_array($data) || $data instanceof Traversable) {
foreach($data as $priority=>$item)
@@ -622,7 +668,7 @@ class TPriorityList extends TList
*/
public function offsetExists($offset)
{
- return ($offset>=0 && $offset<$this->_c);
+ return ($offset>=0 && $offset<$this->getCount());
}
/**
@@ -637,8 +683,14 @@ class TPriorityList extends TList
}
/**
- * Sets the element at the specified offset.
- * This method is required by the interface ArrayAccess.
+ * Sets the element at the specified offset. This method is required by the interface ArrayAccess.
+ * Setting elements in a priority list is not straight forword when appending and setting at the
+ * end boundary. When appending without an offset (a null offset), the item will be added at
+ * the default priority. The item may not be the last item in the list. When appending with an
+ * offset equal to the count of the list, the item will get be appended with the last items priority.
+ *
+ * All together, when setting the location of an item, the item stays in that location, but appending
+ * an item into a priority list doesn't mean the item is at the end of the list.
* @param integer the offset to set element
* @param mixed the element value
*/
@@ -646,8 +698,13 @@ class TPriorityList extends TList
{
if($offset === null)
return $this->add($item);
- $priority = $this->priorityAt($offset, true);
- $this->removeAtIndexInPriority($priority[1], $priority[0]);
+ if($offset === $this->getCount()) {
+ $priority = $this->priorityAt($offset-1, true);
+ $priority[1]++;
+ } else {
+ $priority = $this->priorityAt($offset, true);
+ $this->removeAtIndexInPriority($priority[1], $priority[0]);
+ }
$this->insertAtIndexInPriority($item, $priority[1], $priority[0]);
}
@@ -668,12 +725,12 @@ class TPriorityList extends TList
* TPriorityListIterator implements Iterator interface.
*
* TPriorityListIterator is used by TPriorityList. It allows TPriorityList to return a new iterator
- * for traversing the items in the map.
+ * for traversing the items in the priority list.
*
- * @author Qiang Xue <qiang.xue@gmail.com>
- * @version $Id: TPriorityList.php 2541 2008-10-21 15:05:13Z qiang.xue $
+ * @author Brad Anderson <javalizard@gmail.com>
+ * @version $Id: TPriorityList.php 2541 2010-10-03 15:05:13Z javalizard $
* @package System.Collections
- * @since 3.0
+ * @since 3.2a
*/
class TPriorityListIterator implements Iterator
{
diff --git a/tests/unit/Collections/TListTest.php b/tests/unit/Collections/TListTest.php
index a97273dc..a5a1b2b0 100644
--- a/tests/unit/Collections/TListTest.php
+++ b/tests/unit/Collections/TListTest.php
@@ -51,6 +51,16 @@ class TListTest extends PHPUnit_Framework_TestCase {
$this->assertEquals(2,$this->list->Count);
}
+ public function testItemAt() {
+ $this->assertTrue($this->list->itemAt(0) === $this->item1);
+ $this->assertTrue($this->list->itemAt(1) === $this->item2);
+ try {
+ $this->list->itemAt(2);
+ $this->fail('exception not raised when adding item at an out-of-range index');
+ } catch(TInvalidDataValueException $e) {
+ }
+ }
+
public function testAdd() {
$this->assertEquals(2,$this->list->add(null));
$this->assertEquals(3,$this->list->add($this->item3));
diff --git a/tests/unit/Collections/TPriorityListTest.php b/tests/unit/Collections/TPriorityListTest.php
index 53377b45..70f7c159 100644
--- a/tests/unit/Collections/TPriorityListTest.php
+++ b/tests/unit/Collections/TPriorityListTest.php
@@ -1,8 +1,13 @@
<?php
-require_once dirname(__FILE__).'/../phpunit.php';
+require_once dirname(__FILE__) . '/../phpunit.php';
-class PriorityListItem {
- public $data='data';
+class PriorityListItem
+{
+ var $data = 'data';
+ function __construct($d)
+ {
+ $this->data = $d;
+ }
}
Prado::using('System.Collections.TPriorityList');
@@ -13,495 +18,1055 @@ Prado::using('System.Collections.TPriorityList');
*
* @package System.Collections
*/
-class TPriorityListTest extends PHPUnit_Framework_TestCase {
-
+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->pitem3, 100);
- $this->plist->add($this->pitem2);
- $this->plist->add($this->pfirst, -10000000);
- // 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;
- }
-
+
+ public function setUp()
+ {
+ $this->list = new TPriorityList;
+ $this->item1 = new PriorityListItem(1);
+ $this->item2 = new PriorityListItem(2);
+ $this->item3 = new PriorityListItem(3);
+ $this->item4 = new PriorityListItem(4);
+ $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(5);
+ $this->pitem1 = new PriorityListItem(6);
+ $this->pitem2 = new PriorityListItem(7);
+ $this->pitem3 = new PriorityListItem(8);
+ $this->pitem4 = new PriorityListItem(9);
+ $this->pitem5 = new PriorityListItem(0);
+ $this->plist->add($this->pitem1);
+ $this->plist->add($this->pitem3, 100);
+ $this->plist->add($this->pitem2);
+ $this->plist->add($this->pfirst, -10000000);
+ // 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() {
+ 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() {
+
+ public function testGetCountTList()
+ {
+ $this->assertEquals(2, $this->list->getCount());
+ $this->assertEquals(2, $this->list->Count);
+ }
+
+ public function testItemAt()
+ {
+ $this->assertTrue($this->list->itemAt(0) === $this->item1);
+ $this->assertTrue($this->list->itemAt(1) === $this->item2);
+ }
+
+ 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 testCanNotAddWhenReadOnlyTList()
+ {
+ $list = new TPriorityList(array(), true);
+ try {
+ $list->add(1);
+ self::fail('An expected TInvalidOperationException was not raised');
+ }
+ catch (TInvalidOperationException $e) {
+ }
+ }
+
+ 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 testCanNotInsertAtWhenReadOnlyTList()
+ {
$list = new TPriorityList(array(), true);
try {
$list->insertAt(1, 2);
self::fail('An expected TInvalidOperationException was not raised');
- } catch(TInvalidOperationException $e) {
+ }
+ catch (TInvalidOperationException $e) {
}
try {
$list->insertAt(0, 2);
self::fail('An expected TInvalidOperationException was not raised');
- } catch(TInvalidOperationException $e) {
+ }
+ 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);
+
+ 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 testCanNotInsertBeforeWhenReadOnlyTList()
+ {
+ $list = new TPriorityList(array(
+ 5
+ ), true);
+ try {
+ $list->insertBefore(5, 6);
+ self::fail('An expected TInvalidOperationException was not raised');
+ }
+ catch (TInvalidOperationException $e) {
+ }
+ try {
+ $list->insertBefore(8, 6);
+ self::fail('An expected TInvalidOperationException was not raised');
+ }
+ catch (TInvalidOperationException $e) {
+ }
+ }
+
+ 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 testCanNotInsertAfterWhenReadOnlyTList()
+ {
+ $list = new TPriorityList(array(
+ 5
+ ), true);
+ try {
+ $list->insertAfter(5, 6);
+ self::fail('An expected TInvalidOperationException was not raised');
+ }
+ catch (TInvalidOperationException $e) {
+ }
+ try {
+ $list->insertAfter(8, 6);
+ 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 testCanNotRemoveWhenReadOnlyTList()
+ {
+ $list = new TPriorityList(array(
+ 1,2,3
+ ), true);
+ try {
+ $list->remove(2);
+ self::fail('An expected TInvalidOperationException was not raised');
+ }
+ catch (TInvalidOperationException $e) {
+ }
+
+ $list = new TPriorityList(array(
+ 1,2,3
+ ), true);
+ try {
+ $list->remove(10);
+ self::fail('An expected TInvalidOperationException was not raised');
+ }
+ catch (TInvalidOperationException $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 testCanNotRemoveAtWhenReadOnlyTList()
+ {
+ $list = new TPriorityList(array(
+ 1,2,3
+ ), true);
try {
$list->removeAt(2);
- } catch(TInvalidOperationException $e) {
+ self::fail('An expected TInvalidOperationException was not raised');
+ }
+ catch (TInvalidOperationException $e) {
+ }
+
+ $list = new TPriorityList(array(
+ 1,2,3
+ ), true);
+ try {
+ $list->removeAt(10);
+ self::fail('An expected TInvalidOperationException was not raised');
+ }
+ catch (TInvalidOperationException $e) {
+ }
+ }
+
+ 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 testCanNotClearWhenReadOnlyTList()
+ {
+ $list = new TPriorityList(array(
+ 1,2,3
+ ), true);
+ try {
+ $list->clear();
+ }
+ 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));
+
+ 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());
+ self::assertEquals(array(
+ 1,2,3,4
+ ), $list->toArray());
}
-
- public function testOffsetSetReplaceTList() {
- $list = new TPriorityList(array(1, 2, 3));
+
+ public function testOffsetSetReplaceTList()
+ {
+ $list = new TPriorityList(array(
+ 1,2,3
+ ));
$list->offsetSet(1, 4);
- self::assertEquals(array(1, 4, 3), $list->toArray());
+ self::assertEquals(array(
+ 1,4,3
+ ), $list->toArray());
}
- public function testOffsetUnsetTList() {
- $list = new TPriorityList(array(1, 2, 3));
+ public function testOffsetUnsetTList()
+ {
+ $list = new TPriorityList(array(
+ 1,2,3
+ ));
$list->offsetUnset(1);
- self::assertEquals(array(1, 3), $list->toArray());
+ 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($list2->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->pitem4));
- $this->assertEquals(6,$this->plist->getCount());
- $this->assertEquals(4,$this->plist->indexOf($this->pitem4));
- $this->assertEquals($this->plist->DefaultPriority,$this->plist->priorityAt(4));
- $this->assertEquals(100,$this->plist->priorityAt(5));
- }
-
- public function testInsertAtPriorities() {
- $this->assertNull($this->plist->insertAt(0,$this->pitem3));
- $this->assertEquals(5,$this->plist->getCount());
- $this->assertEquals(3,$this->plist->indexOf($this->pitem2));
- $this->assertEquals(0,$this->plist->indexOf($this->pitem3));
- $this->assertEquals(1,$this->plist->indexOf($this->pitem1));
- 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() {
+ //******* end test cases for TList operations
+ //*****************************************************************
+
+
+ public function testConstructTPriorityList()
+ {
+ $a = array(
+ 'a' => 1,'0.5' => 2,9 => 8
+ );
+
+ $list = new TPriorityList($a);
+ $this->assertEquals(3, $list->getCount());
+
+ $list2 = new TPriorityList($this->plist);
+ // validate that the elements were copied
+ $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($list2->DefaultPriority, $list2->priorityAt(2));
+ $this->assertEquals(100, $list2->priorityAt(3));
+ }
+
+ public function testGetPriorityCountTPriorityList()
+ {
+ $this->assertEquals(2, $this->plist->getPriorityCount());
+ $this->assertEquals(2, $this->plist->getPriorityCount(null));
+ $this->assertEquals(1, $this->plist->getPriorityCount(100));
+ $this->assertEquals(1, $this->plist->getPriorityCount(-10000000));
+ }
+
+ public function testItemsAtPriorityTPriorityList()
+ {
+ $items = $this->plist->itemsAtPriority();
+
+ $this->assertEquals(2, count($items));
+ $this->assertEquals($this->pitem2, $items[1]);
+
+
+ $items = $this->plist->itemsAtPriority(100);
+
+ $this->assertEquals(1, count($items));
+ $this->assertEquals($this->pitem3, $items[0]);
+
+ }
+
+ public function testItemAtTPriorityList()
+ {
+ $this->assertTrue($this->plist->itemAt(0) === $this->pfirst);
+ $this->assertTrue($this->plist->itemAt(1) === $this->pitem1);
+ $this->assertTrue($this->plist->itemAt(2) === $this->pitem2);
+ $this->assertTrue($this->plist->itemAt(3) === $this->pitem3);
+ }
+
+ public function testAddTPriorityList()
+ {
+ $plist = new TPriorityList($this->plist);
+
+ $plist->add($this->pitem3, 200);
+ $this->assertEquals(200, $plist->priorityAt(4));
+
+ // try a negative precision and a different default priority
+ $list = new TPriorityList(null, false, 256, -1);
+
+ $this->assertEquals(260, $list->getDefaultPriority());
+ $this->assertEquals(-1, $list->getPrecision());
+ $list->add(-10);
+ $list->add(-11, 255);
+ $list->add(-12, 250);
+ $list->add(-13, 201);
+ $this->assertEquals(200, $list->priorityAt(0));
+ $this->assertEquals(250, $list->priorityAt(1));
+ $this->assertEquals(260, $list->priorityAt(2));
+ $this->assertEquals(260, $list->priorityAt(3));
+
+ $priorities = $list->getPriorities();
+ $this->assertEquals(3, count($priorities));
+ $this->assertEquals(200, $priorities[0]);
+ $this->assertEquals(250, $priorities[1]);
+ $this->assertEquals(260, $priorities[2]);
+
+ // try a negative precision and a different default priority
+ $list = new TPriorityList(null, false, 0, 4);
+
+ $this->assertEquals(0, $list->getDefaultPriority());
+ $this->assertEquals(4, $list->getPrecision());
+ $list->add(-10);
+ $list->add(-11, 0.0001);
+ $list->add(-12, 0.00001);
+ $list->add(-13, 0.001);
+ $this->assertEquals(0, $list->priorityAt(0));
+ $this->assertEquals(0, $list->priorityAt(1));
+ $this->assertEquals(0.0001, $list->priorityAt(2));
+ $this->assertEquals(0.001, $list->priorityAt(3));
+
+ $priorities = $list->getPriorities();
+ $this->assertEquals(3, count($priorities));
+ $this->assertEquals(0, $priorities[0]);
+ $this->assertEquals(0.0001, $priorities[1]);
+ $this->assertEquals(0.001, $priorities[2]);
+ }
+
+ public function testInsertAtTPriorityList()
+ {
+ $plist = new TPriorityList($this->plist);
+ $this->assertNull($plist->insertAt(0, $this->pitem3));
+ $this->assertEquals(-10000000, $plist->priorityAt(0));
+ try {
+ $plist->insertAt(5, $this->pitem3);
+ $this->fail('exception not raised when adding item at an out-of-range index');
+ }
+ catch (TInvalidDataValueException $e) {
+ }
+ $this->assertEquals(100, $plist->priorityAt(4));
+ }
+
+ public function testInsertBeforeTPriorityList()
+ {
+ $plist = new TPriorityList($this->plist);
+
+ $this->assertEquals(4, $plist->getCount());
+ $plist->insertBefore($this->pitem3, $this->pitem4);
+ $this->assertEquals(100, $plist->priorityOf($this->pitem4));
+ }
+
+ public function testInsertAfterTPriorityList()
+ {
+ $plist = new TPriorityList($this->plist);
+
+ $this->assertEquals(4, $plist->getCount());
+ $plist->insertAfter($this->pfirst, $this->pitem4);
+ $this->assertEquals(-10000000, $plist->priorityOf($this->pitem4));
+ }
+
+ public function testRemoveTPriorityList()
+ {
+ $plist = new TPriorityList($this->plist);
+
+ $this->assertEquals(2, $plist->remove($this->pitem2));
+ $this->assertEquals(1, $plist->getPriorityCount());
+ }
+
+ public function testRemoveAtTPriorityList()
+ {
+ $plist = new TPriorityList($this->plist);
+
+ $this->assertEquals($this->pitem1, $plist->removeAt(1));
+ $this->assertEquals(-1, $plist->indexOf($this->pitem1));
+ $this->assertEquals(1, $plist->indexOf($this->pitem2));
+ $this->assertEquals(0, $plist->indexOf($this->pfirst));
+ try {
+ $plist->removeAt(3);
+ $this->fail('exception not raised when removing item with invalid index');
+ }
+ catch (TInvalidDataValueException $e) {
+ }
+ }
+
+ public function testRemoveFromPriorityTPriorityList()
+ {
+ $plist = new TPriorityList($this->plist);
+
+ try {
+ $plist->removeFromPriority($this->pitem5);
+ $this->fail('Exception not raised: TInvalidDataValueException: The item cannot be found in the list');
+ }
+ catch (TInvalidDataValueException $v) {
+ }
+
+ try {
+ $plist->removeFromPriority($this->pitem3);
+ $this->fail('Exception not raised: TInvalidDataValueException: The item cannot be found in the list');
+ }
+ catch (TInvalidDataValueException $v) {
+ }
+
+ try {
+ $plist->removeFromPriority($this->pitem3, null);
+ $this->fail('Exception not raised: TInvalidDataValueException: The item cannot be found in the list');
+ }
+ catch (TInvalidDataValueException $v) {
+ }
+
+ try {
+ $plist->removeFromPriority($this->pitem1, 100);
+ $this->fail('Exception not raised: TInvalidDataValueException: The item cannot be found in the list');
+ }
+ catch (TInvalidDataValueException $v) {
+ }
+
+ $plist->insertBefore($this->pitem3, $this->pitem4);
+ $this->assertEquals(1, $plist->removeFromPriority($this->pitem3, 100));
+
+
+ }
+
+ public function testItemAtIndexPriorityTPriorityList()
+ {
+ $this->assertEquals($this->pitem2, $this->plist->itemAtIndexInPriority(1));
+ $this->assertEquals($this->pitem1, $this->plist->itemAtIndexInPriority(0, $this->plist->getDefaultPriority()));
+ $this->assertEquals($this->pfirst, $this->plist->itemAtIndexInPriority(0, -10000000));
+ $this->assertEquals($this->pitem3, $this->plist->itemAtIndexInPriority(0, 100));
+ }
+
+
+ public function testInsertAtIndexInPriorityTPriorityList()
+ {
+ $plist = new TPriorityList();
+
+ $plist->insertAtIndexInPriority(3);
+ $this->assertEquals(array(
+ 3
+ ), $plist->toArray());
+
+ $plist->insertAtIndexInPriority(4, false);
+ $this->assertEquals(array(
+ 3,4
+ ), $plist->toArray());
+ $plist->insertAtIndexInPriority(5, 2);
+ $this->assertEquals(array(
+ 3,4,5
+ ), $plist->toArray());
+
+ $plist->insertAtIndexInPriority(6, false, null);
+ $this->assertEquals(array(
+ 3,4,5,6
+ ), $plist->toArray());
+ $plist->insertAtIndexInPriority(7, 5, null);
+ $this->assertEquals(array(
+ 3,4,5,6,7
+ ), $plist->toArray());
+
+ $plist->insertAtIndexInPriority(8, false, 10);
+ $this->assertEquals(array(
+ 3,4,5,6,7,8
+ ), $plist->toArray());
+ $plist->insertAtIndexInPriority(9, 7, 10);
+ $this->assertEquals(array(
+ 3,4,5,6,7,8,9
+ ), $plist->toArray());
+
+ $plist->insertAtIndexInPriority(10, false, 100);
+ $this->assertEquals(array(
+ 3,4,5,6,7,8,9,10
+ ), $plist->toArray());
+ $plist->insertAtIndexInPriority(11, 1, 100);
+ $this->assertEquals(array(
+ 3,4,5,6,7,8,9,10,11
+ ), $plist->toArray());
+
+ $plist = new TPriorityList();
+
+ $plist->insertAtIndexInPriority(3, false, null, true);
+ $this->assertEquals(array(
+ 3
+ ), $plist->toArray());
+
+ $plist->insertAtIndexInPriority(4, false, null, true);
+ $this->assertEquals(array(
+ 3,4
+ ), $plist->toArray());
+ $plist->insertAtIndexInPriority(5, 2, null, true);
+ $this->assertEquals(array(
+ 3,4,5
+ ), $plist->toArray());
+
+ $plist->insertAtIndexInPriority(6, false, null, true);
+ $this->assertEquals(array(
+ 3,4,5,6
+ ), $plist->toArray());
+ $plist->insertAtIndexInPriority(7, 5, null, true);
+ $this->assertEquals(array(
+ 3,4,5,6,7
+ ), $plist->toArray());
+
+ $plist->insertAtIndexInPriority(8, false, 10, true);
+ $this->assertEquals(array(
+ 3,4,5,6,7,8
+ ), $plist->toArray());
+ $plist->insertAtIndexInPriority(9, 7, 10, true);
+ $this->assertEquals(array(
+ 3,4,5,6,7,8,9
+ ), $plist->toArray());
+
+ $plist->insertAtIndexInPriority(10, false, 100, true);
+ $this->assertEquals(array(
+ 3,4,5,6,7,8,9,10
+ ), $plist->toArray());
+ $plist->insertAtIndexInPriority(11, 1, 100, true);
+ $this->assertEquals(array(
+ 3,4,5,6,7,8,9,10,11
+ ), $plist->toArray());
+
+ $plist = new TPriorityList();
+
+ $plist->insertAtIndexInPriority(3, false, null, false);
+ $this->assertEquals(array(
+ 3
+ ), $plist->toArray());
+
+ $plist->insertAtIndexInPriority(4, false, null, false);
+ $this->assertEquals(array(
+ 3,4
+ ), $plist->toArray());
+ $plist->insertAtIndexInPriority(5, 2, null, false);
+ $this->assertEquals(array(
+ 3,4,5
+ ), $plist->toArray());
+
+ $plist->insertAtIndexInPriority(6, false, null, false);
+ $this->assertEquals(array(
+ 3,4,5,6
+ ), $plist->toArray());
+ $plist->insertAtIndexInPriority(7, 5, null, false);
+ $this->assertEquals(array(
+ 3,4,5,6,7
+ ), $plist->toArray());
+
+ $plist->insertAtIndexInPriority(8, false, 10, false);
+ $this->assertEquals(array(
+ 3,4,5,6,7,8
+ ), $plist->toArray());
+ $plist->insertAtIndexInPriority(9, 7, 10, false);
+ $this->assertEquals(array(
+ 3,4,5,6,7,8,9
+ ), $plist->toArray());
+
+ $plist->insertAtIndexInPriority(10, false, 100, false);
+ $this->assertEquals(array(
+ 3,4,5,6,7,8,9,10
+ ), $plist->toArray());
+ $plist->insertAtIndexInPriority(11, 1, 100, false);
+ $this->assertEquals(array(
+ 3,4,5,6,7,8,9,10,11
+ ), $plist->toArray());
+ }
+
+ public function testCanNotInsertAtIndexInPriorityWhenReadOnlyTList()
+ {
$list = new TPriorityList(array(), true);
try {
- $list->insertAt(1, 2);
- } catch(TInvalidOperationException $e) {
- return;
+ $list->insertAtIndexInPriority(1);
+ self::fail('An expected TInvalidOperationException was not raised');
+ }
+ catch (TInvalidOperationException $e) {
}
- self::fail('An expected TInvalidOperationException was not raised');
}
-
- public function testRemovePriorities() {
- $this->assertEquals(1,$this->plist->remove($this->pitem1));
- $this->assertEquals(3,$this->plist->getCount());
- $this->assertEquals(-1,$this->plist->indexOf($this->pitem1));
- $this->assertEquals(1,$this->plist->indexOf($this->pitem2));
- $this->assertEquals(2,$this->plist->indexOf($this->pitem3));
- 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);
+
+
+ public function testRemoveAtIndexInPriorityTPriorityList()
+ {
+ $plist = new TPriorityList($this->plist);
try {
- $list->removeAt(2);
- } catch(TInvalidOperationException $e) {
- return;
+ $plist->removeAtIndexInPriority(1, 100);
+ $this->fail('TInvalidDataValueException not raised when accessing item with out-of-range index');
}
- self::fail('An expected TInvalidOperationException was not raised');
+ catch (TInvalidDataValueException $e) {
+ }
+ $this->assertEquals($this->pitem2, $plist->removeAtIndexInPriority(1));
+ $this->assertEquals($this->pitem3, $plist->removeAtIndexInPriority(0, 100));
+ $this->assertEquals($this->pitem1, $plist->removeAtIndexInPriority(0, 10));
+ try {
+ $plist->removeAtIndexInPriority(0, 200);
+ $this->fail('TInvalidDataValueException not raised when accessing item with out-of-range index');
+ }
+ catch (TInvalidDataValueException $e) {
+ }
+ $this->assertEquals($this->pfirst, $plist->removeAtIndexInPriority(0, -10000000));
+ $this->assertEquals(0, $plist->getCount());
+
}
-
- 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 testCanNotRemoveAtIndexInPriorityWhenReadOnlyTList()
+ {
+ $plist = new TPriorityList($this->plist, true);
+ try {
+ $plist->removeAtIndexInPriority(0);
+ self::fail('An expected TInvalidOperationException was not raised');
+ }
+ catch (TInvalidOperationException $e) {
+ }
}
-
- public function testOffsetSetReplacePriorities() {
- $list = new TPriorityList(array(1, 2, 3));
+
+ public function testPriorityOfTPriorityList()
+ {
+ $this->assertEquals(10, $this->plist->priorityOf($this->pitem1));
+ $this->assertEquals(100, $this->plist->priorityOf($this->pitem3));
+
+ $priority = $this->plist->priorityOf($this->pfirst, true);
+
+ $this->assertEquals(-10000000, $priority[0]);
+ $this->assertEquals(0, $priority[1]);
+ $this->assertEquals(0, $priority[2]);
+
+ $this->assertEquals(-10000000, $priority['priority']);
+ $this->assertEquals(0, $priority['index']);
+ $this->assertEquals(0, $priority['absindex']);
+
+ $priority = $this->plist->priorityOf($this->pitem2, true);
+
+ $this->assertEquals(10, $priority[0]);
+ $this->assertEquals(1, $priority[1]);
+ $this->assertEquals(2, $priority[2]);
+
+ $this->assertEquals(10, $priority['priority']);
+ $this->assertEquals(1, $priority['index']);
+ $this->assertEquals(2, $priority['absindex']);
+ }
+ public function testPriorityAtTPriorityList()
+ {
+ $this->assertEquals(10, $this->plist->priorityAt(2));
+ $this->assertEquals(100, $this->plist->priorityAt(3));
+
+ $priority = $this->plist->priorityAt(0, true);
+
+ $this->assertEquals(-10000000, $priority[0]);
+ $this->assertEquals(0, $priority[1]);
+ $this->assertEquals(0, $priority[2]);
+
+ $this->assertEquals(-10000000, $priority['priority']);
+ $this->assertEquals(0, $priority['index']);
+ $this->assertEquals(0, $priority['absindex']);
+
+ $priority = $this->plist->priorityAt(2, true);
+
+ $this->assertEquals(10, $priority[0]);
+ $this->assertEquals(1, $priority[1]);
+ $this->assertEquals(2, $priority[2]);
+
+ $this->assertEquals(10, $priority['priority']);
+ $this->assertEquals(1, $priority['index']);
+ $this->assertEquals(2, $priority['absindex']);
+ }
+
+
+ public function testGetPrioritiesTPriorityList()
+ {
+ $priorities = $this->plist->getPriorities();
+ $this->assertEquals(3, count($priorities));
+ $this->assertEquals(-10000000, $priorities[0]);
+ $this->assertEquals(10, $priorities[1]);
+ $this->assertEquals(100, $priorities[2]);
+ }
+
+ public function testClearTPriorityList()
+ {
+ $plist = new TPriorityList($this->plist);
+ $plist->clear();
+ $this->assertEquals(0, $plist->getCount());
+ $this->assertEquals(-1, $plist->indexOf($this->pitem1));
+ $this->assertEquals(-1, $plist->indexOf($this->pitem3));
+ }
+
+ public function testContainTPriorityLists()
+ {
+ $plist = new TPriorityList($this->plist);
+ $this->assertTrue($plist->contains($this->pfirst));
+ $this->assertTrue($plist->contains($this->pitem1));
+ $this->assertTrue($plist->contains($this->pitem2));
+ $this->assertTrue($plist->contains($this->pitem3));
+ $this->assertFalse($plist->contains($this->pitem5));
+ }
+
+ public function testIndexOfTPriorityList()
+ {
+ $plist = new TPriorityList($this->plist);
+ $this->assertEquals(0, $plist->indexOf($this->pfirst));
+ $this->assertEquals(1, $plist->indexOf($this->pitem1));
+ $this->assertEquals(2, $plist->indexOf($this->pitem2));
+ $this->assertEquals(3, $plist->indexOf($this->pitem3));
+ $this->assertEquals(-1, $plist->indexOf($this->pitem4));
+ }
+
+ public function testCopyFromTPriorityList()
+ {
+ $plist = new TPriorityList();
+ $plist->copyFrom($this->plist);
+ $this->assertEquals(0, $plist->indexOf($this->pfirst));
+ $this->assertEquals(1, $plist->indexOf($this->pitem1));
+ $this->assertEquals(2, $plist->indexOf($this->pitem2));
+ $this->assertEquals(3, $plist->indexOf($this->pitem3));
+ $this->assertEquals(-10000000, $plist->priorityOf($this->pfirst));
+ $this->assertEquals(10, $plist->priorityOf($this->pitem1));
+ $this->assertEquals(10, $plist->priorityOf($this->pitem2));
+ $this->assertEquals(100, $plist->priorityOf($this->pitem3));
+ $this->assertEquals(-1, $plist->indexOf($this->pitem4));
+ }
+
+ public function testMergeWithTPriorityList()
+ {
+ $plist = new TPriorityList(array(
+ $this->item3,$this->item1
+ ));
+ $plist->mergeWith($this->plist);
+ $this->assertEquals(6, $plist->getCount());
+ $this->assertEquals(0, $plist->indexOf($this->pfirst));
+ $this->assertEquals(1, $plist->indexOf($this->item3));
+ $this->assertEquals(2, $plist->indexOf($this->item1));
+ $this->assertEquals(3, $plist->indexOf($this->pitem1));
+ $this->assertEquals(4, $plist->indexOf($this->pitem2));
+ $this->assertEquals(5, $plist->indexOf($this->pitem3));
+ $this->assertEquals(-10000000, $plist->priorityOf($this->pfirst));
+ $this->assertEquals(10, $plist->priorityOf($this->item3));
+ $this->assertEquals(10, $plist->priorityOf($this->item1));
+ $this->assertEquals(10, $plist->priorityOf($this->pitem1));
+ $this->assertEquals(10, $plist->priorityOf($this->pitem2));
+ $this->assertEquals(100, $plist->priorityOf($this->pitem3));
+ $this->assertEquals(-1, $plist->indexOf($this->pitem4));
+ }
+
+ public function testToArrayTPriorityList()
+ {
+ $array = $this->plist->toArray();
+ $this->assertEquals(4, count($array));
+ $this->assertEquals($this->pfirst, $array[0]);
+ $this->assertEquals($this->pitem1, $array[1]);
+ $this->assertEquals($this->pitem2, $array[2]);
+ $this->assertEquals($this->pitem3, $array[3]);
+ }
+
+ public function testToPriorityArrayTPriorityList()
+ {
+ $array = $this->plist->toPriorityArray();
+ $this->assertEquals(3, count($array));
+ $this->assertEquals(1, count($array[-10000000]));
+ $this->assertEquals($this->pfirst, $array[-10000000][0]);
+ $this->assertEquals(2, count($array[10]));
+ $this->assertEquals($this->pitem1, $array[10][0]);
+ $this->assertEquals($this->pitem2, $array[10][1]);
+ $this->assertEquals(1, count($array[100]));
+ $this->assertEquals($this->pitem3, $array[100][0]);
+ }
+
+ public function testArrayReadTPriorityList()
+ {
+ $this->assertTrue($this->plist[0] === $this->pfirst);
+ $this->assertTrue($this->plist[1] === $this->pitem1);
+ $this->assertTrue($this->plist[2] === $this->pitem2);
+ $this->assertTrue($this->plist[3] === $this->pitem3);
+ try {
+ $a = $this->plist[4];
+ $this->fail('exception not raised when accessing item with out-of-range index');
+ }
+ catch (TInvalidDataValueException $e) {
+ }
+ }
+
+ public function testGetIteratorTPriorityList()
+ {
+ $n = 0;
+ $found = 0;
+
+ foreach ($this->list as $a => $b); // test of iterator
+
+ foreach ($this->plist as $index => $item) {
+ $n++;
+ if ($index === 0 && $item === $this->pfirst)
+ $found++;
+ if ($index === 1 && $item === $this->pitem1)
+ $found++;
+ if ($index === 2 && $item === $this->pitem2)
+ $found++;
+ if ($index === 3 && $item === $this->pitem3)
+ $found++;
+ }
+ $this->assertTrue($n == 4 && $found == 4);
+ }
+
+ public function testArrayMiscTPriorityList()
+ {
+ $this->assertEquals($this->plist->Count, count($this->plist));
+ $this->assertTrue(isset($this->plist[0]));
+ $this->assertTrue(isset($this->plist[1]));
+ $this->assertTrue(isset($this->plist[2]));
+ $this->assertTrue(isset($this->plist[3]));
+ $this->assertFalse(isset($this->plist[4]));
+ }
+
+ public function testOffsetSetAddTPriorityList()
+ {
+ $list = new TPriorityList();
+ $list->add(2);
+ $list->add(1, 5);
+ $list->add(3, 15);
+ $list->offsetSet(null, 4); // Appending like this, items get the default priority; not linear behavior
+ self::assertEquals(array(
+ 1,2,4,3
+ ), $list->toArray());
+ }
+
+ public function testOffsetSetReplaceTPriorityList()
+ {
+ $list = new TPriorityList();
+ $list->add(2);
+ $list->add(1, 5);
+ $list->add(3, 15);
$list->offsetSet(1, 4);
- self::assertEquals(array(1, 4, 3), $list->toArray());
+ 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());
+ public function testOffsetSetAppendTPriorityList()
+ {
+ $list = new TPriorityList();
+ $list->add(2);
+ $list->add(1, 5);
+ $list->add(3, 15);
+ $list->offsetSet(3, 4);
+ self::assertEquals(array(
+ 1,2,3,4
+ ), $list->toArray());
}
+ public function testOffsetUnsetTPriorityList()
+ {
+ $list = new TPriorityList();
+ $list->add(2);
+ $list->add(1, 5);
+ $list->add(3, 15);
+ $list->offsetUnset(1);
+ self::assertEquals(array(
+ 1,3
+ ), $list->toArray());
+ }
} \ No newline at end of file