Skip to content

Commit 83cfd0c

Browse files
author
Raghubansh Kumar
committed
New testcases for array_combine() function
1 parent d08088d commit 83cfd0c

9 files changed

+1951
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
--TEST--
2+
Test array_combine() function : basic functionality
3+
--FILE--
4+
<?php
5+
/* Prototype : array array_combine(array $keys, array $values)
6+
* Description: Creates an array by using the elements of the first parameter as keys
7+
* and the elements of the second as the corresponding values
8+
* Source code: ext/standard/array.c
9+
*/
10+
11+
echo "*** Testing array_combine() : basic functionality ***\n";
12+
13+
/* Different arrays for $keys and $values arguments */
14+
15+
// array with default keys for $keys and $values arguments
16+
$keys_array = array(1, 2);
17+
$values_array = array(3,4);
18+
var_dump( array_combine($keys_array, $values_array) );
19+
20+
// associative arrays for $keys and $values arguments
21+
$keys_array = array(1 => "a", 2 => 'b');
22+
$values_array = array(3 => 'c', 4 => "d");
23+
var_dump( array_combine($keys_array, $values_array) );
24+
25+
// mixed array for $keys and $values arguments
26+
$keys_array = array(1, 2 => "b");
27+
$values_array = array(3 => 'c', 4);
28+
var_dump( array_combine($keys_array, $values_array) );
29+
30+
echo "Done";
31+
?>
32+
--EXPECTF--
33+
*** Testing array_combine() : basic functionality ***
34+
array(2) {
35+
[1]=>
36+
int(3)
37+
[2]=>
38+
int(4)
39+
}
40+
array(2) {
41+
["a"]=>
42+
string(1) "c"
43+
["b"]=>
44+
string(1) "d"
45+
}
46+
array(2) {
47+
[1]=>
48+
string(1) "c"
49+
["b"]=>
50+
int(4)
51+
}
52+
Done
53+
--UEXPECTF--
54+
*** Testing array_combine() : basic functionality ***
55+
array(2) {
56+
[1]=>
57+
int(3)
58+
[2]=>
59+
int(4)
60+
}
61+
array(2) {
62+
[u"a"]=>
63+
unicode(1) "c"
64+
[u"b"]=>
65+
unicode(1) "d"
66+
}
67+
array(2) {
68+
[1]=>
69+
unicode(1) "c"
70+
[u"b"]=>
71+
int(4)
72+
}
73+
Done
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
--TEST--
2+
Test array_combine() function : error conditions
3+
--FILE--
4+
<?php
5+
/* Prototype : array array_combine(array $keys, array $values)
6+
* Description: Creates an array by using the elements of the first parameter as keys
7+
* and the elements of the second as the corresponding values
8+
* Source code: ext/standard/array.c
9+
*/
10+
11+
echo "*** Testing array_combine() : error conditions ***\n";
12+
13+
// Zero arguments
14+
echo "\n-- Testing array_combine() function with Zero arguments --\n";
15+
var_dump( array_combine() );
16+
17+
//Test array_combine with one more than the expected number of arguments
18+
echo "\n-- Testing array_combine() function with more than expected no. of arguments --\n";
19+
$keys = array(1, 2);
20+
$values = array(1, 2);
21+
$extra_arg = 10;
22+
var_dump( array_combine($keys,$values, $extra_arg) );
23+
24+
// Testing array_combine with one less than the expected number of arguments
25+
echo "\n-- Testing array_combine() function with less than expected no. of arguments --\n";
26+
$keys = array(1, 2);
27+
var_dump( array_combine($keys) );
28+
29+
echo "Done";
30+
?>
31+
--EXPECTF--
32+
*** Testing array_combine() : error conditions ***
33+
34+
-- Testing array_combine() function with Zero arguments --
35+
36+
Warning: array_combine() expects exactly 2 parameters, 0 given in %s on line %d
37+
NULL
38+
39+
-- Testing array_combine() function with more than expected no. of arguments --
40+
41+
Warning: array_combine() expects exactly 2 parameters, 3 given in %s on line %d
42+
NULL
43+
44+
-- Testing array_combine() function with less than expected no. of arguments --
45+
46+
Warning: array_combine() expects exactly 2 parameters, 1 given in %s on line %d
47+
NULL
48+
Done
49+
--UEXPECTF--
50+
*** Testing array_combine() : error conditions ***
51+
52+
-- Testing array_combine() function with Zero arguments --
53+
54+
Warning: array_combine() expects exactly 2 parameters, 0 given in %s on line %d
55+
NULL
56+
57+
-- Testing array_combine() function with more than expected no. of arguments --
58+
59+
Warning: array_combine() expects exactly 2 parameters, 3 given in %s on line %d
60+
NULL
61+
62+
-- Testing array_combine() function with less than expected no. of arguments --
63+
64+
Warning: array_combine() expects exactly 2 parameters, 1 given in %s on line %d
65+
NULL
66+
Done
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
--TEST--
2+
Test array_combine() function : error conditions - empty array
3+
--FILE--
4+
<?php
5+
/* Prototype : array array_combine(array $keys, array $values)
6+
* Description: Creates an array by using the elements of the first parameter as keys
7+
* and the elements of the second as the corresponding values
8+
* Source code: ext/standard/array.c
9+
*/
10+
11+
echo "*** Testing array_combine() : error conditions specific to array_combine() ***\n";
12+
13+
// Testing array_combine by passing empty arrays to $keys and $values arguments
14+
echo "\n-- Testing array_combine() function with empty arrays --\n";
15+
var_dump( array_combine(array(), array()) );
16+
17+
// Testing array_combine by passing empty array to $keys
18+
echo "\n-- Testing array_combine() function with empty array for \$keys argument --\n";
19+
var_dump( array_combine(array(), array(1, 2)) );
20+
21+
// Testing array_combine by passing empty array to $values
22+
echo "\n-- Testing array_combine() function with empty array for \$values argument --\n";
23+
var_dump( array_combine(array(1, 2), array()) );
24+
25+
// Testing array_combine with arrays having unequal number of elements
26+
echo "\n-- Testing array_combine() function by passing array with unequal number of elements --\n";
27+
var_dump( array_combine(array(1, 2), array(1, 2, 3)) );
28+
29+
echo "Done";
30+
?>
31+
--EXPECTF--
32+
*** Testing array_combine() : error conditions specific to array_combine() ***
33+
34+
-- Testing array_combine() function with empty arrays --
35+
36+
Warning: array_combine(): Both parameters should have at least 1 element in %s on line %d
37+
bool(false)
38+
39+
-- Testing array_combine() function with empty array for $keys argument --
40+
41+
Warning: array_combine(): Both parameters should have an equal number of elements in %s on line %d
42+
bool(false)
43+
44+
-- Testing array_combine() function with empty array for $values argument --
45+
46+
Warning: array_combine(): Both parameters should have an equal number of elements in %s on line %d
47+
bool(false)
48+
49+
-- Testing array_combine() function by passing array with unequal number of elements --
50+
51+
Warning: array_combine(): Both parameters should have an equal number of elements in %s on line %d
52+
bool(false)
53+
Done
54+
--UEXPECTF--
55+
*** Testing array_combine() : error conditions specific to array_combine() ***
56+
57+
-- Testing array_combine() function with empty arrays --
58+
59+
Warning: array_combine(): Both parameters should have at least 1 element in %s on line %d
60+
bool(false)
61+
62+
-- Testing array_combine() function with empty array for $keys argument --
63+
64+
Warning: array_combine(): Both parameters should have an equal number of elements in %s on line %d
65+
bool(false)
66+
67+
-- Testing array_combine() function with empty array for $values argument --
68+
69+
Warning: array_combine(): Both parameters should have an equal number of elements in %s on line %d
70+
bool(false)
71+
72+
-- Testing array_combine() function by passing array with unequal number of elements --
73+
74+
Warning: array_combine(): Both parameters should have an equal number of elements in %s on line %d
75+
bool(false)
76+
Done

0 commit comments

Comments
 (0)