Annotation of embedaddon/php/ext/standard/tests/array/uasort_variation4.phpt, revision 1.1
1.1 ! misho 1: --TEST--
! 2: Test uasort() function : usage variations - sort different numeric values
! 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: * sorting different types of numeric arrays containing data of following type:
! 12: * integer, octal, hexadecimal & float
! 13: */
! 14:
! 15: // comparision function
! 16: /* Prototype : int cmp_function(mixed $value1, mixed $value2)
! 17: * Parameters : $value1 and $value2 - values to be compared
! 18: * Return value : 0 - if both values are same
! 19: * 1 - if value1 is greater than value2
! 20: * -1 - if value1 is less than value2
! 21: * Description : compares value1 and value2
! 22: */
! 23: function cmp_function($value1, $value2)
! 24: {
! 25: if($value1 == $value2) {
! 26: return 0;
! 27: }
! 28: else if($value1 > $value2) {
! 29: return 1;
! 30: }
! 31: else {
! 32: return -1;
! 33: }
! 34: }
! 35:
! 36: echo "*** Testing uasort() : different numeric arrays as 'array_arg' ***\n";
! 37:
! 38: // Int array
! 39: $int_values = array(0 => 3, 1 => 2, 3 => 100, 4 => 150, 5 => 25, 6 => 350, 7 => 0, 8 => -3, 9 => -1200);
! 40: echo "-- Sorting Integer array --\n";
! 41: var_dump( uasort($int_values, 'cmp_function') ); // expecting: bool(true)
! 42: var_dump($int_values);
! 43:
! 44: // Octal array
! 45: $octal_values = array(0 => 056, 1 => 023, 2 => 090, 3 => 015, 4 => -045, 5 => 01, 6 => -078);
! 46: echo "-- Sorting Octal array --\n";
! 47: var_dump( uasort($octal_values, 'cmp_function') ); // expecting: bool(true)
! 48: var_dump($octal_values);
! 49:
! 50: // Hexadecimal array
! 51: $hex_values = array(0 => 0xAE, 1 => 0x2B, 2 => 0X10, 3 => -0xCF, 4 => 0X12, 5 => -0XF2);
! 52: echo "-- Sorting Hex array --\n";
! 53: var_dump( uasort($hex_values, 'cmp_function') ); // expecting: bool(true)
! 54: var_dump($hex_values);
! 55:
! 56: // Float array
! 57: $float_values = array( 0 => 10.2, 1 => 2.4, 2 => -3.4, 3 => 0, 4 => 0.5, 5 => 7.3e3, 6 => -9.34E-2);
! 58: echo "-- Sorting Float array --\n";
! 59: var_dump( uasort($float_values, 'cmp_function') ); // expecting: bool(true)
! 60: var_dump($float_values);
! 61:
! 62: // empty array
! 63: $empty_array = array();
! 64: echo "-- Sorting empty array --\n";
! 65: var_dump( uasort($empty_array, 'cmp_function') ); // expecting: bool(true)
! 66: var_dump($empty_array);
! 67:
! 68: echo "Done"
! 69: ?>
! 70: --EXPECTF--
! 71: *** Testing uasort() : different numeric arrays as 'array_arg' ***
! 72: -- Sorting Integer array --
! 73: bool(true)
! 74: array(9) {
! 75: [9]=>
! 76: int(-1200)
! 77: [8]=>
! 78: int(-3)
! 79: [7]=>
! 80: int(0)
! 81: [1]=>
! 82: int(2)
! 83: [0]=>
! 84: int(3)
! 85: [5]=>
! 86: int(25)
! 87: [3]=>
! 88: int(100)
! 89: [4]=>
! 90: int(150)
! 91: [6]=>
! 92: int(350)
! 93: }
! 94: -- Sorting Octal array --
! 95: bool(true)
! 96: array(7) {
! 97: [4]=>
! 98: int(-37)
! 99: [6]=>
! 100: int(-7)
! 101: [2]=>
! 102: int(0)
! 103: [5]=>
! 104: int(1)
! 105: [3]=>
! 106: int(13)
! 107: [1]=>
! 108: int(19)
! 109: [0]=>
! 110: int(46)
! 111: }
! 112: -- Sorting Hex array --
! 113: bool(true)
! 114: array(6) {
! 115: [5]=>
! 116: int(-242)
! 117: [3]=>
! 118: int(-207)
! 119: [2]=>
! 120: int(16)
! 121: [4]=>
! 122: int(18)
! 123: [1]=>
! 124: int(43)
! 125: [0]=>
! 126: int(174)
! 127: }
! 128: -- Sorting Float array --
! 129: bool(true)
! 130: array(7) {
! 131: [2]=>
! 132: float(-3.4)
! 133: [6]=>
! 134: float(-0.0934)
! 135: [3]=>
! 136: int(0)
! 137: [4]=>
! 138: float(0.5)
! 139: [1]=>
! 140: float(2.4)
! 141: [0]=>
! 142: float(10.2)
! 143: [5]=>
! 144: float(7300)
! 145: }
! 146: -- Sorting empty array --
! 147: bool(true)
! 148: array(0) {
! 149: }
! 150: Done
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>