From 60b0b651562ea2ecf918ab09e0203065da86774f Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Thu, 21 Jan 2016 21:13:51 +0100 Subject: [PATCH 01/19] Remove wrapper types --- src/main/php/lang/types/ArrayList.class.php | 199 --------- src/main/php/lang/types/ArrayMap.class.php | 175 -------- src/main/php/lang/types/Boolean.class.php | 95 ----- src/main/php/lang/types/Byte.class.php | 25 -- src/main/php/lang/types/Bytes.class.php | 180 -------- src/main/php/lang/types/Character.class.php | 110 ----- src/main/php/lang/types/Double.class.php | 23 -- src/main/php/lang/types/Float.class.php | 23 -- src/main/php/lang/types/Integer.class.php | 25 -- src/main/php/lang/types/Long.class.php | 25 -- src/main/php/lang/types/Number.class.php | 84 ---- src/main/php/lang/types/Short.class.php | 25 -- src/main/php/lang/types/String.class.php | 339 --------------- src/main/php/lang/types/package-info.xp | 39 -- src/test/config/unittest/not-on-php-7.0.ini | 47 --- src/test/config/unittest/types.ini | 16 - .../core/types/ArrayListTest.class.php | 234 ----------- .../core/types/ArrayMapTest.class.php | 176 -------- .../unittest/core/types/BooleanTest.class.php | 103 ----- .../unittest/core/types/BytesTest.class.php | 391 ------------------ .../core/types/CharacterTest.class.php | 84 ---- .../unittest/core/types/NumberTest.class.php | 219 ---------- .../unittest/core/types/StringTest.class.php | 352 ---------------- .../PrimitiveAndWrappersTest.class.php | 195 --------- 24 files changed, 3184 deletions(-) delete mode 100755 src/main/php/lang/types/ArrayList.class.php delete mode 100755 src/main/php/lang/types/ArrayMap.class.php delete mode 100755 src/main/php/lang/types/Boolean.class.php delete mode 100755 src/main/php/lang/types/Byte.class.php delete mode 100755 src/main/php/lang/types/Bytes.class.php delete mode 100755 src/main/php/lang/types/Character.class.php delete mode 100755 src/main/php/lang/types/Double.class.php delete mode 100755 src/main/php/lang/types/Float.class.php delete mode 100755 src/main/php/lang/types/Integer.class.php delete mode 100755 src/main/php/lang/types/Long.class.php delete mode 100755 src/main/php/lang/types/Number.class.php delete mode 100755 src/main/php/lang/types/Short.class.php delete mode 100755 src/main/php/lang/types/String.class.php delete mode 100644 src/main/php/lang/types/package-info.xp delete mode 100755 src/test/config/unittest/not-on-php-7.0.ini delete mode 100644 src/test/config/unittest/types.ini delete mode 100755 src/test/php/net/xp_framework/unittest/core/types/ArrayListTest.class.php delete mode 100755 src/test/php/net/xp_framework/unittest/core/types/ArrayMapTest.class.php delete mode 100755 src/test/php/net/xp_framework/unittest/core/types/BooleanTest.class.php delete mode 100755 src/test/php/net/xp_framework/unittest/core/types/BytesTest.class.php delete mode 100755 src/test/php/net/xp_framework/unittest/core/types/CharacterTest.class.php delete mode 100755 src/test/php/net/xp_framework/unittest/core/types/NumberTest.class.php delete mode 100755 src/test/php/net/xp_framework/unittest/core/types/StringTest.class.php delete mode 100755 src/test/php/net/xp_framework/unittest/reflection/PrimitiveAndWrappersTest.class.php diff --git a/src/main/php/lang/types/ArrayList.class.php b/src/main/php/lang/types/ArrayList.class.php deleted file mode 100755 index f74e22508c..0000000000 --- a/src/main/php/lang/types/ArrayList.class.php +++ /dev/null @@ -1,199 +0,0 @@ - values or an int => size - * @return lang.types.ArrayList - */ - public static function newInstance($arg) { - if (is_array($arg)) { - $self= new self(); - $self->values= array_values($arg); - $self->length= sizeof($self->values); - } else { - $self= new self(); - $self->length= (int)$arg; - } - return $self; - } - - /** - * Returns a hashcode for this number - * - * @return string - */ - public function hashCode() { - return $this->length.'['.serialize($this->values); - } - - /** - * Constructor - * - * @param var* values - */ - public function __construct() { - if (0 != ($this->length= func_num_args())) { - $this->values= func_get_args(); - } - } - - /** - * Returns an iterator for use in foreach() - * - * @see php://language.oop5.iterations - * @return php.Iterator - */ - public function getIterator() { - return new \ArrayIterator($this->values); - } - - /** - * = list[] overloading - * - * @param int offset - * @return var - * @throws lang.IndexOutOfBoundsException if key does not exist - */ - public function offsetGet($offset) { - if ($offset >= $this->length || $offset < 0) { - throw new IndexOutOfBoundsException('Offset '.$offset.' out of bounds'); - } - return $this->values[$offset]; - } - - /** - * list[]= overloading - * - * @param int offset - * @param var value - * @throws lang.IllegalArgumentException if key is neither numeric (set) nor NULL (add) - */ - public function offsetSet($offset, $value) { - if (!is_int($offset)) { - throw new IllegalArgumentException('Incorrect type '.gettype($offset).' for index'); - } - - if ($offset >= $this->length || $offset < 0) { - throw new IndexOutOfBoundsException('Offset '.$offset.' out of bounds'); - } - $this->values[$offset]= $value; - } - - /** - * isset() overloading - * - * @param int offset - * @return bool - */ - public function offsetExists($offset) { - return ($offset >= 0 && $offset < $this->length); - } - - /** - * unset() overloading - * - * @param int offset - */ - public function offsetUnset($offset) { - throw new IllegalArgumentException('Cannot remove from immutable list'); - } - - /** - * Get a value - * - * @param int offset - * @param var default - * @return var - */ - public function get($offset, $default= null) { - return isset($this->values[$offset]) ? $this->values[$offset] : $default; - } - - /** - * Returns whether a given value exists in this list - * - * @param var value - * @return bool - */ - public function contains($value) { - if ($value instanceof Generic) { - foreach ($this->values as $v) { - if ($value->equals($v)) return true; - } - return false; - } else if ($value instanceof Value) { - foreach ($this->values as $v) { - if (0 === $value->compareTo($v)) return true; - } - return false; - } else { - return in_array($value, $this->values, true); - } - return false; - } - - /** - * Helper method to compare two arrays recursively - * - * @param [:var] $a - * @param [:var] $b - * @return bool - */ - protected function arrayequals($a1, $a2) { - if (sizeof($a1) !== sizeof($a2)) return false; - foreach ($a1 as $key => $value) { - if (!array_key_exists($key, $a2)) { - return false; - } else if (is_array($value)) { - if (!$this->arrayequals($value, $a2[$key])) return false; - } else if ($value instanceof Value) { - if (0 !== $value->compareTo($a2[$key])) return false; - } else if ($value instanceof Generic) { - if (!$value->equals($a2[$key])) return false; - } else if ($value !== $a2[$key]) { - return false; - } - } - return true; - } - - /** - * Checks whether a given object is equal to this arraylist - * - * @param lang.Object cmp - * @return bool - */ - public function equals($cmp) { - return $cmp instanceof self && $this->arrayequals($this->values, $cmp->values); - } - - /** - * Returns a string representation of this object - * - * @return string - */ - public function toString() { - return ( - nameof($this).'['.sizeof($this->values)."]@{". - implode(', ', array_map(['xp', 'stringOf'], $this->values)). - '}' - ); - } -} diff --git a/src/main/php/lang/types/ArrayMap.class.php b/src/main/php/lang/types/ArrayMap.class.php deleted file mode 100755 index 568ad965cf..0000000000 --- a/src/main/php/lang/types/ArrayMap.class.php +++ /dev/null @@ -1,175 +0,0 @@ -size.'{'.serialize($this->values); - } - - /** - * Constructor - * - * @param [:var] values - */ - public function __construct(array $values) { - $this->values= $values; - $this->size= sizeof($this->values); - } - - /** - * Returns an iterator for use in foreach() - * - * @see php://language.oop5.iterations - * @return php.Iterator - */ - public function getIterator() { - return new \ArrayIterator($this->values); - } - - /** - * = list[] overloading - * - * @param int key - * @return var - * @throws lang.IndexOutOfBoundsException if key does not exist - */ - public function offsetGet($key) { - if (!isset($this->values[$key])) { - throw new IndexOutOfBoundsException('No element for key "'.$key.'"'); - } - return $this->values[$key]; - } - - /** - * list[]= overloading - * - * @param string $key - * @param var $value - * @throws lang.IllegalArgumentException if key is neither a string (set) nor NULL (add) - */ - public function offsetSet($key, $value) { - if (!is_string($key)) { - throw new IllegalArgumentException('Incorrect type '.gettype($key).' for index'); - } - $this->values[$key]= $value; - } - - /** - * isset() overloading - * - * @param string $key - * @return bool - */ - public function offsetExists($key) { - return array_key_exists($key, $this->values); - } - - /** - * unset() overloading - * - * @param string $key - */ - public function offsetUnset($key) { - unset($this->values[$key]); - } - - /** - * Get a value - * - * @param string key - * @param var default - * @return var - */ - public function get($key, $default= null) { - return isset($this->values[$key]) ? $this->values[$key] : $default; - } - - /** - * Returns whether a given value exists in this list - * - * @param var value - * @return bool - */ - public function contains($value) { - if ($value instanceof Generic) { - foreach ($this->values as $v) { - if ($value->equals($v)) return true; - } - return false; - } else if ($value instanceof Value) { - foreach ($this->values as $v) { - if (0 === $value->compareTo($v)) return true; - } - return false; - } else { - return in_array($value, $this->values, true); - } - } - - /** - * Helper method to compare two arrays recursively - * - * @param [:var] $a - * @param [:var] $b - * @return bool - */ - protected function arrayequals($a1, $a2) { - if (sizeof($a1) !== sizeof($a2)) return false; - foreach ($a1 as $key => $value) { - if (!array_key_exists($key, $a2)) { - return false; - } else if (is_array($value)) { - if (!$this->arrayequals($value, $a2[$key])) return false; - } else if ($value instanceof Value) { - if (0 !== $value->compareTo($a2[$key])) return false; - } else if ($value instanceof Generic) { - if (!$value->equals($a2[$key])) return false; - } else if ($value !== $a2[$key]) { - return false; - } - } - return true; - } - - /** - * Checks whether a given object is equal to this arraylist - * - * @param lang.Object cmp - * @return bool - */ - public function equals($cmp) { - return $cmp instanceof self && $this->arrayequals($this->values, $cmp->values); - } - - /** - * Returns a string representation of this object - * - * @return string - */ - public function toString() { - $r= ''; - foreach ($this->values as $key => $value) { - $r.= ', '.$key.' = '.\xp::stringOf($value); - } - return nameof($this).'['.sizeof($this->values).']@{'.substr($r, 2).'}'; - } -} diff --git a/src/main/php/lang/types/Boolean.class.php b/src/main/php/lang/types/Boolean.class.php deleted file mode 100755 index d115faf717..0000000000 --- a/src/main/php/lang/types/Boolean.class.php +++ /dev/null @@ -1,95 +0,0 @@ - - *
  • The values TRUE or FALSE
  • - *
  • An integer - any non-zero value will be regarded TRUE
  • - *
  • The strings "true" and "false", case-insensitive
  • - *
  • Numeric strings - any non-zero value will be regarded TRUE
  • - * - * - * @param var value - * @throws lang.IllegalArgumentException if value is not acceptable - */ - public function __construct($value) { - if (true === $value || false === $value) { - $this->value= $value; - } else if (is_int($value)) { - $this->value= 0 !== $value; - } else if ('0' === $value) { - $this->value= false; - } else if (is_string($value) && ($l= strlen($value)) && strspn($value, '1234567890') === $l) { - $this->value= true; - } else if (0 === strncasecmp($value, 'true', 4)) { - $this->value= true; - } else if (0 === strncasecmp($value, 'false', 5)) { - $this->value= false; - } else { - throw new \lang\IllegalArgumentException('Not a valid boolean: '.\xp::stringOf($value)); - } - } - - /** - * ValueOf factory - * - * @param string $value - * @return self - */ - public static function valueOf($value) { - return new self($value); - } - - /** - * Returns the value of this number as an int. - * - * @return int - */ - public function intValue() { - return (int)$this->value; - } - - /** - * Returns a hashcode for this number - * - * @return string - */ - public function hashCode() { - return $this->value ? 'true' : 'false'; - } - - /** - * Returns a string representation of this number object - * - * @return string - */ - public function toString() { - return nameof($this).'('.($this->value ? 'true' : 'false').')'; - } - - /** - * Indicates whether some other object is "equal to" this one. - * - * @param lang.Object cmp - * @return bool TRUE if the compared object is equal to this object - */ - public function equals($cmp) { - return $cmp instanceof self && $this->value === $cmp->value; - } -} diff --git a/src/main/php/lang/types/Byte.class.php b/src/main/php/lang/types/Byte.class.php deleted file mode 100755 index eeced7163e..0000000000 --- a/src/main/php/lang/types/Byte.class.php +++ /dev/null @@ -1,25 +0,0 @@ -value) : $in{0}) - ; - } - - /** - * Constructor - * - * @param var initial default NULL - * @throws lang.IllegalArgumentException in case argument is of incorrect type. - */ - public function __construct($initial= null) { - if (null === $initial) { - // Intentionally empty - } else if (is_array($initial)) { - $this->buffer= implode('', array_map([$this, 'asByte'], $initial)); - } else if (is_string($initial)) { - $this->buffer= $initial; - } else { - throw new \lang\IllegalArgumentException('Expected either Byte[], char[], int[] or string but was '.\xp::typeOf($initial)); - } - $this->size= strlen($this->buffer); - } - - /** - * Returns an iterator for use in foreach() - * - * @see php://language.oop5.iterations - * @return php.Iterator - */ - public function getIterator() { - if (!$this->iterator) $this->iterator= newinstance('Iterator', [$this], '{ - private $i= 0, $v; - public function __construct($v) { $this->v= $v; } - public function current() { $n= ord($this->v->buffer{$this->i}); return new \lang\types\Byte($n < 128 ? $n : $n - 256); } - public function key() { return $this->i; } - public function next() { $this->i++; } - public function rewind() { $this->i= 0; } - public function valid() { return $this->i < $this->v->size; } - }'); - return $this->iterator; - } - - /** - * = list[] overloading - * - * @param int offset - * @return lang.types.Byte - * @throws lang.IndexOutOfBoundsException if offset does not exist - */ - public function offsetGet($offset) { - if ($offset >= $this->size || $offset < 0) { - throw new IndexOutOfBoundsException('Offset '.$offset.' out of bounds'); - } - $n= ord($this->buffer{$offset}); - return new Byte($n < 128 ? $n : $n - 256); - } - - /** - * list[]= overloading - * - * @param int offset - * @param var value - * @throws lang.IllegalArgumentException if key is neither numeric (set) nor NULL (add) - * @throws lang.IndexOutOfBoundsException if key does not exist - */ - public function offsetSet($offset, $value) { - if (null === $offset) { - $this->buffer.= $this->asByte($value); - $this->size++; - } else if ($offset >= $this->size || $offset < 0) { - throw new IndexOutOfBoundsException('Offset '.$offset.' out of bounds'); - } else { - $this->buffer{$offset}= $this->asByte($value); - } - } - - /** - * isset() overloading - * - * @param int offset - * @return bool - */ - public function offsetExists($offset) { - return ($offset >= 0 && $offset < $this->size); - } - - /** - * unset() overloading - * - * @param int offset - * @throws lang.IndexOutOfBoundsException if offset does not exist - */ - public function offsetUnset($offset) { - if ($offset >= $this->size || $offset < 0) { - throw new IndexOutOfBoundsException('Offset '.$offset.' out of bounds'); - } - $this->buffer= ( - substr($this->buffer, 0, $offset). - substr($this->buffer, $offset+ 1, $this->size) - ); - $this->size--; - } - - /** - * Returns this byte list's size - * - * @return string - */ - public function size() { - return $this->size; - } - - /** - * Returns whether a given object is equal to this object - * - * @param lang.Generic cmp - * @return bool - */ - public function equals($cmp) { - return ( - $cmp instanceof self && - $this->size === $cmp->size && - $this->buffer === $cmp->buffer - ); - } - - /** - * Returns a hashcode for this bytes object - * - * @return string - */ - public function hashCode() { - return md5($this->buffer); - } - - /** - * Returns a string representation of this string. - * - * @return string - */ - public function toString() { - return nameof($this).'('.$this->size.')@{'.addcslashes($this->buffer, "\0..\37\177..\377").'}'; - } - - /** - * String conversion overloading. This is for use with fwrite() - * - * @return string - */ - public function __toString() { - return $this->buffer; - } -} diff --git a/src/main/php/lang/types/Character.class.php b/src/main/php/lang/types/Character.class.php deleted file mode 100755 index 8787a73ef2..0000000000 --- a/src/main/php/lang/types/Character.class.php +++ /dev/null @@ -1,110 +0,0 @@ - - * $c= new Character(8364); // The EUR symbol (U+20AC) - * $c= new Character(0x20AC); // ...same, using hexadecimal - * $c= new Character('�'); // The German Umlaut A (capital) - * - * $s= new String('�bercoder'); - * $c= $s->charAt(0); // The German Umlaut U (capital) - * $c= $s[0]; // ...same, via [] operator - * - * $c= $s->charAt(1); // "b" - * $c= $s[1]; // "b" - * - * - * @deprecated Wrapper types will move to their own library - * @ext iconv - * @test xp://net.xp_framework.unittest.core.types.CharacterTest - */ -class Character extends \lang\Object { - protected $buffer= ''; - - /** - * Constructor - * - * @param var arg either a string or an int - * @param string charset default NULL - */ - public function __construct($arg, $charset= null) { - if (is_int($arg)) { - $this->buffer= iconv('UCS-4BE', 'utf-8', pack('N', $arg)); - return; - } - - $charset= strtolower($charset ?: \xp::ENCODING); - - // Convert the input to internal encoding - $this->buffer= iconv($charset, 'utf-8', $arg); - if (\xp::errorAt(__FILE__, __LINE__ - 1)) { - $message= key(\xp::$errors[__FILE__][__LINE__ - 2]); - \xp::gc(__FILE__); - throw new \lang\FormatException($message.($charset == 'utf-8' - ? ' with charset '.$charset - : $message.' while converting input from '.$charset.' to '.'utf-8' - )); - } - - if (1 != ($l= iconv_strlen($this->buffer, 'utf-8'))) { - throw new \lang\IllegalArgumentException('Given argument is too long ('.$l.')'); - } - } - - /** - * Returns whether a given object is equal to this object - * - * @param lang.Generic cmp - * @return bool - */ - public function equals($cmp) { - return $cmp instanceof self && $this->buffer === $cmp->buffer; - } - - /** - * Returns a hashcode for this string object - * - * @return string - */ - public function hashCode() { - return $this->buffer; - } - - /** - * Returns a string representation of this string. Uses the current - * output encoding and transliteration. - * - * @return string - */ - public function toString() { - return $this->buffer; - } - - /** - * Returns a string representation of this string. Uses the current - * output encoding and transliteration. - * - * @return string - */ - public function __toString() { - return $this->buffer; - } - - /** - * Returns the bytes representing this character - * - * @param string charset default NULL - * @return lang.types.Bytes - */ - public function getBytes($charset= null) { - $charset= strtolower($charset ?: \xp::ENCODING); - - return new Bytes(\xp::ENCODING === $charset - ? $this->buffer - : iconv(\xp::ENCODING, $charset, $this->buffer) - ); - } -} diff --git a/src/main/php/lang/types/Double.class.php b/src/main/php/lang/types/Double.class.php deleted file mode 100755 index 629a041ff4..0000000000 --- a/src/main/php/lang/types/Double.class.php +++ /dev/null @@ -1,23 +0,0 @@ -value= (string)hexdec($value); - } else { - $this->value= (string)$value; - } - } - - /** - * ValueOf factory - * - * @param string $value - * @return self - * @throws lang.IllegalArgumentException - */ - public static function valueOf($value) { - throw new MethodNotImplementedException('Abstract base class', __METHOD__); - } - - /** - * Returns the value of this number as an int. - * - * @return int - */ - public function intValue() { - return $this->value + 0; - } - - /** - * Returns the value of this number as a float. - * - * @return double - */ - public function doubleValue() { - return $this->value + 0.0; - } - - /** - * Returns a hashcode for this number - * - * @return string - */ - public function hashCode() { - return $this->value; - } - - /** - * Returns a string representation of this number object - * - * @return string - */ - public function toString() { - return nameof($this).'('.$this->value.')'; - } - - /** - * Indicates whether some other object is "equal to" this one. - * - * @param lang.Object cmp - * @return bool TRUE if the compared object is equal to this object - */ - public function equals($cmp) { - return $cmp instanceof $this && $this->value === $cmp->value; - } -} diff --git a/src/main/php/lang/types/Short.class.php b/src/main/php/lang/types/Short.class.php deleted file mode 100755 index 9d20722b78..0000000000 --- a/src/main/php/lang/types/Short.class.php +++ /dev/null @@ -1,25 +0,0 @@ -buffer; - } else if ($arg instanceof Character) { - return $arg->getBytes(\xp::ENCODING)->buffer; - } else { - $charset= strtolower($charset ?: \xp::ENCODING); - - // Convert the input to internal encoding - $buffer= iconv($charset, \xp::ENCODING, $arg); - if (\xp::errorAt(__FILE__, __LINE__ - 1)) { - $message= key(\xp::$errors[__FILE__][__LINE__ - 2]); - \xp::gc(__FILE__); - throw new \lang\FormatException($message.($charset == \xp::ENCODING - ? ' with charset '.$charset - : $message.' while converting input from '.$charset.' to '.\xp::ENCODING - )); - } - return $buffer; - } - } - - /** - * Constructor - * - * @param string initial default '' - * @param string charset default NULL - */ - public function __construct($initial= '', $charset= null) { - if (null === $initial) return; - $this->buffer= $this->asIntern($initial, $charset); - $this->length= __str::len($this->buffer); - } - - /** - * = list[] overloading - * - * @param int offset - * @return lang.types.Character - * @throws lang.IndexOutOfBoundsException if key does not exist - */ - public function offsetGet($offset) { - return $this->charAt($offset); - } - - /** - * list[]= overloading - * - * @param int offset - * @param var value - * @throws lang.IllegalArgumentException if key is neither numeric (set) nor NULL (add) - */ - public function offsetSet($offset, $value) { - if (!is_int($offset)) { - throw new \lang\IllegalArgumentException('Incorrect type '.gettype($offset).' for index'); - } - - if ($offset >= $this->length || $offset < 0) { - throw new IndexOutOfBoundsException('Offset '.$offset.' out of bounds'); - } - - $char= $this->asIntern($value); - if (1 != __str::len($char)) { - throw new IllegalArgumentException('Set only allows to set one character!'); - } - - $this->buffer= ( - __str::substr($this->buffer, 0, $offset). - $char. - __str::substr($this->buffer, $offset+ 1, $this->length) - ); - } - - /** - * isset() overloading - * - * @param int offset - * @return bool - */ - public function offsetExists($offset) { - return ($offset >= 0 && $offset < $this->length); - } - - /** - * unset() overloading - * - * @param int offset - */ - public function offsetUnset($offset) { - if ($offset >= $this->length || $offset < 0) { - throw new IndexOutOfBoundsException('Offset '.$offset.' out of bounds'); - } - $this->buffer= ( - __str::substr($this->buffer, 0, $offset). - __str::substr($this->buffer, $offset+ 1, $this->length) - ); - $this->length= __str::len($this->buffer); - } - - /** - * Returns the string's length (the number of characters in this - * string, not the number of bytes) - * - * @return string - */ - public function length() { - return $this->length; - } - - /** - * Returns the character at the given position - * - * @param int offset - * @return lang.types.Character - * @throws lang.IndexOutOfBoundsException if key does not exist - */ - public function charAt($offset) { - if ($offset >= $this->length || $offset < 0) { - throw new IndexOutOfBoundsException('Offset '.$offset.' out of bounds'); - } - return new Character(__str::substr($this->buffer, $offset, 1), \xp::ENCODING); - } - - /** - * Returns the index within this string of the first occurrence of - * the specified substring. - * - * @param var arg either a string or a String - * @param int start default 0 - * @return bool - */ - public function indexOf($arg, $start= 0) { - if ('' === ($needle= $this->asIntern($arg))) return -1; - $r= __str::pos($this->buffer, $needle, $start); - return false === $r ? -1 : $r; - } - - /** - * Returns the index within this string of the last occurrence of - * the specified substring. - * - * @param var arg either a string or a String - * @return bool - */ - public function lastIndexOf($arg) { - if ('' === ($needle= $this->asIntern($arg))) return -1; - $r= __str::rpos($this->buffer, $needle); - return false === $r ? -1 : $r; - } - - /** - * Returns a new string that is a substring of this string. - * - * @param int start - * @param int length default 0 - * @return lang.types.String - */ - public function substring($start, $length= 0) { - if (0 === $length) $length= $this->length; - $self= new self(null); - $self->buffer= __str::substr($this->buffer, $start, $length); - $self->length= __str::len($self->buffer); - return $self; - } - - /** - * Returns whether a given substring is contained in this string - * - * @param var arg - * @return bool - */ - public function contains($arg) { - if ('' === ($needle= $this->asIntern($arg))) return false; - return false !== __str::pos($this->buffer, $needle, 0); - } - - /** - * Returns whether a given substring is contained in this string - * - * @param var old - * @param var new default '' - * @return lang.types.String this string - */ - public function replace($old, $new= '') { - $this->buffer= str_replace($this->asIntern($old), $this->asIntern($new), $this->buffer); - $this->length= __str::len($this->buffer); - return $this; - } - - /** - * Concatenates the given argument to the end of this string and returns - * this String so it can be used in chained calls: - * - * - * $s= new String('Hello'); - * $s->concat(' ')->concat('World'); - * - * - * @param var arg - * @return lang.types.String this string - */ - public function concat($arg) { - $this->buffer.= $this->asIntern($arg); - $this->length= __str::len($this->buffer); - return $this; - } - - /** - * Returns whether this string starts with a given argument. - * - * @param var arg either a string or a String - * @return bool - */ - public function startsWith($arg) { - $bytes= $this->asIntern($arg); - $l= strlen($bytes); - return $l > 0 && 0 === strncmp($this->buffer, $bytes, $l); - } - - /** - * Returns whether this string ends with a given argument. - * - * @param var arg either a string or a String - * @return bool - */ - public function endsWith($arg) { - $bytes= $this->asIntern($arg); - $l= strlen($bytes); - return $l > 0 && $l <= $this->length && 0 === substr_compare($this->buffer, $bytes, -$l, $l); - } - - /** - * Returns whether a given object is equal to this object - * - * @param lang.Generic cmp - * @return bool - */ - public function equals($cmp) { - return $cmp instanceof self && $this->buffer === $cmp->buffer; - } - - /** - * Returns a hashcode for this string object - * - * @return string - */ - public function hashCode() { - return md5($this->buffer); - } - - /** - * Returns a string representation of this string. Uses the current - * output encoding and transliteration. - * - * @return string - */ - public function toString() { - return $this->buffer; - } - - /** - * Returns a string representation of this string. Uses the current - * output encoding and transliteration. - * - * @return string - */ - public function __toString() { - return $this->buffer; - } - - /** - * Returns the bytes representing this string - * - * @param string charset default NULL - * @return lang.types.Bytes - */ - public function getBytes($charset= null) { - $charset= strtolower($charset ?: \xp::ENCODING); - if (\xp::ENCODING === $charset) { - return new Bytes($this->buffer); - } - $bytes= iconv(\xp::ENCODING, $charset, $this->buffer); - if (\xp::errorAt(__FILE__, __LINE__ - 1)) { - $message= key(\xp::$errors[__FILE__][__LINE__ - 2]); - \xp::gc(__FILE__); - throw new \lang\FormatException($message.' while converting input from '.\xp::ENCODING.' to '.$charset); - } - return new Bytes($bytes); - } -} diff --git a/src/main/php/lang/types/package-info.xp b/src/main/php/lang/types/package-info.xp deleted file mode 100644 index 13ccdeeccc..0000000000 --- a/src/main/php/lang/types/package-info.xp +++ /dev/null @@ -1,39 +0,0 @@ - - *
  • Byte
  • - *
  • Float
  • - *
  • Long
  • - *
  • Short
  • - *
  • Integer
  • - * - * ...and map to whatever the "remote" type is defined as. They all take - * strings as arguments to preserve the value exactly. The respective - * classes extend from the base class lang.reflect.Number. - * - * ArrayList - * ========= - * This class represents a zero-indexed list of elements of any type. - * It is used to give marshalling utilities an easy way to see whether - * an array is "numeric" or not (PHP's array types can be either numeric, - * hashes or a mix of both and aren't necessarily zero-indexed). - * - * @deprecated Wrapper types will move to their own library - * @see http://news.xp-framework.net/article/52/2005/05/29/ - * @see http://news.xp-framework.net/article/54/2005/06/12/ - * @see http://developer.xp-framework.net/xml/rfc/view?0038 - * @see xp://lang.types.Number - * @see xp://lang.types.ArrayList - */ -package lang.types { -} diff --git a/src/test/config/unittest/not-on-php-7.0.ini b/src/test/config/unittest/not-on-php-7.0.ini deleted file mode 100755 index f8306996e4..0000000000 --- a/src/test/config/unittest/not-on-php-7.0.ini +++ /dev/null @@ -1,47 +0,0 @@ -[this] -description="XP tests (not PHP7 compatible)" - -; core.ini -[primitives-and-wrappers] -class="net.xp_framework.unittest.reflection.PrimitiveAndWrappersTest" - -; types.ini -[numbers] -class="net.xp_framework.unittest.core.types.NumberTest" - -[string] -class="net.xp_framework.unittest.core.types.StringTest" - -[character] -class="net.xp_framework.unittest.core.types.CharacterTest" - -[bytes] -class="net.xp_framework.unittest.core.types.BytesTest" - -; collections.ini -[hashset] -class="net.xp_framework.unittest.util.collections.HashSetTest" - -[hashtable] -class="net.xp_framework.unittest.util.collections.HashTableTest" - -[lrubuffer] -class="net.xp_framework.unittest.util.collections.LRUBufferTest" - -[queue] -class="net.xp_framework.unittest.util.collections.QueueTest" - -[stack] -class="net.xp_framework.unittest.util.collections.StackTest" - -[vector] -class="net.xp_framework.unittest.util.collections.VectorTest" - -[generics] -class="net.xp_framework.unittest.util.collections.GenericsTest" - -[arrayaccess] -class="net.xp_framework.unittest.util.collections.ArrayAccessTest" - -[arrays] -class="net.xp_framework.unittest.util.collections.ArraysTest" diff --git a/src/test/config/unittest/types.ini b/src/test/config/unittest/types.ini deleted file mode 100644 index 762691f5a1..0000000000 --- a/src/test/config/unittest/types.ini +++ /dev/null @@ -1,16 +0,0 @@ -;; -; Wrapper type tests -; -; $Id$ - -[this] -description="Wrapper type tests" - -[arraylist] -class="net.xp_framework.unittest.core.types.ArrayListTest" - -[arraymap] -class="net.xp_framework.unittest.core.types.ArrayMapTest" - -[boolean] -class="net.xp_framework.unittest.core.types.BooleanTest" diff --git a/src/test/php/net/xp_framework/unittest/core/types/ArrayListTest.class.php b/src/test/php/net/xp_framework/unittest/core/types/ArrayListTest.class.php deleted file mode 100755 index d5f0471877..0000000000 --- a/src/test/php/net/xp_framework/unittest/core/types/ArrayListTest.class.php +++ /dev/null @@ -1,234 +0,0 @@ -assertEquals(0, (new ArrayList())->length); - } - - #[@test] - public function a_newly_created_arraylist_is_empty() { - $this->assertEquals(0, sizeof((new ArrayList())->values)); - } - - #[@test] - public function two_newly_created_arraylists_are_equal() { - $this->assertEquals(new ArrayList(), new ArrayList()); - } - - #[@test] - public function equals_with_objects() { - $neverEqual= newinstance(Object::class, [], [ - 'equals' => function($cmp) { return false; } - ]); - $this->assertNotEquals(new ArrayList($neverEqual), new ArrayList($neverEqual)); - } - - #[@test] - public function equals_with_values() { - $neverEqual= newinstance(Value::class, [], [ - 'compareTo' => function($cmp) { return -1; }, - 'hashCode' => function() { return '0xAB'; }, - 'toString' => function() { return 'test'; } - ]); - $this->assertNotEquals(new ArrayList($neverEqual), new ArrayList($neverEqual)); - } - - #[@test] - public function is_usable_in_foreach() { - foreach (new ArrayList(0, 1, 2) as $i => $value) { - $this->assertEquals($i, $value); - } - $this->assertEquals(2, $i); - } - - #[@test] - public function is_usable_in_for() { - for ($l= new ArrayList(0, 1, 2), $i= 0; $i < $l->length; $i++) { - $this->assertEquals($i, $l[$i]); - } - $this->assertEquals(3, $i); - } - - #[@test] - public function is_usable_in_nested_foreach() { - $r= ''; - foreach (new ArrayList(new ArrayList(1, 2, 3), new ArrayList(4, 5, 6)) as $i => $value) { - foreach ($value as $j => $v) { - $r.= $i.'.'.$j.':'.$v.', '; - } - } - $this->assertEquals('0.0:1, 0.1:2, 0.2:3, 1.0:4, 1.1:5, 1.2:6', substr($r, 0, -2)); - } - - #[@test] - public function inner_iteration() { - $a= new ArrayList(1, 2, 3); - $r= []; - foreach ($a as $vo) { - foreach ($a as $vi) { - $r[]= $vi; - } - } - $this->assertEquals([1, 2, 3, 1, 2, 3, 1, 2, 3], $r); - } - - #[@test, @values([0, 1, 2])] - public function array_access_operator_is_overloaded($value) { - $c= new ArrayList(1, 2, 3); - $this->assertEquals($value + 1, $c[$value]); - } - - #[@test] - public function array_access_operator_allows_reassigning() { - $c= new ArrayList(1, 2, 3); - $c[0]= 4; - $this->assertEquals(4, $c[0]); - } - - #[@test] - public function array_access_operator_allows_modifying() { - $c= new ArrayList(1, 2, 3); - $c[2]+= 1; // $c[2]++ does NOT work due to a bug in PHP - $this->assertEquals(4, $c[2]); - } - - #[@test, @expect(IndexOutOfBoundsException::class)] - public function reading_non_existant_element_raises_an_exception() { - $c= new ArrayList(); - $c[0]; - } - - #[@test] - public function accessing_non_existant_key_via_get_returns_default() { - $this->assertNull((new ArrayList())->get(0, null)); - } - - #[@test, @expect(IllegalArgumentException::class)] - public function adding_an_element_raises_an_exception() { - $c= new ArrayList(); - $c[]= 4; - } - - #[@test, @values([0, 4]), @expect(IndexOutOfBoundsException::class)] - public function adding_an_element_by_supplying_nonexistant_offset_raises_an_exception($offset) { - $c= new ArrayList(); - $c[$offset]= 4; - } - - #[@test, @expect(IndexOutOfBoundsException::class)] - public function negative_key_raises_an_exception() { - $c= new ArrayList(); - $c[-1]= 4; - } - - #[@test, @expect(IllegalArgumentException::class)] - public function key_of_incorrect_type_raises_an_exception() { - $c= new ArrayList(1, 2, 3); - $c['foo']= 4; - } - - #[@test] - public function newInstance_size() { - $a= ArrayList::newInstance(4); - $this->assertEquals(4, $a->length); - $a[0]= 1; - $a[1]= 2; - $a[2]= 3; - $a[3]= 4; - try { - $a[4]= 5; - $this->fail('Should not be able to add a fifth element'); - } catch (IndexOutOfBoundsException $expected) { } - $this->assertEquals(4, $a->length); - } - - #[@test] - public function newInstance_array() { - $a= ArrayList::newInstance([1, 2, 3, 4]); - $this->assertEquals(4, $a->length); - $this->assertEquals(1, $a[0]); - $this->assertEquals(2, $a[1]); - $this->assertEquals(3, $a[2]); - $this->assertEquals(4, $a[3]); - } - - #[@test, @values([0, 1, 2])] - public function array_isset_operator_returns_true_for_existing_offsets($offset) { - $c= new ArrayList(1, 2, 3); - $this->assertTrue(isset($c[$offset])); - } - - #[@test, @values([3, 4, -1])] - public function array_isset_operator_returns_false_for_offsets_oob($offset) { - $c= new ArrayList(1, 2, 3); - $this->assertFalse(isset($c[$offset])); - } - - #[@test, @values([0, 1, 2, 3, 4, -1]), @expect(IllegalArgumentException::class)] - public function array_unset_operator_is_overloaded() { - $c= new ArrayList(1, 2, 3); - unset($c[0]); - } - - #[@test] - public function arraylist_is_intact_after_unset() { - $c= new ArrayList(1, 2, 3); - try { - unset($c[0]); - } catch (\lang\IllegalArgumentException $expected) { } - - $this->assertEquals(new ArrayList(1, 2, 3), $c); - } - - #[@test, @values([1, 2, 3])] - public function int_contained_in_list_of_ints($value) { - $this->assertTrue((new ArrayList(1, 2, 3))->contains($value)); - } - - #[@test, @values([0, -1, '1', 1.0, true, false, null])] - public function values_not_contained_in_list_of_ints($value) { - $this->assertFalse((new ArrayList(1, 2, 3))->contains($value)); - } - - #[@test, @values([1, -1, 0, '', false, null])] - public function an_empty_list_does_not_contain_anything($value) { - $this->assertFalse((new ArrayList())->contains($value)); - } - - #[@test] - public function a_list_of_an_object_contains_the_given_object() { - $o= new \lang\Object(); - $this->assertTrue((new ArrayList($o))->contains($o)); - } - - #[@test] - public function a_list_of_an_value_contains_the_given_value() { - $o= new Name('Test'); - $this->assertTrue((new ArrayList($o))->contains($o)); - } - - #[@test] - public function a_list_of_an_object_does_not_contain_null() { - $this->assertFalse((new ArrayList(new \lang\Object()))->contains(null)); - } - - #[@test] - public function a_list_of_strings_does_not_contain_an_object() { - $this->assertFalse((new ArrayList('T', 'e', 's', 't'))->contains(new \lang\Object())); - } -} diff --git a/src/test/php/net/xp_framework/unittest/core/types/ArrayMapTest.class.php b/src/test/php/net/xp_framework/unittest/core/types/ArrayMapTest.class.php deleted file mode 100755 index a9005ddc8f..0000000000 --- a/src/test/php/net/xp_framework/unittest/core/types/ArrayMapTest.class.php +++ /dev/null @@ -1,176 +0,0 @@ - 'value'], 1, 'lang.types.ArrayMap[1]@{key = "value"}'], - [['color' => 'green', 'price' => 12.99], 2, 'lang.types.ArrayMap[2]@{color = "green", price = 12.99}'] - ]; - } - - #[@test, @values('fixtures')] - public function can_create($value) { - new ArrayMap($value); - } - - #[@test, @values('fixtures')] - public function size($value, $size) { - $this->assertEquals($size, (new ArrayMap($value))->size); - } - - #[@test, @values('fixtures')] - public function pairs($value) { - $this->assertEquals($value, (new ArrayMap($value))->values); - } - - #[@test, @values('fixtures')] - public function iteration($value) { - $iterated= []; - foreach (new ArrayMap($value) as $key => $val) { - $iterated[$key]= $val; - } - $this->assertEquals($value, $iterated); - } - - #[@test, @values('fixtures')] - public function equals_another_instance_with_same_pairs($value) { - $this->assertEquals(new ArrayMap($value), new ArrayMap($value)); - } - - #[@test] - public function equals_with_objects() { - $neverEqual= newinstance(Object::class, [], [ - 'equals' => function($cmp) { return false; } - ]); - $this->assertNotEquals(new ArrayMap(['test' => $neverEqual]), new ArrayMap(['test' => $neverEqual])); - } - - #[@test] - public function equals_with_values() { - $neverEqual= newinstance(Value::class, [], [ - 'compareTo' => function($cmp) { return -1; }, - 'hashCode' => function() { return '0xAB'; }, - 'toString' => function() { return 'test'; } - ]); - $this->assertNotEquals(new ArrayMap(['test' => $neverEqual]), new ArrayMap(['test' => $neverEqual])); - } - - #[@test, @values([ - # [[]], - # [['color' => 'value']], - # [['key' => null]], - # [['key' => 'color']], - # [['color' => 'green', 'price' => 12.99]] - #])] - public function does_not_equal_different_map($value) { - $this->assertNotEquals(new ArrayMap(['key' => 'value']), new ArrayMap($value)); - } - - #[@test] - public function get_value() { - $this->assertEquals('Test', (new ArrayMap(['key' => 'Test']))['key']); - } - - #[@test, @expect(IndexOutOfBoundsException::class)] - public function accessing_non_existant_key_raises_an_exception() { - (new ArrayMap([]))['key']; - } - - #[@test] - public function accessing_non_existant_key_via_get_returns_default() { - $this->assertNull((new ArrayMap([]))->get('key', null)); - } - - #[@test, @values([[['key' => 'value']], [['key' => null]]])] - public function test_existing_value($value) { - $map= new ArrayMap($value); - $this->assertTrue(isset($map['key'])); - } - - #[@test] - public function test_non_existant_value() { - $map= new ArrayMap([]); - $this->assertFalse(isset($map['key'])); - } - - #[@test] - public function put_value() { - $map= new ArrayMap([]); - $map['key']= 'Written'; - $this->assertEquals('Written', $map['key']); - } - - #[@test, @expect(IllegalArgumentException::class)] - public function adding_values_is_forbidden() { - $map= new ArrayMap([]); - $map[]= 'Forbidden'; - } - - #[@test] - public function overwrite_value() { - $map= new ArrayMap(['key' => 'Test']); - $map['key']= 'Overwritten'; - $this->assertEquals('Overwritten', $map['key']); - } - - #[@test] - public function remove_value() { - $map= new ArrayMap(['key' => 'Test']); - unset($map['key']); - $this->assertFalse(isset($map['key'])); - } - - #[@test, @values('fixtures')] - public function string_representation($value, $size, $repr) { - $this->assertEquals($repr, (new ArrayMap($value))->toString() - ); - } - - #[@test, @values([1, 2, 3])] - public function int_contained_in_map_of_ints($value) { - $this->assertTrue((new ArrayMap(['one' => 1, 'two' => 2, 'three' => 3]))->contains($value)); - } - - #[@test, @values([1, -1, 0, '', false, null])] - public function an_empty_map_does_not_contain_anything($value) { - $this->assertFalse((new ArrayMap([]))->contains($value)); - } - - #[@test] - public function a_map_of_an_object_contains_the_given_object() { - $o= new Object(); - $this->assertTrue((new ArrayMap(['key' => $o]))->contains($o)); - } - - #[@test] - public function a_map_of_an_object_contains_the_given_value() { - $o= new Name('Test'); - $this->assertTrue((new ArrayMap(['key' => $o]))->contains($o)); - } - - #[@test] - public function a_map_of_an_object_does_not_contain_null() { - $this->assertFalse((new ArrayMap(['key' => new Object()]))->contains(null)); - } - - #[@test] - public function a_map_of_strings_does_not_contain_an_object() { - $this->assertFalse((new ArrayMap(['T' => 'e', 's' => 't']))->contains(new Object())); - } -} \ No newline at end of file diff --git a/src/test/php/net/xp_framework/unittest/core/types/BooleanTest.class.php b/src/test/php/net/xp_framework/unittest/core/types/BooleanTest.class.php deleted file mode 100755 index 3bfd7787d5..0000000000 --- a/src/test/php/net/xp_framework/unittest/core/types/BooleanTest.class.php +++ /dev/null @@ -1,103 +0,0 @@ -assertEquals(Boolean::$TRUE, new Boolean(true)); - } - - #[@test] - public function falseBoolPrimitiveIsFalse() { - $this->assertEquals(Boolean::$FALSE, new Boolean(false)); - } - - #[@test] - public function oneIntPrimitiveIsTrue() { - $this->assertEquals(Boolean::$TRUE, new Boolean(1)); - } - - #[@test] - public function otherNonZeroIntPrimitiveIsTrue() { - $this->assertEquals(Boolean::$TRUE, new Boolean(6100)); - } - - #[@test] - public function zeroIntPrimitiveIsFalse() { - $this->assertEquals(Boolean::$FALSE, new Boolean(0)); - } - - #[@test] - public function trueString() { - $this->assertEquals(Boolean::$TRUE, new Boolean('true')); - } - - #[@test] - public function trueStringMixedCase() { - $this->assertEquals(Boolean::$TRUE, new Boolean('True')); - } - - #[@test] - public function falseString() { - $this->assertEquals(Boolean::$FALSE, new Boolean('false')); - } - - #[@test] - public function falseStringMixedCase() { - $this->assertEquals(Boolean::$FALSE, new Boolean('False')); - } - - #[@test] - public function trueIsOne() { - $this->assertEquals(1, Boolean::$TRUE->intValue()); - } - - #[@test] - public function falseIsZero() { - $this->assertEquals(0, Boolean::$FALSE->intValue()); - } - - #[@test] - public function trueHashCode() { - $this->assertEquals('true', Boolean::$TRUE->hashCode()); - } - - #[@test] - public function falseHashCode() { - $this->assertEquals('false', Boolean::$FALSE->hashCode()); - } - - #[@test] - public function numericStringIsAValidBoolean() { - $this->assertEquals(Boolean::$TRUE, new Boolean('1')); - } - - #[@test] - public function zeroNumericStringIsAValidBoolean() { - $this->assertEquals(Boolean::$FALSE, new Boolean('0')); - } - - #[@test, @expect(IllegalArgumentException::class)] - public function emptyStringIsNotAValidBoolean() { - new Boolean(''); - } - - #[@test, @expect(IllegalArgumentException::class)] - public function misspelledFalse() { - new Boolean('fals3'); - } - - #[@test, @expect(IllegalArgumentException::class)] - public function doublePrimitiveIsNotAValidBoolean() { - new Boolean(1.0); - } -} diff --git a/src/test/php/net/xp_framework/unittest/core/types/BytesTest.class.php b/src/test/php/net/xp_framework/unittest/core/types/BytesTest.class.php deleted file mode 100755 index a4ed85263e..0000000000 --- a/src/test/php/net/xp_framework/unittest/core/types/BytesTest.class.php +++ /dev/null @@ -1,391 +0,0 @@ -assertEquals(0, (new Bytes())->size()); - } - - #[@test] - public function creating_an_empty_bytes_from_an_empty_string() { - $this->assertEquals(0, (new Bytes(''))->size()); - } - - #[@test] - public function creating_an_empty_bytes_from_an_empty_array() { - $this->assertEquals(0, (new Bytes([]))->size()); - } - - #[@test] - public function fromString() { - $b= new Bytes('abcd'); - $this->assertEquals(4, $b->size()); - $this->assertEquals(new Byte(97), $b[0]); - $this->assertEquals(new Byte(98), $b[1]); - $this->assertEquals(new Byte(99), $b[2]); - $this->assertEquals(new Byte(100), $b[3]); - } - - #[@test] - public function fromIntegerArray() { - $b= new Bytes([97, 98, 99, 100]); - $this->assertEquals(4, $b->size()); - $this->assertEquals(new Byte(97), $b[0]); - $this->assertEquals(new Byte(98), $b[1]); - $this->assertEquals(new Byte(99), $b[2]); - $this->assertEquals(new Byte(100), $b[3]); - } - - #[@test] - public function fromCharArray() { - $b= new Bytes(['a', 'b', 'c', 'd']); - $this->assertEquals(4, $b->size()); - $this->assertEquals(new Byte(97), $b[0]); - $this->assertEquals(new Byte(98), $b[1]); - $this->assertEquals(new Byte(99), $b[2]); - $this->assertEquals(new Byte(100), $b[3]); - } - - #[@test] - public function fromByteArray() { - $b= new Bytes([new Byte(97), new Byte(98), new Byte(99), new Byte(100)]); - $this->assertEquals(4, $b->size()); - $this->assertEquals(new Byte(97), $b[0]); - $this->assertEquals(new Byte(98), $b[1]); - $this->assertEquals(new Byte(99), $b[2]); - $this->assertEquals(new Byte(100), $b[3]); - } - - #[@test, @expect(IllegalArgumentException::class)] - public function illegalConstructorArgument() { - new Bytes(1); - } - - #[@test] - public function sizeChangesAfterAppending() { - $b= new Bytes(); - $this->assertEquals(0, $b->size()); - $b[]= 1; - $this->assertEquals(1, $b->size()); - } - - #[@test] - public function sizeChangesAfterRemoving() { - $b= new Bytes("\0"); - $this->assertEquals(1, $b->size()); - unset($b[0]); - $this->assertEquals(0, $b->size()); - } - - #[@test] - public function sizeDoesNotChangeWhenSetting() { - $b= new Bytes("\0"); - $this->assertEquals(1, $b->size()); - $b[0]= "\1"; - $this->assertEquals(1, $b->size()); - } - - #[@test] - public function appendInteger() { - $b= new Bytes(); - $b[]= 1; - $this->assertEquals(new Byte(1), $b[0]); - } - - #[@test] - public function appendChar() { - $b= new Bytes(); - $b[]= "\1"; - $this->assertEquals(new Byte(1), $b[0]); - } - - #[@test] - public function appendByte() { - $b= new Bytes(); - $b[]= new Byte(1); - $this->assertEquals(new Byte(1), $b[0]); - } - - #[@test] - public function setInteger() { - $b= new Bytes("\1\2"); - $b[0]= 3; - $this->assertEquals(new Byte(3), $b[0]); - } - - #[@test] - public function setChar() { - $b= new Bytes("\1\2"); - $b[0]= "\3"; - $this->assertEquals(new Byte(3), $b[0]); - } - - #[@test] - public function setByte() { - $b= new Bytes("\1\2"); - $b[0]= new Byte(3); - $this->assertEquals(new Byte(3), $b[0]); - } - - #[@test, @expect(IndexOutOfBoundsException::class)] - public function setNegative() { - $b= new Bytes('negative'); - $b[-1]= new Byte(3); - } - - #[@test, @expect(IndexOutOfBoundsException::class)] - public function setPastEnd() { - $b= new Bytes('ends'); - $b[5]= new Byte(3); - } - - #[@test, @expect(IndexOutOfBoundsException::class)] - public function getNegative() { - $b= new Bytes('negative'); - $read= $b[-1]; - } - - #[@test, @expect(IndexOutOfBoundsException::class)] - public function getPastEnd() { - $b= new Bytes('ends'); - $read= $b[5]; - } - - #[@test] - public function testingOffsets() { - $b= new Bytes('GIF89a'); - $this->assertFalse(isset($b[-1]), 'offset -1'); - $this->assertTrue(isset($b[0]), 'offset 0'); - $this->assertTrue(isset($b[5]), 'offset 5'); - $this->assertFalse(isset($b[6]), 'offset 6'); - } - - #[@test] - public function removingFromBeginning() { - $b= new Bytes('GIF89a'); - unset($b[0]); - $this->assertEquals(new Bytes('IF89a'), $b); - } - - #[@test] - public function removingFromEnd() { - $b= new Bytes('GIF89a'); - unset($b[5]); - $this->assertEquals(new Bytes('GIF89'), $b); - } - - #[@test] - public function removingInBetween() { - $b= new Bytes('GIF89a'); - unset($b[3]); - $this->assertEquals(new Bytes('GIF9a'), $b); - } - - #[@test, @expect(IndexOutOfBoundsException::class)] - public function removingNegative() { - $b= new Bytes('negative'); - unset($b[-1]); - } - - #[@test, @expect(IndexOutOfBoundsException::class)] - public function removingPastEnd() { - $b= new Bytes('ends'); - unset($b[5]); - } - - #[@test] - public function binarySafeBeginning() { - $b= new Bytes(["\0", 'A', 'B']); - $this->assertEquals(new Byte(0), $b[0]); - $this->assertEquals(new Byte(65), $b[1]); - $this->assertEquals(new Byte(66), $b[2]); - } - - #[@test] - public function binarySafeInBetween() { - $b= new Bytes(['A', "\0", 'B']); - $this->assertEquals(new Byte(65), $b[0]); - $this->assertEquals(new Byte(0), $b[1]); - $this->assertEquals(new Byte(66), $b[2]); - } - - #[@test] - public function binarySafeInEnd() { - $b= new Bytes(['A', 'B', "\0"]); - $this->assertEquals(new Byte(65), $b[0]); - $this->assertEquals(new Byte(66), $b[1]); - $this->assertEquals(new Byte(0), $b[2]); - } - - #[@test] - public function abcBytesToString() { - $this->assertEquals( - 'lang.types.Bytes(6)@{@ ABC!}', - (new Bytes('@ ABC!'))->toString() - ); - } - - #[@test] - public function controlCharsToString() { - $this->assertEquals( - 'lang.types.Bytes(32)@{'. - '\000\001\002\003\004\005\006\a'. // 0 - 7 - '\b\t\n\v\f\r\016\017'. // 8 - 15 - '\020\021\022\023\024\025\026\027'. // 16 - 23 - '\030\031\032\033\034\035\036\037'. // 24 - 31 - '}', - (new Bytes(range(0, 31)))->toString() - ); - } - - #[@test] - public function umlautsToString() { - $this->assertEquals( - 'lang.types.Bytes(6)@{A\344O\366U\374}', - (new Bytes('A�O�U�'))->toString() - ); - } - - #[@test] - public function stringCasting() { - $this->assertEquals('Hello', (string)new Bytes('Hello')); - } - - /** - * Test creating an integer from bytes using "N" as format - * (unsigned long (always 32 bit, big endian byte order)) - * - * @see php://unpack - */ - #[@test] - public function unpackUnsignedLong() { - $r= unpack('Nnumber', new Bytes("\000\000\003\350")); - $this->assertEquals(1000, $r['number']); - } - - /** - * Test creating bytes from an integer using "N" as format - * (unsigned long (always 32 bit, big endian byte order)) - * - * @see php://pack - */ - #[@test] - public function packUnsignedLong() { - $this->assertEquals(new Bytes("\000\000\003\350"), new Bytes(pack('N', 1000))); - } - - #[@test] - public function string_from_iso88591_bytes() { - $this->assertEquals( - new String("H\xe4llo", 'iso-8859-1'), - new String(new Bytes("H\344llo"), 'iso-8859-1') - ); - } - - #[@test] - public function string_from_utf8_bytes() { - $this->assertEquals( - new String("H\xe4llo", 'iso-8859-1'), - new String(new Bytes("H\303\244llo"), 'utf-8') - ); - } - - #[@test, @expect(FormatException::class)] - public function string_from_invalid_utf8_bytes() { - new String(new Bytes("H\344llo"), 'utf-8'); - } - - #[@test] - public function utf8_bytes_from_string() { - $this->assertEquals( - new Bytes("H\303\244llo"), - (new String("H\xe4llo", 'iso-8859-1'))->getBytes('utf-8') - ); - } - - #[@test] - public function iso88591_bytes_from_string() { - $this->assertEquals( - new Bytes("H\344llo"), - (new String("H\xe4llo", 'iso-8859-1'))->getBytes('iso-8859-1') - ); - } - - #[@test] - public function character_from_iso88591_bytes() { - $this->assertEquals( - new Character("\xe4", 'iso-8859-1'), - new Character(new Bytes("\344"), 'iso-8859-1') - ); - } - - #[@test] - public function character_from_utf8_bytes() { - $this->assertEquals( - new Character("\xe4", 'iso-8859-1'), - new Character(new Bytes("\303\244"), 'utf-8') - ); - } - - #[@test] - public function utf8_bytes_from_character() { - $this->assertEquals( - new Bytes("\303\244"), - (new Character("\xe4", 'iso-8859-1'))->getBytes('utf-8') - ); - } - - #[@test] - public function iso88591_bytes_from_character() { - $this->assertEquals( - new Bytes("\344"), - (new Character("\xe4", 'iso-8859-1'))->getBytes('iso-8859-1') - ); - } - - #[@test] - public function worksWithEchoStatement() { - ob_start(); - echo new Bytes('�'); - $this->assertEquals('�', ob_get_clean()); - } - - #[@test] - public function integerArrayToBytes() { - $b= new Bytes([228, 246, 252]); - $this->assertEquals(new Byte(-28), $b[0]); - $this->assertEquals(new Byte(-10), $b[1]); - $this->assertEquals(new Byte(-4), $b[2]); - } - - #[@test] - public function byteArrayToBytes() { - $b= new Bytes([new Byte(-28)]); - $this->assertEquals(new Byte(-28), $b[0]); - } - - #[@test] - public function iteration() { - $c= ['H', "\303", "\244", 'l', 'l', 'o']; - $b= new Bytes($c); - foreach ($b as $i => $byte) { - $this->assertEquals($c[$i], chr($byte->intValue())); - } - $this->assertEquals($i, sizeof($c)- 1); - } -} diff --git a/src/test/php/net/xp_framework/unittest/core/types/CharacterTest.class.php b/src/test/php/net/xp_framework/unittest/core/types/CharacterTest.class.php deleted file mode 100755 index e6781acd19..0000000000 --- a/src/test/php/net/xp_framework/unittest/core/types/CharacterTest.class.php +++ /dev/null @@ -1,84 +0,0 @@ -assertEquals(new Bytes("\x00"), (new Character(0))->getBytes()); - } - - #[@test] - public function euroSymbol() { - $this->assertEquals(new Bytes("\xe2\x82\xac"), (new Character(8364))->getBytes('utf-8')); // € in HTML - } - - #[@test, @expect(FormatException::class)] - public function illegalCharacter() { - new Character("\xe4", 'US-ASCII'); - } - - #[@test, @expect(IllegalArgumentException::class)] - public function illegalLength() { - new Character('ABC'); - } - - #[@test] - public function usAsciiCharacter() { - $this->assertEquals(new Bytes('H'), (new Character('H'))->getBytes()); - } - - #[@test] - public function umlautCharacter() { - $this->assertEquals(new Bytes("\303\244"), (new Character('ä', 'utf-8'))->getBytes('utf-8')); - } - - #[@test] - public function utf8Character() { - $this->assertEquals( - new Character('ä', 'utf-8'), - new Character("\xe4", 'iso-8859-1') - ); - } - - #[@test, @ignore('Does not work with all iconv implementations')] - public function transliteration() { - $this->assertEquals('c', (new String('č', 'utf-8'))->toString()); - } - - #[@test] - public function worksWithEchoStatement() { - ob_start(); - echo new Character('w'); - $this->assertEquals('w', ob_get_clean()); - } - - #[@test] - public function stringCast() { - $this->assertEquals('w', (string)new Character('w')); - } - - #[@test] - public function usedInStringFunction() { - $this->assertEquals( - 'z', - str_replace('Z', 'z', new Character('Z') - )); - } -} diff --git a/src/test/php/net/xp_framework/unittest/core/types/NumberTest.class.php b/src/test/php/net/xp_framework/unittest/core/types/NumberTest.class.php deleted file mode 100755 index 058eb74ff1..0000000000 --- a/src/test/php/net/xp_framework/unittest/core/types/NumberTest.class.php +++ /dev/null @@ -1,219 +0,0 @@ -assertEquals($int, $number->intValue(), 'intValue'); - $this->assertEquals($float, $number->doubleValue(), 'doubleValue'); - $this->assertEquals($number, clone($number), 'clone'); - } - - #[@test] - public function longType() { - $this->testType(new Long(0), 0, 0.0); - } - - #[@test] - public function byteType() { - $this->testType(new Byte(0), 0, 0.0); - } - - #[@test] - public function shortType() { - $this->testType(new Short(0), 0, 0.0); - } - - #[@test] - public function integerType() { - $this->testType(new Integer(0), 0, 0.0); - } - - #[@test] - public function doubleType() { - $this->testType(new Double(0), 0, 0.0); - } - - #[@test] - public function floatType() { - $this->testType(new Float(0), 0, 0.0); - } - - #[@test] - public function an_integer_is_not_a_long() { - $this->assertNotEquals(new Integer(1), new Long(1)); - } - - #[@test] - public function a_byte_is_not_a_short() { - $this->assertNotEquals(new Byte(1), new Short(1)); - } - - #[@test] - public function a_double_is_not_a_float() { - $this->assertNotEquals(new Double(1.0), new Float(1.0)); - } - - #[@test] - public function shortNumericStringIsANumber() { - $long= new Long('1'); - $this->assertEquals(1, $long->intValue()); - } - - #[@test, @values(['12389192458912430951248958921958154', '-12389192458912430951248958921958154'])] - public function longNumericStringIsANumber($string) { - $this->assertEquals($string, (new Long($string))->hashCode()); - } - - #[@test] - public function zeroIsANumber() { - $this->assertEquals(0, (new Long(0))->intValue()); - } - - #[@test, @values([['-1', -1], ['+1', 1]])] - public function prefixedNumbersAreNumbers($arg, $num) { - $this->assertEquals($num, (new Long($arg))->intValue()); - } - - #[@test, @values([['1e4', 1e4], ['1E4', 1e4], ['1e-4', 1e-4], ['1E-4', 1e-4]])] - public function numbers_with_exponent_notation($arg, $num) { - $this->assertEquals($num, (new Double($arg))->doubleValue()); - } - - #[@test, @values([['-1e4', -1e4], ['-1E4', -1e4], ['-1e-4', -1e-4], ['-1E-4', -1e-4]])] - public function negative_numbers_with_exponent_notation($arg, $num) { - $this->assertEquals($num, (new Double($arg))->doubleValue()); - } - - #[@test, @values([['1.5', 1.5], ['+0.5', +0.5], ['-0.5', -0.5], ['0.0', 0.0]])] - public function floatingNotationIsANumber($arg, $num) { - $this->assertEquals($num, (new Double($arg))->doubleValue()); - } - - #[@test] - public function numberWithLeadingSpaceIsANumber() { - $this->assertEquals(123, (new Long(' 123'))->intValue()); - } - - #[@test] - public function hexNumbersAreNumbers() { - $long= new Long('0xAAAA'); - $this->assertEquals(43690, $long->intValue()); - } - - #[@test, @expect(IllegalArgumentException::class)] - public function stringIsNotANumber() { - Long::valueOf('string'); - } - - #[@test, @expect(IllegalArgumentException::class)] - public function booleanIsNotANumber() { - Long::valueOf(true); - } - - #[@test, @expect(IllegalArgumentException::class)] - public function nullIsNotANumber() { - Long::valueOf(null); - } - - #[@test, @expect(IllegalArgumentException::class)] - public function writtenNumberIsNotANumber() { - Long::valueOf('one'); - } - - #[@test, @expect(IllegalArgumentException::class)] - public function commaNotationIsNotANumber() { - Long::valueOf('1,1'); - } - - #[@test, @values(['1E+', '1E-', '1E', 'E+4', 'E-4', 'E4', 'E']), @expect(IllegalArgumentException::class)] - public function brokenExponentNotationIsNotANumber($value) { - Long::valueOf($value); - } - - #[@test, @values(['..5', '--1', '++1']), @expect(IllegalArgumentException::class)] - public function doubleLeadingSignsAreNotNumeric($value) { - Long::valueOf($value); - } - - #[@test, @expect(IllegalArgumentException::class)] - public function leadingLetterIsNotANumber() { - Long::valueOf('a123'); - } - - #[@test, @expect(IllegalArgumentException::class)] - public function currencyValueIsNotANumber() { - Long::valueOf('$44.00'); - } - - #[@test, @expect(IllegalArgumentException::class)] - public function whitespaceSeparatedNumbersAreNotNumeric() { - Long::valueOf('4 4'); - } - - #[@test, @expect(IllegalArgumentException::class)] - public function floatingPointNumberIsNotLong() { - Long::valueOf(4.4); - } - - #[@test, @expect(IllegalArgumentException::class)] - public function floatingPointInStringIsNotLong() { - Long::valueOf('4.4'); - } - - #[@test, @expect(IllegalArgumentException::class)] - public function floatingPointNumberIsNotInteger() { - Integer::valueOf(4.4); - } - - #[@test, @expect(IllegalArgumentException::class)] - public function floatingPointInStringIsNotInteger() { - Integer::valueOf('4.4'); - } - - #[@test, @expect(IllegalArgumentException::class)] - public function floatingPointNumberIsNotShort() { - Short::valueOf(4.4); - } - - #[@test, @expect(IllegalArgumentException::class)] - public function floatingPointInStringIsNotShort() { - Short::valueOf('4.4'); - } - - #[@test, @expect(IllegalArgumentException::class)] - public function floatingPointNumberIsNotByte() { - Byte::valueOf(4.4); - } - - #[@test, @expect(IllegalArgumentException::class)] - public function floatingPointInStringIsNotByte() { - Byte::valueOf('4.4'); - } -} diff --git a/src/test/php/net/xp_framework/unittest/core/types/StringTest.class.php b/src/test/php/net/xp_framework/unittest/core/types/StringTest.class.php deleted file mode 100755 index 7ee1430f04..0000000000 --- a/src/test/php/net/xp_framework/unittest/core/types/StringTest.class.php +++ /dev/null @@ -1,352 +0,0 @@ -assertTrue($a->equals($a)); - } - - #[@test] - public function stringIsEqualSameString() { - $this->assertTrue((new String('ABC'))->equals(new String('ABC'))); - } - - #[@test] - public function stringIsNotEqualToDifferentString() { - $this->assertFalse((new String('ABC'))->equals(new String('CBA'))); - } - - #[@test, @expect(FormatException::class)] - public function incompleteMultiByteCharacter() { - new String("\303|", 'utf-8'); - } - - #[@test, @expect(FormatException::class)] - public function illegalCharacter() { - new String('ä', 'US-ASCII'); - } - - #[@test] - public function usAsciiString() { - $str= new String('Hello'); - $this->assertEquals(new \lang\types\Bytes('Hello'), $str->getBytes()); - $this->assertEquals(5, $str->length()); - } - - #[@test] - public function integerString() { - $str= new String(1); - $this->assertEquals(new \lang\types\Bytes('1'), $str->getBytes()); - $this->assertEquals(1, $str->length()); - } - - #[@test] - public function characterString() { - $str= new String(new \lang\types\Character('Ä')); - $this->assertEquals(new \lang\types\Bytes("\304"), $str->getBytes('iso-8859-1')); - $this->assertEquals(1, $str->length()); - } - - #[@test] - public function doubleString() { - $str= new String(1.1); - $this->assertEquals(new \lang\types\Bytes('1.1'), $str->getBytes()); - $this->assertEquals(3, $str->length()); - } - - #[@test] - public function trueString() { - $str= new String(TRUE); - $this->assertEquals(new \lang\types\Bytes('1'), $str->getBytes()); - $this->assertEquals(1, $str->length()); - } - - #[@test] - public function falseString() { - $str= new String(FALSE); - $this->assertEquals(new \lang\types\Bytes(''), $str->getBytes()); - $this->assertEquals(0, $str->length()); - } - - #[@test] - public function nullString() { - $str= new String(NULL); - $this->assertEquals(new \lang\types\Bytes(''), $str->getBytes()); - $this->assertEquals(0, $str->length()); - } - - #[@test] - public function umlautString() { - $str= new String('Hällo'); - $this->assertEquals(new \lang\types\Bytes("H\303\244llo"), $str->getBytes('utf-8')); - $this->assertEquals(5, $str->length()); - } - - #[@test] - public function utf8String() { - $this->assertEquals( - new String('Hällo', 'utf-8'), - new String('Hällo', 'iso-8859-1') - ); - } - - #[@test, @ignore('Does not work with all iconv implementations')] - public function transliteration() { - $this->assertEquals( - 'Trenciansky kraj', - (new String('Trenčiansky kraj', 'utf-8'))->toString() - ); - } - - #[@test] - public function indexOf() { - $str= new String('Hällo'); - $this->assertEquals(0, $str->indexOf('H')); - $this->assertEquals(1, $str->indexOf('ä')); - $this->assertEquals(1, $str->indexOf(new String('ä'))); - $this->assertEquals(-1, $str->indexOf('')); - $this->assertEquals(-1, $str->indexOf('4')); - } - - #[@test] - public function lastIndexOf() { - $str= new String('HälloH'); - $this->assertEquals($str->length()- 1, $str->lastIndexOf('H')); - $this->assertEquals(1, $str->lastIndexOf('ä')); - $this->assertEquals(1, $str->lastIndexOf(new String('ä'))); - $this->assertEquals(-1, $str->lastIndexOf('')); - $this->assertEquals(-1, $str->lastIndexOf('4')); - } - - #[@test] - public function contains() { - $str= new String('Hällo'); - $this->assertTrue($str->contains('H')); - $this->assertTrue($str->contains('ä')); - $this->assertTrue($str->contains('o')); - $this->assertFalse($str->contains('')); - $this->assertFalse($str->contains('4')); - } - - #[@test] - public function substring() { - $str= new String('Hällo'); - $this->assertEquals(new String('ällo'), $str->substring(1)); - $this->assertEquals(new String('ll'), $str->substring(2, -1)); - $this->assertEquals(new String('o'), $str->substring(-1, 1)); - } - - #[@test] - public function startsWith() { - $str= new String('www.müller.com'); - $this->assertTrue($str->startsWith('www.')); - $this->assertFalse($str->startsWith('ww.')); - $this->assertFalse($str->startsWith('müller')); - } - - #[@test] - public function does_not_start_with_empty_string() { - $this->assertFalse((new String('test'))->startsWith('')); - } - - #[@test, @values(['', 'test'])] - public function empty_string_does_not_start_with_anything($value) { - $this->assertFalse(String::$EMPTY->startsWith($value)); - } - - #[@test] - public function endsWith() { - $str= new String('www.müller.com'); - $this->assertTrue($str->endsWith('.com')); - $this->assertTrue($str->endsWith('üller.com')); - $this->assertFalse($str->endsWith('.co')); - $this->assertFalse($str->endsWith('müller')); - } - - #[@test] - public function does_not_end_with_empty_string() { - $this->assertFalse((new String('test'))->endsWith('')); - } - - #[@test, @values(['', 'test'])] - public function empty_string_does_not_end_with_anything($value) { - $this->assertFalse(String::$EMPTY->endsWith($value)); - } - - #[@test] - public function concat() { - $this->assertEquals(new String('www.müller.com'), (new String('www')) - ->concat(new \lang\types\Character('.')) - ->concat('müller') - ->concat('.com') - ); - } - - #[@test] - public function hashesOfSameStringEqual() { - $this->assertEquals( - (new String(''))->hashCode(), - (new String(''))->hashCode() - ); - } - - #[@test] - public function hashesOfDifferentStringsNotEqual() { - $this->assertNotEquals( - (new String('A'))->hashCode(), - (new String('B'))->hashCode() - ); - } - - #[@test] - public function charAt() { - $this->assertEquals(new \lang\types\Character('ü'), (new String('www.müller.com'))->charAt(5)); - } - - #[@test, @expect(IndexOutOfBoundsException::class)] - public function charAtNegative() { - (new String('ABC'))->charAt(-1); - } - - #[@test, @expect(IndexOutOfBoundsException::class)] - public function charAtAfterEnd() { - (new String('ABC'))->charAt(4); - } - - #[@test, @expect(IndexOutOfBoundsException::class)] - public function charAtEnd() { - (new String('ABC'))->charAt(3); - } - - #[@test] - public function replace() { - $str= new String('www.müller.com'); - $this->assertEquals(new String('müller'), $str->replace('www.')->replace('.com')); - $this->assertEquals(new String('muller'), $str->replace('ü', 'u')); - } - - #[@test] - public function offsetSet() { - $str= new String('www.müller.com'); - $str[5]= 'u'; - $this->assertEquals(new String('www.muller.com'), $str); - } - - #[@test, @expect(IndexOutOfBoundsException::class)] - public function offsetSetNegative() { - $str= new String('www.müller.com'); - $str[-1]= 'u'; - } - - #[@test, @expect(IndexOutOfBoundsException::class)] - public function offsetSetAfterEnd() { - $str= new String('www.müller.com'); - $str[$str->length()]= 'u'; - } - - #[@test, @expect(IllegalArgumentException::class)] - public function offsetSetIncorrectLength() { - $str= new String('www.müller.com'); - $str[5]= 'ue'; - } - - #[@test, @expect(IllegalArgumentException::class)] - public function offsetAdd() { - $str= new String('www.müller.com'); - $str[]= '.'; - } - - #[@test] - public function offsetGet() { - $str= new String('www.müller.com'); - $this->assertEquals(new \lang\types\Character('ü'), $str[5]); - } - - #[@test] - public function offsetExists() { - $str= new String('www.müller.com'); - $this->assertTrue(isset($str[0]), 0); - $this->assertTrue(isset($str[5]), 5); - $this->assertFalse(isset($str[-1]), -1); - $this->assertFalse(isset($str[1024]), 1024); - } - - #[@test] - public function offsetUnsetAtBeginning() { - $str= new String('www.müller.com'); - unset($str[0]); - $this->assertEquals(new String('ww.müller.com'), $str); - } - - #[@test] - public function offsetUnsetAtEnd() { - $str= new String('www.müller.com'); - unset($str[$str->length()- 1]); - $this->assertEquals(new String('www.müller.co'), $str); - } - - #[@test] - public function offsetUnsetInBetween() { - $str= new String('www.müller.com'); - unset($str[5]); - $this->assertEquals(new String('www.mller.com'), $str); - } - - #[@test, @expect(IndexOutOfBoundsException::class)] - public function offsetUnsetNegative() { - $str= new String('www.müller.com'); - unset($str[-1]); - } - - #[@test, @expect(IndexOutOfBoundsException::class)] - public function offsetUnsetAfterEnd() { - $str= new String('www.müller.com'); - unset($str[1024]); - } - - #[@test] - public function worksWithEchoStatement() { - ob_start(); - echo new String('www.müller.com'); - $this->assertEquals('www.müller.com', ob_get_clean()); - } - - #[@test] - public function stringCast() { - $this->assertEquals('www.müller.com', (string)new String('www.müller.com')); - } - - #[@test] - public function usedInStringFunction() { - $this->assertEquals( - 'ftp.müller.com', - str_replace('www', 'ftp', new String('www.müller.com') - )); - } - - #[@test, @expect(FormatException::class)] - public function getUmlautsAsAsciiBytes() { - (new String('äöü', 'iso-8859-1'))->getBytes('ASCII'); - } - - #[@test] - public function getAsciiAsAsciiBytes() { - $this->assertEquals( - new \lang\types\Bytes('aou'), - (new String('aou', 'iso-8859-1'))->getBytes('ASCII') - ); - } -} diff --git a/src/test/php/net/xp_framework/unittest/reflection/PrimitiveAndWrappersTest.class.php b/src/test/php/net/xp_framework/unittest/reflection/PrimitiveAndWrappersTest.class.php deleted file mode 100755 index c32a1c55dd..0000000000 --- a/src/test/php/net/xp_framework/unittest/reflection/PrimitiveAndWrappersTest.class.php +++ /dev/null @@ -1,195 +0,0 @@ -assertEquals(new String('Hello'), Primitive::boxed('Hello')); - } - - #[@test]] - public function boxInteger() { - $this->assertEquals(new Integer(1), Primitive::boxed(1)); - } - - #[@test]] - public function boxDouble() { - $this->assertEquals(new Double(1.0), Primitive::boxed(1.0)); - } - - #[@test]] - public function boxBoolean() { - $this->assertEquals(Boolean::$TRUE, Primitive::boxed(true), 'true'); - $this->assertEquals(Boolean::$FALSE, Primitive::boxed(false), 'false'); - } - - #[@test]] - public function boxArray() { - $this->assertEquals(new ArrayList(1, 2, 3), Primitive::boxed([1, 2, 3])); - } - - #[@test]] - public function boxObject() { - $o= new \lang\Object(); - $this->assertEquals($o, Primitive::boxed($o)); - } - - #[@test]] - public function boxNull() { - $this->assertEquals(null, Primitive::boxed(null)); - } - - #[@test]] - public function boxResource() { - $fd= Streams::readableFd(new MemoryInputStream('test')); - try { - Primitive::boxed($fd); - } catch (IllegalArgumentException $expected) { - return; - } finally { - fclose($fd); // Necessary, PHP will segfault otherwise - } - $this->fail('Expected exception not caught', null, 'lang.IllegalArgumentException'); - } - - #[@test]] - public function unboxString() { - $this->assertEquals('Hello', Primitive::unboxed(new String('Hello'))); - } - - #[@test]] - public function unboxInteger() { - $this->assertEquals(1, Primitive::unboxed(new Integer(1))); - } - - #[@test]] - public function unboxDouble() { - $this->assertEquals(1.0, Primitive::unboxed(new Double(1.0))); - } - - #[@test]] - public function unboxBoolean() { - $this->assertEquals(true, Primitive::unboxed(Boolean::$TRUE), 'true'); - $this->assertEquals(false, Primitive::unboxed(Boolean::$FALSE), 'false'); - } - - #[@test]] - public function unboxArray() { - $this->assertEquals([1, 2, 3], Primitive::unboxed(new ArrayList(1, 2, 3))); - } - - #[@test, @expect(IllegalArgumentException::class)]] - public function unboxObject() { - Primitive::unboxed(new \lang\Object()); - } - - #[@test]] - public function unboxNull() { - $this->assertEquals(null, Primitive::unboxed(null)); - } - - #[@test] - public function unboxPrimitive() { - $this->assertEquals(1, Primitive::unboxed(1)); - } - - #[@test, @values([ - # ['4711', new Integer(4711)], ['32', new Short(32)], - # ['4711', new Double(4711.0)], ['4711', new Float(4711.0)], - # ['1', true], ['1', Boolean::$TRUE], ['', Boolean::$FALSE], - # ['Test', new String('Test')], ['', new String('')] - #])] - public function newInstance_of_string_with_wrappers($expected, $value) { - $this->assertEquals($expected, Primitive::$STRING->newInstance($value)); - } - - #[@test, @values([ - # [4711, new Integer(4711)], [32, new Short(32)], - # [4711, new Double(4711.0)], [4711, new Float(4711.0)], - # [1, Boolean::$TRUE], [0, Boolean::$FALSE], - # [4711, new String('4711')], [0, new String('')] - #])] - public function newInstance_of_int($expected, $value) { - $this->assertEquals($expected, Primitive::$INT->newInstance($value)); - } - - #[@test, @values([ - # [4711.0, new Integer(4711)], [32.0, new Short(32)], - # [47.11, new Double(47.11)], [47.11, new Float(47.11)], - # [1.0, Boolean::$TRUE], [0.0, Boolean::$FALSE], - # [47.11, new String('47.11')], [0.0, new String('')] - #])] - public function newInstance_of_double($expected, $value) { - $this->assertEquals($expected, Primitive::$DOUBLE->newInstance($value)); - } - - #[@test, @values([ - # [true, new Integer(4711)], [true, new Short(32)], - # [true, new Double(4711.0)], [true, new Float(4711.0)], - # [true, Boolean::$TRUE], [false, Boolean::$FALSE], - # [true, new String('Test')], [false, new String('')] - #])] - public function newInstance_of_bool($expected, $value) { - $this->assertEquals($expected, Primitive::$BOOL->newInstance($value)); - } - - #[@test, @values([ - # ['4711', new Integer(4711)], ['32', new Short(32)], - # ['4711', new Double(4711.0)], ['4711', new Float(4711.0)], - # ['1', Boolean::$TRUE], ['', Boolean::$FALSE], - # ['Test', new String('Test')], ['', new String('')] - #])] - public function cast_of_string($expected, $value) { - $this->assertEquals($expected, Primitive::$STRING->cast($value)); - } - - #[@test, @values([ - # [4711, new Integer(4711)], [32, new Short(32)], - # [4711, new Double(4711.0)], [4711, new Float(4711.0)], - # [1, Boolean::$TRUE], [0, Boolean::$FALSE], - # [4711, new String('4711')], [0, new String('')] - #])] - public function cast_of_int($expected, $value) { - $this->assertEquals($expected, Primitive::$INT->cast($value)); - } - - #[@test, @values([ - # [4711.0, new Integer(4711)], [32.0, new Short(32)], - # [47.11, new Double(47.11)], [47.11, new Float(47.11)], - # [1.0, Boolean::$TRUE], [0.0, Boolean::$FALSE], - # [47.11, new String('47.11')], [0.0, new String('')] - #])] - public function cast_of_double($expected, $value) { - $this->assertEquals($expected, Primitive::$DOUBLE->cast($value)); - } - - #[@test, @values([ - # [true, new Integer(4711)], [true, new Short(4711)], - # [true, new Double(4711.0)], [true, new Float(47.11)], - # [true, Boolean::$TRUE], [false, Boolean::$FALSE], - # [true, new String('Test')], [false, new String('')] - #])] - public function cast_of_bool($expected, $value) { - $this->assertEquals($expected, Primitive::$BOOL->cast($value)); - } -} \ No newline at end of file From fd2de8810822b28304710efb2385772da7944e8e Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Thu, 21 Jan 2016 21:14:50 +0100 Subject: [PATCH 02/19] Remove "grep -v", we do not have any "do not run on 7.0" tests anymore --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 7baadb52d5..8a5df973a4 100755 --- a/.travis.yml +++ b/.travis.yml @@ -14,4 +14,4 @@ before_script: - echo "date.timezone=Europe/Berlin" >> xp.ini script: - - (EXCD=0; for i in `ls -1 src/test/config/unittest/*.ini | grep -v not-on-php-$TRAVIS_PHP_VERSION`; do echo "---> $i"; ./xp -cp test.xar xp.unittest.Runner $i; RES=$?; if [ $RES -ne 0 ]; then EXCD=$RES; fi; done; exit $EXCD;) + - (EXCD=0; for i in `ls -1 src/test/config/unittest/*.ini`; do echo "---> $i"; ./xp -cp test.xar xp.unittest.Runner $i; RES=$?; if [ $RES -ne 0 ]; then EXCD=$RES; fi; done; exit $EXCD;) From 4009f65a7a862ed2069c8d455083e5759d36c194 Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Thu, 21 Jan 2016 21:21:21 +0100 Subject: [PATCH 03/19] Rewrite test to no longer used class from lang.types --- .../net/xp_framework/unittest/reflection/ProxyTest.class.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/php/net/xp_framework/unittest/reflection/ProxyTest.class.php b/src/test/php/net/xp_framework/unittest/reflection/ProxyTest.class.php index 118eec3195..84ef5e37cd 100644 --- a/src/test/php/net/xp_framework/unittest/reflection/ProxyTest.class.php +++ b/src/test/php/net/xp_framework/unittest/reflection/ProxyTest.class.php @@ -193,9 +193,9 @@ public function overloadedMethod() { #[@test] public function namespaced_typehinted_parameters_handled_correctly() { - $proxy= $this->newProxyWith('{ public function fixture(\lang\types\Long $param); }'); + $proxy= $this->newProxyWith('{ public function fixture(\util\Date $param); }'); $this->assertEquals( - XPClass::forName('lang.types.Long'), + XPClass::forName('util.Date'), $proxy->getMethod('fixture')->getParameters()[0]->getTypeRestriction() ); } From d512a1beb2e30a612ac54d14aabb1fea6702cc42 Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Thu, 21 Jan 2016 21:23:52 +0100 Subject: [PATCH 04/19] Remove tests relying on lang.types --- .../io/streams/TextWriterTest.class.php | 25 ------------------- 1 file changed, 25 deletions(-) diff --git a/src/test/php/net/xp_framework/unittest/io/streams/TextWriterTest.class.php b/src/test/php/net/xp_framework/unittest/io/streams/TextWriterTest.class.php index c41102c186..f2021ac318 100644 --- a/src/test/php/net/xp_framework/unittest/io/streams/TextWriterTest.class.php +++ b/src/test/php/net/xp_framework/unittest/io/streams/TextWriterTest.class.php @@ -1,6 +1,5 @@ assertEquals("\303\234bercoder\n", $this->out->getBytes()); } - #[@test, @action(new RuntimeVersion('<7.0.0-dev'))] - public function writeUtf8StringInstance() { - $this->newWriter('utf-8')->write(new \lang\types\String('Übercoder')); - $this->assertEquals("\303\234bercoder", $this->out->getBytes()); - } - - #[@test, @action(new RuntimeVersion('<7.0.0-dev'))] - public function writeLineUtf8StringInstance() { - $this->newWriter('utf-8')->writeLine(new \lang\types\String('Übercoder')); - $this->assertEquals("\303\234bercoder\n", $this->out->getBytes()); - } - - #[@test] - public function writeUtf8CharacterInstance() { - $this->newWriter('utf-8')->write(new Character('Ü')); - $this->assertEquals("\303\234", $this->out->getBytes()); - } - - #[@test] - public function writeLineUtf8CharacterInstance() { - $this->newWriter('utf-8')->writeLine(new Character('Ü')); - $this->assertEquals("\303\234\n", $this->out->getBytes()); - } - #[@test] public function closingTwice() { $w= $this->newWriter(''); From 5e008c51eb693ff1824b60de5f571ac8c3491b84 Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Thu, 21 Jan 2016 21:40:34 +0100 Subject: [PATCH 05/19] Reintroduce lang.types.Bytes as util.Bytes --- src/main/php/util/Bytes.class.php | 176 ++++++++++ src/main/php/util/UUID.class.php | 3 +- src/test/config/unittest/util.ini | 3 + .../unittest/util/BytesTest.class.php | 318 ++++++++++++++++++ .../unittest/util/UUIDTest.class.php | 5 +- 5 files changed, 501 insertions(+), 4 deletions(-) create mode 100755 src/main/php/util/Bytes.class.php create mode 100755 src/test/php/net/xp_framework/unittest/util/BytesTest.class.php diff --git a/src/main/php/util/Bytes.class.php b/src/main/php/util/Bytes.class.php new file mode 100755 index 0000000000..d410e94c71 --- /dev/null +++ b/src/main/php/util/Bytes.class.php @@ -0,0 +1,176 @@ +buffer= implode('', array_map([$this, 'asByte'], $initial)); + } else if (is_string($initial)) { + $this->buffer= $initial; + } else { + throw new \lang\IllegalArgumentException('Expected either char[], int[] or string but was '.\xp::typeOf($initial)); + } + $this->size= strlen($this->buffer); + } + + /** + * Returns an iterator for use in foreach() + * + * @see php://language.oop5.iterations + * @return php.Iterator + */ + public function getIterator() { + if (!$this->iterator) $this->iterator= newinstance('Iterator', [$this], '{ + private $i= 0, $v; + public function __construct($v) { $this->v= $v; } + public function current() { $n= ord($this->v->buffer{$this->i}); return new \lang\types\Byte($n < 128 ? $n : $n - 256); } + public function key() { return $this->i; } + public function next() { $this->i++; } + public function rewind() { $this->i= 0; } + public function valid() { return $this->i < $this->v->size; } + }'); + return $this->iterator; + } + + /** + * = list[] overloading + * + * @param int offset + * @return lang.types.Byte + * @throws lang.IndexOutOfBoundsException if offset does not exist + */ + public function offsetGet($offset) { + if ($offset >= $this->size || $offset < 0) { + throw new IndexOutOfBoundsException('Offset '.$offset.' out of bounds'); + } + $n= ord($this->buffer{$offset}); + return $n < 128 ? $n : $n - 256; + } + + /** + * list[]= overloading + * + * @param int offset + * @param var value + * @throws lang.IllegalArgumentException if key is neither numeric (set) nor NULL (add) + * @throws lang.IndexOutOfBoundsException if key does not exist + */ + public function offsetSet($offset, $value) { + if (null === $offset) { + $this->buffer.= $this->asByte($value); + $this->size++; + } else if ($offset >= $this->size || $offset < 0) { + throw new IndexOutOfBoundsException('Offset '.$offset.' out of bounds'); + } else { + $this->buffer{$offset}= $this->asByte($value); + } + } + + /** + * isset() overloading + * + * @param int offset + * @return bool + */ + public function offsetExists($offset) { + return ($offset >= 0 && $offset < $this->size); + } + + /** + * unset() overloading + * + * @param int offset + * @throws lang.IndexOutOfBoundsException if offset does not exist + */ + public function offsetUnset($offset) { + if ($offset >= $this->size || $offset < 0) { + throw new IndexOutOfBoundsException('Offset '.$offset.' out of bounds'); + } + $this->buffer= ( + substr($this->buffer, 0, $offset). + substr($this->buffer, $offset+ 1, $this->size) + ); + $this->size--; + } + + /** + * Returns this byte list's size + * + * @return string + */ + public function size() { + return $this->size; + } + + /** + * Returns whether a given object is equal to this object + * + * @param lang.Generic cmp + * @return bool + */ + public function equals($cmp) { + return ( + $cmp instanceof self && + $this->size === $cmp->size && + $this->buffer === $cmp->buffer + ); + } + + /** + * Returns a hashcode for this bytes object + * + * @return string + */ + public function hashCode() { + return md5($this->buffer); + } + + /** + * Returns a string representation of this string. + * + * @return string + */ + public function toString() { + return nameof($this).'('.$this->size.')@{'.addcslashes($this->buffer, "\0..\37\177..\377").'}'; + } + + /** + * String conversion overloading. This is for use with fwrite() + * + * @return string + */ + public function __toString() { + return $this->buffer; + } +} diff --git a/src/main/php/util/UUID.class.php b/src/main/php/util/UUID.class.php index 48be0e90a3..cd96bbf040 100755 --- a/src/main/php/util/UUID.class.php +++ b/src/main/php/util/UUID.class.php @@ -1,6 +1,5 @@ hashCode()))); diff --git a/src/test/config/unittest/util.ini b/src/test/config/unittest/util.ini index 365cd3dcf7..f721a56719 100644 --- a/src/test/config/unittest/util.ini +++ b/src/test/config/unittest/util.ini @@ -74,3 +74,6 @@ class="net.xp_framework.unittest.util.OpenSSLSecretTest" [plaintext-secret] class="net.xp_framework.unittest.util.PlainTextSecretTest" + +[bytes] +class="net.xp_framework.unittest.util.BytesTest" diff --git a/src/test/php/net/xp_framework/unittest/util/BytesTest.class.php b/src/test/php/net/xp_framework/unittest/util/BytesTest.class.php new file mode 100755 index 0000000000..562bb2116b --- /dev/null +++ b/src/test/php/net/xp_framework/unittest/util/BytesTest.class.php @@ -0,0 +1,318 @@ +assertEquals(0, (new Bytes())->size()); + } + + #[@test] + public function creating_an_empty_bytes_from_an_empty_string() { + $this->assertEquals(0, (new Bytes(''))->size()); + } + + #[@test] + public function creating_an_empty_bytes_from_an_empty_array() { + $this->assertEquals(0, (new Bytes([]))->size()); + } + + #[@test] + public function fromString() { + $b= new Bytes('abcd'); + $this->assertEquals(4, $b->size()); + $this->assertEquals(97, $b[0]); + $this->assertEquals(98, $b[1]); + $this->assertEquals(99, $b[2]); + $this->assertEquals(100, $b[3]); + } + + #[@test] + public function fromIntegerArray() { + $b= new Bytes([97, 98, 99, 100]); + $this->assertEquals(4, $b->size()); + $this->assertEquals(97, $b[0]); + $this->assertEquals(98, $b[1]); + $this->assertEquals(99, $b[2]); + $this->assertEquals(100, $b[3]); + } + + #[@test] + public function fromCharArray() { + $b= new Bytes(['a', 'b', 'c', 'd']); + $this->assertEquals(4, $b->size()); + $this->assertEquals(97, $b[0]); + $this->assertEquals(98, $b[1]); + $this->assertEquals(99, $b[2]); + $this->assertEquals(100, $b[3]); + } + + #[@test] + public function fromByteArray() { + $b= new Bytes([97, 98, 99, 100]); + $this->assertEquals(4, $b->size()); + $this->assertEquals(97, $b[0]); + $this->assertEquals(98, $b[1]); + $this->assertEquals(99, $b[2]); + $this->assertEquals(100, $b[3]); + } + + #[@test, @expect(IllegalArgumentException::class)] + public function illegalConstructorArgument() { + new Bytes(1); + } + + #[@test] + public function sizeChangesAfterAppending() { + $b= new Bytes(); + $this->assertEquals(0, $b->size()); + $b[]= 1; + $this->assertEquals(1, $b->size()); + } + + #[@test] + public function sizeChangesAfterRemoving() { + $b= new Bytes("\0"); + $this->assertEquals(1, $b->size()); + unset($b[0]); + $this->assertEquals(0, $b->size()); + } + + #[@test] + public function sizeDoesNotChangeWhenSetting() { + $b= new Bytes("\0"); + $this->assertEquals(1, $b->size()); + $b[0]= "\1"; + $this->assertEquals(1, $b->size()); + } + + #[@test] + public function appendInteger() { + $b= new Bytes(); + $b[]= 1; + $this->assertEquals(1, $b[0]); + } + + #[@test] + public function appendChar() { + $b= new Bytes(); + $b[]= "\1"; + $this->assertEquals(1, $b[0]); + } + + #[@test] + public function appendByte() { + $b= new Bytes(); + $b[]= 1; + $this->assertEquals(1, $b[0]); + } + + #[@test] + public function setInteger() { + $b= new Bytes("\1\2"); + $b[0]= 3; + $this->assertEquals(3, $b[0]); + } + + #[@test] + public function setChar() { + $b= new Bytes("\1\2"); + $b[0]= "\3"; + $this->assertEquals(3, $b[0]); + } + + #[@test] + public function setByte() { + $b= new Bytes("\1\2"); + $b[0]= 3; + $this->assertEquals(3, $b[0]); + } + + #[@test, @expect(IndexOutOfBoundsException::class)] + public function setNegative() { + $b= new Bytes('negative'); + $b[-1]= 3; + } + + #[@test, @expect(IndexOutOfBoundsException::class)] + public function setPastEnd() { + $b= new Bytes('ends'); + $b[5]= 3; + } + + #[@test, @expect(IndexOutOfBoundsException::class)] + public function getNegative() { + $b= new Bytes('negative'); + $read= $b[-1]; + } + + #[@test, @expect(IndexOutOfBoundsException::class)] + public function getPastEnd() { + $b= new Bytes('ends'); + $read= $b[5]; + } + + #[@test] + public function testingOffsets() { + $b= new Bytes('GIF89a'); + $this->assertFalse(isset($b[-1]), 'offset -1'); + $this->assertTrue(isset($b[0]), 'offset 0'); + $this->assertTrue(isset($b[5]), 'offset 5'); + $this->assertFalse(isset($b[6]), 'offset 6'); + } + + #[@test] + public function removingFromBeginning() { + $b= new Bytes('GIF89a'); + unset($b[0]); + $this->assertEquals(new Bytes('IF89a'), $b); + } + + #[@test] + public function removingFromEnd() { + $b= new Bytes('GIF89a'); + unset($b[5]); + $this->assertEquals(new Bytes('GIF89'), $b); + } + + #[@test] + public function removingInBetween() { + $b= new Bytes('GIF89a'); + unset($b[3]); + $this->assertEquals(new Bytes('GIF9a'), $b); + } + + #[@test, @expect(IndexOutOfBoundsException::class)] + public function removingNegative() { + $b= new Bytes('negative'); + unset($b[-1]); + } + + #[@test, @expect(IndexOutOfBoundsException::class)] + public function removingPastEnd() { + $b= new Bytes('ends'); + unset($b[5]); + } + + #[@test] + public function binarySafeBeginning() { + $b= new Bytes(["\0", 'A', 'B']); + $this->assertEquals(0, $b[0]); + $this->assertEquals(65, $b[1]); + $this->assertEquals(66, $b[2]); + } + + #[@test] + public function binarySafeInBetween() { + $b= new Bytes(['A', "\0", 'B']); + $this->assertEquals(65, $b[0]); + $this->assertEquals(0, $b[1]); + $this->assertEquals(66, $b[2]); + } + + #[@test] + public function binarySafeInEnd() { + $b= new Bytes(['A', 'B', "\0"]); + $this->assertEquals(65, $b[0]); + $this->assertEquals(66, $b[1]); + $this->assertEquals(0, $b[2]); + } + + #[@test] + public function abcBytesToString() { + $this->assertEquals( + 'util.Bytes(6)@{@ ABC!}', + (new Bytes('@ ABC!'))->toString() + ); + } + + #[@test] + public function controlCharsToString() { + $this->assertEquals( + 'util.Bytes(32)@{'. + '\000\001\002\003\004\005\006\a'. // 0 - 7 + '\b\t\n\v\f\r\016\017'. // 8 - 15 + '\020\021\022\023\024\025\026\027'. // 16 - 23 + '\030\031\032\033\034\035\036\037'. // 24 - 31 + '}', + (new Bytes(range(0, 31)))->toString() + ); + } + + #[@test] + public function umlautsToString() { + $this->assertEquals( + 'util.Bytes(9)@{A\303\244O\303\266U\303\274}', + (new Bytes('AäOöUü'))->toString() + ); + } + + #[@test] + public function stringCasting() { + $this->assertEquals('Hello', (string)new Bytes('Hello')); + } + + /** + * Test creating an integer from bytes using "N" as format + * (unsigned long (always 32 bit, big endian byte order)) + * + * @see php://unpack + */ + #[@test] + public function unpackUnsignedLong() { + $r= unpack('Nnumber', new Bytes("\000\000\003\350")); + $this->assertEquals(1000, $r['number']); + } + + /** + * Test creating bytes from an integer using "N" as format + * (unsigned long (always 32 bit, big endian byte order)) + * + * @see php://pack + */ + #[@test] + public function packUnsignedLong() { + $this->assertEquals(new Bytes("\000\000\003\350"), new Bytes(pack('N', 1000))); + } + + #[@test] + public function worksWithEchoStatement() { + ob_start(); + echo new Bytes('ü'); + $this->assertEquals('ü', ob_get_clean()); + } + + #[@test] + public function integerArrayToBytes() { + $b= new Bytes([228, 246, 252]); + $this->assertEquals(-28, $b[0]); + $this->assertEquals(-10, $b[1]); + $this->assertEquals(-4, $b[2]); + } + + #[@test] + public function byteArrayToBytes() { + $b= new Bytes([-28]); + $this->assertEquals(-28, $b[0]); + } + + #[@test] + public function iteration() { + $c= ['H', "\303", "\244", 'l', 'l', 'o']; + $b= new Bytes($c); + foreach ($b as $i => $byte) { + $this->assertEquals($c[$i], chr($byte->intValue())); + } + $this->assertEquals($i, sizeof($c)- 1); + } +} diff --git a/src/test/php/net/xp_framework/unittest/util/UUIDTest.class.php b/src/test/php/net/xp_framework/unittest/util/UUIDTest.class.php index 61fc241c1a..90d238128c 100644 --- a/src/test/php/net/xp_framework/unittest/util/UUIDTest.class.php +++ b/src/test/php/net/xp_framework/unittest/util/UUIDTest.class.php @@ -1,6 +1,7 @@ assertEquals( - new \lang\types\Bytes("k\xa7\xb8\x11\x9d\xad\x11\xd1\x80\xb4\x00\xc0O\xd40\xc8"), + new Bytes("k\xa7\xb8\x11\x9d\xad\x11\xd1\x80\xb4\x00\xc0O\xd40\xc8"), $this->fixture->getBytes() ); } @@ -70,7 +71,7 @@ public function fixtureEqualToUrnNotation() { #[@test] public function fixtureEqualsToBytes() { - $this->assertEquals($this->fixture, new UUID(new \lang\types\Bytes("k\xa7\xb8\x11\x9d\xad\x11\xd1\x80\xb4\x00\xc0O\xd40\xc8"))); + $this->assertEquals($this->fixture, new UUID(new Bytes("k\xa7\xb8\x11\x9d\xad\x11\xd1\x80\xb4\x00\xc0O\xd40\xc8"))); } #[@test, @expect(FormatException::class)] From d6d8b683bd35b478c97c3efea066075dc65b2b26 Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Thu, 21 Jan 2016 21:45:48 +0100 Subject: [PATCH 06/19] Remove lang.types.Double from util.Money API --- src/main/php/util/Money.class.php | 14 ++++++-------- .../xp_framework/unittest/util/MoneyTest.class.php | 13 ++++++------- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/src/main/php/util/Money.class.php b/src/main/php/util/Money.class.php index ba2adc97e4..8418815ed9 100755 --- a/src/main/php/util/Money.class.php +++ b/src/main/php/util/Money.class.php @@ -1,8 +1,5 @@ amount); + public function amount($round= 0) { + return $round ? sprintf('%.'.$round.'f', $this->amount) : $this->amount; } /** diff --git a/src/test/php/net/xp_framework/unittest/util/MoneyTest.class.php b/src/test/php/net/xp_framework/unittest/util/MoneyTest.class.php index b9d809d6c5..643d3b4c69 100644 --- a/src/test/php/net/xp_framework/unittest/util/MoneyTest.class.php +++ b/src/test/php/net/xp_framework/unittest/util/MoneyTest.class.php @@ -3,7 +3,6 @@ use util\Money; use util\Currency; use lang\IllegalArgumentException; -use lang\types\Double; /** * TestCase @@ -15,24 +14,24 @@ class MoneyTest extends \unittest\TestCase { #[@test] public function tenUsDollarsFromInt() { $this->assertEquals( - new Double(10.00), - (new Money(10, Currency::$USD))->amount() + '10.00', + (new Money(10, Currency::$USD))->amount(2) ); } #[@test] public function tenUsDollarsFromFloat() { $this->assertEquals( - new Double(10.00), - (new Money(10.00, Currency::$USD))->amount() + '10.00', + (new Money(10.00, Currency::$USD))->amount(2) ); } #[@test] public function tenUsDollarsFromString() { $this->assertEquals( - new Double(10.00), - (new Money('10.00', Currency::$USD))->amount() + '10.00', + (new Money('10.00', Currency::$USD))->amount(2) ); } From 607e7f643b911e5e272e927e61cc950a97279a15 Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Thu, 21 Jan 2016 21:50:12 +0100 Subject: [PATCH 07/19] Use new util.Bytes class --- .../io/streams/AbstractCompressingOutputStreamTest.class.php | 4 ++-- .../io/streams/AbstractDecompressingInputStreamTest.class.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test/php/net/xp_framework/unittest/io/streams/AbstractCompressingOutputStreamTest.class.php b/src/test/php/net/xp_framework/unittest/io/streams/AbstractCompressingOutputStreamTest.class.php index 88cb7a6a08..a058a7ecf9 100644 --- a/src/test/php/net/xp_framework/unittest/io/streams/AbstractCompressingOutputStreamTest.class.php +++ b/src/test/php/net/xp_framework/unittest/io/streams/AbstractCompressingOutputStreamTest.class.php @@ -1,7 +1,7 @@ Date: Thu, 21 Jan 2016 21:50:28 +0100 Subject: [PATCH 08/19] Remove instanceof checks for lang.types classes --- src/main/php/io/streams/TextWriter.class.php | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/main/php/io/streams/TextWriter.class.php b/src/main/php/io/streams/TextWriter.class.php index e744d41da0..f52e89e2e1 100755 --- a/src/main/php/io/streams/TextWriter.class.php +++ b/src/main/php/io/streams/TextWriter.class.php @@ -85,10 +85,7 @@ public function getNewLine() { * @param string text */ public function write($text) { - $this->stream->write($text instanceof \lang\types\String || $text instanceof \lang\types\Character - ? $text->getBytes($this->charset) - : iconv(\xp::ENCODING, $this->charset, $text) - ); + $this->stream->write(iconv(\xp::ENCODING, $this->charset, $text)); } /** @@ -97,9 +94,6 @@ public function write($text) { * @param string text */ public function writeLine($text= '') { - $this->stream->write(($text instanceof \lang\types\String || $text instanceof \lang\types\Character - ? $text->getBytes($this->charset) - : iconv(\xp::ENCODING, $this->charset, $text) - ).$this->newLine); + $this->stream->write(iconv(\xp::ENCODING, $this->charset, $text).$this->newLine); } } From 0dd89a0ea9ce331f86bfb3f51e6c3b6faff6fbbe Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Thu, 21 Jan 2016 21:51:51 +0100 Subject: [PATCH 09/19] Return int from iteration --- src/main/php/util/Bytes.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/php/util/Bytes.class.php b/src/main/php/util/Bytes.class.php index d410e94c71..b90438a5d2 100755 --- a/src/main/php/util/Bytes.class.php +++ b/src/main/php/util/Bytes.class.php @@ -54,7 +54,7 @@ public function getIterator() { if (!$this->iterator) $this->iterator= newinstance('Iterator', [$this], '{ private $i= 0, $v; public function __construct($v) { $this->v= $v; } - public function current() { $n= ord($this->v->buffer{$this->i}); return new \lang\types\Byte($n < 128 ? $n : $n - 256); } + public function current() { $n= ord($this->v->buffer{$this->i}); return $n < 128 ? $n : $n - 256; } public function key() { return $this->i; } public function next() { $this->i++; } public function rewind() { $this->i= 0; } From 7cdc9dc3e2ff54e5e1fd18cf18797200ba7ddd73 Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Thu, 21 Jan 2016 21:52:51 +0100 Subject: [PATCH 10/19] Remove util.collections tests --- .../AbstractHashImplementationTest.class.php | 44 -- .../collections/ArrayAccessTest.class.php | 187 --------- .../util/collections/ArraysTest.class.php | 83 ---- .../DJBX33AHashImplementationTest.class.php | 37 -- .../util/collections/GenericsTest.class.php | 348 ---------------- .../collections/HashProviderTest.class.php | 41 -- .../util/collections/HashSetTest.class.php | 165 -------- .../util/collections/HashTableTest.class.php | 381 ------------------ .../util/collections/LRUBufferTest.class.php | 131 ------ .../MD5HashImplementationTest.class.php | 37 -- .../MD5HexHashImplementationTest.class.php | 37 -- .../unittest/util/collections/Name.class.php | 39 -- .../util/collections/PairTest.class.php | 99 ----- .../util/collections/QueueTest.class.php | 136 ------- .../util/collections/StackTest.class.php | 82 ---- .../util/collections/VectorTest.class.php | 313 -------------- 16 files changed, 2160 deletions(-) delete mode 100755 src/test/php/net/xp_framework/unittest/util/collections/AbstractHashImplementationTest.class.php delete mode 100644 src/test/php/net/xp_framework/unittest/util/collections/ArrayAccessTest.class.php delete mode 100644 src/test/php/net/xp_framework/unittest/util/collections/ArraysTest.class.php delete mode 100755 src/test/php/net/xp_framework/unittest/util/collections/DJBX33AHashImplementationTest.class.php delete mode 100644 src/test/php/net/xp_framework/unittest/util/collections/GenericsTest.class.php delete mode 100644 src/test/php/net/xp_framework/unittest/util/collections/HashProviderTest.class.php delete mode 100644 src/test/php/net/xp_framework/unittest/util/collections/HashSetTest.class.php delete mode 100644 src/test/php/net/xp_framework/unittest/util/collections/HashTableTest.class.php delete mode 100644 src/test/php/net/xp_framework/unittest/util/collections/LRUBufferTest.class.php delete mode 100755 src/test/php/net/xp_framework/unittest/util/collections/MD5HashImplementationTest.class.php delete mode 100755 src/test/php/net/xp_framework/unittest/util/collections/MD5HexHashImplementationTest.class.php delete mode 100755 src/test/php/net/xp_framework/unittest/util/collections/Name.class.php delete mode 100755 src/test/php/net/xp_framework/unittest/util/collections/PairTest.class.php delete mode 100644 src/test/php/net/xp_framework/unittest/util/collections/QueueTest.class.php delete mode 100644 src/test/php/net/xp_framework/unittest/util/collections/StackTest.class.php delete mode 100644 src/test/php/net/xp_framework/unittest/util/collections/VectorTest.class.php diff --git a/src/test/php/net/xp_framework/unittest/util/collections/AbstractHashImplementationTest.class.php b/src/test/php/net/xp_framework/unittest/util/collections/AbstractHashImplementationTest.class.php deleted file mode 100755 index 37adc97c21..0000000000 --- a/src/test/php/net/xp_framework/unittest/util/collections/AbstractHashImplementationTest.class.php +++ /dev/null @@ -1,44 +0,0 @@ -fixture= $this->newFixture(); - } - - /** - * Creates new fixture - * - * @return util.collections.HashImplementation - */ - protected abstract function newFixture(); - - /** - * Tests hashOf() - */ - #[@test] - public function hashof_returns_non_empty_string_for_empty_input() { - $this->assertNotEquals('', $this->fixture->hashOf('')); - } - - /** - * Tests hashOf() - */ - #[@test] - public function hashof_returns_non_empty_string_for_non_empty_input() { - $this->assertNotEquals('', $this->fixture->hashOf('Test')); - } -} diff --git a/src/test/php/net/xp_framework/unittest/util/collections/ArrayAccessTest.class.php b/src/test/php/net/xp_framework/unittest/util/collections/ArrayAccessTest.class.php deleted file mode 100644 index bc1959b084..0000000000 --- a/src/test/php/net/xp_framework/unittest/util/collections/ArrayAccessTest.class.php +++ /dev/null @@ -1,187 +0,0 @@ -put(new Name('hello'), $world); - $this->assertEquals($world, $c[new Name('hello')]); - } - - #[@test] - public function hashTableReadNonExistantElement() { - $c= new HashTable(); - $this->assertEquals(null, $c[new Name('hello')]); - } - - #[@test, @expect(IllegalArgumentException::class)] - public function hashTableReadIllegalElement() { - $c= create('new util.collections.HashTable()'); - $c[STDIN]; - } - - #[@test] - public function hashTableWriteElement() { - $c= new HashTable(); - $world= new Name('world'); - $c[new Name('hello')]= $world; - $this->assertEquals($world, $c->get(new Name('hello'))); - } - - #[@test, @expect(IllegalArgumentException::class)] - public function hashTableWriteIllegalKey() { - $c= create('new util.collections.HashTable()'); - $c[STDIN]= new Name('Hello'); - } - - #[@test, @expect(IllegalArgumentException::class)] - public function hashTableWriteIllegalValue() { - $c= create('new util.collections.HashTable()'); - $c['hello']= 'scalar'; - } - - #[@test] - public function hashTableTestElement() { - $c= new HashTable(); - $c->put(new Name('hello'), new Name('world')); - $this->assertTrue(isset($c[new Name('hello')])); - $this->assertFalse(isset($c[new Name('world')])); - } - - #[@test] - public function hashTableRemoveElement() { - $c= new HashTable(); - $c->put(new Name('hello'), new Name('world')); - $this->assertTrue(isset($c[new Name('hello')])); - unset($c[new Name('hello')]); - $this->assertFalse(isset($c[new Name('hello')])); - } - - #[@test] - public function vectorReadElement() { - $v= new Vector(); - $world= new Name('world'); - $v->add($world); - $this->assertEquals($world, $v[0]); - } - - #[@test, @expect(IndexOutOfBoundsException::class)] - public function vectorReadNonExistantElement() { - $v= new Vector(); - $v[0]; - } - - #[@test] - public function vectorAddElement() { - $v= new Vector(); - $world= new Name('world'); - $v[]= $world; - $this->assertEquals($world, $v[0]); - } - - #[@test] - public function vectorWriteElement() { - $v= new Vector([new Name('hello')]); - $world= new Name('world'); - $v[0]= $world; - $this->assertEquals($world, $v[0]); - } - - #[@test, @expect(IndexOutOfBoundsException::class)] - public function vectorWriteElementBeyondBoundsKey() { - $v= new Vector(); - $v[0]= new Name('world'); - } - - #[@test, @expect(IndexOutOfBoundsException::class)] - public function vectorWriteElementNegativeKey() { - $v= new Vector(); - $v[-1]= new Name('world'); - } - - #[@test] - public function vectorTestElement() { - $v= new Vector(); - $v[]= new Name('world'); - $this->assertTrue(isset($v[0])); - $this->assertFalse(isset($v[1])); - $this->assertFalse(isset($v[-1])); - } - - #[@test] - public function vectorRemoveElement() { - $v= new Vector(); - $v[]= new Name('world'); - unset($v[0]); - $this->assertFalse(isset($v[0])); - } - - #[@test] - public function vectorIsUsableInForeach() { - $values= [new Name('hello'), new Name('world')]; - foreach (new Vector($values) as $i => $value) { - $this->assertEquals($values[$i], $value); - } - $this->assertEquals(sizeof($values)- 1, $i); - } - - #[@test] - public function hashSetAddElement() { - $s= new HashSet(); - $s[]= new Name('X'); - $this->assertTrue($s->contains(new Name('X'))); - } - - #[@test, @expect(IllegalArgumentException::class)] - public function hashSetWriteElement() { - $s= new HashSet(); - $s[0]= new Name('X'); - } - - #[@test, @expect(IllegalArgumentException::class)] - public function hashSetReadElement() { - $s= new HashSet(); - $s[]= new Name('X'); - $x= $s[0]; - } - - #[@test] - public function hashSetTestElement() { - $s= new HashSet(); - $this->assertFalse(isset($s[new Name('X')])); - $s[]= new Name('X'); - $this->assertTrue(isset($s[new Name('X')])); - } - - #[@test] - public function hashSetRemoveElement() { - $s= new HashSet(); - $s[]= new Name('X'); - unset($s[new Name('X')]); - $this->assertFalse(isset($s[new Name('X')])); - } - - #[@test] - public function hashSetUsableInForeach() { - $s= new HashSet(); - $s->addAll([new Name('0'), new Name('1'), new Name('2')]); - foreach ($s as $i => $element) { - $this->assertEquals(new Name($i), $element); - } - } -} diff --git a/src/test/php/net/xp_framework/unittest/util/collections/ArraysTest.class.php b/src/test/php/net/xp_framework/unittest/util/collections/ArraysTest.class.php deleted file mode 100644 index 22e098de5e..0000000000 --- a/src/test/php/net/xp_framework/unittest/util/collections/ArraysTest.class.php +++ /dev/null @@ -1,83 +0,0 @@ -assertInstanceOf(IList::class, $list); - $this->assertEquals(3, $list->size()); - $this->assertEquals(new Integer(1), $list->get(0)); - $this->assertEquals(new Integer(2), $list->get(1)); - $this->assertEquals(new Integer(3), $list->get(2)); - } - - #[@test] - public function asListWithPrimitives() { - $list= Arrays::asList(new ArrayList('one', 'two', 'three')); - $this->assertInstanceOf(IList::class, $list); - $this->assertEquals(3, $list->size()); - $this->assertEquals('one', $list->get(0)); - $this->assertEquals('two', $list->get(1)); - $this->assertEquals('three', $list->get(2)); - } - - #[@test] - public function empty_array() { - $this->assertEquals(ArrayList::newInstance(0), Arrays::$EMPTY); - } - - #[@test] - public function sort() { - $a= new ArrayList(1, 4, 3, 2); - Arrays::sort($a); - $this->assertEquals(new ArrayList(1, 2, 3, 4), $a); - } - - #[@test] - public function sortWithComparator() { - $a= new ArrayList(new Integer(2), new Integer(4), new Integer(3)); - Arrays::sort($a, newinstance(Comparator::class, [], [ - 'compare' => function($a, $b) { - return $a->value - $b->value; - } - ])); - $this->assertEquals(new ArrayList(new Integer(2), new Integer(3), new Integer(4)), $a); - } - - #[@test] - public function sorted() { - $a= new ArrayList(1, 4, 3, 2); - $this->assertEquals(new ArrayList(1, 2, 3, 4), Arrays::sorted($a)); - $this->assertEquals(new ArrayList(1, 4, 3, 2), $a); - } - - #[@test] - public function containsWithPrimitives() { - $a= new ArrayList(1, 4, 3, 2); - $this->assertTrue(Arrays::contains($a, 1)); - $this->assertFalse(Arrays::contains($a, 5)); - $this->assertFalse(Arrays::contains($a, '1')); - } - - #[@test] - public function containsWithGenerics() { - $a= new ArrayList(new Integer(1), new Integer(2), new Integer(3)); - $this->assertTrue(Arrays::contains($a, new Integer(1))); - $this->assertFalse(Arrays::contains($a, new Integer(5))); - $this->assertFalse(Arrays::contains($a, new Float(1.0))); - } -} diff --git a/src/test/php/net/xp_framework/unittest/util/collections/DJBX33AHashImplementationTest.class.php b/src/test/php/net/xp_framework/unittest/util/collections/DJBX33AHashImplementationTest.class.php deleted file mode 100755 index 8b81fc7251..0000000000 --- a/src/test/php/net/xp_framework/unittest/util/collections/DJBX33AHashImplementationTest.class.php +++ /dev/null @@ -1,37 +0,0 @@ -assertEquals(5381, $this->fixture->hashOf('')); - } - - /** - * Tests hashOf() - */ - #[@test, @ignore('Different result on 64-bit systems!')] - public function hashof_test() { - $this->assertEquals(2090756197, $this->fixture->hashOf('test')); - } -} diff --git a/src/test/php/net/xp_framework/unittest/util/collections/GenericsTest.class.php b/src/test/php/net/xp_framework/unittest/util/collections/GenericsTest.class.php deleted file mode 100644 index bb892abdc0..0000000000 --- a/src/test/php/net/xp_framework/unittest/util/collections/GenericsTest.class.php +++ /dev/null @@ -1,348 +0,0 @@ -assertNotEquals( - create('new util.collections.HashTable'), - create('new util.collections.HashTable') - ); - } - - #[@test] - public function sameGenericHashTablesAreEqual() { - $this->assertEquals( - create('new util.collections.HashTable'), - create('new util.collections.HashTable') - ); - } - - #[@test] - public function differingGenericHashSetsNotEquals() { - $this->assertNotEquals( - create('new util.collections.HashSet'), - create('new util.collections.HashSet') - ); - } - - #[@test] - public function sameGenericHashSetsAreEqual() { - $this->assertEquals( - create('new util.collections.HashSet'), - create('new util.collections.HashSet') - ); - } - - #[@test] - public function differingGenericVectorsNotEquals() { - $this->assertNotEquals( - create('new util.collections.Vector'), - create('new util.collections.Vector') - ); - } - - #[@test] - public function sameGenericVectorsAreEqual() { - $this->assertEquals( - create('new util.collections.Vector'), - create('new util.collections.Vector') - ); - } - - #[@test] - public function differingGenericQueuesNotEquals() { - $this->assertNotEquals( - create('new util.collections.Queue'), - create('new util.collections.Queue') - ); - } - - #[@test] - public function sameGenericQueuesAreEqual() { - $this->assertEquals( - create('new util.collections.Queue'), - create('new util.collections.Queue') - ); - } - - #[@test] - public function differingGenericStacksNotEquals() { - $this->assertNotEquals( - create('new util.collections.Stack'), - create('new util.collections.Stack') - ); - } - - #[@test] - public function sameGenericStacksAreEqual() { - $this->assertEquals( - create('new util.collections.Stack'), - create('new util.collections.Stack') - ); - } - - #[@test] - public function differingGenericLRUBuffersNotEquals() { - $this->assertNotEquals( - create('new util.collections.LRUBuffer', [10]), - create('new util.collections.LRUBuffer', [10]) - ); - } - - #[@test] - public function sameGenericLRUBuffersAreEqual() { - $this->assertEquals( - create('new util.collections.LRUBuffer', [10]), - create('new util.collections.LRUBuffer', [10]) - ); - } - - #[@test, @expect(IllegalArgumentException::class)] - public function nonGenericPassedToCreate() { - create('new lang.Object'); - } - - #[@test] - public function stringVector() { - create('new util.collections.Vector')->add(new \lang\types\String('Hi')); - } - - #[@test] - public function createStringVector() { - $this->assertEquals( - new \lang\types\String('one'), - create('new util.collections.Vector', [new \lang\types\String('one')])->get(0) - ); - } - - #[@test, @expect(IllegalArgumentException::class)] - public function stringVectorAddIllegalValue() { - create('new util.collections.Vector')->add(new \lang\types\Integer(1)); - } - - #[@test, @expect(IllegalArgumentException::class)] - public function stringVectorSetIllegalValue() { - create('new util.collections.Vector', [new \lang\types\String('')])->set(0, new \lang\types\Integer(1)); - } - - #[@test, @expect(IllegalArgumentException::class)] - public function stringVectorContainsIllegalValue() { - create('new util.collections.Vector')->contains(new \lang\types\Integer(1)); - } - - #[@test, @expect(IllegalArgumentException::class)] - public function createStringVectorWithIllegalValue() { - create('new util.collections.Vector', [new \lang\types\Integer(1)]); - } - - #[@test] - public function stringStack() { - create('new util.collections.Stack')->push(new \lang\types\String('One')); - } - - #[@test, @expect(IllegalArgumentException::class)] - public function stringStackPushIllegalValue() { - create('new util.collections.Stack')->push(new \lang\types\Integer(1)); - } - - #[@test, @expect(IllegalArgumentException::class)] - public function stringStackSearchIllegalValue() { - create('new util.collections.Stack')->search(new \lang\types\Integer(1)); - } - - #[@test] - public function stringQueue() { - create('new util.collections.Queue')->put(new \lang\types\String('One')); - } - - #[@test, @expect(IllegalArgumentException::class)] - public function stringQueuePutIllegalValue() { - create('new util.collections.Queue')->put(new \lang\types\Integer(1)); - } - - #[@test, @expect(IllegalArgumentException::class)] - public function stringQueueSearchIllegalValue() { - create('new util.collections.Queue')->search(new \lang\types\Integer(1)); - } - - #[@test, @expect(IllegalArgumentException::class)] - public function stringQueueRemoveIllegalValue() { - create('new util.collections.Queue')->remove(new \lang\types\Integer(1)); - } - - #[@test] - public function stringLRUBuffer() { - create('new util.collections.LRUBuffer', 1)->add(new \lang\types\String('One')); - } - - #[@test, @expect(IllegalArgumentException::class)] - public function stringLRUBufferAddIllegalValue() { - create('new util.collections.LRUBuffer', 1)->add(new \lang\types\Integer(1)); - } - - #[@test, @expect(IllegalArgumentException::class)] - public function stringLRUBufferUpdateIllegalValue() { - create('new util.collections.LRUBuffer', 1)->update(new \lang\types\Integer(1)); - } - - #[@test] - public function stringHashSet() { - create('new util.collections.HashSet')->add(new \lang\types\String('One')); - } - - #[@test, @expect(IllegalArgumentException::class)] - public function stringHashSetAddIllegalValue() { - create('new util.collections.HashSet')->add(new \lang\types\Integer(1)); - } - - #[@test, @expect(IllegalArgumentException::class)] - public function stringHashSetContainsIllegalValue() { - create('new util.collections.HashSet')->contains(new \lang\types\Integer(1)); - } - - #[@test, @expect(IllegalArgumentException::class)] - public function stringHashSetRemoveIllegalValue() { - create('new util.collections.HashSet')->remove(new \lang\types\Integer(1)); - } - - #[@test, @expect(IllegalArgumentException::class)] - public function stringHashSetAddAllIllegalValue() { - create('new util.collections.HashSet')->addAll([ - new \lang\types\String('HELLO'), // Still OK - new \lang\types\Integer(2), // Blam - ]); - } - - #[@test] - public function stringHashSetUnchangedAferAddAllIllegalValue() { - $h= create('new util.collections.HashSet'); - try { - $h->addAll([new \lang\types\String('HELLO'), new \lang\types\Integer(2)]); - } catch (\lang\IllegalArgumentException $expected) { - } - $this->assertTrue($h->isEmpty()); - } - - #[@test] - public function arrayAsKeyLookupWithMatchingKey() { - with ($h= create('new util.collections.HashTable')); { - $h->put(['hello'], new \lang\types\String('World')); - $this->assertEquals(new \lang\types\String('World'), $h->get(['hello'])); - } - } - - #[@test] - public function arrayAsKeyLookupWithMismatchingKey() { - with ($h= create('new util.collections.HashTable')); { - $h->put(['hello'], new \lang\types\String('World')); - $this->assertNull($h->get(['world'])); - } - } - - #[@test, @expect(IllegalArgumentException::class)] - public function arrayAsKeyArrayComponentTypeMismatch() { - create('new util.collections.HashTable')->put([1], new \lang\types\String('World')); - } - - #[@test, @expect(IllegalArgumentException::class)] - public function arrayAsKeyTypeMismatch() { - create('new util.collections.HashTable')->put('hello', new \lang\types\String('World')); - } - - /** - * Tests HashTable with float keys - * - * @see issue://31 - */ - #[@test] - public function floatKeyHashTable() { - $c= create('new util.collections.HashTable'); - $c[0.1]= '1/10'; - $c[0.2]= '2/10'; - $this->assertEquals('1/10', $c[0.1], '0.1'); - $this->assertEquals('2/10', $c[0.2], '0.2'); - } - - /** - * Tests HashSet with floats - * - * @see issue://31 - */ - #[@test] - public function floatInHashSet() { - $c= create('new util.collections.HashSet'); - $c->add(0.1); - $c->add(0.2); - $this->assertEquals([0.1, 0.2], $c->toArray()); - } - - /** - * Tests LRUBuffer with floats - * - * @see issue://31 - */ - #[@test] - public function floatInLRUBuffer() { - $c= create('new util.collections.LRUBuffer', $irrelevant= 10); - $c->add(0.1); - $c->add(0.2); - $this->assertEquals(2, $c->numElements()); - } - - /** - * Tests HashTable::toString() in conjunction with primitives - * - * @see issue://32 - */ - #[@test] - public function primitiveInHashTableToString() { - $c= create('new util.collections.HashTable'); - $c->put('hello', 'World'); - $this->assertNotEquals('', $c->toString()); - } - - /** - * Tests HashSet::toString() in conjunction with primitives - * - * @see issue://32 - */ - #[@test] - public function primitiveInHashSetToString() { - $c= create('new util.collections.HashSet'); - $c->add('hello'); - $this->assertNotEquals('', $c->toString()); - } - - /** - * Tests Vector::toString() in conjunction with primitives - * - * @see issue://32 - */ - #[@test] - public function primitiveInVectorToString() { - $c= create('new util.collections.Vector'); - $c->add('hello'); - $this->assertNotEquals('', $c->toString()); - } -} diff --git a/src/test/php/net/xp_framework/unittest/util/collections/HashProviderTest.class.php b/src/test/php/net/xp_framework/unittest/util/collections/HashProviderTest.class.php deleted file mode 100644 index ce21ca4990..0000000000 --- a/src/test/php/net/xp_framework/unittest/util/collections/HashProviderTest.class.php +++ /dev/null @@ -1,41 +0,0 @@ -fixture= HashProvider::getInstance(); - } - - #[@test] - public function implementation_accessors() { - $impl= newinstance(HashImplementation::class, [], [ - 'hashOf' => function($str) { /* Intentionally empty */ } - ]); - - $backup= $this->fixture->getImplementation(); // Backup - $this->fixture->setImplementation($impl); - $cmp= $this->fixture->getImplementation(); - $this->fixture->setImplementation($backup); // Restore - - $this->assertEquals($impl, $cmp); - } - - #[@test] - public function hashof_uses_implementations_hashof() { - $this->assertEquals( - $this->fixture->getImplementation()->hashOf('Test'), - HashProvider::hashOf('Test') - ); - } -} diff --git a/src/test/php/net/xp_framework/unittest/util/collections/HashSetTest.class.php b/src/test/php/net/xp_framework/unittest/util/collections/HashSetTest.class.php deleted file mode 100644 index 75fbf65377..0000000000 --- a/src/test/php/net/xp_framework/unittest/util/collections/HashSetTest.class.php +++ /dev/null @@ -1,165 +0,0 @@ -set= new HashSet(); - } - - #[@test] - public function initiallyEmpty() { - $this->assertTrue($this->set->isEmpty()); - } - - #[@test] - public function equalsClone() { - $this->set->add(new Name('green')); - $this->assertTrue($this->set->equals(clone($this->set))); - } - - #[@test] - public function equalsOtherSetWithSameContents() { - $other= new HashSet(); - $this->set->add(new Name('color')); - $other->add(new Name('color')); - $this->assertTrue($this->set->equals($other)); - } - - #[@test] - public function doesNotEqualSetWithDifferentContents() { - $other= new HashSet(); - $this->set->add(new Name('blue')); - $other->add(new Name('yellow')); - $this->assertFalse($this->set->equals($other)); - } - - #[@test] - public function add() { - $this->set->add(new Name('green')); - $this->assertFalse($this->set->isEmpty()); - $this->assertEquals(1, $this->set->size()); - } - - #[@test] - public function addAll() { - $array= [new Name('one'), new Name('two'), new Name('three')]; - $this->set->addAll($array); - $this->assertFalse($this->set->isEmpty()); - $this->assertEquals(3, $this->set->size()); - } - - #[@test] - public function addAllUniques() { - $array= [new Name('one'), new Name('one'), new Name('two')]; - $this->set->addAll($array); - $this->assertFalse($this->set->isEmpty()); - $this->assertEquals(2, $this->set->size()); // Name{"one"} and Name{"two"} - } - - #[@test] - public function addAllReturnsWhetherSetHasChanged() { - $array= [new Name('caffeine'), new Name('nicotine')]; - $this->assertTrue($this->set->addAll($array)); - $this->assertFalse($this->set->addAll($array)); - $this->assertFalse($this->set->addAll([new Name('caffeine')])); - $this->assertFalse($this->set->addAll([])); - } - - #[@test] - public function contains() { - $this->set->add(new Name('key')); - $this->assertTrue($this->set->contains(new Name('key'))); - $this->assertFalse($this->set->contains(new Name('non-existant-key'))); - } - - #[@test] - public function addSameValueTwice() { - $color= new Name('green'); - $this->assertTrue($this->set->add($color)); - $this->assertFalse($this->set->add($color)); - } - - #[@test] - public function remove() { - $this->set->add(new Name('key')); - $this->assertTrue($this->set->remove(new Name('key'))); - $this->assertTrue($this->set->isEmpty()); - } - - #[@test] - public function removeOnEmptySet() { - $this->assertFalse($this->set->remove(new Name('irrelevant-set-is-empty-anyway'))); - } - - #[@test] - public function removeNonExistantObject() { - $this->set->add(new Name('key')); - $this->assertFalse($this->set->remove(new Name('non-existant-key'))); - } - - #[@test] - public function clear() { - $this->set->add(new Name('key')); - $this->set->clear(); - $this->assertTrue($this->set->isEmpty()); - } - - #[@test] - public function toArray() { - $color= new Name('red'); - $this->set->add($color); - $this->assertEquals([$color], $this->set->toArray()); - } - - #[@test] - public function toArrayOnEmptySet() { - $this->assertEquals([], $this->set->toArray()); - } - - #[@test] - public function iteration() { - $this->set->add(new Name('1')); - $this->set->add(new Name('2')); - $this->set->add(new Name('3')); - - foreach ($this->set as $i => $value) { - $this->assertEquals(new Name($i+ 1), $value); - } - } - - #[@test] - public function stringRepresentation() { - $this->set->add(new Name('color')); - $this->set->add(new Name('price')); - $this->assertEquals( - "util.collections.HashSet[2] {\n color,\n price\n}", - $this->set->toString() - ); - } - - #[@test] - public function stringRepresentationOfEmptySet() { - $this->assertEquals( - 'util.collections.HashSet[0] { }', - $this->set->toString() - ); - } - - #[@test] - public function addFunction() { - $f= function() { return 'test'; }; - $this->set->add($f); - $this->assertEquals([$f], $this->set->toArray()); - } -} diff --git a/src/test/php/net/xp_framework/unittest/util/collections/HashTableTest.class.php b/src/test/php/net/xp_framework/unittest/util/collections/HashTableTest.class.php deleted file mode 100644 index fdd12cff60..0000000000 --- a/src/test/php/net/xp_framework/unittest/util/collections/HashTableTest.class.php +++ /dev/null @@ -1,381 +0,0 @@ -'), [ - new Pair('hello', new Name('World')), - new Pair('hallo', new Name('Welt')) - ]], - [create('new util.collections.HashTable'), [ - new Pair(new Integer(1), ['one', 'eins']), - new Pair(new Integer(2), ['two', 'zwei']) - ]], - [create('new util.collections.HashTable'), [ - new Pair([1, 2], 3), - new Pair([0, -1], 'Test') - ]], - [create('new util.collections.HashTable'), [ - new Pair('color', function() { return 'purple'; }), - new Pair('price', function() { return 12.99; }), - ]], - ]; - } - - /** @return var[] */ - protected function variations() { - return [ - [new HashTable()], - [create('new util.collections.HashTable')] - ]; - } - - /** @return lang.Object */ - protected function hashCodeCounter() { - return newinstance(Object::class, [], [ - 'invoked' => 0, - 'hashCode' => function() { $this->invoked++; } - ]); - } - - #[@test, @values('fixtures')] - public function can_create($fixture, $pairs) { - // Intentionally empty - } - - #[@test, @values('fixtures')] - public function map_is_initially_empty($fixture, $pairs) { - $this->assertTrue($fixture->isEmpty()); - } - - #[@test, @values('fixtures')] - public function map_size_is_initially_zero($fixture, $pairs) { - $this->assertEquals(0, $fixture->size()); - } - - #[@test, @values('fixtures')] - public function put($fixture, $pairs) { - $fixture->put($pairs[0]->key, $pairs[0]->value); - } - - #[@test, @values('variations')] - public function put_uses_hashCode_for_keys($fixture) { - $object= $this->hashCodeCounter(); - $fixture->put($object, $this); - $this->assertEquals(1, $object->invoked); - } - - #[@test, @values('variations')] - public function put_uses_hashCode_for_values($fixture) { - $object= $this->hashCodeCounter(); - $fixture->put($this, $object); - $this->assertEquals(1, $object->invoked); - } - - #[@test, @values('fixtures')] - public function array_access_for_writing($fixture, $pairs) { - $fixture[$pairs[0]->key]= $pairs[0]->value; - } - - #[@test, @values('fixtures')] - public function put_returns_previously_value($fixture, $pairs) { - $fixture->put($pairs[0]->key, $pairs[0]->value); - $this->assertEquals($pairs[0]->value, $fixture->put($pairs[0]->key, $pairs[1]->value)); - } - - #[@test, @values('fixtures')] - public function map_no_longer_empty_after_put($fixture, $pairs) { - $fixture->put($pairs[0]->key, $pairs[0]->value); - $this->assertFalse($fixture->isEmpty()); - } - - #[@test, @values('fixtures')] - public function map_size_no_longer_zero_after_put($fixture, $pairs) { - $fixture->put($pairs[0]->key, $pairs[0]->value); - $this->assertEquals(1, $fixture->size()); - } - - #[@test, @expect(IllegalArgumentException::class)] - public function put_illegal_type_in_key() { - create('new util.collections.HashTable')->put(5, new Name('hello')); - } - - #[@test, @expect(IllegalArgumentException::class)] - public function put_illegal_type_in_value() { - create('new util.collections.HashTable')->put('hello', new Integer(1)); - } - - #[@test, @expect(IllegalArgumentException::class)] - public function put_raises_when_using_null_for_string_instance() { - create('new util.collections.HashTable')->put('test', null); - } - - #[@test, @expect(IllegalArgumentException::class)] - public function put_raises_when_using_null_for_arrays() { - create('new util.collections.HashTable')->put('test', null); - } - - #[@test, @values('fixtures')] - public function get_returns_null_when_key_does_not_exist($fixture, $pairs) { - $this->assertNull($fixture->get($pairs[0]->key)); - } - - #[@test, @values('fixtures')] - public function get_returns_previously_put_element($fixture, $pairs) { - $fixture->put($pairs[0]->key, $pairs[0]->value); - $this->assertEquals($pairs[0]->value, $fixture->get($pairs[0]->key)); - } - - #[@test, @values('fixtures')] - public function array_access_for_reading_non_existant($fixture, $pairs) { - $this->assertNull($fixture[$pairs[0]->key]); - } - - #[@test, @values('fixtures')] - public function array_access_for_reading($fixture, $pairs) { - $fixture->put($pairs[0]->key, $pairs[0]->value); - $this->assertEquals($pairs[0]->value, $fixture[$pairs[0]->key]); - } - - #[@test, @expect(IllegalArgumentException::class)] - public function get_illegal_type_in_argument() { - create('new util.collections.HashTable')->get(new Integer(1)); - } - - #[@test, @values('fixtures')] - public function containsKey_returns_false_when_element_does_not_exist($fixture, $pairs) { - $this->assertFalse($fixture->containsKey($pairs[0]->key)); - } - - #[@test, @values('fixtures')] - public function containsKey_returns_true_when_element_exists($fixture, $pairs) { - $fixture->put($pairs[0]->key, $pairs[0]->value); - $this->assertTrue($fixture->containsKey($pairs[0]->key)); - } - - #[@test, @values('fixtures')] - public function array_access_for_testing_non_existant($fixture, $pairs) { - $this->assertFalse(isset($fixture[$pairs[0]->key])); - } - - #[@test, @values('fixtures')] - public function array_access_for_testing($fixture, $pairs) { - $fixture->put($pairs[0]->key, $pairs[0]->value); - $this->assertTrue(isset($fixture[$pairs[0]->key])); - } - - #[@test, @expect(IllegalArgumentException::class)] - public function containsKey_illegal_type_in_argument() { - create('new util.collections.HashTable')->containsKey(new Integer(1)); - } - - #[@test, @values('fixtures')] - public function containsValue_returns_false_when_element_does_not_exist($fixture, $pairs) { - $this->assertFalse($fixture->containsValue($pairs[0]->value)); - } - - #[@test, @values('fixtures')] - public function containsValue_returns_true_when_element_exists($fixture, $pairs) { - $fixture->put($pairs[0]->key, $pairs[0]->value); - $this->assertTrue($fixture->containsValue($pairs[0]->value)); - } - - #[@test, @expect(IllegalArgumentException::class)] - public function containsValue_illegal_type_in_argument() { - create('new util.collections.HashTable')->containsValue(new Integer(1)); - } - - #[@test, @values('fixtures')] - public function remove_returns_previously_value($fixture, $pairs) { - $fixture->put($pairs[0]->key, $pairs[0]->value); - $this->assertEquals($pairs[0]->value, $fixture->remove($pairs[0]->key)); - } - - #[@test, @values('fixtures')] - public function remove_previously_put_element($fixture, $pairs) { - $fixture->put($pairs[0]->key, $pairs[0]->value); - $fixture->remove($pairs[0]->key); - $this->assertFalse($fixture->containsKey($pairs[0]->key)); - } - - #[@test, @values('fixtures')] - public function array_access_for_removing($fixture, $pairs) { - $fixture->put($pairs[0]->key, $pairs[0]->value); - unset($fixture[$pairs[0]->key]); - $this->assertFalse($fixture->containsKey($pairs[0]->key)); - } - - #[@test, @values('fixtures')] - public function remove_non_existant_element($fixture, $pairs) { - $fixture->remove($pairs[0]->key); - $this->assertFalse($fixture->containsKey($pairs[0]->key)); - } - - #[@test, @values('fixtures')] - public function array_access_for_removing_non_existant_element($fixture, $pairs) { - unset($fixture[$pairs[0]->key]); - $this->assertFalse($fixture->containsKey($pairs[0]->key)); - } - - #[@test, @expect(IllegalArgumentException::class)] - public function remove_illegal_type_in_argument() { - create('new util.collections.HashTable')->remove(new Integer(1)); - } - - #[@test, @values('fixtures')] - public function equals_its_clone($fixture, $pairs) { - $fixture->put($pairs[0]->key, $pairs[0]->value); - $this->assertEquals($fixture, clone $fixture); - } - - #[@test, @values('fixtures')] - public function equals_its_clone_when_empty($fixture, $pairs) { - $this->assertEquals($fixture, clone $fixture); - } - - #[@test, @values('fixtures')] - public function does_not_equal_empty_map($fixture, $pairs) { - $other= clone $fixture; - $fixture->put($pairs[0]->key, $pairs[0]->value); - $this->assertNotEquals($fixture, $other); - } - - #[@test, @values('fixtures')] - public function does_not_equal_map_with_different_elements($fixture, $pairs) { - $other= clone $fixture; - $fixture->put($pairs[0]->key, $pairs[0]->value); - $other->put($pairs[1]->key, $pairs[1]->value); - $this->assertNotEquals($fixture, $other); - } - - #[@test, @values('fixtures')] - public function clear($fixture, $pairs) { - $fixture->put($pairs[0]->key, $pairs[0]->value); - $fixture->clear(); - $this->assertTrue($fixture->isEmpty()); - } - - #[@test, @values('fixtures')] - public function keys_returns_empty_array_for_empty_map($fixture, $pairs) { - $this->assertEquals([], $fixture->keys()); - } - - #[@test, @values('fixtures')] - public function keys_returns_array_of_added_keys($fixture, $pairs) { - $fixture->put($pairs[0]->key, $pairs[0]->value); - $fixture->put($pairs[1]->key, $pairs[1]->value); - $this->assertEquals([$pairs[0]->key, $pairs[1]->key], $fixture->keys()); - } - - #[@test, @values('fixtures')] - public function values_returns_empty_array_for_empty_map($fixture, $pairs) { - $this->assertEquals([], $fixture->values()); - } - - #[@test, @values('fixtures')] - public function values_returns_array_of_added_values($fixture, $pairs) { - $fixture->put($pairs[0]->key, $pairs[0]->value); - $fixture->put($pairs[1]->key, $pairs[1]->value); - $this->assertEquals([$pairs[0]->value, $pairs[1]->value], $fixture->values()); - } - - #[@test, @values('fixtures')] - public function can_be_used_in_foreach($fixture, $pairs) { - $fixture->put($pairs[0]->key, $pairs[0]->value); - $fixture->put($pairs[1]->key, $pairs[1]->value); - $iterated= []; - foreach ($fixture as $pair) { - $iterated[]= $pair; - } - $this->assertEquals([$pairs[0], $pairs[1]], $iterated); - } - - #[@test, @values('fixtures')] - public function can_be_used_in_foreach_with_empty_map($fixture, $pairs) { - $iterated= []; - foreach ($fixture as $pair) { - $iterated[]= $pair; - } - $this->assertEquals([], $iterated); - } - - #[@test, @values('fixtures')] - public function iteration_invoked_twice($fixture, $pairs) { - $fixture->put($pairs[0]->key, $pairs[0]->value); - $fixture->put($pairs[1]->key, $pairs[1]->value); - $iterated= []; - foreach ($fixture as $pair) { - $iterated[]= $pair; - } - foreach ($fixture as $pair) { - $iterated[]= $pair; - } - $this->assertEquals([$pairs[0], $pairs[1], $pairs[0], $pairs[1]], $iterated); - } - - #[@test, @values('fixtures')] - public function second_iteration_with_break_statement($fixture, $pairs) { - $fixture->put($pairs[0]->key, $pairs[0]->value); - $fixture->put($pairs[1]->key, $pairs[1]->value); - $iterated= []; - foreach ($fixture as $pair) { - $iterated[]= $pair; - } - foreach ($fixture as $pair) { - break; - } - $this->assertEquals([$pairs[0], $pairs[1]], $iterated); - } - - #[@test] - public function string_representation_of_empty_map() { - $this->assertEquals( - 'util.collections.HashTable[0] { }', - (new HashTable())->toString() - ); - } - - #[@test] - public function string_representation_of_map_with_contents() { - $fixture= new HashTable(); - $fixture->put('hello', 'World'); - $fixture->put('hallo', 'Welt'); - $this->assertEquals( - "util.collections.HashTable[2] {\n". - " \"hello\" => \"World\",\n". - " \"hallo\" => \"Welt\"\n". - "}", - $fixture->toString() - ); - } - - #[@test] - public function string_representation_of_generic_map() { - $this->assertEquals( - 'util.collections.HashTable[0] { }', - create('new util.collections.HashTable')->toString() - ); - } -} diff --git a/src/test/php/net/xp_framework/unittest/util/collections/LRUBufferTest.class.php b/src/test/php/net/xp_framework/unittest/util/collections/LRUBufferTest.class.php deleted file mode 100644 index 6f17ecac60..0000000000 --- a/src/test/php/net/xp_framework/unittest/util/collections/LRUBufferTest.class.php +++ /dev/null @@ -1,131 +0,0 @@ -buffer= new LRUBuffer(self::DEFAULT_SIZE); - } - - #[@test] - public function initiallyEmpty() { - $this->assertEquals(0, $this->buffer->numElements()); - } - - #[@test] - public function getSize() { - $this->assertEquals(self::DEFAULT_SIZE, $this->buffer->getSize()); - } - - #[@test] - public function add() { - $this->buffer->add(new Name('one')); - $this->assertEquals(1, $this->buffer->numElements()); - } - - #[@test] - public function addReturnsVictim() { - - // We should be able to add at least as many as the buffer's size - // elements to the LRUBuffer. Nothing should be deleted from it - // during this loop. - for ($i= 0, $s= $this->buffer->getSize(); $i < $s; $i++) { - if (null === ($victim= $this->buffer->add(new Name('item #'.$i)))) continue; - - return $this->fail( - 'Victim '.\xp::stringOf($victim).' when inserting item #'.($i + 1).'/'.$s, - $victim, - null - ); - } - - // The LRUBuffer is now "full". Next time we add something, the - // element last recently used should be returned. - $this->assertEquals( - new Name('item #0'), - $this->buffer->add(new Name('last item')) - ); - } - - /** - * Add a specified number of strings to the buffer. - * - * @param int num - */ - protected function addElements($num) { - for ($i= 0; $i < $num; $i++) { - $this->buffer->add(new Name('item #'.$i)); - } - } - - #[@test] - public function bufferDoesNotGrowBeyondSize() { - $this->addElements($this->buffer->getSize()+ 1); - $this->assertEquals($this->buffer->getSize(), $this->buffer->numElements()); - } - - #[@test] - public function update() { - - // Fill the LRUBuffer until its size is reached - $this->addElements($this->buffer->getSize()); - - // Update the first item - $this->buffer->update(new Name('item #0')); - - // Now the second item should be chosen the victim when adding - // another element - $this->assertEquals( - new Name('item #1'), - $this->buffer->add(new Name('last item')) - ); - } - - #[@test] - public function setSize() { - $this->buffer->setSize(10); - $this->assertEquals(10, $this->buffer->getSize()); - } - - #[@test, @expect(IllegalArgumentException::class)] - public function illegalSize() { - $this->buffer->setSize(0); - } - - #[@test] - public function equalsClone() { - $this->assertTrue($this->buffer->equals(clone $this->buffer)); - } - - #[@test] - public function doesNotEqualWithDifferentSize() { - $this->assertFalse($this->buffer->equals(new LRUBuffer(self::DEFAULT_SIZE - 1))); - } - - #[@test] - public function doesNotEqualWithSameElements() { - $other= new LRUBuffer(self::DEFAULT_SIZE); - with ($string= new Name('Hello')); { - $other->add($string); - $this->buffer->add($string); - } - $this->assertFalse($this->buffer->equals($other)); - } - - #[@test] - public function addFunction() { - $f= function() { return 'test'; }; - $this->buffer->add($f); - $this->assertEquals(1, $this->buffer->numElements()); - } -} diff --git a/src/test/php/net/xp_framework/unittest/util/collections/MD5HashImplementationTest.class.php b/src/test/php/net/xp_framework/unittest/util/collections/MD5HashImplementationTest.class.php deleted file mode 100755 index 5e115cfb32..0000000000 --- a/src/test/php/net/xp_framework/unittest/util/collections/MD5HashImplementationTest.class.php +++ /dev/null @@ -1,37 +0,0 @@ -assertEquals('0xd41d8cd98f00b204e9800998ecf8427e', $this->fixture->hashOf('')); - } - - /** - * Tests hashOf() - */ - #[@test] - public function hashof_test() { - $this->assertEquals('0x098f6bcd4621d373cade4e832627b4f6', $this->fixture->hashOf('test')); - } -} diff --git a/src/test/php/net/xp_framework/unittest/util/collections/MD5HexHashImplementationTest.class.php b/src/test/php/net/xp_framework/unittest/util/collections/MD5HexHashImplementationTest.class.php deleted file mode 100755 index b57589694b..0000000000 --- a/src/test/php/net/xp_framework/unittest/util/collections/MD5HexHashImplementationTest.class.php +++ /dev/null @@ -1,37 +0,0 @@ -assertEquals(0xd41d8cd98f00b204e9800998ecf8427e, $this->fixture->hashOf('')); - } - - /** - * Tests hashOf() - */ - #[@test] - public function hashof_test() { - $this->assertEquals(0x098f6bcd4621d373cade4e832627b4f6, $this->fixture->hashOf('test')); - } -} diff --git a/src/test/php/net/xp_framework/unittest/util/collections/Name.class.php b/src/test/php/net/xp_framework/unittest/util/collections/Name.class.php deleted file mode 100755 index 9dfd83aaa4..0000000000 --- a/src/test/php/net/xp_framework/unittest/util/collections/Name.class.php +++ /dev/null @@ -1,39 +0,0 @@ -value= (string)$value; } - - /** @return string */ - public function value() { return $this->value; } - - /** - * Returns a hashcode - * - * @return string - */ - public function hashCode() { - return crc32($this->value); - } - - /** - * Returns whether this name is equal to another - * - * @param var $cmp - * @return bool - */ - public function equals($cmp) { - return $cmp instanceof self && $this->value === $cmp->value; - } - - /** - * Returns a string representation - * - * @return string - */ - public function toString() { - return $this->value; - } -} \ No newline at end of file diff --git a/src/test/php/net/xp_framework/unittest/util/collections/PairTest.class.php b/src/test/php/net/xp_framework/unittest/util/collections/PairTest.class.php deleted file mode 100755 index 23e06c322f..0000000000 --- a/src/test/php/net/xp_framework/unittest/util/collections/PairTest.class.php +++ /dev/null @@ -1,99 +0,0 @@ -assertEquals('key', $p->key); - } - - #[@test] - public function value() { - $p= new Pair(null, 'value'); - $this->assertEquals('value', $p->value); - } - - #[@test] - public function equals_same_instance() { - $p= new Pair(null, null); - $this->assertEquals($p, $p); - } - - #[@test] - public function equals_null_key_null_value() { - $this->assertEquals(new Pair(null, null), new Pair(null, null)); - } - - #[@test] - public function equals_primitive_key_null_value() { - $this->assertEquals(new Pair('key', null), new Pair('key', null)); - } - - #[@test] - public function equals_primitive_key_primitive_value() { - $this->assertEquals(new Pair('key', 'value'), new Pair('key', 'value')); - } - - #[@test] - public function equals_key_instance_value_instance() { - $this->assertEquals( - new Pair(new Name('key'), new Name('value')), - new Pair(new Name('key'), new Name('value')) - ); - } - - #[@test] - public function primitive_key_and_value_not_equal_to_null_key_and_value() { - $this->assertNotEquals(new Pair('key', 'value'), new Pair(null, null)); - } - - #[@test] - public function instance_key_and_value_not_equal_to_null_key_and_value() { - $this->assertNotEquals( - new Pair(new Name('key'), new Name('value')), - new Pair(null, null) - ); - } - - #[@test] - public function pair_not_equals_to_null() { - $this->assertNotEquals(new Pair(null, null), null); - } - - #[@test] - public function hashcode_of_nulls_equal() { - $this->assertEquals( - (new Pair(null, null))->hashCode(), - (new Pair(null, null))->hashCode() - ); - } - - #[@test] - public function hashcode_of_different_keys_not_equal() { - $this->assertNotEquals( - (new Pair(null, null))->hashCode(), - (new Pair('key', null))->hashCode() - ); - } - - #[@test] - public function hashcode_of_different_values_not_equal() { - $this->assertNotEquals( - (new Pair(null, null))->hashCode(), - (new Pair(null, 'value'))->hashCode() - ); - } -} diff --git a/src/test/php/net/xp_framework/unittest/util/collections/QueueTest.class.php b/src/test/php/net/xp_framework/unittest/util/collections/QueueTest.class.php deleted file mode 100644 index 3bcff87003..0000000000 --- a/src/test/php/net/xp_framework/unittest/util/collections/QueueTest.class.php +++ /dev/null @@ -1,136 +0,0 @@ -queue= new Queue(); - } - - #[@test] - public function initiallyEmpty() { - $this->assertTrue($this->queue->isEmpty()); - } - - #[@test] - public function equalsClone() { - $this->queue->put(new Name('green')); - $this->assertTrue($this->queue->equals(clone($this->queue))); - } - - #[@test] - public function put() { - $this->queue->put(new Name('green')); - $this->assertFalse($this->queue->isEmpty()); - $this->assertEquals(1, $this->queue->size()); - } - - #[@test] - public function get() { - $color= new Name('red'); - $this->queue->put($color); - $this->assertEquals($color, $this->queue->get()); - $this->assertTrue($this->queue->isEmpty()); - } - - #[@test, @expect(NoSuchElementException::class)] - public function exceptionOnNoMoreElements() { - $this->queue->get(); - } - - #[@test] - public function peek() { - $color= new Name('blue'); - $this->queue->put($color); - $this->assertEquals($color, $this->queue->peek()); - $this->assertFalse($this->queue->isEmpty()); - } - - #[@test] - public function peekReturnsNullOnNoMoreElements() { - $this->assertNull($this->queue->peek()); - } - - #[@test] - public function remove() { - $color= new Name('blue'); - $this->queue->put($color); - $this->queue->remove($color); - $this->assertTrue($this->queue->isEmpty()); - } - - #[@test] - public function removeReturnsWhetherDeleted() { - $color= new Name('pink'); - $this->queue->put($color); - $this->assertTrue($this->queue->remove($color)); - $this->assertFalse($this->queue->remove(new Name('purple'))); - $this->assertTrue($this->queue->isEmpty()); - $this->assertFalse($this->queue->remove($color)); - $this->assertFalse($this->queue->remove(new Name('purple'))); - } - - #[@test] - public function elementAt() { - $this->queue->put(new Name('red')); - $this->queue->put(new Name('green')); - $this->queue->put(new Name('blue')); - $this->assertEquals(new Name('red'), $this->queue->elementAt(0)); - $this->assertEquals(new Name('green'), $this->queue->elementAt(1)); - $this->assertEquals(new Name('blue'), $this->queue->elementAt(2)); - } - - #[@test] - public function iterativeUse() { - $input= [new Name('red'), new Name('green'), new Name('blue')]; - - // Add - for ($i= 0, $s= sizeof($input); $i < sizeof($input); $i++) { - $this->queue->put($input[$i]); - } - - // Retrieve - $i= 0; - while (!$this->queue->isEmpty()) { - $element= $this->queue->get(); - - if (!$input[$i]->equals($element)) { - $this->fail('Not equal at offset #'.$i, $element, $input[$i]); - break; - } - $i++; - } - } - - #[@test, @expect(IndexOutOfBoundsException::class)] - public function elementAtIllegalOffset() { - $this->queue->elementAt(-1); - } - - #[@test, @expect(IndexOutOfBoundsException::class)] - public function elementAtOffsetOutOfBounds() { - $this->queue->put(new Name('one')); - $this->queue->elementAt($this->queue->size() + 1); - } - - #[@test, @expect(IndexOutOfBoundsException::class)] - public function elementAtEmptyList() { - $this->queue->elementAt(0); - } - - #[@test] - public function addFunction() { - $f= function() { return 'test'; }; - $this->queue->put($f); - $this->assertEquals($f, $this->queue->elementAt(0)); - } -} diff --git a/src/test/php/net/xp_framework/unittest/util/collections/StackTest.class.php b/src/test/php/net/xp_framework/unittest/util/collections/StackTest.class.php deleted file mode 100644 index 02f3fb3107..0000000000 --- a/src/test/php/net/xp_framework/unittest/util/collections/StackTest.class.php +++ /dev/null @@ -1,82 +0,0 @@ -stack= new Stack(); - } - - #[@test] - public function initiallyEmpty() { - $this->assertTrue($this->stack->isEmpty()); - } - - #[@test] - public function equalsClone() { - $this->stack->push(new Name('green')); - $this->assertTrue($this->stack->equals(clone($this->stack))); - } - - #[@test] - public function push() { - $this->stack->push(new Name('green')); - $this->assertFalse($this->stack->isEmpty()); - $this->assertEquals(1, $this->stack->size()); - } - - #[@test] - public function pop() { - $color= new Name('green'); - $this->stack->push($color); - $this->assertEquals($color, $this->stack->pop()); - $this->assertTrue($this->stack->isEmpty()); - } - - #[@test] - public function peek() { - $color= new Name('green'); - $this->stack->push($color); - $this->assertEquals($color, $this->stack->peek()); - $this->assertFalse($this->stack->isEmpty()); - } - - #[@test] - public function search() { - $color= new Name('green'); - $this->stack->push($color); - $this->assertEquals(0, $this->stack->search($color)); - $this->assertEquals(-1, $this->stack->search(new Name('non-existant'))); - } - - #[@test] - public function elementAt() { - $this->stack->push(new Name('red')); - $this->stack->push(new Name('green')); - $this->stack->push(new Name('blue')); - - $this->assertEquals(new Name('blue'), $this->stack->elementAt(0)); - $this->assertEquals(new Name('green'), $this->stack->elementAt(1)); - $this->assertEquals(new Name('red'), $this->stack->elementAt(2)); - } - - #[@test, @expect(IndexOutOfBoundsException::class)] - public function elementAtIllegalOffset() { - $this->stack->elementAt(-1); - } - - #[@test] - public function addFunction() { - $f= function() { return 'test'; }; - $this->stack->push($f); - $this->assertEquals($f, $this->stack->elementAt(0)); - } -} diff --git a/src/test/php/net/xp_framework/unittest/util/collections/VectorTest.class.php b/src/test/php/net/xp_framework/unittest/util/collections/VectorTest.class.php deleted file mode 100644 index 3d59297c7f..0000000000 --- a/src/test/php/net/xp_framework/unittest/util/collections/VectorTest.class.php +++ /dev/null @@ -1,313 +0,0 @@ -assertTrue((new Vector())->isEmpty()); - } - - #[@test] - public function sizeOfEmptyVector() { - $this->assertEquals(0, (new Vector())->size()); - } - - #[@test] - public function nonEmptyVector() { - $v= new Vector([new Object()]); - $this->assertEquals(1, $v->size()); - $this->assertFalse($v->isEmpty()); - } - - #[@test] - public function adding() { - $v= new Vector(); - $v->add(new Object()); - $this->assertEquals(1, $v->size()); - } - - #[@test] - public function addAllArray() { - $v= new Vector(); - $this->assertTrue($v->addAll([new Object(), new Object()])); - $this->assertEquals(2, $v->size()); - } - - #[@test] - public function addAllVector() { - $v1= new Vector(); - $v2= new Vector(); - $v2->add(new Object()); - $v2->add(new Object()); - $this->assertTrue($v1->addAll($v2)); - $this->assertEquals(2, $v1->size()); - } - - #[@test] - public function addAllArrayList() { - $v= new Vector(); - $this->assertTrue($v->addAll(new ArrayList(new Object(), new Object()))); - $this->assertEquals(2, $v->size()); - } - - #[@test] - public function addAllEmptyArray() { - $this->assertFalse((new Vector())->addAll([])); - } - - #[@test] - public function addAllEmptyVector() { - $this->assertFalse((new Vector())->addAll(new Vector())); - } - - #[@test] - public function addAllEmptyArrayList() { - $this->assertFalse((new Vector())->addAll(new ArrayList())); - } - - #[@test] - public function unchangedAfterNullInAddAll() { - $v= create('new util.collections.Vector()'); - try { - $v->addAll([new Object(), null]); - $this->fail('addAll() did not throw an exception', null, 'lang.IllegalArgumentException'); - } catch (IllegalArgumentException $expected) { - } - $this->assertTrue($v->isEmpty()); - } - - #[@test] - public function unchangedAfterIntInAddAll() { - $v= create('new util.collections.Vector()'); - try { - $v->addAll(['hello', 5]); - $this->fail('addAll() did not throw an exception', null, 'lang.IllegalArgumentException'); - } catch (IllegalArgumentException $expected) { - } - $this->assertTrue($v->isEmpty()); - } - - #[@test, @expect(IllegalArgumentException::class)] - public function addingNull() { - create('new util.collections.Vector()')->add(null); - } - - #[@test] - public function replacing() { - $v= new Vector(); - $o= new Name('one'); - $v->add($o); - $r= $v->set(0, new Name('two')); - $this->assertEquals(1, $v->size()); - $this->assertEquals($o, $r); - } - - #[@test, @expect(IllegalArgumentException::class)] - public function replacingWithNull() { - create('new util.collections.Vector', [new Object()])->set(0, null); - } - - #[@test, @expect(IndexOutOfBoundsException::class)] - public function settingPastEnd() { - (new Vector())->set(0, new Object()); - } - - #[@test, @expect(IndexOutOfBoundsException::class)] - public function settingNegative() { - (new Vector())->set(-1, new Object()); - } - - #[@test] - public function reading() { - $v= new Vector(); - $o= new Name('one'); - $v->add($o); - $r= $v->get(0); - $this->assertEquals($o, $r); - } - - #[@test, @expect(IndexOutOfBoundsException::class)] - public function readingPastEnd() { - (new Vector())->get(0); - } - - #[@test, @expect(IndexOutOfBoundsException::class)] - public function readingNegative() { - (new Vector())->get(-1); - } - - #[@test] - public function removing() { - $v= new Vector(); - $o= new Name('one'); - $v->add($o); - $r= $v->remove(0); - $this->assertEquals(0, $v->size()); - $this->assertEquals($o, $r); - } - - #[@test, @expect(IndexOutOfBoundsException::class)] - public function removingPastEnd() { - (new Vector())->get(0); - } - - #[@test, @expect(IndexOutOfBoundsException::class)] - public function removingNegative() { - (new Vector())->get(-1); - } - - #[@test] - public function clearing() { - $v= new Vector([new Name('Goodbye cruel world')]); - $this->assertFalse($v->isEmpty()); - $v->clear(); - $this->assertTrue($v->isEmpty()); - } - - #[@test] - public function elementsOfEmptyVector() { - $this->assertEquals([], (new Vector())->elements()); - } - - #[@test] - public function elementsOf() { - $el= [new Name('a'), new Object()]; - $this->assertEquals($el, (new Vector($el))->elements()); - } - - #[@test] - public function addedNameIsContained() { - $v= new Vector(); - $o= new Name('one'); - $v->add($o); - $this->assertTrue($v->contains($o)); - } - - #[@test] - public function emptyVectorDoesNotContainName() { - $this->assertFalse((new Vector())->contains(new Object())); - } - - #[@test] - public function indexOfOnEmptyVector() { - $this->assertFalse((new Vector())->indexOf(new Object())); - } - - #[@test] - public function indexOf() { - $a= new Name('A'); - $this->assertEquals(0, (new Vector([$a]))->indexOf($a)); - } - - #[@test] - public function indexOfElementContainedTwice() { - $a= new Name('A'); - $this->assertEquals(0, (new Vector([$a, new Object(), $a]))->indexOf($a)); - } - - #[@test] - public function lastIndexOfOnEmptyVector() { - $this->assertFalse((new Vector())->lastIndexOf(new Object())); - } - - #[@test] - public function lastIndexOf() { - $a= new Name('A'); - $this->assertEquals(0, (new Vector([$a]))->lastIndexOf($a)); - } - - #[@test] - public function lastIndexOfElementContainedTwice() { - $a= new Name('A'); - $this->assertEquals(2, (new Vector([$a, new Object(), $a]))->lastIndexOf($a)); - } - - #[@test] - public function stringOfEmptyVector() { - $this->assertEquals( - "util.collections.Vector[0]@{\n}", - (new Vector())->toString() - ); - } - - #[@test] - public function stringOf() { - $this->assertEquals( - "util.collections.Vector[2]@{\n 0: One\n 1: Two\n}", - (new Vector([new Name('One'), new Name('Two')]))->toString() - ); - } - - #[@test] - public function iteration() { - $v= new Vector(); - for ($i= 0; $i < 5; $i++) { - $v->add(new Name('#'.$i)); - } - - $i= 0; - foreach ($v as $offset => $string) { - $this->assertEquals($offset, $i); - $this->assertEquals(new Name('#'.$i), $string); - $i++; - } - } - - #[@test] - public function twoEmptyVectorsAreEqual() { - $this->assertTrue((new Vector())->equals(new Vector())); - } - - #[@test] - public function sameVectorsAreEqual() { - $a= new Vector([new Name('One'), new Name('Two')]); - $this->assertTrue($a->equals($a)); - } - - #[@test] - public function vectorsWithSameContentsAreEqual() { - $a= new Vector([new Name('One'), new Name('Two')]); - $b= new Vector([new Name('One'), new Name('Two')]); - $this->assertTrue($a->equals($b)); - } - - #[@test] - public function aVectorIsNotEqualToNull() { - $this->assertFalse((new Vector())->equals(null)); - } - - #[@test] - public function twoVectorsOfDifferentSizeAreNotEqual() { - $this->assertFalse((new Vector([new Object()]))->equals(new Vector())); - } - - #[@test] - public function orderMattersForEquality() { - $a= [new Name('a'), new Name('b')]; - $b= [new Name('b'), new Name('a')]; - $this->assertFalse((new Vector($a))->equals(new Vector($b))); - } - - #[@test] - public function addFunction() { - $f= function() { return 'test'; }; - $this->assertEquals($f, (new Vector([$f]))[0]); - } - - #[@test] - public function addFunctions() { - $f= [function() { return 'one'; }, function() { return 'two'; }]; - $this->assertEquals($f, (new Vector($f))->elements()); - } -} From c26cf72717e41f8bb720a5a21766a01ee2d7c786 Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Thu, 21 Jan 2016 21:54:14 +0100 Subject: [PATCH 11/19] Remove deprecated boxing, unboxing and access to wrapper types --- src/main/php/lang/Primitive.class.php | 86 ++------------------------- 1 file changed, 4 insertions(+), 82 deletions(-) diff --git a/src/main/php/lang/Primitive.class.php b/src/main/php/lang/Primitive.class.php index cb7f5eabc6..605f20c952 100755 --- a/src/main/php/lang/Primitive.class.php +++ b/src/main/php/lang/Primitive.class.php @@ -25,61 +25,6 @@ static function __static() { self::$BOOL= new self('bool', false); } - /** - * Returns the wrapper class for this primitive - * - * @deprecated Wrapper types will move to their own library - * @see http://en.wikipedia.org/wiki/Wrapper_class - * @return lang.XPClass - */ - public function wrapperClass() { - switch ($this) { - case self::$STRING: return XPClass::forName('lang.types.String'); - case self::$INT: return XPClass::forName('lang.types.Integer'); - case self::$DOUBLE: return XPClass::forName('lang.types.Double'); - case self::$BOOL: return XPClass::forName('lang.types.Boolean'); - } - } - - /** - * Boxes a type - that is, turns Generics into primitives - * - * @deprecated Wrapper types will move to their own library - * @param var in - * @return var the primitive if not already primitive - * @throws lang.IllegalArgumentException in case in cannot be unboxed. - */ - public static function unboxed($in) { - if ($in instanceof \lang\types\String) return $in->toString(); - if ($in instanceof \lang\types\Double) return $in->doubleValue(); - if ($in instanceof \lang\types\Integer) return $in->intValue(); - if ($in instanceof \lang\types\Boolean) return $in->value; - if ($in instanceof \lang\types\ArrayList) return $in->values; // deprecated - if ($in instanceof Generic) { - throw new IllegalArgumentException('Cannot unbox '.\xp::typeOf($in)); - } - return $in; // Already primitive - } - - /** - * Boxes a type - that is, turns primitives into Generics - * - * @deprecated Wrapper types will move to their own library - * @param var in - * @return lang.Generic the Generic if not already generic - * @throws lang.IllegalArgumentException in case in cannot be boxed. - */ - public static function boxed($in) { - if (null === $in || $in instanceof Generic) return $in; - $t= gettype($in); - if ('string' === $t) return new \lang\types\String($in); - if ('integer' === $t) return new \lang\types\Integer($in); - if ('double' === $t) return new \lang\types\Double($in); - if ('boolean' === $t) return new \lang\types\Boolean($in); - if ('array' === $t) return \lang\types\ArrayList::newInstance($in); // deprecated - throw new IllegalArgumentException('Cannot box '.\xp::typeOf($in)); - } - /** * Get a type instance for a given name * @@ -129,33 +74,10 @@ public function isInstance($obj) { */ protected function coerce($value, $default) { if (!is_array($value)) switch ($this) { - case self::$STRING: - if ($value instanceof \lang\types\String) return $value->toString(); - if ($value instanceof \lang\types\Number) return (string)$value->value; - if ($value instanceof \lang\types\Boolean) return (string)$value->value; - if ($value instanceof \lang\types\Generic) return $value->toString(); - return (string)$value; - - case self::$INT: - if ($value instanceof \lang\types\String) return (int)$value->toString(); - if ($value instanceof \lang\types\Number) return $value->intValue(); - if ($value instanceof \lang\types\Boolean) return (int)$value->value; - if ($value instanceof \lang\types\Generic) return (int)$value->toString(); - return (int)$value; - - case self::$DOUBLE: - if ($value instanceof \lang\types\String) return (double)$value->toString(); - if ($value instanceof \lang\types\Number) return $value->doubleValue(); - if ($value instanceof \lang\types\Boolean) return (double)$value->value; - if ($value instanceof \lang\types\Generic) return (double)$value->toString(); - return (double)$value; - - case self::$BOOL: - if ($value instanceof \lang\types\String) return (bool)$value->toString(); - if ($value instanceof \lang\types\Number) return (bool)$value->value; - if ($value instanceof \lang\types\Boolean) return $value->value; - if ($value instanceof \lang\types\Generic) return (bool)$value->toString(); - return (bool)$value; + case self::$STRING: return (string)$value; + case self::$INT: return (int)$value; + case self::$DOUBLE: return (double)$value; + case self::$BOOL: return (bool)$value; } return $default($value); From 0ae510dc10ae520a929e9f18e7e0d44041c0f4cd Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Thu, 21 Jan 2016 21:56:32 +0100 Subject: [PATCH 12/19] QA: Use hex escape sequences for special chars in class names --- .../unittest/core/generics/AnonymousInstanceTest.class.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/test/php/net/xp_framework/unittest/core/generics/AnonymousInstanceTest.class.php b/src/test/php/net/xp_framework/unittest/core/generics/AnonymousInstanceTest.class.php index 8800c4d7db..8c49356ab7 100755 --- a/src/test/php/net/xp_framework/unittest/core/generics/AnonymousInstanceTest.class.php +++ b/src/test/php/net/xp_framework/unittest/core/generics/AnonymousInstanceTest.class.php @@ -1,6 +1,5 @@ ', [])); - $this->assertEquals('util.collections.Vector��lang�Object', substr($name, 0, strrpos($name, '�')), $name); + $this->assertEquals("util.collections.Vector\xb7\xb7lang\xa6Object", substr($name, 0, strrpos($name, "\xb7")), $name); } #[@test] @@ -41,8 +40,8 @@ protected function accept($e) { return true; } }'); $n= nameof($instance); $this->assertEquals( - 'net.xp_framework.unittest.core.generics.ArrayFilter��lang�Object', - substr($n, 0, strrpos($n, '�')), + "net.xp_framework.unittest.core.generics.ArrayFilter\xb7\xb7lang\xa6Object", + substr($n, 0, strrpos($n, "\xb7")), $n ); } From e5204b8ebe3a89bb26ba3f23db977d9edbb0f8de Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Thu, 21 Jan 2016 21:57:30 +0100 Subject: [PATCH 13/19] Fix iteration() test --- src/test/php/net/xp_framework/unittest/util/BytesTest.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/php/net/xp_framework/unittest/util/BytesTest.class.php b/src/test/php/net/xp_framework/unittest/util/BytesTest.class.php index 562bb2116b..4067c60f93 100755 --- a/src/test/php/net/xp_framework/unittest/util/BytesTest.class.php +++ b/src/test/php/net/xp_framework/unittest/util/BytesTest.class.php @@ -311,7 +311,7 @@ public function iteration() { $c= ['H', "\303", "\244", 'l', 'l', 'o']; $b= new Bytes($c); foreach ($b as $i => $byte) { - $this->assertEquals($c[$i], chr($byte->intValue())); + $this->assertEquals($c[$i], chr($byte)); } $this->assertEquals($i, sizeof($c)- 1); } From b2a67b137ce3655d9d0864fe9e7c3e7ff2f3afa1 Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Thu, 21 Jan 2016 21:59:46 +0100 Subject: [PATCH 14/19] QA: Rewrite example to no longer use lang.types --- src/main/php/lang/reflect/Method.class.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/php/lang/reflect/Method.class.php b/src/main/php/lang/reflect/Method.class.php index ae487d610c..99ef90bf7e 100755 --- a/src/main/php/lang/reflect/Method.class.php +++ b/src/main/php/lang/reflect/Method.class.php @@ -26,8 +26,8 @@ class Method extends Routine { * * Example (passing arguments) * ```php - * $method= XPClass::forName('lang.types.String')->getMethod('concat'); - * $str= $method->invoke(new String('Hello'), ['World']); + * $method= XPClass::forName('util.Date')->getMethod('format'); + * $str= $method->invoke(Date::now(), ['%d.%m.%Y']); * ``` * * Example (static invokation): From d63e25d2cfa7a5d1e3f90ecd0a76a2363b4c4b4c Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Sat, 23 Jan 2016 16:29:53 +0100 Subject: [PATCH 15/19] QA: Fix size() return type --- src/main/php/util/Bytes.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/php/util/Bytes.class.php b/src/main/php/util/Bytes.class.php index b90438a5d2..f277fc81e3 100755 --- a/src/main/php/util/Bytes.class.php +++ b/src/main/php/util/Bytes.class.php @@ -127,7 +127,7 @@ public function offsetUnset($offset) { /** * Returns this byte list's size * - * @return string + * @return int */ public function size() { return $this->size; From 3093f8549a2b2349d5d6e1feb1509648d6ff7ece Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Sat, 23 Jan 2016 16:31:55 +0100 Subject: [PATCH 16/19] QA: Make buffer and size private --- src/main/php/util/Bytes.class.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/main/php/util/Bytes.class.php b/src/main/php/util/Bytes.class.php index f277fc81e3..4a81dd4c7d 100755 --- a/src/main/php/util/Bytes.class.php +++ b/src/main/php/util/Bytes.class.php @@ -10,10 +10,7 @@ */ class Bytes extends \lang\Object implements \ArrayAccess, \IteratorAggregate { private $iterator = null; - - public - $buffer = '', - $size = 0; + private $buffer, $size; /** * Returns input as byte From 348d6bb06fd6f92f50723d4be11d6f56f20d4e17 Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Sat, 23 Jan 2016 16:37:10 +0100 Subject: [PATCH 17/19] QA: Rewrite getIterator() to use yield statement --- src/main/php/util/Bytes.class.php | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/src/main/php/util/Bytes.class.php b/src/main/php/util/Bytes.class.php index 4a81dd4c7d..eab102a36c 100755 --- a/src/main/php/util/Bytes.class.php +++ b/src/main/php/util/Bytes.class.php @@ -9,7 +9,6 @@ * @test xp://net.xp_framework.unittest.util.BytesTest */ class Bytes extends \lang\Object implements \ArrayAccess, \IteratorAggregate { - private $iterator = null; private $buffer, $size; /** @@ -48,16 +47,10 @@ public function __construct($initial= null) { * @return php.Iterator */ public function getIterator() { - if (!$this->iterator) $this->iterator= newinstance('Iterator', [$this], '{ - private $i= 0, $v; - public function __construct($v) { $this->v= $v; } - public function current() { $n= ord($this->v->buffer{$this->i}); return $n < 128 ? $n : $n - 256; } - public function key() { return $this->i; } - public function next() { $this->i++; } - public function rewind() { $this->i= 0; } - public function valid() { return $this->i < $this->v->size; } - }'); - return $this->iterator; + for ($offset= 0; $offset < $this->size; $offset++) { + $n= ord($this->buffer{$offset}); + yield $n < 128 ? $n : $n - 256; + } } /** From fdefe46e0c7bae6c4217454b2c0a1312054aef5f Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Sat, 23 Jan 2016 17:01:45 +0100 Subject: [PATCH 18/19] Implement lang.Value instead of extending lang.Object --- src/main/php/util/Bytes.class.php | 14 +++++--------- .../xp_framework/unittest/util/BytesTest.class.php | 10 ++++++++++ 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/main/php/util/Bytes.class.php b/src/main/php/util/Bytes.class.php index eab102a36c..d70f8088ab 100755 --- a/src/main/php/util/Bytes.class.php +++ b/src/main/php/util/Bytes.class.php @@ -8,7 +8,7 @@ * @deprecated Wrapper types will move to their own library * @test xp://net.xp_framework.unittest.util.BytesTest */ -class Bytes extends \lang\Object implements \ArrayAccess, \IteratorAggregate { +class Bytes implements \lang\Value, \ArrayAccess, \IteratorAggregate { private $buffer, $size; /** @@ -126,15 +126,11 @@ public function size() { /** * Returns whether a given object is equal to this object * - * @param lang.Generic cmp - * @return bool + * @param var $value + * @return int */ - public function equals($cmp) { - return ( - $cmp instanceof self && - $this->size === $cmp->size && - $this->buffer === $cmp->buffer - ); + public function compareTo($value) { + return $value instanceof self ? strcmp($this->buffer, $value->buffer) : 1; } /** diff --git a/src/test/php/net/xp_framework/unittest/util/BytesTest.class.php b/src/test/php/net/xp_framework/unittest/util/BytesTest.class.php index 4067c60f93..b5e5641d6a 100755 --- a/src/test/php/net/xp_framework/unittest/util/BytesTest.class.php +++ b/src/test/php/net/xp_framework/unittest/util/BytesTest.class.php @@ -315,4 +315,14 @@ public function iteration() { } $this->assertEquals($i, sizeof($c)- 1); } + + #[@test, @values([ + # [new Bytes('Test'), 0], + # [new Bytes('T'), +3], + # [new Bytes('Testing'), -3], + # [null, 1] + #])] + public function compare($value, $expected) { + $this->assertEquals($expected, (new Bytes('Test'))->compareTo($value)); + } } From 83f5aefb86cf0eec5e133ab11500a5b13a187f3e Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Sat, 23 Jan 2016 18:20:43 +0100 Subject: [PATCH 19/19] QA: Adjust apidocs syntax --- src/main/php/util/Bytes.class.php | 44 +++++++++++++++---------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/src/main/php/util/Bytes.class.php b/src/main/php/util/Bytes.class.php index d70f8088ab..ee99cc2279 100755 --- a/src/main/php/util/Bytes.class.php +++ b/src/main/php/util/Bytes.class.php @@ -14,8 +14,8 @@ class Bytes implements \lang\Value, \ArrayAccess, \IteratorAggregate { /** * Returns input as byte * - * @param var in - * @return string + * @param var $in + * @return string */ protected function asByte($in) { return is_int($in) ? chr($in) : $in{0}; @@ -24,8 +24,8 @@ protected function asByte($in) { /** * Constructor * - * @param var initial default NULL - * @throws lang.IllegalArgumentException in case argument is of incorrect type. + * @param var $initial default NULL + * @throws lang.IllegalArgumentException in case argument is of incorrect type. */ public function __construct($initial= null) { if (null === $initial) { @@ -43,8 +43,8 @@ public function __construct($initial= null) { /** * Returns an iterator for use in foreach() * - * @see php://language.oop5.iterations - * @return php.Iterator + * @see php://language.oop5.iterations + * @return php.Iterator */ public function getIterator() { for ($offset= 0; $offset < $this->size; $offset++) { @@ -56,9 +56,9 @@ public function getIterator() { /** * = list[] overloading * - * @param int offset - * @return lang.types.Byte - * @throws lang.IndexOutOfBoundsException if offset does not exist + * @param int $offset + * @return int + * @throws lang.IndexOutOfBoundsException if offset does not exist */ public function offsetGet($offset) { if ($offset >= $this->size || $offset < 0) { @@ -71,9 +71,9 @@ public function offsetGet($offset) { /** * list[]= overloading * - * @param int offset - * @param var value - * @throws lang.IllegalArgumentException if key is neither numeric (set) nor NULL (add) + * @param int $offset + * @param var $value + * @throws lang.IllegalArgumentException if key is neither numeric (set) nor NULL (add) * @throws lang.IndexOutOfBoundsException if key does not exist */ public function offsetSet($offset, $value) { @@ -90,8 +90,8 @@ public function offsetSet($offset, $value) { /** * isset() overloading * - * @param int offset - * @return bool + * @param int $offset + * @return bool */ public function offsetExists($offset) { return ($offset >= 0 && $offset < $this->size); @@ -100,8 +100,8 @@ public function offsetExists($offset) { /** * unset() overloading * - * @param int offset - * @throws lang.IndexOutOfBoundsException if offset does not exist + * @param int $offset + * @throws lang.IndexOutOfBoundsException if offset does not exist */ public function offsetUnset($offset) { if ($offset >= $this->size || $offset < 0) { @@ -117,7 +117,7 @@ public function offsetUnset($offset) { /** * Returns this byte list's size * - * @return int + * @return int */ public function size() { return $this->size; @@ -136,25 +136,25 @@ public function compareTo($value) { /** * Returns a hashcode for this bytes object * - * @return string + * @return string */ public function hashCode() { return md5($this->buffer); } /** - * Returns a string representation of this string. + * Returns a string representation of this bytes instance. * - * @return string + * @return string */ public function toString() { return nameof($this).'('.$this->size.')@{'.addcslashes($this->buffer, "\0..\37\177..\377").'}'; } /** - * String conversion overloading. This is for use with fwrite() + * String conversion overloading * - * @return string + * @return string */ public function __toString() { return $this->buffer;