| 1 | # |
| 2 | # Test the length oeprator, which dash supports. Dash doesn't support most |
| 3 | # other ops. |
| 4 | |
| 5 | #### String length |
| 6 | v=foo |
| 7 | echo ${#v} |
| 8 | ## stdout: 3 |
| 9 | |
| 10 | #### Unicode string length (UTF-8) |
| 11 | v=$'_\u03bc_' |
| 12 | echo ${#v} |
| 13 | ## stdout: 3 |
| 14 | ## N-I dash stdout: 9 |
| 15 | ## N-I mksh stdout: 4 |
| 16 | |
| 17 | #### Unicode string length (spec/testdata/utf8-chars.txt) |
| 18 | v=$(cat $REPO_ROOT/spec/testdata/utf8-chars.txt) |
| 19 | echo ${#v} |
| 20 | ## stdout: 7 |
| 21 | ## N-I dash stdout: 13 |
| 22 | ## N-I mksh stdout: 13 |
| 23 | |
| 24 | #### String length with incomplete utf-8 |
| 25 | for num_bytes in 0 1 2 3 4 5 6 7 8 9 10 11 12 13; do |
| 26 | s=$(head -c $num_bytes $REPO_ROOT/spec/testdata/utf8-chars.txt) |
| 27 | echo ${#s} |
| 28 | done 2> $TMP/err.txt |
| 29 | |
| 30 | grep 'warning:' $TMP/err.txt |
| 31 | true # exit 0 |
| 32 | |
| 33 | ## STDOUT: |
| 34 | 0 |
| 35 | 1 |
| 36 | 2 |
| 37 | -1 |
| 38 | 3 |
| 39 | 4 |
| 40 | -1 |
| 41 | -1 |
| 42 | 5 |
| 43 | 6 |
| 44 | -1 |
| 45 | -1 |
| 46 | -1 |
| 47 | 7 |
| 48 | [ stdin ]:3: warning: Incomplete UTF-8 character |
| 49 | [ stdin ]:3: warning: Incomplete UTF-8 character |
| 50 | [ stdin ]:3: warning: Incomplete UTF-8 character |
| 51 | [ stdin ]:3: warning: Incomplete UTF-8 character |
| 52 | [ stdin ]:3: warning: Incomplete UTF-8 character |
| 53 | [ stdin ]:3: warning: Incomplete UTF-8 character |
| 54 | ## END |
| 55 | # zsh behavior actually matches bash! |
| 56 | ## BUG bash/zsh stderr-json: "" |
| 57 | ## BUG bash/zsh STDOUT: |
| 58 | 0 |
| 59 | 1 |
| 60 | 2 |
| 61 | 3 |
| 62 | 3 |
| 63 | 4 |
| 64 | 5 |
| 65 | 6 |
| 66 | 5 |
| 67 | 6 |
| 68 | 7 |
| 69 | 8 |
| 70 | 9 |
| 71 | 7 |
| 72 | ## END |
| 73 | ## BUG dash/mksh stderr-json: "" |
| 74 | ## N-I dash/mksh STDOUT: |
| 75 | 0 |
| 76 | 1 |
| 77 | 2 |
| 78 | 3 |
| 79 | 4 |
| 80 | 5 |
| 81 | 6 |
| 82 | 7 |
| 83 | 8 |
| 84 | 9 |
| 85 | 10 |
| 86 | 11 |
| 87 | 12 |
| 88 | 13 |
| 89 | ## END |
| 90 | |
| 91 | #### String length with invalid utf-8 continuation bytes |
| 92 | for num_bytes in 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14; do |
| 93 | s=$(head -c $num_bytes $REPO_ROOT/spec/testdata/utf8-chars.txt)$(echo -e "\xFF") |
| 94 | echo ${#s} |
| 95 | done 2> $TMP/err.txt |
| 96 | |
| 97 | grep 'warning:' $TMP/err.txt |
| 98 | true |
| 99 | |
| 100 | ## STDOUT: |
| 101 | -1 |
| 102 | -1 |
| 103 | -1 |
| 104 | -1 |
| 105 | -1 |
| 106 | -1 |
| 107 | -1 |
| 108 | -1 |
| 109 | -1 |
| 110 | -1 |
| 111 | -1 |
| 112 | -1 |
| 113 | -1 |
| 114 | -1 |
| 115 | -1 |
| 116 | [ stdin ]:3: warning: Invalid start of UTF-8 character |
| 117 | [ stdin ]:3: warning: Invalid start of UTF-8 character |
| 118 | [ stdin ]:3: warning: Invalid start of UTF-8 character |
| 119 | [ stdin ]:3: warning: Invalid UTF-8 continuation byte |
| 120 | [ stdin ]:3: warning: Invalid start of UTF-8 character |
| 121 | [ stdin ]:3: warning: Invalid start of UTF-8 character |
| 122 | [ stdin ]:3: warning: Invalid UTF-8 continuation byte |
| 123 | [ stdin ]:3: warning: Invalid UTF-8 continuation byte |
| 124 | [ stdin ]:3: warning: Invalid start of UTF-8 character |
| 125 | [ stdin ]:3: warning: Invalid start of UTF-8 character |
| 126 | [ stdin ]:3: warning: Invalid UTF-8 continuation byte |
| 127 | [ stdin ]:3: warning: Invalid UTF-8 continuation byte |
| 128 | [ stdin ]:3: warning: Invalid UTF-8 continuation byte |
| 129 | [ stdin ]:3: warning: Invalid start of UTF-8 character |
| 130 | [ stdin ]:3: warning: Invalid start of UTF-8 character |
| 131 | ## END |
| 132 | ## BUG bash/zsh stderr-json: "" |
| 133 | ## BUG bash/zsh STDOUT: |
| 134 | 1 |
| 135 | 2 |
| 136 | 3 |
| 137 | 4 |
| 138 | 4 |
| 139 | 5 |
| 140 | 6 |
| 141 | 7 |
| 142 | 6 |
| 143 | 7 |
| 144 | 8 |
| 145 | 9 |
| 146 | 10 |
| 147 | 8 |
| 148 | 8 |
| 149 | ## N-I dash stderr-json: "" |
| 150 | ## N-I dash STDOUT: |
| 151 | 7 |
| 152 | 8 |
| 153 | 9 |
| 154 | 10 |
| 155 | 11 |
| 156 | 12 |
| 157 | 13 |
| 158 | 14 |
| 159 | 15 |
| 160 | 16 |
| 161 | 17 |
| 162 | 18 |
| 163 | 19 |
| 164 | 20 |
| 165 | 20 |
| 166 | ## END |
| 167 | ## N-I mksh stderr-json: "" |
| 168 | ## N-I mksh STDOUT: |
| 169 | 1 |
| 170 | 2 |
| 171 | 3 |
| 172 | 4 |
| 173 | 5 |
| 174 | 6 |
| 175 | 7 |
| 176 | 8 |
| 177 | 9 |
| 178 | 10 |
| 179 | 11 |
| 180 | 12 |
| 181 | 13 |
| 182 | 14 |
| 183 | 14 |
| 184 | ## END |
| 185 | |
| 186 | #### Length of undefined variable |
| 187 | echo ${#undef} |
| 188 | ## stdout: 0 |
| 189 | |
| 190 | #### Length of undefined variable with nounset |
| 191 | set -o nounset |
| 192 | echo ${#undef} |
| 193 | ## status: 1 |
| 194 | ## OK dash status: 2 |
| 195 | |
| 196 | #### Length operator can't be followed by test operator |
| 197 | echo ${#x-default} |
| 198 | |
| 199 | x='' |
| 200 | echo ${#x-default} |
| 201 | |
| 202 | x='foo' |
| 203 | echo ${#x-default} |
| 204 | |
| 205 | ## status: 2 |
| 206 | ## OK bash/mksh status: 1 |
| 207 | ## stdout-json: "" |
| 208 | ## BUG zsh status: 0 |
| 209 | ## BUG zsh STDOUT: |
| 210 | 7 |
| 211 | 0 |
| 212 | 3 |
| 213 | ## END |
| 214 | ## BUG dash status: 0 |
| 215 | ## BUG dash STDOUT: |
| 216 | 0 |
| 217 | 0 |
| 218 | 3 |
| 219 | ## END |