]> granicus.if.org Git - flex/commitdiff
Extend test-charset to check C++ scanner too
authorMariusz Pluciński <mplucinski@mplucinski.com>
Tue, 24 Jun 2014 17:21:11 +0000 (19:21 +0200)
committerWill Estes <westes575@gmail.com>
Mon, 1 Dec 2014 00:22:43 +0000 (19:22 -0500)
tests/test-charset/Makefile.am
tests/test-charset/scanner.l.in

index d20050bda2f5a40e1d0ffcd158abf6b7ea562b74..79745eb5cf7ca81af08e6af51b54ac821b61beae 100644 (file)
@@ -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
index 61446462abf93dda9bb767a9184278686727fd32..bf9fb2896b9faac676ac6d3875d58701c530d0ae 100644 (file)
 %{
 #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
 
 %}
 
@@ -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
@@ -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,
@@ -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) {
@@ -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 );