PageRenderTime 31ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/hphp/test/zend/good/ext/standard/tests/array/ksort_basic.php

http://github.com/facebook/hiphop-php
PHP | 72 lines | 47 code | 12 blank | 13 comment | 0 complexity | dfdaf28d224ec658973bab42afb32885 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-2-Clause, BSD-3-Clause, MPL-2.0-no-copyleft-exception, MIT, LGPL-2.0, Apache-2.0
  1. <?hh
  2. /* Prototype : bool ksort ( array &$array [, int $sort_flags] )
  3. * Description: Sort an array by key, maintaining key to data correlation
  4. * Source code: ext/standard/array.c
  5. */
  6. /*
  7. * Testing ksort() by providing array of integer/string values to check the basic functionality with following flag values :
  8. * 1.flag value as defualt
  9. * 2.SORT_REGULAR - compare items normally
  10. * 3.SORT_NUMERIC - compare items numerically
  11. * 4.SORT_STRING - compare items as strings
  12. */
  13. <<__EntryPoint>> function main(): void {
  14. echo "*** Testing ksort() : basic functionality ***\n";
  15. // an array containing unsorted string values with indices
  16. $unsorted_strings = darray[
  17. "l" => "lemon", "o" => "orange",
  18. "O" => "Orange", "O1" => "Orange1", "o2" => "orange2", "O3" => "Orange3", "o20" => "orange20",
  19. "b" => "banana",
  20. ];
  21. // an array containing unsorted numeric values with indices
  22. $unsorted_numerics = darray[ 100 => 4, 33 => 3, 555 => 2, 22 => 1 ];
  23. echo "\n-- Testing ksort() by supplying string array, 'flag' value is defualt --\n";
  24. $temp_array = $unsorted_strings;
  25. var_dump( ksort(inout $temp_array) ); // expecting : bool(true)
  26. var_dump( $temp_array);
  27. echo "\n-- Testing ksort() by supplying numeric array, 'flag' value is defualt --\n";
  28. $temp_array = $unsorted_numerics;
  29. var_dump( ksort(inout $temp_array) ); // expecting : bool(true)
  30. var_dump( $temp_array);
  31. echo "\n-- Testing ksort() by supplying string array, 'flag' = SORT_REGULAR --\n";
  32. $temp_array = $unsorted_strings;
  33. var_dump( ksort(inout $temp_array, SORT_REGULAR) ); // expecting : bool(true)
  34. var_dump( $temp_array);
  35. echo "\n-- Testing ksort() by supplying numeric array, 'flag' = SORT_REGULAR --\n";
  36. $temp_array = $unsorted_numerics;
  37. var_dump( ksort(inout $temp_array, SORT_REGULAR) ); // expecting : bool(true)
  38. var_dump( $temp_array);
  39. echo "\n-- Testing ksort() by supplying string array, 'flag' = SORT_STRING --\n";
  40. $temp_array = $unsorted_strings;
  41. var_dump( ksort(inout $temp_array, SORT_STRING) ); // expecting : bool(true)
  42. var_dump( $temp_array);
  43. echo "\n-- Testing ksort() by supplying string array (case insensitive), 'flag' = SORT_STRING|SORT_FLAG_CASE --\n";
  44. $temp_array = $unsorted_strings;
  45. var_dump( sort(inout $temp_array, SORT_STRING|SORT_FLAG_CASE) ); // expecting : bool(true)
  46. var_dump( $temp_array);
  47. echo "\n-- Testing ksort() by supplying string array (natural), 'flag' = SORT_NATURAL --\n";
  48. $temp_array = $unsorted_strings;
  49. var_dump( sort(inout $temp_array, SORT_NATURAL) ); // expecting : bool(true)
  50. var_dump( $temp_array);
  51. echo "\n-- Testing ksort() by supplying string array (natural, case insensitive), 'flag' = SORT_NATURAL|SORT_FLAG_CASE --\n";
  52. $temp_array = $unsorted_strings;
  53. var_dump( sort(inout $temp_array, SORT_NATURAL|SORT_FLAG_CASE) ); // expecting : bool(true)
  54. var_dump( $temp_array);
  55. echo "\n-- Testing ksort() by supplying numeric array, 'flag' = SORT_NUMERIC --\n";
  56. $temp_array = $unsorted_numerics;
  57. var_dump( ksort(inout $temp_array, SORT_NUMERIC) ); // expecting : bool(true)
  58. var_dump( $temp_array);
  59. echo "Done\n";
  60. }