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