fork download
  1. <?php
  2.  
  3. /**
  4.  * Função que testa se o array é sequencial.
  5.  *
  6.  * @param array Array a ser testado
  7.  * @return bool True se $array for sequencial, False caso contrário
  8.  */
  9.  
  10. function is_sequential (array $array) {
  11. return array_filter($array, "is_int", ARRAY_FILTER_USE_KEY) == true;
  12. }
  13.  
  14. /**
  15.  * Função que testa se o array é associativo.
  16.  *
  17.  * @param array Array a ser testado
  18.  * @return bool True se $array for associativo, False caso contrário
  19.  */
  20.  
  21. function is_associative (array $array) {
  22. return array_filter($array, "is_string", ARRAY_FILTER_USE_KEY) == true;
  23. }
  24.  
  25.  
  26. /**
  27.  * Lista de testes que a função deve ser submetida.
  28.  *
  29.  * Cada item da lista deve ser um array com três índices.
  30.  * O primeiro, "array", com o array a ser testado pela função.
  31.  * O segundo, "is_sequential", um valor booleano esperado como retorno da função is_sequential.
  32.  * O terceiro, "is_associative", um valor booleano esperado como retorno da função is_associative.
  33.  */
  34.  
  35. $tests = array();
  36.  
  37. // Teste 1: Array com índices numéricos sequenciais
  38.  
  39. $tests[] = [
  40. "array" => ["a", "b", "c", "d", "e"],
  41. "is_sequential" => true,
  42. "is_associative" => false
  43. ];
  44.  
  45. // Teste 2: Array associativo
  46.  
  47. $tests[] = [
  48. "array" => ["name" => "foo", "lastname" => "bar"],
  49. "is_sequential" => false,
  50. "is_associative" => true
  51. ];
  52.  
  53. // Teste 3: Array com chave do tipo string contendo inteiro válido
  54.  
  55. $tests[] = [
  56. "array" => ["0" => "foo", "1" => "bar"],
  57. "is_sequential" => true,
  58. "is_associative" => false
  59. ];
  60.  
  61. // Teste 4: Array com índices do tipo float
  62.  
  63. $tests[] = [
  64. "array" => [0.5 => "foo", -3.5 => "bar"],
  65. "is_sequential" => true,
  66. "is_associative" => false
  67. ];
  68.  
  69. // Teste 5: Array com índices do tipo booleanos
  70.  
  71. $tests[] = [
  72. "array" => [true => "foo", false => "bar"],
  73. "is_sequential" => true,
  74. "is_associative" => false
  75. ];
  76.  
  77. // Teste 6: Array com índice nulo
  78.  
  79. $tests[] = [
  80. "array" => [null => "foo"],
  81. "is_sequential" => false,
  82. "is_associative" => true
  83. ];
  84.  
  85. // Teste 7: Array misto
  86.  
  87. $tests[] = [
  88. "array" => ["foo", "baz" => "bar"],
  89. "is_sequential" => true,
  90. "is_associative" => true
  91. ];
  92.  
  93. // Teste 8: Array de índices numéricos desordenados
  94.  
  95. $tests[] = [
  96. "array" => [1 => "foo", 0 => "bar"],
  97. "is_sequential" => true,
  98. "is_associative" => false
  99. ];
  100.  
  101. // Teste 9: Array de índices numéricos ordenados não sequenciais
  102.  
  103. $tests[] = [
  104. "array" => [0 => "foo", 1 => "bar", 6 => "baz"],
  105. "is_sequential" => true,
  106. "is_associative" => false
  107. ];
  108.  
  109.  
  110. /**
  111.  * Executa os testes.
  112.  */
  113.  
  114. try {
  115. foreach ($tests as $i => $test) {
  116. if ($test["is_sequential"] !== is_sequential($test["array"])) {
  117. throw new Exception(sprintf("is_sequential: Algo errado não está certo! Teste %d falhou.", $i+1));
  118. }
  119.  
  120. if ($test["is_associative"] !== is_associative($test["array"])) {
  121. throw new Exception(sprintf("is_associative: Algo errado não está certo! Teste %d falhou.", $i+1));
  122. }
  123. }
  124.  
  125. echo "Parabéns! Sua função passou em todos os testes.", PHP_EOL;
  126. } catch (Exception $e) {
  127. echo $e->getMessage(), PHP_EOL;
  128. }
  129.  
  130.  
Success #stdin #stdout 0.01s 82880KB
stdin
Standard input is empty
stdout
Parabéns! Sua função passou em todos os testes.