Permalink
Browse files

Added tests for array_part()

  • Loading branch information...
1 parent 537759a commit 59d8ef1185d9094a62db8d0a7f5d7abcd9282cdf @cataphract committed May 22, 2012
@@ -0,0 +1,26 @@
+--TEST--
+array_part(): bad arguments
+--FILE--
+<?php
+
+var_dump(array_part());
+var_dump(array_part(1,2,3,4));
+var_dump(array_part(new stdclass,[1]));
+var_dump(array_part([0, 1], new stdclass));
+var_dump(array_part([0, 1], [1], array()));
+--EXPECTF--
+
+Warning: array_part() expects at least 2 parameters, 0 given in %s on line %d
+bool(false)
+
+Warning: array_part() expects at most 3 parameters, 4 given in %s on line %d
+bool(false)
+
+Warning: array_part() expects parameter 1 to be array, object given in %s on line %d
+bool(false)
+
+Warning: array_part() expects parameter 2 to be array, object given in %s on line %d
+bool(false)
+
+Warning: array_part() expects parameter 3 to be boolean, array given in %s on line %d
+bool(false)
@@ -0,0 +1,64 @@
+--TEST--
+array_part(): bad offsets for input
+--FILE--
+<?php
+
+var_dump(
+array_part([],[-1]),
+array_part([],[-1], true),
+array_part([1],[1]),
+array_part([1],[1],true),
+array_part([1],['foo'],true),
+array_part([1],[-2]),
+array_part([1],[[0,1]]),
+array_part([1],[[0,1]],true),
+array_part([],[['start'=>1]]),
+array_part([],[['start'=>1]],true),
+array_part([1],[['start'=>1]]),
+array_part([1],[['start'=>1]],true),
+array_part([1],[['end'=>'foo']],true),
+array_part([1],[0,1])
+);
+--EXPECTF--
+
+Warning: array_part(): Tried to get part of empty array in %s on line %d
+
+Warning: array_part(): Tried to get part of empty array in %s on line %d
+
+Warning: array_part(): Tried to get offset '1' from array with only 1 elements in %s on line %d
+
+Warning: array_part(): Tried to get element with key '1' from array that does not have it in %s on line %d
+
+Warning: array_part(): Tried to get element with key 'foo' from array that does not have it in %s on line %d
+
+Warning: array_part(): The offset '-2' is too large in absolute value when accessing an array with only 1 elements in %s on line %d
+
+Warning: array_part(): Tried to get offset '1' from array with only 1 elements in %s on line %d
+
+Warning: array_part(): Tried to get element with key '1' from array that does not have it in %s on line %d
+
+Warning: array_part(): Tried to get part of empty array with span specification that does not include all elements in %s on line %d
+
+Warning: array_part(): Tried to get part of empty array with span specification that does not include all elements in %s on line %d
+
+Warning: array_part(): Tried to get offset '1' from array with only 1 elements in %s on line %d
+
+Warning: array_part(): Tried to get element with key '1' from array that does not have it in %s on line %d
+
+Warning: array_part(): Tried to get element with key 'foo' from array that does not have it in %s on line %d
+
+Warning: array_part(): The depth of the part specification is too large; tried to get part of non-array with part of index '1' in %s on line %d
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+bool(false)
@@ -0,0 +1,192 @@
+--TEST--
+array_part(): bad part specifications
+--FILE--
+<?php
+
+$spe = [
+ [[[]]],
+ [[1e50]],
+ ["foobar"],
+ [["start"=>null, "step"=>'foobar']],
+ [["start"=>null, "step"=>0]],
+ [["start"=>null, "stepppp"=>0]],
+ [["start"=>[], "end"=>"1."]],
+ [["start"=>"1.", "end"=>[]]],
+ [[]],
+ [[-1=>1]],
+ [["foo"=>1]],
+ [["1", [], 2], 1],
+ [fopen('php://memory', 'r+')],
+ [],
+ [-1=>1],
+ ['foo'=>1]
+];
+foreach ($spe as $s) {
+var_dump(
+ $s,
+ array_part([], $s)
+);
+}
+--EXPECTF--
+
+Warning: array_part(): In part specification with index '0', expected only numeric values, but an incompatible data type has been found in %s on line %d
+array(1) {
+ [0]=>
+ array(1) {
+ [0]=>
+ array(0) {
+ }
+ }
+}
+bool(false)
+
+Warning: array_part(): In part specification with index '0', found number out of bounds: '100000000000000007629769841091887003294964970946560.000000' in %s on line %d
+array(1) {
+ [0]=>
+ array(1) {
+ [0]=>
+ float(1.0E+50)
+ }
+}
+bool(false)
+
+Warning: array_part(): In part specification with index '0', expected only numeric values, but the string 'foobar' does not satisfy this requirement in %s on line %d
+array(1) {
+ [0]=>
+ string(6) "foobar"
+}
+bool(false)
+
+Warning: array_part(): In part specification with index '0', expected only numeric values, but the string 'foobar' does not satisfy this requirement in %s on line %d
+array(1) {
+ [0]=>
+ array(2) {
+ ["start"]=>
+ NULL
+ ["step"]=>
+ string(6) "foobar"
+ }
+}
+bool(false)
+
+Warning: array_part(): In part specification with index '0', a step of size 0 was specified in %s on line %d
+array(1) {
+ [0]=>
+ array(2) {
+ ["start"]=>
+ NULL
+ ["step"]=>
+ int(0)
+ }
+}
+bool(false)
+
+Warning: array_part(): In part specification with index '0', found span specification with extraneous elements in %s on line %d
+array(1) {
+ [0]=>
+ array(2) {
+ ["start"]=>
+ NULL
+ ["stepppp"]=>
+ int(0)
+ }
+}
+array(0) {
+}
+
+Warning: array_part(): In part specification with index '0', expected only numeric values, but an incompatible data type has been found in %s on line %d
+array(1) {
+ [0]=>
+ array(2) {
+ ["start"]=>
+ array(0) {
+ }
+ ["end"]=>
+ string(2) "1."
+ }
+}
+bool(false)
+
+Warning: array_part(): In part specification with index '0', expected only numeric values, but an incompatible data type has been found in %s on line %d
+array(1) {
+ [0]=>
+ array(2) {
+ ["start"]=>
+ string(2) "1."
+ ["end"]=>
+ array(0) {
+ }
+ }
+}
+bool(false)
+
+Warning: array_part(): Part specification with index '0' is empty in %s on line %d
+array(1) {
+ [0]=>
+ array(0) {
+ }
+}
+bool(false)
+
+Warning: array_part(): List of indexes in part specification with index '0' should be a numeric array, but either found non-sequential keys or the first key was not 0 (expected '0', got '-1') in %s on line %d
+array(1) {
+ [0]=>
+ array(1) {
+ [-1]=>
+ int(1)
+ }
+}
+bool(false)
+
+Warning: array_part(): List of indexes in part specification with index '0' should be a numeric array, but found string index 'foo' in %s on line %d
+array(1) {
+ [0]=>
+ array(1) {
+ ["foo"]=>
+ int(1)
+ }
+}
+bool(false)
+
+Warning: array_part(): In part specification with index '0', expected only numeric values, but an incompatible data type has been found in %s on line %d
+array(2) {
+ [0]=>
+ array(3) {
+ [0]=>
+ string(1) "1"
+ [1]=>
+ array(0) {
+ }
+ [2]=>
+ int(2)
+ }
+ [1]=>
+ int(1)
+}
+bool(false)
+
+Warning: array_part(): In part specification with index '0', expected only numeric values, but an incompatible data type has been found in %s on line %d
+array(1) {
+ [0]=>
+ resource(5) of type (stream)
+}
+bool(false)
+
+Warning: array_part(): Empty list of part specifications given in %s on line %d
+array(0) {
+}
+bool(false)
+
+Warning: array_part(): List of part specifications should be a numeric array, but found non-sequential keys or the first key was not 0 (expected '0', got '-1') in %s on line %d
+array(1) {
+ [-1]=>
+ int(1)
+}
+bool(false)
+
+Warning: array_part(): List of part specifications should be a numeric array, but found string index 'foo' in %s on line %d
+array(1) {
+ ["foo"]=>
+ int(1)
+}
+bool(false)
@@ -0,0 +1,66 @@
+--TEST--
+array_part(): basic tests in key mode
+--FILE--
+<?php
+$array = [
+ [1, 2, 'foo' => 3],
+ ['a', 'foo' => 'b', 'c', 'd'],
+];
+
+print_r(
+ array_part($array, [
+ ['start'=>null, 'end'=>null],
+ "foo",
+ ], true)
+);
+
+print_r(
+ array_part($array, [
+ ['end'=>null],
+ ['start' => "foo", 'step' => -1],
+ ], true)
+);
+
+print_r(
+ array_part($array, [
+ ['end'=>null],
+ ["0", "1"],
+ ], true)
+);
+--EXPECT--
+Array
+(
+ [0] => 3
+ [1] => b
+)
+Array
+(
+ [0] => Array
+ (
+ [0] => 3
+ [1] => 2
+ [2] => 1
+ )
+
+ [1] => Array
+ (
+ [0] => b
+ [1] => a
+ )
+
+)
+Array
+(
+ [0] => Array
+ (
+ [0] => 1
+ [1] => 2
+ )
+
+ [1] => Array
+ (
+ [0] => a
+ [1] => c
+ )
+
+)
Oops, something went wrong.

0 comments on commit 59d8ef1

Please sign in to comment.