]> granicus.if.org Git - php/blob
b41871e1c4
[php] /
1 --TEST--
2 Test array_map() function : error conditions
3 --FILE--
4 <?php
5 /* Prototype  : array array_map  ( callback $callback  , array $arr1  [, array $...  ] )
6  * Description: Applies the callback to the elements of the given arrays
7  * Source code: ext/standard/array.c
8  */
9
10 echo "*** Testing array_map() : error conditions ***\n";
11
12 // Testing array_map with one less than the expected number of arguments
13 echo "\n-- Testing array_map() function with one less than expected no. of arguments --\n";
14 function callback1() {
15   return 1;
16 }
17 try {
18     var_dump( array_map('callback1') );
19 } catch (Throwable $e) {
20     echo "Exception: " . $e->getMessage() . "\n";
21 }
22
23 echo "\n-- Testing array_map() function with less no. of arrays than callback function arguments --\n";
24 $arr1 = array(1, 2);
25 function callback2($p, $q) {
26   return $p * $q;
27 }
28 try {
29     var_dump( array_map('callback2', $arr1) );
30 } catch (Throwable $e) {
31     echo "Exception: " . $e->getMessage() . "\n";
32 }
33
34 echo "\n-- Testing array_map() function with more no. of arrays than callback function arguments --\n";
35 $arr2 = array(3, 4);
36 $arr3 = array(5, 6);
37 var_dump( array_map('callback2', $arr1, $arr2, $arr3) );
38
39 echo "Done";
40 ?>
41 --EXPECT--
42 *** Testing array_map() : error conditions ***
43
44 -- Testing array_map() function with one less than expected no. of arguments --
45 Exception: array_map() expects at least 2 parameters, 1 given
46
47 -- Testing array_map() function with less no. of arrays than callback function arguments --
48 Exception: Too few arguments to function callback2(), 1 passed and exactly 2 expected
49
50 -- Testing array_map() function with more no. of arrays than callback function arguments --
51 array(2) {
52   [0]=>
53   int(3)
54   [1]=>
55   int(8)
56 }
57 Done