summaryrefslogtreecommitdiff
path: root/testsuite/convert-number.sh
blob: 7f04b8b1823fc009157efaa7441e1701170be12c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/bin/sh
# Test number conversion from escape sequences \xNN \oNNN \dNNN
# (compile.c:convert_number())

# Copyright (C) 2016-2023 Free Software Foundation, Inc.

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
. "${srcdir=.}/testsuite/init.sh"; path_prepend_ ./sed
print_ver_ sed

#
# Test \dNNN conversions
#
printf "%s\n" a a a a a a a > in-d || framework_failure_

# Each line is a separate test case
cat <<\EOF >prog-d
# Expected output: ASCII 0x0D '\r'
1s/./\d13/

# Expected output: ASCII 0xff '\3ff'
2s/./\d255/

# Expected (?) output: 'dB'
# (\d followed by character >= base 10, treated as '\d', which is 'd').
3s/./\dB/

# Expected (?) output: 'dQ'
# (\d followed by non-hex character, treated as '\d', which is 'd').
4s/./\dQ/

# Expected output: '{4'
# \dNNN is limited to three digits.
# The first three digits are 123 = 0x7b = '{'. '4' is treated as-is.
5s/./\d1234/

# Expected (?) output: '\1'
# undocumented implementation-specific limitation:
# After 3 digit limits, the 8-bit value is used,
# decimal 513 wraps-around to 1.
6s/./\d513/

# Expected output: '\0','7'
# (three digit limit)
7s/./\d0007/
EOF

printf '\r\n\377\ndB\ndQ\n{4\n\1\n\0007\n' > exp-d || framework_failure_

sed -f prog-d in-d > out-d || fail=1
compare_ exp-d out-d || fail=1

if test "$fail" -eq 1 ; then
    od -tx1c prog-d
    od -tx1c exp-d
    od -tx1c out-d
fi




#
# Test \oNNN conversions
#
printf "%s\n" a a a a a a > in-o || framework_failure_

# Each line is a separate test case
cat <<\EOF >prog-o
# Expected output: '\5'
1s/./\o5/

# Expected output: ASCII 0xff '\3ff'
2s/./\o377/

# Expected (?) output: 'o9'
# (\o followed by character >= base 18, treated as '\o', which is 'o').
3s/./\o9/

# Expected (?) output: 'oQ'
# (\o followed by non-hex character, treated as '\o', which is 'o').
4s/./\oQ/

# Expected output: 'S4'
# \oNNN is limited to three digits.
# The first three digits are o123 = 0x53 = 'S'. '4' is treated as-is.
5s/./\o1234/

# Expected (?) output: '\1'
# undocumented implementation-specific limitation:
# After 3 digit limits, the 8-bit value is used,
# octal 401 wraps-around to 1.
6s/./\o401/
EOF

printf '\5\n\377\no9\noQ\nS4\n\1\n' > exp-o || framework_failure_

sed -f prog-o in-o > out-o || fail=1
compare_ exp-o out-o || fail=1

if test "$fail" -eq 1 ; then
    od -tx1c prog-o
    od -tx1c exp-o
    od -tx1c out-o
fi





#
# Test \xNN conversions
#
printf "%s\n" a a a a > in-x || framework_failure_

# Each line is a separate test case
cat <<\EOF >prog-x
# Expected output: ASCII 0x06 '\6'
1s/./\x6/

# Expected output: ASCII 0xCE '\316'
2s/./\xce/

# Expected (?) output: 'xy'
# (\x followed by non-hex character, treated as '\x', which is 'x').
3s/./\xy/

# Expected output: '\253' 'c' (0xAB = 253 octal)
# \xNN is limited to two digits.
4s/./\xabc/
EOF

printf '\6\n\316\nxy\n\253c\n' > exp-x || framework_failure_

sed -f prog-x in-x > out-x || fail=1
compare_ exp-x out-x || fail=1

if test "$fail" -eq 1 ; then
    od -tx1c prog-x
    od -tx1c exp-x
    od -tx1c out-x
fi


# for completeness, cover all possible letters/digits

printf "%s\n" a a a a a a a a a a a > cnv-num-in || framework_failure_
cat << \EOF > cnv-num-prog || framework_failure_
1s/./\x01/
2s/./\x23/
3s/./\x45/
4s/./\x67/
5s/./\x89/
6s/./\xAB/
7s/./\xab/
8s/./\xCD/
9s/./\xcd/
10s/./\xef/
11s/./\xEF/
EOF

printf '\1\n#\nE\ng\n\211\n\253\n\253\n\315\n\315\n\357\n\357\n' \
    > cnv-num-exp || framework_failure_

sed -f cnv-num-prog cnv-num-in > cnv-num-out || fail=1
compare_ cnv-num-exp cnv-num-out || fail=1

Exit $fail