1--TEST--
2Test array_is_list() function
3--FILE--
4<?php
5
6function test_is_list(string $desc, $val) : void {
7    try {
8        printf("%s: %s\n", $desc, json_encode(array_is_list($val)));
9    } catch (TypeError $e) {
10        printf("%s: threw %s\n", $desc, $e->getMessage());
11    }
12}
13
14test_is_list("empty", []);
15test_is_list("one", [1]);
16test_is_list("two", [1,2]);
17test_is_list("three", [1,2,3]);
18test_is_list("four", [1,2,3,4]);
19test_is_list("ten", range(0, 10));
20
21test_is_list("null", null);
22test_is_list("int", 123);
23test_is_list("float", 1.23);
24test_is_list("string", "string");
25test_is_list("object", new stdClass());
26test_is_list("true", true);
27test_is_list("false", false);
28
29test_is_list("string key", ["a" => 1]);
30test_is_list("mixed keys", [0 => 0, "a" => 1]);
31test_is_list("ordered keys", [0 => 0, 1 => 1]);
32test_is_list("shuffled keys", [1 => 0, 0 => 1]);
33test_is_list("skipped keys", [0 => 0, 2 => 2]);
34
35$arr = [1, 2, 3];
36unset($arr[0]);
37test_is_list("unset first", $arr);
38
39$arr = [1, 2, 3];
40unset($arr[1]);
41test_is_list("unset middle", $arr);
42
43$arr = [1, 2, 3];
44unset($arr[2]);
45test_is_list("unset end", $arr);
46
47$arr = [1, "a" => "a", 2];
48unset($arr["a"]);
49test_is_list("unset string key", $arr);
50
51$arr = [1 => 1, 0 => 0];
52unset($arr[1]);
53test_is_list("unset into order", $arr);
54
55$arr = ["a" => 1];
56unset($arr["a"]);
57test_is_list("unset to empty", $arr);
58
59$arr = [1, 2, 3];
60$arr[] = 4;
61test_is_list("append implicit", $arr);
62
63$arr = [1, 2, 3];
64$arr[3] = 4;
65test_is_list("append explicit", $arr);
66
67$arr = [1, 2, 3];
68$arr[4] = 5;
69test_is_list("append with gap", $arr);
70
71--EXPECT--
72empty: true
73one: true
74two: true
75three: true
76four: true
77ten: true
78null: threw array_is_list(): Argument #1 ($array) must be of type array, null given
79int: threw array_is_list(): Argument #1 ($array) must be of type array, int given
80float: threw array_is_list(): Argument #1 ($array) must be of type array, float given
81string: threw array_is_list(): Argument #1 ($array) must be of type array, string given
82object: threw array_is_list(): Argument #1 ($array) must be of type array, stdClass given
83true: threw array_is_list(): Argument #1 ($array) must be of type array, true given
84false: threw array_is_list(): Argument #1 ($array) must be of type array, false given
85string key: false
86mixed keys: false
87ordered keys: true
88shuffled keys: false
89skipped keys: false
90unset first: false
91unset middle: false
92unset end: true
93unset string key: true
94unset into order: true
95unset to empty: true
96append implicit: true
97append explicit: true
98append with gap: false
99