Browse code

webkernel_test: sbuf_memory test

Dario Rodriguez authored on 20/06/2014 18:57:58
Showing 1 changed files
... ...
@@ -28,12 +28,14 @@ typedef enum test_action {
28 28
 char *test1(test_action action);
29 29
 char *socklib_connect(test_action action);
30 30
 char *socklib_sselect(test_action action);
31
+char *sbuf_memory(test_action action);
31 32
 
32 33
 struct {
33 34
         char *(*test)(/* test_action action */);
34 35
 } tests[]={{test1},
35 36
            {socklib_connect},
36 37
            {socklib_sselect},
38
+           {sbuf_memory},
37 39
           };
38 40
 
39 41
 int
... ...
@@ -273,3 +275,69 @@ socklib_sselect(test_action action)
273 275
         return(STRING_OK);
274 276
 }
275 277
 
278
+char *
279
+sbuf_memory(test_action action)
280
+{
281
+        int bufsize=2048;
282
+        const unsigned char data[]={0,2,4,1,2,4,1,76,91,147,135,253,121,56,5,9};
283
+        unsigned char moredata[16];
284
+        unsigned char *ptr;
285
+        const char *line,lines[]={"one\nand two\nthree"};
286
+        int i;
287
+        sbuf *buf;
288
+        if(action==test_name)
289
+                return("sbuf_memory");
290
+        else if(action==test_description)
291
+                return("memory operations with sbuf");
292
+        if((buf=sbuf_init(bufsize))==NULL)
293
+                return(STRING_FAIL ": couldn't alloc new sbuf");
294
+        if(sbuf_add(buf,(char *)data,sizeof(data))!=sizeof(data)) {
295
+                sbuf_free(buf),buf=NULL;
296
+                return(STRING_FAIL ": couldn't push data");
297
+        }
298
+        for(i=0;i<sizeof(moredata);i++)
299
+                moredata[i]=i;
300
+        if(sbuf_add(buf,(char *)moredata,sizeof(moredata))!=sizeof(moredata)) {
301
+                sbuf_free(buf),buf=NULL;
302
+                return(STRING_FAIL ": (2) couldn't push data");
303
+        }
304
+        if(sbuf_count(buf)!=(sizeof(data)+sizeof(moredata))) {
305
+                sbuf_free(buf),buf=NULL;
306
+                return(STRING_FAIL ": bad count");
307
+        }
308
+        for(i=0;i<sizeof(data);i++) {
309
+                if((ptr=(unsigned char *)sbuf_getbytes(buf,1))==NULL || *ptr!=data[i]) {
310
+                        sbuf_free(buf),buf=NULL;
311
+                        return(STRING_FAIL ": bad retrieval");
312
+                }
313
+        }
314
+        sbuf_discard(buf);
315
+        if((ptr=(unsigned char *)sbuf_getbytes(buf,sizeof(moredata)))==NULL || memcmp(ptr,moredata,sizeof(moredata))!=0) {
316
+                sbuf_free(buf),buf=NULL;
317
+                return(STRING_FAIL ": (2) bad retrieval");
318
+        }
319
+        if(sbuf_getbytes(buf,1)!=NULL) {
320
+                sbuf_free(buf),buf=NULL;
321
+                return(STRING_FAIL ": (3) bad retrieval");
322
+        }
323
+        sbuf_discard(buf);
324
+        if(sbuf_addstr(buf,lines)!=(sizeof(lines)-1)) {
325
+                sbuf_free(buf),buf=NULL;
326
+                return(STRING_FAIL ": (3) couldn't push data");
327
+        }
328
+        if((line=sbuf_getline(buf))==NULL || strcmp(line,"one")!=0) {
329
+                sbuf_free(buf),buf=NULL;
330
+                return(STRING_FAIL ": bad line retrieval");
331
+        }
332
+        if((line=sbuf_getline(buf))==NULL || strcmp(line,"and two")!=0) {
333
+                sbuf_free(buf),buf=NULL;
334
+                return(STRING_FAIL ": (2) bad line retrieval");
335
+        }
336
+        if(sbuf_getline(buf)!=NULL) {
337
+                sbuf_free(buf),buf=NULL;
338
+                return(STRING_FAIL ": (3) bad line retrieval");
339
+        }
340
+        sbuf_free(buf),buf=NULL;
341
+        return(STRING_OK);
342
+}
343
+