-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Add implementation and tests for array_key_first, array_key_last and array_value_first, array_value_last #3256
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
ec2332b
780b05e
03803eb
f9ca90d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,234 @@ | ||
--TEST-- | ||
Test array_key_first() function | ||
--FILE-- | ||
<?php | ||
|
||
array_key_first($GLOBALS); | ||
|
||
/* Various combinations of arrays to be used for the test */ | ||
$mixed_array = array( | ||
array(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should add test cases with:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good additional cases, I'll add them to the tests for all four functions. Thanks for the input! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've added the cases to the accepted two functions |
||
array( 1,2,3,4,5,6,7,8,9 ), | ||
array( "One", "_Two", "Three", "Four", "Five" ), | ||
array( 6, "six", 7, "seven", 8, "eight", 9, "nine" ), | ||
array( "a" => "aaa", "A" => "AAA", "c" => "ccc", "d" => "ddd", "e" => "eee" ), | ||
array( "1" => "one", "2" => "two", "3" => "three", "4" => "four", "5" => "five" ), | ||
array( 1 => "one", 2 => "two", 3 => 7, 4 => "four", 5 => "five" ), | ||
array( "f" => "fff", "1" => "one", 4 => 6, "" => "blank", 2.4 => "float", "F" => "FFF", | ||
"blank" => "", 3.7 => 3.7, 5.4 => 7, 6 => 8.6, '5' => "Five", "4name" => "jonny", "a" => NULL, NULL => 3 ), | ||
array( 12, "name", 'age', '45' ), | ||
array( array("oNe", "tWo", 4), array(10, 20, 30, 40, 50), array() ), | ||
array( "one" => 1, "one" => 2, "three" => 3, 3, 4, 3 => 33, 4 => 44, 5, 6, | ||
5.4 => 54, 5.7 => 57, "5.4" => 554, "5.7" => 557 ), | ||
array( "foo" ), | ||
array( 1 => "42" ) | ||
); | ||
|
||
/* Loop to test normal functionality with different arrays inputs */ | ||
echo "\n*** Normal testing with various array inputs ***\n"; | ||
|
||
$counter = 1; | ||
foreach( $mixed_array as $sub_array ) | ||
{ | ||
echo "\n-- Input Array for Iteration $counter is --\n"; | ||
print_r( $sub_array ); | ||
echo "\nFirst key is :\n"; | ||
var_dump( array_key_first($sub_array) ); | ||
$counter++; | ||
} | ||
|
||
echo"\nDone"; | ||
?> | ||
--EXPECT-- | ||
*** Normal testing with various array inputs *** | ||
|
||
-- Input Array for Iteration 1 is -- | ||
Array | ||
( | ||
) | ||
|
||
First key is : | ||
NULL | ||
|
||
-- Input Array for Iteration 2 is -- | ||
Array | ||
( | ||
[0] => 1 | ||
[1] => 2 | ||
[2] => 3 | ||
[3] => 4 | ||
[4] => 5 | ||
[5] => 6 | ||
[6] => 7 | ||
[7] => 8 | ||
[8] => 9 | ||
) | ||
|
||
First key is : | ||
int(0) | ||
|
||
-- Input Array for Iteration 3 is -- | ||
Array | ||
( | ||
[0] => One | ||
[1] => _Two | ||
[2] => Three | ||
[3] => Four | ||
[4] => Five | ||
) | ||
|
||
First key is : | ||
int(0) | ||
|
||
-- Input Array for Iteration 4 is -- | ||
Array | ||
( | ||
[0] => 6 | ||
[1] => six | ||
[2] => 7 | ||
[3] => seven | ||
[4] => 8 | ||
[5] => eight | ||
[6] => 9 | ||
[7] => nine | ||
) | ||
|
||
First key is : | ||
int(0) | ||
|
||
-- Input Array for Iteration 5 is -- | ||
Array | ||
( | ||
[a] => aaa | ||
[A] => AAA | ||
[c] => ccc | ||
[d] => ddd | ||
[e] => eee | ||
) | ||
|
||
First key is : | ||
string(1) "a" | ||
|
||
-- Input Array for Iteration 6 is -- | ||
Array | ||
( | ||
[1] => one | ||
[2] => two | ||
[3] => three | ||
[4] => four | ||
[5] => five | ||
) | ||
|
||
First key is : | ||
int(1) | ||
|
||
-- Input Array for Iteration 7 is -- | ||
Array | ||
( | ||
[1] => one | ||
[2] => two | ||
[3] => 7 | ||
[4] => four | ||
[5] => five | ||
) | ||
|
||
First key is : | ||
int(1) | ||
|
||
-- Input Array for Iteration 8 is -- | ||
Array | ||
( | ||
[f] => fff | ||
[1] => one | ||
[4] => 6 | ||
[] => 3 | ||
[2] => float | ||
[F] => FFF | ||
[blank] => | ||
[3] => 3.7 | ||
[5] => Five | ||
[6] => 8.6 | ||
[4name] => jonny | ||
[a] => | ||
) | ||
|
||
First key is : | ||
string(1) "f" | ||
|
||
-- Input Array for Iteration 9 is -- | ||
Array | ||
( | ||
[0] => 12 | ||
[1] => name | ||
[2] => age | ||
[3] => 45 | ||
) | ||
|
||
First key is : | ||
int(0) | ||
|
||
-- Input Array for Iteration 10 is -- | ||
Array | ||
( | ||
[0] => Array | ||
( | ||
[0] => oNe | ||
[1] => tWo | ||
[2] => 4 | ||
) | ||
|
||
[1] => Array | ||
( | ||
[0] => 10 | ||
[1] => 20 | ||
[2] => 30 | ||
[3] => 40 | ||
[4] => 50 | ||
) | ||
|
||
[2] => Array | ||
( | ||
) | ||
|
||
) | ||
|
||
First key is : | ||
int(0) | ||
|
||
-- Input Array for Iteration 11 is -- | ||
Array | ||
( | ||
[one] => 2 | ||
[three] => 3 | ||
[0] => 3 | ||
[1] => 4 | ||
[3] => 33 | ||
[4] => 44 | ||
[5] => 57 | ||
[6] => 6 | ||
[5.4] => 554 | ||
[5.7] => 557 | ||
) | ||
|
||
First key is : | ||
string(3) "one" | ||
|
||
-- Input Array for Iteration 12 is -- | ||
Array | ||
( | ||
[0] => foo | ||
) | ||
|
||
First key is : | ||
int(0) | ||
|
||
-- Input Array for Iteration 13 is -- | ||
Array | ||
( | ||
[1] => 42 | ||
) | ||
|
||
First key is : | ||
int(1) | ||
|
||
Done |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
--TEST-- | ||
Test array_key_first() function (errors) | ||
--FILE-- | ||
<?php | ||
|
||
$empty_array = array(); | ||
$number = 5; | ||
$str = "abc"; | ||
|
||
/* Various combinations of arrays to be used for the test */ | ||
$mixed_array = array( | ||
array( 1,2,3,4,5,6,7,8,9 ), | ||
array( "One", "_Two", "Three", "Four", "Five" ) | ||
); | ||
|
||
/* Testing Error Conditions */ | ||
echo "\n*** Testing Error Conditions ***\n"; | ||
|
||
/* Zero argument */ | ||
var_dump( array_key_first() ); | ||
|
||
/* Scalar argument */ | ||
var_dump( array_key_first($number) ); | ||
|
||
/* String argument */ | ||
var_dump( array_key_first($str) ); | ||
|
||
/* Invalid Number of arguments */ | ||
var_dump( array_key_first($mixed_array[0],$mixed_array[1]) ); | ||
|
||
/* Empty Array as argument */ | ||
var_dump( array_key_first($empty_array) ); | ||
|
||
echo"\nDone"; | ||
?> | ||
--EXPECTF-- | ||
*** Testing Error Conditions *** | ||
|
||
Warning: array_key_first() expects exactly 1 parameter, 0 given in %s on line %d | ||
NULL | ||
|
||
Warning: array_key_first() expects parameter 1 to be array, int given in %s on line %d | ||
NULL | ||
|
||
Warning: array_key_first() expects parameter 1 to be array, string given in %s on line %d | ||
NULL | ||
|
||
Warning: array_key_first() expects exactly 1 parameter, 2 given in %s on line %d | ||
NULL | ||
NULL | ||
|
||
Done |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
--TEST-- | ||
Test array_key_first() function (variation) | ||
--FILE-- | ||
<?php | ||
|
||
/* Various combinations of arrays to be used for the test */ | ||
$array = array( 1,2,3,4,5,6,7,8,9 ); | ||
|
||
echo"\n*** Checking for internal array pointer not being changed by array_key_first ***\n"; | ||
|
||
echo "\nCurrent Element is : "; | ||
var_dump( current($array) ); | ||
|
||
echo "\nNext Element is : "; | ||
var_dump( next($array) ); | ||
|
||
echo "\nFirst key is : "; | ||
var_dump( array_key_first($array) ); | ||
|
||
echo "\nCurrent Element after array_key_first operation is: "; | ||
var_dump( current($array) ); | ||
|
||
echo"\nDone"; | ||
?> | ||
--EXPECT-- | ||
*** Checking for internal array pointer not being changed by array_key_first *** | ||
|
||
Current Element is : int(1) | ||
|
||
Next Element is : int(2) | ||
|
||
First key is : int(0) | ||
|
||
Current Element after array_key_first operation is: int(2) | ||
|
||
Done |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is the comment ARRAY_INFO, instead of just using
ZEND_ARG_ARRAY_INFO
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we don't use the typed arginfo parameters, yet. @nikic recently suggested to stick with that for now to avoid duplicate checks, IIRC.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I adopted it from existing functions for consistency so I guess keep it?