Skip to content

Commit 0fe5125

Browse files
committed
Extend test-charset to check C++ scanner too
1 parent 3f19e6f commit 0fe5125

File tree

2 files changed

+47
-7
lines changed

2 files changed

+47
-7
lines changed

tests/test-charset/Makefile.am

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,22 @@ scanner-nr.l: $(srcdir)/scanner.l.in
4545
scanner-r.l: $(srcdir)/scanner.l.in
4646
m4 -P -DVARIANT_REENTRANT=1 $< > $@
4747

48+
scanner-cxx.l: $(srcdir)/scanner.l.in
49+
m4 -P -DVARIANT_CPLUSPLUS=1 $< > $@
50+
4851
scanner-%.c: scanner-%.l
4952
$(FLEX) -o $@ $<
5053

54+
scanner.cpp: scanner-cxx.l
55+
$(FLEX) -o $@ $<
56+
5157
test-charset-%$(EXEEXT): scanner-%.c
5258
$(CC) $(AM_CPPFLAGS) $(CPPFLAGS) $(CFLAGS) -o $@ $(LDFLAGS) $< $(LOADLIBES)
5359

54-
test-charset-%-test: test-charset-%$(EXEEXT)
60+
test-charset-cpp$(EXEEXT): scanner.cpp
61+
$(CXX) $(AM_CPPFLAGS) $(CPPFLAGS) $(CXXFLAGS) -o $@ $(LDFLAGS) $< $(LOADLIBES)
62+
63+
test-charset-%-test: test-charset-%$(EXEEXT) test-charset-cpp
5564
for c in $(cases) ; do \
5665
(./$(<) $$c < $(srcdir)/test-$$c.input | diff -au - $(srcdir)/test-$$c.output) || (echo "Test $$exe failed in $$c case"; exit 1 ); \
5766
done

tests/test-charset/scanner.l.in

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,17 @@
3737
%{
3838
#include <stdio.h>
3939
#include <stdlib.h>
40+
#include <assert.h>
4041
#include "config.h"
4142

4243
m4_ifdef(`VARIANT_REENTRANT', `#define VARIANT_REENTRANT 1', `')
44+
m4_ifdef(`VARIANT_CPLUSPLUS', `#define VARIANT_CPLUSPLUS 1', `')
45+
46+
#if VARIANT_CPLUSPLUS
47+
#define out(str) (*yyout << str)
48+
#else
49+
#define out(str) (fprintf(yyout, str))
50+
#endif
4351

4452
%}
4553

@@ -48,16 +56,20 @@ m4_ifdef(`VARIANT_REENTRANT', `#define VARIANT_REENTRANT 1', `')
4856
%option warn
4957

5058
m4_ifdef(`VARIANT_REENTRANT', `%option reentrant', `')
51-
59+
m4_ifdef(`VARIANT_CPLUSPLUS', `%option c++')
5260

5361
%%
5462

55-
[A-Z������������������������������] { fprintf(yyout, "U"); }
56-
[a-z��������������������������������] { fprintf(yyout, "L"); }
57-
[0-9] { fprintf(yyout, "N"); }
63+
[A-Z������������������������������] { out("U"); }
64+
[a-z��������������������������������] { out("L"); }
65+
[0-9] { out("N"); }
5866

5967
%%
6068

69+
#if VARIANT_CPLUSPLUS
70+
class TestFlexLexer: public yyFlexLexer {
71+
public:
72+
#endif
6173
/*
6274
* The function provided by scanner to handle encodings. It gets set of incoming
6375
* bytes and convert into set of characters in internal representation - in the
@@ -76,12 +88,18 @@ m4_ifdef(`VARIANT_REENTRANT', `%option reentrant', `')
7688
* RETURNS: number of characters that has been written into "target" buffer.
7789
* Must not be greater than value of "target_length" parameter.
7890
*/
91+
#if VARIANT_CPLUSPLUS
92+
size_t yycharset_handler(char *charset, char* source, size_t source_bytes,
93+
YY_CHAR* target, size_t target_length, size_t* converted_bytes)
94+
#else
7995
size_t charset_handler(char *charset, char* source, size_t source_bytes,
8096
YY_CHAR* target, size_t target_length, size_t* converted_bytes
8197
#if VARIANT_REENTRANT
8298
, yyscan_t yyscanner
8399
#endif
84-
) {
100+
)
101+
#endif
102+
{
85103
/* conversion from CP850 to ISO-8859-1. Unrepresentable values are set to -1 */
86104
static int conversion_table_cp850[256] = {
87105
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
@@ -150,6 +168,10 @@ size_t charset_handler(char *charset, char* source, size_t source_bytes,
150168
return source_bytes;
151169
}
152170

171+
#if VARIANT_CPLUSPLUS
172+
};
173+
#endif
174+
153175
int main (int argc, char *argv[])
154176
{
155177
if(argc < 2) {
@@ -158,15 +180,24 @@ int main (int argc, char *argv[])
158180
}
159181
char *charset = argv[1];
160182

161-
#if VARIANT_REENTRANT
183+
#if VARIANT_CPLUSPLUS
184+
TestFlexLexer lexer;
185+
lexer.set_charset(charset);
186+
assert(strcmp(lexer.get_charset(), charset)==0);
187+
lexer.yylex();
188+
#elif VARIANT_REENTRANT
162189
yyscan_t lexer;
163190

164191
yylex_init(&lexer);
165192

166193
yyset_in(stdin, lexer);
167194
yyset_out(stdout, lexer);
195+
168196
yyset_charset(charset, lexer);
197+
assert(strcmp(yyget_charset(lexer), charset)==0);
198+
169199
yyset_charset_handler(charset_handler, lexer);
200+
assert(yyget_charset_handler(lexer) == charset_handler);
170201

171202
yylex( lexer );
172203

0 commit comments

Comments
 (0)