1 #include "UserTestTraits.hpp"
2 #include "t005lexer.hpp"
3
4 #include <sys/types.h>
5
6 #include <iostream>
7 #include <sstream>
8 #include <fstream>
9
10 using namespace Antlr3Test;
11 using namespace std;
12
13 int testValid(string const& data);
14 int testMalformedInput1(string const& data);
15 int testMalformedInput2(string const& data);
16
17 static t005lexer *lxr;
18
19 struct TokenData
20 {
21 t005lexerTokens::Tokens type;
22 unsigned start;
23 unsigned stop;
24 const char* text;
25 };
26
27 static TokenData ExpectedTokens[] =
28 {
29 // "fofoofooo"
30 { t005lexerTokens::FOO, 0, 1, "fo"},
31 { t005lexerTokens::FOO, 2, 4, "foo"},
32 { t005lexerTokens::FOO, 5, 8, "fooo"},
33 { t005lexerTokens::EOF_TOKEN, 9, 9, "<EOF>"}
34 };
35
main(int argc,char * argv[])36 int main (int argc, char *argv[])
37 {
38 testValid("fofoofooo");
39 testMalformedInput1("2");
40 testMalformedInput2("f");
41 return 0;
42 }
43
testValid(string const & data)44 int testValid(string const& data)
45 {
46 t005lexerTraits::InputStreamType* input = new t005lexerTraits::InputStreamType((const ANTLR_UINT8 *)data.c_str(),
47 ANTLR_ENC_8BIT,
48 data.length(), //strlen(data.c_str()),
49 (ANTLR_UINT8*)"t005");
50 if (lxr == NULL)
51 lxr = new t005lexer(input);
52 else
53 lxr->setCharStream(input);
54
55 std::cout << "testValid: \"" << data << '"' <<std::endl;
56
57 std::cout << "Text:" << '\t'
58 << "Type:" << '\t'
59 << "Start:" << '\t'
60 << "Stop:" << '\t'
61 << "Text:" << '\t' << std::endl;
62
63 for(unsigned i = 0; i < sizeof(ExpectedTokens)/sizeof(TokenData) ; i++)
64 {
65 // nextToken does not allocate any new Token instance(the same instance is returned again and again)
66 t005lexerTraits::CommonTokenType *token = lxr->nextToken();
67
68 size_t startIndex = ((const char*)token->get_startIndex()) - data.c_str();
69 size_t stopIndex = ((const char*)token->get_stopIndex()) - data.c_str();
70
71 std::cout << token->getText()
72 << '\t' << (token->getType() == ExpectedTokens[i].type ? "OK" : "Fail")
73 << '\t' << (startIndex == ExpectedTokens[i].start ? "OK" : "Fail")
74 << '\t' << (stopIndex == ExpectedTokens[i].stop ? "OK" : "Fail")
75 << '\t' << (token->getText() == ExpectedTokens[i].text ? "OK" : "Fail")
76 << std::endl;
77
78 }
79 delete lxr; lxr = NULL;
80 delete input;
81 return 0;
82 }
83
testMalformedInput1(string const & data)84 int testMalformedInput1(string const& data)
85 {
86 t005lexerTraits::InputStreamType* input = new t005lexerTraits::InputStreamType((const ANTLR_UINT8 *)data.c_str(),
87 ANTLR_ENC_8BIT,
88 data.length(), //strlen(data.c_str()),
89 (ANTLR_UINT8*)"t005");
90 if (lxr == NULL)
91 lxr = new t005lexer(input);
92 else
93 lxr->setCharStream(input);
94
95 std::cout << "testMalformedInput1: \"" << data << '"' <<std::endl;
96
97 t005lexerTraits::CommonTokenType *token0 = lxr->nextToken();
98 std::cout << token0->getText() << std::endl;
99
100 //except antlr3.MismatchedTokenException as exc:
101 //self.assertEqual(exc.expecting, 'f')
102 //self.assertEqual(exc.unexpectedType, '2')
103
104 delete lxr; lxr = NULL;
105 delete input;
106 return 0;
107 }
108
testMalformedInput2(string const & data)109 int testMalformedInput2(string const& data)
110 {
111 t005lexerTraits::InputStreamType* input = new t005lexerTraits::InputStreamType((const ANTLR_UINT8 *)data.c_str(),
112 ANTLR_ENC_8BIT,
113 data.length(), //strlen(data.c_str()),
114 (ANTLR_UINT8*)"t005");
115 if (lxr == NULL)
116 lxr = new t005lexer(input);
117 else
118 lxr->setCharStream(input);
119
120 std::cout << "testMalformedInput2: \"" << data << '"' <<std::endl;
121
122 t005lexerTraits::CommonTokenType *token0 = lxr->nextToken();
123 std::cout << token0->getText() << std::endl;
124
125 //except antlr3.EarlyExitException as exc:
126 //self.assertEqual(exc.unexpectedType, antlr3.EOF)
127
128 delete lxr; lxr = NULL;
129 delete input;
130 return 0;
131 }
132