gwenhywfar  5.10.1
buffer-t.c
Go to the documentation of this file.
1 /***************************************************************************
2  begin : Mon Feb 10 2020
3  copyright : (C) 2020 by Martin Preuss
4  email : martin@libchipcard.de
5 
6  ***************************************************************************
7  * *
8  * This library is free software; you can redistribute it and/or *
9  * modify it under the terms of the GNU Lesser General Public *
10  * License as published by the Free Software Foundation; either *
11  * version 2.1 of the License, or (at your option) any later version. *
12  * *
13  * This library is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
16  * Lesser General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU Lesser General Public *
19  * License along with this library; if not, write to the Free Software *
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
21  * MA 02111-1307 USA *
22  * *
23  ***************************************************************************/
24 
25 
26 /* This file is included by "buffer.c" */
27 
28 
29 #include <gwenhywfar/testframework.h>
30 #include "buffer-t.h"
31 
32 
33 #ifdef GWENHYWFAR_ENABLE_TESTCODE
34 
35 
36 
37 /* ------------------------------------------------------------------------------------------------
38  * forward declarations
39  * ------------------------------------------------------------------------------------------------
40  */
41 
42 static int GWENHYWFAR_CB test1(GWEN_TEST_MODULE *mod);
43 static int GWENHYWFAR_CB test2(GWEN_TEST_MODULE *mod);
44 static int GWENHYWFAR_CB test3(GWEN_TEST_MODULE *mod);
45 static int GWENHYWFAR_CB test4(GWEN_TEST_MODULE *mod);
46 static int GWENHYWFAR_CB test5(GWEN_TEST_MODULE *mod);
47 
48 
49 
50 
51 
52 /* ------------------------------------------------------------------------------------------------
53  * implementations
54  * ------------------------------------------------------------------------------------------------
55  */
56 
57 
59 {
60  GWEN_TEST_MODULE *newMod;
61 
62  newMod=GWEN_Test_Module_AddModule(mod, "GWEN_Buffer", NULL);
63 
64  GWEN_Test_Module_AddTest(newMod, "append args (simple)", test1, NULL);
65  GWEN_Test_Module_AddTest(newMod, "append args (long string)", test2, NULL);
66 
67  GWEN_Test_Module_AddTest(newMod, "cutout text between strings (onlyBetween=0)", test3, NULL);
68  GWEN_Test_Module_AddTest(newMod, "cutout text between strings (onlyBetween=1)", test4, NULL);
69  GWEN_Test_Module_AddTest(newMod, "cutout text between strings, add something behind", test5, NULL);
70 
71  return 0;
72 }
73 
74 
75 
77 {
78  GWEN_BUFFER *buf;
79  int rv;
80 
81  buf=GWEN_Buffer_new(0, 16, 0, 1);
82  rv=GWEN_Buffer_AppendArgs(buf, "%s-%d", "TESTSTRING", 1234);
83  if (rv<0) {
84  DBG_ERROR(GWEN_LOGDOMAIN, "Could not append args");
85  GWEN_Buffer_free(buf);
86  return GWEN_ERROR_GENERIC;
87  }
88 
89  if (strcmp(GWEN_Buffer_GetStart(buf), "TESTSTRING-1234")!=0) {
90  DBG_ERROR(GWEN_LOGDOMAIN, "Unexpected string in buffer (%s)", GWEN_Buffer_GetStart(buf));
91  GWEN_Buffer_free(buf);
92  return GWEN_ERROR_GENERIC;
93  }
94  GWEN_Buffer_free(buf);
95 
96  return 0;
97 }
98 
99 
100 
102 {
103  GWEN_BUFFER *buf;
104  int rv;
105  static const char *testString=
106  "0123456789"
107  "0123456789"
108  "0123456789"
109  "0123456789"
110  "0123456789"
111  "0123456789"
112  "0123456789"
113  "0123456789"
114  "0123456789"
115  "0123456789"
116  "0123456789"
117  "0123456789"
118  "0123456789"
119  "0123456789";
120 
121  buf=GWEN_Buffer_new(0, 16, 0, 1);
122  rv=GWEN_Buffer_AppendArgs(buf, "%s", testString);
123  if (rv<0) {
124  DBG_ERROR(GWEN_LOGDOMAIN, "Could not append args");
125  GWEN_Buffer_free(buf);
126  return GWEN_ERROR_GENERIC;
127  }
128 
129  if (strcmp(GWEN_Buffer_GetStart(buf), testString)!=0) {
130  DBG_ERROR(GWEN_LOGDOMAIN, "Unexpected string in buffer (%s)", GWEN_Buffer_GetStart(buf));
131  GWEN_Buffer_free(buf);
132  return GWEN_ERROR_GENERIC;
133  }
134  GWEN_Buffer_free(buf);
135 
136  return 0;
137 }
138 
139 
140 
142 {
143  GWEN_BUFFER *buf;
144  int rv;
145  static const char *testString1=
146  "0000000000"
147  "<begin>"
148  "1111111111"
149  "<end>"
150  "222222222";
151  static const char *testString2=
152  "<begin>"
153  "1111111111"
154  "<end>";
155 
156  buf=GWEN_Buffer_new(0, 16, 0, 1);
157  rv=GWEN_Buffer_AppendString(buf, testString1);
158  if (rv<0) {
159  DBG_ERROR(GWEN_LOGDOMAIN, "Could not append string");
160  GWEN_Buffer_free(buf);
161  return GWEN_ERROR_GENERIC;
162  }
163 
164  rv=GWEN_Buffer_KeepTextBetweenStrings(buf, "<begin>", "<end>", 0);
165  if (rv<0) {
166  DBG_ERROR(GWEN_LOGDOMAIN, "Could not cut data out (%d)", rv);
167  GWEN_Buffer_free(buf);
168  return GWEN_ERROR_GENERIC;
169  }
170 
171 
172  if (strcmp(GWEN_Buffer_GetStart(buf), testString2)!=0) {
173  DBG_ERROR(GWEN_LOGDOMAIN, "Unexpected string in buffer (%s)", GWEN_Buffer_GetStart(buf));
174  GWEN_Buffer_free(buf);
175  return GWEN_ERROR_GENERIC;
176  }
177  GWEN_Buffer_free(buf);
178 
179  return 0;
180 }
181 
182 
183 
185 {
186  GWEN_BUFFER *buf;
187  int rv;
188  static const char *testString1=
189  "0000000000"
190  "<begin>"
191  "1111111111"
192  "<end>"
193  "222222222";
194  static const char *testString2=
195  "1111111111";
196 
197  buf=GWEN_Buffer_new(0, 16, 0, 1);
198  rv=GWEN_Buffer_AppendString(buf, testString1);
199  if (rv<0) {
200  DBG_ERROR(GWEN_LOGDOMAIN, "Could not append string");
201  GWEN_Buffer_free(buf);
202  return GWEN_ERROR_GENERIC;
203  }
204 
205  rv=GWEN_Buffer_KeepTextBetweenStrings(buf, "<begin>", "<end>", 1);
206  if (rv<0) {
207  DBG_ERROR(GWEN_LOGDOMAIN, "Could not cut data out (%d)", rv);
208  GWEN_Buffer_free(buf);
209  return GWEN_ERROR_GENERIC;
210  }
211 
212  if (strcmp(GWEN_Buffer_GetStart(buf), testString2)!=0) {
213  DBG_ERROR(GWEN_LOGDOMAIN, "Unexpected string in buffer (%s)", GWEN_Buffer_GetStart(buf));
214  GWEN_Buffer_free(buf);
215  return GWEN_ERROR_GENERIC;
216  }
217 
218  if (GWEN_Buffer_GetUsedBytes(buf)!=strlen(testString2)) {
219  DBG_ERROR(GWEN_LOGDOMAIN, "Invalid buffer size (%d)", GWEN_Buffer_GetUsedBytes(buf));
220  }
221 
222  GWEN_Buffer_free(buf);
223 
224  return 0;
225 }
226 
227 
228 
230 {
231  GWEN_BUFFER *buf;
232  int rv;
233  static const char *testString1=
234  "0000000000"
235  "<begin>"
236  "1111111111"
237  "<end>"
238  "222222222";
239  static const char *testString2=
240  "1111111111";
241  static const char *testString3=
242  "3333333333";
243  static const char *testString4=
244  "1111111111"
245  "3333333333";
246 
247  buf=GWEN_Buffer_new(0, 16, 0, 1);
248  rv=GWEN_Buffer_AppendString(buf, testString1);
249  if (rv<0) {
250  DBG_ERROR(GWEN_LOGDOMAIN, "Could not append string");
251  GWEN_Buffer_free(buf);
252  return GWEN_ERROR_GENERIC;
253  }
254 
255  rv=GWEN_Buffer_KeepTextBetweenStrings(buf, "<begin>", "<end>", 1);
256  if (rv<0) {
257  DBG_ERROR(GWEN_LOGDOMAIN, "Could not cut data out (%d)", rv);
258  GWEN_Buffer_free(buf);
259  return GWEN_ERROR_GENERIC;
260  }
261 
262  if (strcmp(GWEN_Buffer_GetStart(buf), testString2)!=0) {
263  DBG_ERROR(GWEN_LOGDOMAIN, "Unexpected string in buffer (%s)", GWEN_Buffer_GetStart(buf));
264  GWEN_Buffer_free(buf);
265  return GWEN_ERROR_GENERIC;
266  }
267 
268  if (GWEN_Buffer_GetUsedBytes(buf)!=strlen(testString2)) {
269  DBG_ERROR(GWEN_LOGDOMAIN, "Invalid buffer size (%d)", GWEN_Buffer_GetUsedBytes(buf));
270  }
271 
272  rv=GWEN_Buffer_AppendString(buf, testString3);
273  if (rv<0) {
274  DBG_ERROR(GWEN_LOGDOMAIN, "Could not append 2nd string");
275  GWEN_Buffer_free(buf);
276  return GWEN_ERROR_GENERIC;
277  }
278 
279  if (strcmp(GWEN_Buffer_GetStart(buf), testString4)!=0) {
280  DBG_ERROR(GWEN_LOGDOMAIN, "Unexpected string in buffer (%s)", GWEN_Buffer_GetStart(buf));
281  GWEN_Buffer_free(buf);
282  return GWEN_ERROR_GENERIC;
283  }
284 
285  GWEN_Buffer_free(buf);
286 
287  return 0;
288 }
289 
290 
291 
292 
293 
294 
295 
296 #else
297 
299 {
300  DBG_ERROR(GWEN_LOGDOMAIN, "Gwenhywfar was compiled without test code enabled.");
301  return GWEN_ERROR_GENERIC;
302 }
303 
304 
305 #endif
306 
char * GWEN_Buffer_GetStart(const GWEN_BUFFER *bf)
Definition: buffer.c:235
int test5(int argc, char **argv)
int test3(int argc, char **argv)
Definition: libtest.m:204
uint32_t GWEN_Buffer_GetUsedBytes(const GWEN_BUFFER *bf)
Definition: buffer.c:277
int GWEN_Buffer_KeepTextBetweenStrings(GWEN_BUFFER *bf, const char *openingString, const char *closingString, int onlyBetween)
Definition: buffer.c:1127
#define NULL
Definition: binreloc.c:300
#define GWEN_LOGDOMAIN
Definition: logger.h:35
GWEN_BUFFER * GWEN_Buffer_new(char *buffer, uint32_t size, uint32_t used, int take)
Definition: buffer.c:42
int test2(int argc, char **argv)
GWEN_TEST_MODULE * GWEN_Test_Module_AddTest(GWEN_TEST_MODULE *st, const char *tName, GWEN_TEST_MODULE_TEST_FN fn, const char *tDescr)
Definition: testmodule.c:424
#define GWENHYWFAR_CB
Definition: gwenhywfarapi.h:89
struct GWEN_TEST_MODULE GWEN_TEST_MODULE
Definition: testmodule.h:65
int GWEN_Buffer_AppendArgs(GWEN_BUFFER *bf, const char *fmt,...)
Definition: buffer.c:1084
#define GWEN_ERROR_GENERIC
Definition: error.h:62
void GWEN_Buffer_free(GWEN_BUFFER *bf)
Definition: buffer.c:89
struct GWEN_BUFFER GWEN_BUFFER
A dynamically resizeable text buffer.
Definition: buffer.h:38
int test1()
Definition: libtest.m:63
#define DBG_ERROR(dbg_logger, format, args...)
Definition: debug.h:97
int test4(int argc, char **argv)
int GWEN_Buffer_AddTests(GWEN_UNUSED GWEN_TEST_MODULE *mod)
Definition: buffer-t.c:298
GWEN_TEST_MODULE * GWEN_Test_Module_AddModule(GWEN_TEST_MODULE *st, const char *tName, const char *tDescr)
Definition: testmodule.c:440
#define GWEN_UNUSED
int GWEN_Buffer_AppendString(GWEN_BUFFER *bf, const char *buffer)
Definition: buffer.c:989