Annotation of embedaddon/php/ext/standard/tests/array/uasort_variation10.phpt, revision 1.1.1.1

1.1       misho       1: --TEST--
                      2: Test uasort() function : usage variations - sort array with reference variables
                      3: --FILE--
                      4: <?php
                      5: /* Prototype  : bool uasort(array $array_arg, string $cmp_function)
                      6:  * Description: Sort an array with a user-defined comparison function and maintain index association 
                      7:  * Source code: ext/standard/array.c
                      8: */
                      9: 
                     10: /*
                     11: * Testing uasort() with 'array_arg' containing different reference variables
                     12: */
                     13: 
                     14: // comparision function
                     15: /* Prototype : int cmp_function(mixed $value1, mixed $value2)
                     16:  * Parameters : $value1 and $value2 - values to be compared
                     17:  * Return value : 0 - if both values are same
                     18:  *                1 - if value1 is greater than value2
                     19:  *               -1 - if value1 is less than value2
                     20:  * Description : compares value1 and value2
                     21:  */
                     22: function cmp_function($value1, $value2)
                     23: {
                     24:   if($value1 == $value2) {
                     25:     return 0;
                     26:   }
                     27:   else if($value1 > $value2) {
                     28:     return 1;
                     29:   }
                     30:   else {
                     31:     return -1;
                     32:   }
                     33: }
                     34: 
                     35: echo "*** Testing uasort() : 'array_arg' with elements as reference ***\n";
                     36: 
                     37: // different variables which are used as elements of 'array_arg'
                     38: $value1 = -5; 
                     39: $value2 = 100;
                     40: $value3 = 0;
                     41: $value4 = &$value1;
                     42: 
                     43: // array_args an array containing elements with reference variables
                     44: $array_arg = array(
                     45:   0 => 10,
                     46:   1 => &$value4,
                     47:   2 => &$value2,
                     48:   3 => 200,
                     49:   4 => &$value3,
                     50: );
                     51: 
                     52: echo "-- Sorting 'array_arg' containing different references --\n";
                     53: var_dump( uasort($array_arg, 'cmp_function') );  // expecting: bool(true)
                     54: var_dump($array_arg);
                     55: 
                     56: echo "Done"
                     57: ?>
                     58: --EXPECTF--
                     59: *** Testing uasort() : 'array_arg' with elements as reference ***
                     60: -- Sorting 'array_arg' containing different references --
                     61: bool(true)
                     62: array(5) {
                     63:   [1]=>
                     64:   &int(-5)
                     65:   [4]=>
                     66:   &int(0)
                     67:   [0]=>
                     68:   int(10)
                     69:   [2]=>
                     70:   &int(100)
                     71:   [3]=>
                     72:   int(200)
                     73: }
                     74: Done

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>