rust: prevent dead_code warning
[nbdkit.git] / tests / test-data-optimum.sh
blob533ada6f2026caf8a6e0e6be331ac249fc20c133
1 #!/usr/bin/env bash
2 # nbdkit
3 # Copyright (C) 2018-2021 Red Hat Inc.
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are
7 # met:
9 # * Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
12 # * Redistributions in binary form must reproduce the above copyright
13 # notice, this list of conditions and the following disclaimer in the
14 # documentation and/or other materials provided with the distribution.
16 # * Neither the name of Red Hat nor the names of its contributors may be
17 # used to endorse or promote products derived from this software without
18 # specific prior written permission.
20 # THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
21 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23 # PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
24 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
27 # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30 # OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 # SUCH DAMAGE.
33 # Test optimizations of the data parameter happen.
35 source ./functions.sh
36 set -e
38 requires_run
39 requires_nbdsh_uri
40 requires $SED --version
41 requires tr --version
43 log=data-optimum.log
44 cleanup_fn rm -f $log
45 rm -f $log
47 # Run nbdkit-data-plugin with -D data.AST=1 (to print the final AST)
48 # and check that the expected AST (second parameter) appears in the
49 # debug output.
50 do_test ()
52 data="$1"
53 expected_AST="$2"
55 nbdkit -U - -fv -D data.AST=1 data "$data" --run true >$log 2>&1
57 # Collect up all lines of debug output containing the AST
58 # and concatenate them into a single string.
59 actual_AST="$(
60 $SED -n '
61 /BEGIN AST/,/END AST/{
62 /BEGIN AST/n;
63 /END AST/q;
64 s/.*debug: //p;
65 }' $log | tr -s '[:space:]' ' '
67 actual_AST="${actual_AST% }" ;# remove trailing space
69 # Check expected = actual AST
70 if [ "$expected_AST" != "$actual_AST" ]; then
71 echo "$0: '$data' was not optimized to expected abstract syntax tree"
72 echo "expected AST = $expected_AST"
73 echo "actual AST = $actual_AST"
74 echo "full output from nbdkit:"
75 cat $log
76 exit 1
80 # Single byte.
81 do_test '1' '1'
83 # Sequences of identical bytes are converted to fills.
84 do_test '1 1 1' 'fill(1*3)'
86 # Sequences of non-identical bytes are converted to strings.
87 do_test '0x31 0x32 0x33' '"123"'
89 # Strings of the same byte are converted to fills.
90 do_test '"1111" 0x31' 'fill(49*5)'
92 # Fills and bytes.
93 do_test '"111" 0x31' 'fill(49*4)'
94 do_test '0x31 "111"' 'fill(49*4)'
96 # Adjacent elements of lists are combined.
97 do_test '"12" "3" "456"' '"123456"'
98 do_test '"12" 0x33 "456"' '"123456"'
100 # Multiple nesting is eliminated.
101 do_test '((((1))))' '1'
103 # Adjacent lists are flattened and optimized.
104 do_test '(1 1 1) 1 (1 1 1 1)' 'fill(1*8)'
105 do_test '(((1 1 1))) 1 (1 1 1 1)' 'fill(1*8)'
106 do_test '(((1 1 1) 1)) (1 1 1 1)' 'fill(1*8)'
108 # Adjacent lists with non-identical bytes.
109 do_test '(((2 2 2) 2)) (1 1 1 1)' '( fill(2*4) fill(1*4) )'
110 do_test '(((2 2 2) 2)) (1 1 1 1 3)' '( fill(2*4) fill(1*4) 3 )'
111 # XXX could optimize these better:
112 do_test '(((2 2 2) 2)) (1 1 1 (1 3))' '( fill(2*4) fill(1*3) "\x01\x03" )'
113 do_test '(((2 2 2) 2)) (3 1 1 1 1)' '( fill(2*4) "\x03\x01\x01\x01\x01" )'
115 # Zero repeats become null.
116 do_test '(1 2 3)*0' 'null'
118 # X*Y repeats.
119 do_test '(0x31 0x32 0x33)*4*4' \
120 '"123123123123123123123123123123123123123123123123"'
121 do_test '((0x31 0x32 0x33)*4)*4' \
122 '"123123123123123123123123123123123123123123123123"'
124 # Slices of strings.
125 do_test '"123456"[:]' '"123456"'
126 do_test '"123456"[0:6]' '"123456"'
127 do_test '"123456"[0:]' '"123456"'
128 do_test '"123456"[1:]' '"23456"'
129 do_test '"123456"[2:]' '"3456"'
130 do_test '"123456"[:0]' 'null'
131 do_test '"123456"[:1]' '49'
132 do_test '"123456"[:2]' '"12"'
133 do_test '"123456"[1:2]' '50'
134 do_test '"123456"[2:4]' '"34"'
135 do_test '"123456"[2:2]' 'null'
136 do_test '"123456"[2:6]' '"3456"'
137 do_test '"123456"[5:]' '54'
138 do_test '""[:]' 'null'
140 # Slices of fills.
141 do_test '(1 1 1 1 1 1)[:]' 'fill(1*6)'
142 do_test '(1 1 1 1 1 1)[0:6]' 'fill(1*6)'
143 do_test '(1 1 1 1 1 1)[1:6]' 'fill(1*5)'
144 do_test '(1 1 1 1 1 1)[2:3]' '1'
145 do_test '(1 1 1 1 1 1)[5:]' '1'
146 do_test '(1 1 1 1 1 1)[6:6]' 'null'
148 # Invalid slice can be optimized away.
149 do_test '()[6:]*0' 'null'