Skip to content

Commit

Permalink
Extend test-charset to check C++ scanner too
Browse files Browse the repository at this point in the history
  • Loading branch information
mplucinski committed Jul 25, 2014
1 parent 3f19e6f commit 0fe5125
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 7 deletions.
11 changes: 10 additions & 1 deletion tests/test-charset/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,22 @@ scanner-nr.l: $(srcdir)/scanner.l.in
scanner-r.l: $(srcdir)/scanner.l.in
m4 -P -DVARIANT_REENTRANT=1 $< > $@

scanner-cxx.l: $(srcdir)/scanner.l.in
m4 -P -DVARIANT_CPLUSPLUS=1 $< > $@

scanner-%.c: scanner-%.l
$(FLEX) -o $@ $<

scanner.cpp: scanner-cxx.l
$(FLEX) -o $@ $<

test-charset-%$(EXEEXT): scanner-%.c
$(CC) $(AM_CPPFLAGS) $(CPPFLAGS) $(CFLAGS) -o $@ $(LDFLAGS) $< $(LOADLIBES)

test-charset-%-test: test-charset-%$(EXEEXT)
test-charset-cpp$(EXEEXT): scanner.cpp
$(CXX) $(AM_CPPFLAGS) $(CPPFLAGS) $(CXXFLAGS) -o $@ $(LDFLAGS) $< $(LOADLIBES)

test-charset-%-test: test-charset-%$(EXEEXT) test-charset-cpp
for c in $(cases) ; do \
(./$(<) $$c < $(srcdir)/test-$$c.input | diff -au - $(srcdir)/test-$$c.output) || (echo "Test $$exe failed in $$c case"; exit 1 ); \
done
Expand Down
43 changes: 37 additions & 6 deletions tests/test-charset/scanner.l.in
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,17 @@
%{
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "config.h"

m4_ifdef(`VARIANT_REENTRANT', `#define VARIANT_REENTRANT 1', `')
m4_ifdef(`VARIANT_CPLUSPLUS', `#define VARIANT_CPLUSPLUS 1', `')

#if VARIANT_CPLUSPLUS
#define out(str) (*yyout << str)
#else
#define out(str) (fprintf(yyout, str))
#endif

%}

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

m4_ifdef(`VARIANT_REENTRANT', `%option reentrant', `')

m4_ifdef(`VARIANT_CPLUSPLUS', `%option c++')

%%

[A-Z������������������������������] { fprintf(yyout, "U"); }
[a-z��������������������������������] { fprintf(yyout, "L"); }
[0-9] { fprintf(yyout, "N"); }
[A-Z������������������������������] { out("U"); }
[a-z��������������������������������] { out("L"); }
[0-9] { out("N"); }

%%

#if VARIANT_CPLUSPLUS
class TestFlexLexer: public yyFlexLexer {
public:
#endif
/*
* The function provided by scanner to handle encodings. It gets set of incoming
* bytes and convert into set of characters in internal representation - in the
Expand All @@ -76,12 +88,18 @@ m4_ifdef(`VARIANT_REENTRANT', `%option reentrant', `')
* RETURNS: number of characters that has been written into "target" buffer.
* Must not be greater than value of "target_length" parameter.
*/
#if VARIANT_CPLUSPLUS
size_t yycharset_handler(char *charset, char* source, size_t source_bytes,
YY_CHAR* target, size_t target_length, size_t* converted_bytes)
#else
size_t charset_handler(char *charset, char* source, size_t source_bytes,
YY_CHAR* target, size_t target_length, size_t* converted_bytes
#if VARIANT_REENTRANT
, yyscan_t yyscanner
#endif
) {
)
#endif
{
/* conversion from CP850 to ISO-8859-1. Unrepresentable values are set to -1 */
static int conversion_table_cp850[256] = {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
Expand Down Expand Up @@ -150,6 +168,10 @@ size_t charset_handler(char *charset, char* source, size_t source_bytes,
return source_bytes;
}

#if VARIANT_CPLUSPLUS
};
#endif

int main (int argc, char *argv[])
{
if(argc < 2) {
Expand All @@ -158,15 +180,24 @@ int main (int argc, char *argv[])
}
char *charset = argv[1];

#if VARIANT_REENTRANT
#if VARIANT_CPLUSPLUS
TestFlexLexer lexer;
lexer.set_charset(charset);
assert(strcmp(lexer.get_charset(), charset)==0);
lexer.yylex();
#elif VARIANT_REENTRANT
yyscan_t lexer;

yylex_init(&lexer);

yyset_in(stdin, lexer);
yyset_out(stdout, lexer);

yyset_charset(charset, lexer);
assert(strcmp(yyget_charset(lexer), charset)==0);

yyset_charset_handler(charset_handler, lexer);
assert(yyget_charset_handler(lexer) == charset_handler);

yylex( lexer );

Expand Down

0 comments on commit 0fe5125

Please sign in to comment.