Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- /* Testing
- /*
- /* Testing operators: <, >, ==, ===
- /* Testing type: array
- /*
- /* Expected behaviour:
- /* Based on [1] quote: "Array with fewer members is smaller, if key from operand 1 is not found in operand 2 then arrays are uncomparable, otherwise - compare value by value (see following example)", the expected behaviour would be that < and > only relies on the amount of elements in the arrays.
- /* The expected behaviour for == and === are explained here [2], quote: == -> "TRUE if $a and $b have the same key/value pairs." and === -> "TRUE if $a and $b have the same key/value pairs in the same order and of the same types.".
- /*
- /* Actual behaviour:
- /* The actual behaviour is exactly as described with the code snippet. An array is less than the other array if it has fewer elements. If they have the same amount then each element is compared by value. If one key doesn't exist in the other it's uncomparable, means FLASE.
- /*
- /*
- /* Links:
- /* [1]: http://php.net/manual/en/language.operators.comparison.php
- /* [2]: http://php.net/manual/en/language.operators.array.php
- /*
- */
- /**
- /*
- /* Testing operators: < and >
- /*
- */
- //Test case
- //Variations: amount, values and keys (order)
- //Test count: 9
- // Failed: 0
- // Passed: 9
- {
- //Test case 1.1
- $a = [1];
- $b = [1];
- //Passed
- var_dump("Same amount of elements, keys and values: " . "'<' -> " . bool2str($a < $b) . " '>' -> " . bool2str($a > $b));
- //Test case 1.2
- $a = [1];
- $b = [1, 1];
- //Passed
- var_dump("NOT same amount of elements, but same values: " . "'<' -> " . bool2str($a < $b) . " '>' -> " . bool2str($a > $b));
- //Test case 1.3
- $a = [10];
- $b = [1, 1];
- //Passed
- var_dump("NOT same amount of elements nor values: " . "'<' -> " . bool2str($a < $b) . " '>' -> " . bool2str($a > $b));
- //Test case 1.4
- $a = [1 => 1];
- $b = [10 => 1];
- //Passed
- var_dump("Same amount of element and values, NOT same keys: " . "'<' -> " . bool2str($a < $b) . " '>' -> " . bool2str($a > $b));
- //Test case 1.5
- $a = [10];
- $b = [1];
- //Passed
- var_dump("Same amount of elements and keys, NOT same values: " . "'<' -> " . bool2str($a < $b) . " '>' -> " . bool2str($a > $b));
- //Test case 1.6
- $a = [1 => 1, 2 => 1];
- $b = [2 => 1, 1 => 1];
- //Passed
- var_dump("Same amount of elements and keys in different order: " . "'<' -> " . bool2str($a < $b) . " '>' -> " . bool2str($a > $b));
- //Test case 1.7
- $a = [1 => 1, 2 => 5];
- $b = [2 => 5];
- //Passed
- var_dump("Same values, NOT same amount of elements nor keys: " . "'<' -> " . bool2str($a < $b) . " '>' -> " . bool2str($a > $b));
- //Test case 1.8
- $a = [10 => 1];
- $b = [1 => 10];
- //Passed
- var_dump("NOT same keys nor values: " . "'<' -> " . bool2str($a < $b) . " '>' -> " . bool2str($a > $b));
- //Test case 1.9
- $a = [1 => 1, 2 => 1];
- $b = [2 => 10, 1 => 1];
- //Passed
- var_dump("Same amount of elements and values, NOT same keys nor order: " . "'<' -> " . bool2str($a < $b) . " '>' -> " . bool2str($a > $b));
- }
- echo PHP_EOL . PHP_EOL . PHP_EOL; //Test case separator
- /**
- /*
- /* Test case end
- /*
- */
- /**
- /*
- /* Testing operators: == and ===
- /*
- */
- //Test case
- //Variations: amount, values and keys (order)
- //Test count: 5
- // Failed: 0
- // Passed: 5
- {
- //Test case 2.1
- $a = [1];
- $b = [1];
- //Passed
- var_dump("Same amount of elements, values and keys: " . "'==' -> " . bool2str($a == $b) . " '===' -> " . bool2str($a === $b));
- //Test case 2.2
- $a = [1];
- $b = [10, 1];
- //Passed
- var_dump("NOT same amount of elements, but same values: " . "'==' -> " . bool2str($a == $b) . " '===' -> " . bool2str($a === $b));
- //Test case 2.3
- $a = [10];
- $b = [1];
- //Passed
- var_dump("Same amount of elements, but not values: " . "'==' -> " . bool2str($a == $b) . " '===' -> " . bool2str($a === $b));
- //Test case 2.4
- $a = [1 => 1];
- $b = [10 => 1];
- //Passed
- var_dump("Same amount of elements and values, but not keys: " . "'==' -> " . bool2str($a == $b) . " '===' -> " . bool2str($a === $b));
- //Test case 2.5
- $a = [1 => 1, 2 => 2];
- $b = [2 => 2, 1 => 1];
- //Passed
- var_dump("Same amount of elements, key and values, but different order: " . "'==' -> " . bool2str($a == $b) . " '===' -> " . bool2str($a === $b));
- }
- echo PHP_EOL . PHP_EOL . PHP_EOL; //Test case separator
- /**
- /*
- /* Test case end
- /*
- */
- //NULL, TRUE, FALSE 2 str func
- function bool2str($v){if($v === NULL)return "NULL";elseif($v === FALSE)return "FALSE";elseif($v === TRUE)return "TRUE";else return "UNEXPECTED: '$v'";}
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement