1
0
mirror of https://github.com/php/php-src.git synced 2026-03-26 09:12:14 +01:00
Files
archived-php-src/ext/standard/tests/array/usort_object2.phpt
Peter Kokot d679f02295 Sync leading and final newlines in *.phpt sections
This patch adds missing newlines, trims multiple redundant final
newlines into a single one, and trims redundant leading newlines in all
*.phpt sections.

According to POSIX, a line is a sequence of zero or more non-' <newline>'
characters plus a terminating '<newline>' character. [1] Files should
normally have at least one final newline character.

C89 [2] and later standards [3] mention a final newline:
"A source file that is not empty shall end in a new-line character,
which shall not be immediately preceded by a backslash character."

Although it is not mandatory for all files to have a final newline
fixed, a more consistent and homogeneous approach brings less of commit
differences issues and a better development experience in certain text
editors and IDEs.

[1] http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_206
[2] https://port70.net/~nsz/c/c89/c89-draft.html#2.1.1.2
[3] https://port70.net/~nsz/c/c99/n1256.html#5.1.1.2
2018-10-15 04:33:09 +02:00

166 lines
3.2 KiB
PHP

--TEST--
Test usort() function : object functionality - Different types of classes
--FILE--
<?php
/* Prototype : bool usort(array $array_arg, string $cmp_function)
* Description: Sort an array by values using a user-defined comparison function
* Source code: ext/standard/array.c
*/
/*
* Pass an array of objects which are either:
* 1. Empty
* 2. Static
* 2. Inherited
* to test behaviour of usort()
*/
echo "*** Testing usort() : object functionality ***\n";
function cmp_function($value1, $value2)
{
if($value1 == $value2) {
return 0;
}
else if($value1 > $value2) {
return 1;
}
else
return -1;
}
// Class without any member
class EmptyClass
{
}
// Class with static member
class StaticClass
{
public static $static_value;
public function __construct($value) {
StaticClass::$static_value = $value;
}
}
// Abstract class
abstract class AbstractClass
{
public $pub_value;
public abstract function abstractMethod();
}
// Child class extending abstract class
class ChildClass extends AbstractClass
{
public $child_value = 100;
public function abstractMethod() {
$pub_value = 5;
}
public function __construct($value) {
$this->child_value = $value;
}
}
// Testing uasort with StaticClass objects as elements of 'array_arg'
echo "-- Testing usort() with StaticClass objects --\n";
$array_arg = array(
0 => new StaticClass(20),
1 => new StaticClass(50),
2 => new StaticClass(15),
3 => new StaticClass(70),
);
var_dump( usort($array_arg, 'cmp_function') );
var_dump($array_arg);
// Testing uasort with EmptyClass objects as elements of 'array_arg'
echo "-- Testing usort() with EmptyClass objects --\n";
$array_arg = array(
0 => new EmptyClass(),
1 => new EmptyClass(),
2 => new EmptyClass(),
3 => new EmptyClass(),
);
var_dump( usort($array_arg, 'cmp_function') );
var_dump($array_arg);
// Testing uasort with ChildClass objects as elements of 'array_arg'
echo "-- Testing usort() with ChildClass objects --\n";
$array_arg = array(
0 => new ChildClass(20),
1 => new ChildClass(500),
2 => new ChildClass(15),
3 => new ChildClass(700),
);
var_dump( usort($array_arg, 'cmp_function') );
var_dump($array_arg);
?>
===DONE===
--EXPECTF--
*** Testing usort() : object functionality ***
-- Testing usort() with StaticClass objects --
bool(true)
array(4) {
[0]=>
object(StaticClass)#%d (0) {
}
[1]=>
object(StaticClass)#%d (0) {
}
[2]=>
object(StaticClass)#%d (0) {
}
[3]=>
object(StaticClass)#%d (0) {
}
}
-- Testing usort() with EmptyClass objects --
bool(true)
array(4) {
[0]=>
object(EmptyClass)#%d (0) {
}
[1]=>
object(EmptyClass)#%d (0) {
}
[2]=>
object(EmptyClass)#%d (0) {
}
[3]=>
object(EmptyClass)#%d (0) {
}
}
-- Testing usort() with ChildClass objects --
bool(true)
array(4) {
[0]=>
object(ChildClass)#%d (2) {
["child_value"]=>
int(15)
["pub_value"]=>
NULL
}
[1]=>
object(ChildClass)#%d (2) {
["child_value"]=>
int(20)
["pub_value"]=>
NULL
}
[2]=>
object(ChildClass)#%d (2) {
["child_value"]=>
int(500)
["pub_value"]=>
NULL
}
[3]=>
object(ChildClass)#%d (2) {
["child_value"]=>
int(700)
["pub_value"]=>
NULL
}
}
===DONE===