Annotation of embedaddon/php/ext/standard/tests/array/usort_basic.phpt, revision 1.1.1.1
1.1 misho 1: --TEST--
2: Test usort() function : basic functionality
3: --FILE--
4: <?php
5: /* Prototype : bool usort(array $array_arg, string $cmp_function)
6: * Description: Sort an array by values using a user-defined comparison function
7: * Source code: ext/standard/array.c
8: */
9:
10: /*
11: * Test basic functionality of usort() with indexed and associative arrays
12: */
13:
14: echo "*** Testing usort() : basic functionality ***\n";
15:
16: function cmp($value1, $value2)
17: {
18: if($value1 == $value2) {
19: return 0;
20: }
21: else if($value1 > $value2) {
22: return 1;
23: }
24: else
25: return -1;
26: }
27:
28: // Int array with default keys
29: $int_values = array(1, 8, 9, 3, 2, 6, 7);
30:
31: echo "\n-- Numeric array with default keys --\n";
32: var_dump( usort($int_values, 'cmp') );
33: var_dump($int_values);
34:
35: // String array with default keys
36: $string_values = array("This", "is", 'a', "test");
37:
38: echo "\n-- String array with default keys --\n";
39: var_dump( usort($string_values, 'cmp') );
40: var_dump($string_values);
41:
42: // Associative array with numeric keys
43: $numeric_key_arg = array(1=> 1, 2 => 2, 3 => 7, 5 => 4, 4 => 9);
44:
45: echo "\n-- Associative array with numeric keys --\n";
46: var_dump( usort($numeric_key_arg, 'cmp') );
47: var_dump($numeric_key_arg);
48:
49: // Associative array with string keys
50: $string_key_arg = array('one' => 4, 'two' => 2, 'three' => 1, 'four' => 10);
51:
52: echo "\n-- Associative array with string keys --\n";
53: var_dump( usort($string_key_arg, 'cmp') );
54: var_dump($string_key_arg);
55: ?>
56: ===DONE===
57: --EXPECTF--
58: *** Testing usort() : basic functionality ***
59:
60: -- Numeric array with default keys --
61: bool(true)
62: array(7) {
63: [0]=>
64: int(1)
65: [1]=>
66: int(2)
67: [2]=>
68: int(3)
69: [3]=>
70: int(6)
71: [4]=>
72: int(7)
73: [5]=>
74: int(8)
75: [6]=>
76: int(9)
77: }
78:
79: -- String array with default keys --
80: bool(true)
81: array(4) {
82: [0]=>
83: string(4) "This"
84: [1]=>
85: string(1) "a"
86: [2]=>
87: string(2) "is"
88: [3]=>
89: string(4) "test"
90: }
91:
92: -- Associative array with numeric keys --
93: bool(true)
94: array(5) {
95: [0]=>
96: int(1)
97: [1]=>
98: int(2)
99: [2]=>
100: int(4)
101: [3]=>
102: int(7)
103: [4]=>
104: int(9)
105: }
106:
107: -- Associative array with string keys --
108: bool(true)
109: array(4) {
110: [0]=>
111: int(1)
112: [1]=>
113: int(2)
114: [2]=>
115: int(4)
116: [3]=>
117: int(10)
118: }
119: ===DONE===
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>