GCC Code Coverage Report


Directory: ./
File: pdserv-1.1.0/test/parser.cpp
Date: 2024-11-17 04:08:36
Exec Total Coverage
Lines: 0 104 0.0%
Branches: 0 356 0.0%

Line Branch Exec Source
1 /*****************************************************************************
2 *
3 * $Id$
4 *
5 * Copyright 2010 Richard Hacker (lerichi at gmx dot net)
6 *
7 * This file is part of the pdserv library.
8 *
9 * The pdserv library is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License as published
11 * by the Free Software Foundation, either version 3 of the License, or (at
12 * your option) any later version.
13 *
14 * The pdserv library is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17 * License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with the pdserv library. If not, see <http://www.gnu.org/licenses/>.
21 *
22 *****************************************************************************/
23
24 #include "XmlParser.h"
25 #include <cstring>
26 #include <cstdio>
27 #include <assert.h>
28 #include <stdarg.h>
29
30 #include <iostream>
31 using std::cout;
32 using std::cerr;
33 using std::endl;
34
35 using namespace MsrProto;
36
37 int test_single(const char *s, ...)
38 {
39 va_list ap;
40 XmlParser inbuf;
41 XmlParser::Element command;
42 int i = 0;
43
44 va_start(ap, s);
45
46 printf("%s\n", s);
47
48 for( i = 0; s[i]; ++i) {
49 *inbuf.bufptr() = s[i];
50 inbuf.newData(1);
51 command = inbuf.nextElement();
52
53 if (command) {
54 int idx = va_arg(ap, int);
55 printf("idx=%i i=%i\n", idx, i);
56 assert(idx and idx == i);
57 }
58 }
59
60 i = va_arg(ap, int);
61 assert(!i);
62
63 va_end(ap);
64 return 1;
65 }
66
67 int main(int , const char *[])
68 {
69 XmlParser inbuf;
70 XmlParser::Element command;
71 const char *s;
72 char *buf;
73
74 // Perfectly legal statement
75 s = "<rp index=\"134\" value=13>";
76 test_single(s, 24, 0);
77 buf = inbuf.bufptr();
78 assert( buf); // Buffer may not be null
79 assert( inbuf.free()); // and have some space
80 strcpy( inbuf.bufptr(), s);
81 inbuf.newData(14);
82 assert(!inbuf.nextElement());
83 inbuf.newData(11);
84 assert((command = inbuf.nextElement()));
85 assert(!strcmp(command.getCommand(), "rp")); // which is "rp"
86 assert(!inbuf.nextElement()); // and no next command
87 assert( buf == inbuf.bufptr()); // Buffer pointer does not change
88
89 // An illegal command - no space after '<'
90 s = "< rp>";
91 test_single(s, 0);
92 strcpy( inbuf.bufptr(), s);
93 inbuf.newData(strlen(s));
94 assert(!inbuf.nextElement());
95 assert(buf == inbuf.bufptr()); // Buffer pointer does not change
96
97 // Two legal commands
98 s = "<a_long_command_with_slash /><wp >";
99 test_single(s, 28, 33, 0);
100 strcpy( inbuf.bufptr(), s);
101 inbuf.newData(strlen(s));
102 assert((command = inbuf.nextElement()));
103 assert(command.getCommand());
104 assert(!strcmp(command.getCommand(), "a_long_command_with_slash"));
105 assert((command = inbuf.nextElement()));
106 assert(command.getCommand());
107 assert(!strcmp(command.getCommand(), "wp"));
108 assert(!inbuf.nextElement());
109
110 s = " lkj <wrong/ >\n<still-incorrect /> <right> lkjs dfkl";
111 test_single(s, 41, 0);
112 strcpy( inbuf.bufptr(), s);
113 inbuf.newData(strlen(s));
114 assert((command = inbuf.nextElement()));
115 assert(command.getCommand());
116 assert(!strcmp(command.getCommand(), "right"));
117 assert(!inbuf.nextElement());
118
119 for (s = "<tag true with=no-quote-attr "
120 "trueval=1 falseval=0 truestr=True falsestr=nottrue onstr='on' "
121 " and=\"quoted /> > &quot; &apos;\" />";
122 *s; s++) {
123 *inbuf.bufptr() = *s;
124 inbuf.newData(1); // == (*s == '>' and !s[1]);
125 assert(!s[1] or !inbuf.nextElement());
126 }
127 assert((command = inbuf.nextElement()));
128 assert(command.getCommand());
129 assert(!strcmp(command.getCommand(), "tag"));
130 assert(!command.isTrue("with"));
131 assert(!command.isTrue("unknown"));
132 assert( command.isTrue("true"));
133 assert( command.isTrue("trueval"));
134 assert(!command.isTrue("falseval"));
135 assert( command.isTrue("truestr"));
136 assert(!command.isTrue("falsestr"));
137 assert( command.isTrue("onstr"));
138 assert(command.find("and", s));
139 assert(!strcmp(s, "quoted /> > &quot; &apos;"));
140 std::string str;
141 assert(command.getString("and", str));
142 assert(str == "quoted /> > \" '");
143 assert(!inbuf.nextElement());
144
145 s = "<tag with=no-quot/>attr and=\"quo\\\"ted /> > &quot; &apos;\" />";
146 test_single(s, 18, 0);
147 strcpy( inbuf.bufptr(), s);
148 inbuf.newData(strlen(s));
149 assert((command = inbuf.nextElement()));
150 assert(command.getCommand());
151 assert(!strcmp(command.getCommand(), "tag"));
152 assert(command.isTrue("tag")); // "tag" is also an attribute
153 assert(command.find("with", s));
154 assert(!strcmp(s, "no-quot"));
155 assert(!inbuf.nextElement());
156
157 s = "<with=no-quot-attr and=\"quoted /> > &quot; &apos;\" />";
158 test_single(s, 52, 0);
159 strcpy( inbuf.bufptr(), s);
160 inbuf.newData(strlen(s));
161 assert((command = inbuf.nextElement()));
162 assert(command.getCommand()); // There is no command
163 assert(command.find("with", s));
164 assert(!strcmp(s, "no-quot-attr"));
165 assert(!inbuf.nextElement());
166 return 0;
167 }
168