Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added tests for array_part()

  • Loading branch information...
commit 59d8ef1185d9094a62db8d0a7f5d7abcd9282cdf 1 parent 537759a
@cataphract authored
View
26 ext/standard/tests/array/array_part_bad_args.phpt
@@ -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)
View
64 ext/standard/tests/array/array_part_bad_offsets_for_input.phpt
@@ -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)
View
192 ext/standard/tests/array/array_part_bad_partspec.phpt
@@ -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)
View
66 ext/standard/tests/array/array_part_basic_keymode.phpt
@@ -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
+ )
+
+)
View
105 ext/standard/tests/array/array_part_basic_offsetmode.phpt
@@ -0,0 +1,105 @@
+--TEST--
+array_part(): basic tests in offset mode
+--FILE--
+<?php
+$array = [
+ [1, 2, 'foo' => 3],
+ ['a', 'foo' => 'b', 'c', 'd'],
+];
+
+print_r(
+ array_part($array, [
+ ['start'=>0],
+ ["-1"],
+ ])
+);
+
+print_r(
+ array_part($array, [
+ ['start'=>0, 'end'=>-1],
+ -1,
+ ])
+);
+
+print_r(
+ array_part($array, [
+ 0,
+ -1,
+ ])
+);
+echo "\n";
+
+print_r(
+ array_part($array, [
+ [0],
+ -1,
+ ])
+);
+
+print_r(
+ array_part($array, [
+ ['start'=>0, 'end'=>-1],
+ ['start'=>-2, 'end'=>-1],
+ ])
+);
+
+print_r(
+ array_part($array, [
+ [1, 0],
+ ['start'=>null, 'end'=>-2, 'step'=>-1],
+ ])
+);
+--EXPECT--
+Array
+(
+ [0] => Array
+ (
+ [0] => 3
+ )
+
+ [1] => Array
+ (
+ [0] => d
+ )
+
+)
+Array
+(
+ [0] => 3
+ [1] => d
+)
+3
+Array
+(
+ [0] => 3
+)
+Array
+(
+ [0] => Array
+ (
+ [0] => 2
+ [1] => 3
+ )
+
+ [1] => Array
+ (
+ [0] => c
+ [1] => d
+ )
+
+)
+Array
+(
+ [0] => Array
+ (
+ [0] => d
+ [1] => c
+ )
+
+ [1] => Array
+ (
+ [0] => 3
+ [1] => 2
+ )
+
+)
View
21 ext/standard/tests/array/array_part_cache.phpt
@@ -0,0 +1,21 @@
+--TEST--
+array_part(): usage of cache does not result in wrong returns
+--FILE--
+<?php
+
+$arr = ['a', 'b', 'c', 'foo'=>'d', 'e', 'f', 'g', 'h'];
+
+print_r(
+ array_part($arr, [
+ [2,1,3,-1,-2]
+ ])
+);
+--EXPECT--
+Array
+(
+ [0] => c
+ [1] => b
+ [2] => d
+ [3] => h
+ [4] => g
+)
View
19 ext/standard/tests/array/array_part_odd_valid_indexes.phpt
@@ -0,0 +1,19 @@
+--TEST--
+array_part(): double and object valid indexes
+--FILE--
+<?php
+
+var_dump(
+ array_part(['a', 'b'], [1.])
+);
+
+class A {
+function __toString() { return '1.'; }
+}
+
+var_dump(
+ array_part(['a', 'b'], [new A])
+);
+--EXPECT--
+string(1) "b"
+string(1) "b"
View
51 ext/standard/tests/array/array_part_particular_span.phpt
@@ -0,0 +1,51 @@
+--TEST--
+array_part(): particular span cases
+--FILE--
+<?php
+$arr = ['a'=>'a', 'b'=>'b', 'c'=>'c'];
+
+var_dump(
+ array_part(
+ $arr,
+ [['start'=>2, 'end'=>1], [2]]
+ ),
+ array_part(
+ $arr,
+ [['start'=>'c', 'end'=>'a']],
+ true
+ ),
+ array_part(
+ $arr,
+ [['start'=>'b', 'step'=>2]],
+ true
+ ),
+ array_part(
+ $arr,
+ [['start'=>'c', 'step'=>-3]],
+ true
+ ),
+ array_part(
+ ['a'=>'a', 'b'=>'b', 'c'=>'c', 'd'=>'d'],
+ [['end'=>null, 'step'=>-3]],
+ true
+ )
+);
+--EXPECT--
+array(0) {
+}
+array(0) {
+}
+array(1) {
+ [0]=>
+ string(1) "b"
+}
+array(1) {
+ [0]=>
+ string(1) "c"
+}
+array(2) {
+ [0]=>
+ string(1) "d"
+ [1]=>
+ string(1) "a"
+}
View
54 ext/standard/tests/array/array_part_span_all.phpt
@@ -0,0 +1,54 @@
+--TEST--
+array_part(): spans that encompass all elements
+--FILE--
+<?php
+
+print_r(
+ array_part([], [
+ ['start'=>0, 'end'=>-1],
+ [-1],
+ ])
+);
+
+print_r(
+ array_part([[1,2], []], [
+ [-1],
+ ['start'=>-1, 'end'=>0, 'step'=>-1],
+ ])
+);
+
+print_r(
+ array_part([[[1,2]], []], [
+ ['start'=>null],
+ ['start'=>null],
+ ['start'=>null],
+ ], true)
+);
+--EXPECT--
+Array
+(
+)
+Array
+(
+ [0] => Array
+ (
+ )
+
+)
+Array
+(
+ [0] => Array
+ (
+ [0] => Array
+ (
+ [0] => 1
+ [1] => 2
+ )
+
+ )
+
+ [1] => Array
+ (
+ )
+
+)
Please sign in to comment.
Something went wrong with that request. Please try again.