add test, escape spaces in output

This commit is contained in:
Tilman Kranz 2022-08-21 15:47:44 +02:00
parent 0ae62285f0
commit 69fc45061b
3 changed files with 35 additions and 7 deletions

View File

@ -55,7 +55,14 @@ cpn("aa", "ab", "abc", "abd") = "a{a,b{,c,d}}"
cpn("a", "ab", "abc") = "{a{,b{,c}}}" cpn("a", "ab", "abc") = "{a{,b{,c}}}"
``` ```
*Note:* For the sake of reasoning, strings containing the CPNs reserved characters {, } and , shall be considered invalid input. In a practical implementation, a syntax for „escaping“ these reserved characters should be available. For example, in the reference implementation, the print_trie() function inserts a backslash character \ in front of every literal {, , and }. *Note:* For the sake of reasoning, strings containing the CPNs reserved
characters `{`, `}` and , shall be considered invalid input. In a practical
implementation, a syntax for „escaping“ these reserved characters should be
available. For example, in the reference implementation, the `print_trie()`
function inserts a backslash character `\` in front of every literal `{`, `,`
and `}`; additionally, every space character ` ` is escaped, because it
acts as a word separator in GNU Bourne Again Shell and other interpreters
of brace expansion syntax.
## 2 Implementation ## 2 Implementation
@ -95,8 +102,3 @@ Expected output:
a ab abc a ab abc
``` ```
## Appendix A: References
* [Initial announcement](https://tk-sls.de/wp/6071)
* [Update](https://tk-sls.de/wp/6144)
* [Git repository](https://tk-sls.de/gitlab/tilman/trie)

26
test/01.sh Executable file
View File

@ -0,0 +1,26 @@
#!/bin/bash
dir=$(dirname "$(readlink -f "$0")")
trie="$dir/../trie.py"
read -r -d '' input <<EOF
a b x
a b c
a b c d
EOF
output=$(echo "$input" | "$trie")
# shellcheck disable=SC2086
result=$(eval 'for i in '$output' ; do echo "$i" ; done' | sort)
reference=$(echo "$input" | sort | uniq)
if test "$reference" = "$result" ; then
rv=0
else
printf 'ERROR: reference=\n%s\nresult=\n%s\n' "$reference" "$result"
rv=1
fi
exit $rv

2
trie.py Normal file → Executable file
View File

@ -34,7 +34,7 @@ def print_trie(trie):
count = 1 count = 1
for k in trie: for k in trie:
if k == '{' or k == ',' or k == '}': if k == '{' or k == ',' or k == '}' or k == ' ':
sys.stdout.write('\\') sys.stdout.write('\\')
sys.stdout.write(k) sys.stdout.write(k)