]>
Commit | Line | Data |
---|---|---|
4491c030 CR |
1 | # This program is free software: you can redistribute it and/or modify |
2 | # it under the terms of the GNU General Public License as published by | |
3 | # the Free Software Foundation, either version 3 of the License, or | |
4 | # (at your option) any later version. | |
5 | # | |
6 | # This program is distributed in the hope that it will be useful, | |
7 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
8 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
9 | # GNU General Public License for more details. | |
10 | # | |
11 | # You should have received a copy of the GNU General Public License | |
12 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
13 | # | |
14 | ||
15 | # tests for builtins handling associative array keys `*' and `@', with some | |
16 | # indexed array tests as well (backwards compatible) | |
17 | ||
18 | # derived from test cases posted to bug-bash by myoga.murase@gmail.com | |
19 | ||
20 | declare -A assoc | |
21 | declare -a array | |
22 | ||
23 | assoc[@]=at | |
24 | assoc[*]=star | |
25 | assoc[!]=bang | |
26 | ||
27 | key=@ | |
28 | ||
29 | iref='array[@]' | |
30 | aref='assoc[@]' | |
31 | ||
32 | declare -p assoc | |
33 | ||
34 | unset assoc[@] | |
35 | declare -p assoc | |
36 | ||
37 | unset assoc[*] | |
38 | declare -p assoc | |
39 | ||
40 | unset assoc | |
41 | declare -p assoc | |
42 | ||
43 | declare -A assoc | |
44 | assoc[@]=at | |
45 | assoc[*]=star | |
46 | assoc[!]=bang | |
47 | ||
48 | declare -p assoc | |
49 | ||
50 | unset assoc["$key"] | |
51 | declare -p assoc | |
52 | ||
53 | unset assoc["*"] | |
54 | declare -p assoc | |
55 | ||
56 | assoc[@]=at assoc[*]=star | |
57 | ||
58 | unset assoc['@'] | |
59 | declare -p assoc | |
60 | ||
61 | unset assoc['*'] | |
62 | ||
63 | declare -p assoc | |
64 | ||
65 | assoc[@]=at assoc[*]=star | |
66 | echo ${!aref} | |
67 | ||
68 | declare -n nref=$aref | |
69 | echo ${nref} | |
70 | unset -n nref | |
71 | ||
72 | # for associative arrays, test -v treats @ and * as keys | |
73 | ||
74 | test -v 'assoc[@]' | |
75 | echo $? | |
76 | test -v assoc[$key] | |
77 | echo $? | |
78 | [[ -v assoc[$key] ]] | |
79 | echo $? | |
80 | ||
81 | unset -v 'assoc[@]' | |
82 | ||
83 | test -v 'assoc[@]' | |
84 | echo $? | |
85 | [[ -v assoc[@] ]] | |
86 | echo $? | |
87 | ||
88 | assoc[@]=at | |
89 | ||
90 | printf -v assoc[@] "%10s" key | |
91 | declare -p assoc | |
92 | ||
93 | echo === | |
94 | array=() | |
95 | test -v array[@] | |
96 | echo $? | |
97 | [[ -v array[@] ]] | |
98 | echo $? | |
99 | ||
100 | array=(1 2 3) | |
101 | declare -p array | |
102 | ||
103 | echo ${!iref} | |
104 | declare -n nref=$iref | |
105 | ||
106 | echo $nref | |
107 | unset -n nref | |
108 | ||
109 | # but for indexed arrays, test -v treats @ and * as standing for the entire array | |
110 | test -v 'array[@]' | |
111 | echo $? | |
112 | [[ -v array[@] ]] | |
113 | echo $? | |
114 | ||
115 | printf -v array[@] "%-10s" key | |
116 | declare -p array |