summaryrefslogtreecommitdiff
path: root/agen5/test/string.test
blob: dfcabb3de0919ea04510939396b7a15dbae024d2 (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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#! /bin/sh
#  -*- Mode: Shell-script -*-
# string.test --- test string formation rules
#
# Time-stamp:        "2012-05-12 19:56:52 bkorb"
# Author:            Bruce Korb <bkorb@gnu.org>
##
## This file is part of AutoGen.
## AutoGen Copyright (c) 1992-2012 by Bruce Korb - all rights reserved
##
## AutoGen 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.
##
## AutoGen 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 <http://www.gnu.org/licenses/>.
##
# ----------------------------------------------------------------------

. ./defs

#  There are five different things we need to examine:
#
#  1.  That the autogen internal string.
#  2.  What we expect that string to contain.
#  3.  What is generated as the "C" representation
#  4.  What is generated for raw shell strings
#  5.  What is generated for "cooked" shell strings
#
#  We will compare all these things by generating a C program that
#  will test the various strings and a shell script to invoke the
#  program with the two shell string formats for arguments.
#  The program will also write out the expected string value.
#  That value will be compared with what autogen wrote out
#  as its internal value.
#
#  All this stuff must be generated carefully.
#  Specifically, the '${testname}' expressions need to
#  be expanded in certain parts of the output file.
#  In those areas, the eof marker must *not* be quoted.
#  In other places (e.g., where defining the strings),
#  rather than hassle with understanding shell quoting rules,
#  instead we *will* quote the EOF marker to avoid
#  any shell interpretation at all!!
#
# # # # # # # # # # TEMPLATE FILE # # # # # # # # #

echo creating $testname.tpl
exec 4> $testname.tpl
cat >&4 <<_EOF_
[= AutoGen5 Template c sh =]
[=

CASE (suffix)  =][=

  ==  c

=]#include <stdio.h>
#include <string.h>
#include <unistd.h>

#ifndef STDOUT_FILENO
#  define STDOUT_FILENO 1
#endif[=


  ;;  Create a file containing nothing but the
  ;;  autogen internal contents of the string
  ;;
  (out-push-new "${testname}.raw")

  =][=string=][=

  (out-pop)

=]
_EOF_

test -z "$LINENO" && LINENO=`
    ${GREP} -n FIND-THIS-LINE-NUMBER $0 | sed 's/:.*//'` # close enough
printf '\nchar zTestFile[] = "%s";\n#line %s\n' \
    ${testname}.raw `expr $LINENO + 4` >&4

cat >&4 <<'_EOF_'
char zGened[]  = [=(c-string  (get "string"))=];
char zKrGen[]  = [=(kr-string (get "string"))=];
char zExpect[] = "'\f\r\b\v\t\a\n\n"
       "\\f\\r\\b\\v\\t\\a\\n\n"
       "\"Wow!\"  This'll be \\hard\\'\n"
       "#endif /* .\n"
       "and it'll be a \"hassle\"."
       "\001\002\003\n'";
#define expectSize ((int)(sizeof(zExpect) - 1))
int checkStr( char* pz, char const* pzWhat );
int checkStr( char* pz, char const* pzWhat )
{
    static char const zNotMatch[] =
        "%s generated string mismatches at offset %d of %d\n"
        "Expected char: 0x%02X  saw char: 0x%02X\n"
        "Expected string:\n==>%s<==\n\n"
        "Generated string:\n-->%s<--\n\n";

    char* pzE = zExpect;
    char* pzR = pz;
    int   ix  = strlen( pz );
    int   res = 0;

    if (ix != expectSize) {
        fprintf( stderr, "%s is %d bytes, not %d\n", pzWhat, ix, expectSize );
        res = 1;
    }

    for (ix = 0; ix < expectSize; ix++) {
        if (*(pzE++) != *(pzR++)) {
            fprintf(stderr, zNotMatch, pzWhat, ix, expectSize,
                    (unsigned)pzE[-1], (unsigned)pzR[-1], zExpect, pz);
            return 1;
        }
    }
    if (*pzE != '\0') {
        fputs( "compile error: expected string too long\n", stderr);
        res = 1;
    } else if (*pzR != '\0') {
        fprintf(stderr, "%s has %d residual characters:\n==>%s<==\n",
                pzWhat, (int)strlen(pzR), pzR);
        res = 1;
    }
    return res;
}


int main( int argc, char** argv )
{
    int  resCode = 0;

    /*
     *  Write out the expected value to a file.
     *  The "cmp" program will compare it with the
     *  internal version autogen wrote out itself.
     */
    write( STDOUT_FILENO, zExpect, sizeof( zExpect )-1);
    close( STDOUT_FILENO );

    if (sizeof( zGened ) != sizeof( zExpect )) {
        fputs( "Expected and generated string sizes do not match.\n",
               stderr );
        resCode = 1;
    }

    if (strlen( zGened ) != sizeof( zGened )-1) {
        fputs( "The generated string contains a NUL.\n", stderr );
        resCode++;
    }

    if (checkStr( zGened,  "'C' program" ))
        resCode++;

    if (checkStr( zKrGen,  "K&R 'C' program" ))
        resCode++;

    if (checkStr( argv[1], "Raw shell" ))
        resCode++;

    if (checkStr( argv[2], "Cooked shell" ))
        resCode++;

    return resCode;
}[=

  == sh

=]#! /bin/sh
set -x
_EOF_

cat 1>&4 <<_EOF_
./${testname} [=(raw-shell-str (get "string"))
           =] [=(shell-str (get "string"))=] > ${testname}.out
res=\$?
cmp ${testname}.out ${testname}.raw > /dev/null 2>&1

if [ \$? -ne 0 ]
then
  echo the AutoGen internal content did not match expectations
  res=\`expr \$res + 1\`
fi
if [ \$res -eq 0 ]
then
  echo All string comparisons pass
else
  echo There were \$res string test failures
  exit \$res
fi[=

ESAC

=]
_EOF_
exec 4>&-

# # # # # # # # # # DEFINITIONS FILE # # # # # # # # #

echo creating $testname.def
echo "autogen definitions $testname.tpl;" > $testname.def
cat >> $testname.def <<'_EOF_'

string =
    "'\f\r\b\v\t\a\n
"
    '\f\r\b\v\t\a\n
'
    '"Wow!"  This\'ll be \\hard\\\'
\#endif /* .
'
    "and it'll be a \"hassle\"."
    "\001\x02\X03\n'";

_EOF_

# # # # # # # # # # RUN THE TESTS # # # # # # # # # # #

SHELL=${SHELL-/bin/sh}

run_ag x $testname.def || failure autogen failed

compile

chmod +x *.sh

./${testname}.sh || failure strings do not match

cleanup

## Local Variables:
## mode: shell-script
## indent-tabs-mode: nil
## sh-indentation: 2
## sh-basic-offset: 2
## End:

# end of string.test