gwenhywfar  5.4.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 
45 
46 
47 
48 
49 /* ------------------------------------------------------------------------------------------------
50  * implementations
51  * ------------------------------------------------------------------------------------------------
52  */
53 
54 
56 {
57  GWEN_TEST_MODULE *newMod;
58 
59  newMod=GWEN_Test_Module_AddModule(mod, "GWEN_Buffer", NULL);
60 
61  GWEN_Test_Module_AddTest(newMod, "append args (simple)", test1, NULL);
62  GWEN_Test_Module_AddTest(newMod, "append args (long string)", test2, NULL);
63 
64  return 0;
65 }
66 
67 
68 
70 {
71  GWEN_BUFFER *buf;
72  int rv;
73 
74  buf=GWEN_Buffer_new(0, 16, 0, 1);
75  rv=GWEN_Buffer_AppendArgs(buf, "%s-%d", "TESTSTRING", 1234);
76  if (rv<0) {
77  DBG_ERROR(GWEN_LOGDOMAIN, "Could not append args");
78  GWEN_Buffer_free(buf);
79  return GWEN_ERROR_GENERIC;
80  }
81 
82  if (strcmp(GWEN_Buffer_GetStart(buf), "TESTSTRING-1234")!=0) {
83  DBG_ERROR(GWEN_LOGDOMAIN, "Unexpected string in buffer (%s)", GWEN_Buffer_GetStart(buf));
84  GWEN_Buffer_free(buf);
85  return GWEN_ERROR_GENERIC;
86  }
87  GWEN_Buffer_free(buf);
88 
89  return 0;
90 }
91 
92 
93 
95 {
96  GWEN_BUFFER *buf;
97  int rv;
98  static const char *testString=
99  "0123456789"
100  "0123456789"
101  "0123456789"
102  "0123456789"
103  "0123456789"
104  "0123456789"
105  "0123456789"
106  "0123456789"
107  "0123456789"
108  "0123456789"
109  "0123456789"
110  "0123456789"
111  "0123456789"
112  "0123456789";
113 
114  buf=GWEN_Buffer_new(0, 16, 0, 1);
115  rv=GWEN_Buffer_AppendArgs(buf, "%s", testString);
116  if (rv<0) {
117  DBG_ERROR(GWEN_LOGDOMAIN, "Could not append args");
118  GWEN_Buffer_free(buf);
119  return GWEN_ERROR_GENERIC;
120  }
121 
122  if (strcmp(GWEN_Buffer_GetStart(buf), testString)!=0) {
123  DBG_ERROR(GWEN_LOGDOMAIN, "Unexpected string in buffer (%s)", GWEN_Buffer_GetStart(buf));
124  GWEN_Buffer_free(buf);
125  return GWEN_ERROR_GENERIC;
126  }
127  GWEN_Buffer_free(buf);
128 
129  return 0;
130 }
131 
132 
133 
134 
135 
136 
137 
138 #else
139 
141 {
142  DBG_ERROR(GWEN_LOGDOMAIN, "Gwenhywfar was compiled without test code enabled.");
143  return GWEN_ERROR_GENERIC;
144 }
145 
146 
147 #endif
148 
char * GWEN_Buffer_GetStart(const GWEN_BUFFER *bf)
Definition: buffer.c:235
#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
int GWEN_Buffer_AppendArgs(GWEN_BUFFER *bf, const char *fmt,...)
Definition: buffer.c:1084
#define GWENHYWFAR_CB
Definition: gwenhywfarapi.h:89
struct GWEN_TEST_MODULE GWEN_TEST_MODULE
Definition: testmodule.h:65
#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
GWEN_TEST_MODULE * GWEN_Test_Module_AddModule(GWEN_TEST_MODULE *st, const char *tName, const char *tDescr)
Definition: testmodule.c:440
int GWEN_Buffer_AddTests(GWEN_TEST_MODULE *mod)
Definition: buffer-t.c:140
#define GWEN_UNUSED