Skip to content
This repository has been archived by the owner on May 28, 2019. It is now read-only.

Commit

Permalink
Map and Set now serialize their hash function (fixes #23)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmalloc committed Feb 7, 2013
1 parent 70f15f9 commit 753be98
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 14 deletions.
2 changes: 1 addition & 1 deletion lib/Icecave/Collections/AssociativeKeyGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function __construct($arrayHashFunction = 'md5', $objectHashFunction = 's
*/
public function __invoke($value)
{
$this->typeCheck->__invoke(func_get_args());
$this->typeCheck->validateInvoke(func_get_args());

return $this->generate($value);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Icecave/Collections/LinkedList.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public function isEmpty()
*/
public function __toString()
{
$this->typeCheck->__toString(func_get_args());
$this->typeCheck->validateToString(func_get_args());

if ($this->isEmpty()) {
return '<LinkedList 0>';
Expand Down
13 changes: 9 additions & 4 deletions lib/Icecave/Collections/Map.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function isEmpty()
*/
public function __toString()
{
$this->typeCheck->__toString(func_get_args());
$this->typeCheck->validateToString(func_get_args());

if ($this->isEmpty()) {
return '<Map 0>';
Expand Down Expand Up @@ -1031,7 +1031,12 @@ public function serialize()
{
$this->typeCheck->serialize(func_get_args());

return serialize($this->elements());
return serialize(
array(
$this->elements(),
$this->hashFunction
)
);
}

/**
Expand All @@ -1041,9 +1046,9 @@ public function unserialize($packet)
{
TypeCheck::get(__CLASS__)->unserialize(func_get_args());

$elements = unserialize($packet);
list($elements, $hashFunction) = unserialize($packet);

$this->__construct();
$this->__construct(null, $hashFunction);

foreach ($elements as $element) {
list($key, $value) = $element;
Expand Down
2 changes: 1 addition & 1 deletion lib/Icecave/Collections/PriorityQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function __construct($prioritizer, $collection = null)
*/
public function __toString()
{
$this->typeCheck->__toString(func_get_args());
$this->typeCheck->validateToString(func_get_args());

if ($this->isEmpty()) {
return '<PriorityQueue 0>';
Expand Down
2 changes: 1 addition & 1 deletion lib/Icecave/Collections/Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function isEmpty()
*/
public function __toString()
{
$this->typeCheck->__toString(func_get_args());
$this->typeCheck->validateToString(func_get_args());

if ($this->isEmpty()) {
return '<Queue 0>';
Expand Down
13 changes: 9 additions & 4 deletions lib/Icecave/Collections/Set.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function isEmpty()
*/
public function __toString()
{
$this->typeCheck->__toString(func_get_args());
$this->typeCheck->validateToString(func_get_args());

if ($this->isEmpty()) {
return '<Set 0>';
Expand Down Expand Up @@ -311,7 +311,12 @@ public function serialize()
{
$this->typeCheck->serialize(func_get_args());

return serialize($this->elements());
return serialize(
array(
$this->elements(),
$this->hashFunction
)
);
}

/**
Expand All @@ -321,8 +326,8 @@ public function unserialize($packet)
{
TypeCheck::get(__CLASS__)->unserialize(func_get_args());

$elements = unserialize($packet);
$this->__construct($elements);
list($elements, $hashFunction) = unserialize($packet);
$this->__construct($elements, $hashFunction);
}

////////////////////////////
Expand Down
2 changes: 1 addition & 1 deletion lib/Icecave/Collections/Stack.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function isEmpty()
*/
public function __toString()
{
$this->typeCheck->__toString(func_get_args());
$this->typeCheck->validateToString(func_get_args());

if ($this->isEmpty()) {
return '<Stack 0>';
Expand Down
2 changes: 1 addition & 1 deletion lib/Icecave/Collections/Vector.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function isEmpty()
*/
public function __toString()
{
$this->typeCheck->__toString(func_get_args());
$this->typeCheck->validateToString(func_get_args());

if ($this->isEmpty()) {
return '<Vector 0>';
Expand Down
15 changes: 15 additions & 0 deletions test/suite/Icecave/Collections/MapTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Icecave\Collections;

use Eloquent\Liberator\Liberator;
use PHPUnit_Framework_TestCase;

class MapTest extends PHPUnit_Framework_TestCase
Expand Down Expand Up @@ -53,6 +54,20 @@ public function testSerialization()
$this->assertSame($this->_collection->elements(), $collection->elements());
}

/**
* @group regression
* @link https://github.com/IcecaveStudios/collections/issues/23
*/
public function testSerializationOfHashFunction()
{
$collection = new Map(null, 'sha1');

$packet = serialize($collection);
$collection = unserialize($packet);

$this->assertSame('sha1', Liberator::liberate($collection)->hashFunction);
}

///////////////////////////////////////////
// Implementation of CollectionInterface //
///////////////////////////////////////////
Expand Down
15 changes: 15 additions & 0 deletions test/suite/Icecave/Collections/SetTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Icecave\Collections;

use Eloquent\Liberator\Liberator;
use PHPUnit_Framework_TestCase;

class SetTest extends PHPUnit_Framework_TestCase
Expand Down Expand Up @@ -47,6 +48,20 @@ public function testSerialization()
$this->assertSame($this->_collection->elements(), $collection->elements());
}

/**
* @group regression
* @link https://github.com/IcecaveStudios/collections/issues/23
*/
public function testSerializationOfHashFunction()
{
$collection = new Set(null, 'sha1');

$packet = serialize($collection);
$collection = unserialize($packet);

$this->assertSame('sha1', Liberator::liberate($collection)->hashFunction);
}

///////////////////////////////////////////
// Implementation of CollectionInterface //
///////////////////////////////////////////
Expand Down

0 comments on commit 753be98

Please sign in to comment.