Advertisement
Guest User

array.php

a guest
Mar 27th, 2016
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.33 KB | None | 0 0
  1. <?php
  2.  
  3.     /**
  4.     /* Testing
  5.     /*
  6.     /* Testing operators: <, >, ==, ===
  7.     /* Testing type: array
  8.     /*
  9.     /* Expected behaviour:
  10.     /*   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.
  11.     /*   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.".
  12.     /*
  13.     /* Actual behaviour:
  14.     /*   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.
  15.     /*
  16.     /*
  17.     /* Links:
  18.     /*   [1]: http://php.net/manual/en/language.operators.comparison.php
  19.     /*   [2]: http://php.net/manual/en/language.operators.array.php
  20.     /*
  21.      */
  22.    
  23.  
  24.  
  25.  
  26.  
  27.     /**
  28.     /*
  29.     /* Testing operators: < and >
  30.     /*
  31.      */
  32.  
  33.     //Test case
  34.     //Variations: amount, values and keys (order)
  35.     //Test count: 9
  36.     //    Failed: 0
  37.     //    Passed: 9    
  38.     {
  39.         //Test case 1.1
  40.         $a = [1];
  41.         $b = [1];
  42.  
  43.         //Passed
  44.         var_dump("Same amount of elements, keys and values: " . "'<' -> " . bool2str($a < $b) . " '>' -> " . bool2str($a > $b));
  45.        
  46.         //Test case 1.2
  47.         $a = [1];
  48.         $b = [1, 1];
  49.  
  50.         //Passed
  51.         var_dump("NOT same amount of elements, but same values: " . "'<' -> " . bool2str($a < $b) . " '>' -> " . bool2str($a > $b));
  52.  
  53.         //Test case 1.3
  54.         $a = [10];
  55.         $b = [1, 1];
  56.  
  57.         //Passed
  58.         var_dump("NOT same amount of elements nor values: " . "'<' -> " . bool2str($a < $b) . " '>' -> " . bool2str($a > $b));
  59.        
  60.         //Test case 1.4
  61.         $a = [1 => 1];
  62.         $b = [10 => 1];
  63.  
  64.         //Passed
  65.         var_dump("Same amount of element and values, NOT same keys: " . "'<' -> " . bool2str($a < $b) . " '>' -> " . bool2str($a > $b));
  66.  
  67.         //Test case 1.5
  68.         $a = [10];
  69.         $b = [1];
  70.  
  71.         //Passed
  72.         var_dump("Same amount of elements and keys, NOT same values: " . "'<' -> " . bool2str($a < $b) . " '>' -> " . bool2str($a > $b));
  73.        
  74.         //Test case 1.6
  75.         $a = [1 => 1, 2 => 1];
  76.         $b = [2 => 1, 1 => 1];
  77.  
  78.         //Passed
  79.         var_dump("Same amount of elements and keys in different order: " . "'<' -> " . bool2str($a < $b) . " '>' -> " . bool2str($a > $b));
  80.        
  81.         //Test case 1.7
  82.         $a = [1 => 1, 2 => 5];
  83.         $b = [2 => 5];
  84.  
  85.         //Passed
  86.         var_dump("Same values, NOT same amount of elements nor keys: " . "'<' -> " . bool2str($a < $b) . " '>' -> " . bool2str($a > $b));
  87.        
  88.         //Test case 1.8
  89.         $a = [10 => 1];
  90.         $b = [1 => 10];
  91.  
  92.         //Passed
  93.         var_dump("NOT same keys nor values: " . "'<' -> " . bool2str($a < $b) . " '>' -> " . bool2str($a > $b));
  94.        
  95.         //Test case 1.9
  96.         $a = [1 => 1, 2 => 1];
  97.         $b = [2 => 10, 1 => 1];
  98.  
  99.         //Passed
  100.         var_dump("Same amount of elements and values, NOT same keys nor order: " . "'<' -> " . bool2str($a < $b) . " '>' -> " . bool2str($a > $b));
  101.        
  102.     }
  103.  
  104.  
  105.     echo PHP_EOL . PHP_EOL . PHP_EOL;  //Test case separator
  106.  
  107.     /**
  108.     /*
  109.     /* Test case end
  110.     /*
  111.      */
  112.  
  113.  
  114.  
  115.  
  116.  
  117.     /**
  118.     /*
  119.     /* Testing operators: == and ===
  120.     /*
  121.      */
  122.  
  123.     //Test case
  124.     //Variations: amount, values and keys (order)
  125.     //Test count: 5
  126.     //    Failed: 0
  127.     //    Passed: 5    
  128.     {
  129.         //Test case 2.1
  130.         $a = [1];
  131.         $b = [1];
  132.  
  133.         //Passed
  134.         var_dump("Same amount of elements, values and keys: " . "'==' -> " . bool2str($a == $b) . " '===' -> " . bool2str($a === $b));
  135.        
  136.         //Test case 2.2
  137.         $a = [1];
  138.         $b = [10, 1];
  139.  
  140.         //Passed
  141.         var_dump("NOT same amount of elements, but same values: " . "'==' -> " . bool2str($a == $b) . " '===' -> " . bool2str($a === $b));
  142.  
  143.         //Test case 2.3
  144.         $a = [10];
  145.         $b = [1];
  146.  
  147.         //Passed
  148.         var_dump("Same amount of elements, but not values: " . "'==' -> " . bool2str($a == $b) . " '===' -> " . bool2str($a === $b));
  149.        
  150.         //Test case 2.4
  151.         $a = [1 => 1];
  152.         $b = [10 => 1];
  153.  
  154.         //Passed
  155.         var_dump("Same amount of elements and values, but not keys: " . "'==' -> " . bool2str($a == $b) . " '===' -> " . bool2str($a === $b));
  156.        
  157.         //Test case 2.5
  158.         $a = [1 => 1, 2 => 2];
  159.         $b = [2 => 2, 1 => 1];
  160.  
  161.         //Passed
  162.         var_dump("Same amount of elements, key and values, but different order: " . "'==' -> " . bool2str($a == $b) . " '===' -> " . bool2str($a === $b));
  163.        
  164.     }
  165.  
  166.  
  167.     echo PHP_EOL . PHP_EOL . PHP_EOL;  //Test case separator
  168.  
  169.     /**
  170.     /*
  171.     /* Test case end
  172.     /*
  173.      */
  174.  
  175.  
  176.  
  177.     //NULL, TRUE, FALSE 2 str func
  178.     function bool2str($v){if($v === NULL)return "NULL";elseif($v === FALSE)return "FALSE";elseif($v === TRUE)return "TRUE";else return "UNEXPECTED: '$v'";}
  179.  
  180. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement