ucx_buffere_getc should return an unsigned char (0-255) or EOF (-1)

Tue, 02 Jan 2018 17:00:21 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Tue, 02 Jan 2018 17:00:21 +0100
changeset 269
591473851c95
parent 268
00e88487a654
child 270
3d80d425543b

ucx_buffere_getc should return an unsigned char (0-255) or EOF (-1)

src/buffer.c file | annotate | diff | comparison | revisions
test/buffer_tests.c file | annotate | diff | comparison | revisions
--- a/src/buffer.c	Mon Nov 20 18:06:32 2017 +0100
+++ b/src/buffer.c	Tue Jan 02 17:00:21 2018 +0100
@@ -225,7 +225,7 @@
     if (ucx_buffer_eof(buffer)) {
         return EOF;
     } else {
-        int c = buffer->space[buffer->pos];
+        int c = ((unsigned char*)buffer->space)[buffer->pos];
         buffer->pos++;
         return c;
     }
--- a/test/buffer_tests.c	Mon Nov 20 18:06:32 2017 +0100
+++ b/test/buffer_tests.c	Tue Jan 02 17:00:21 2018 +0100
@@ -342,6 +342,10 @@
 
     UcxBuffer *b = ucx_buffer_new(buffer, 16, UCX_BUFFER_DEFAULT);
     b->size = b->capacity;
+    
+    unsigned char ubuffer[] = {127, 128, 129, 130};
+    UcxBuffer *ub = ucx_buffer_new(ubuffer, 4, UCX_BUFFER_DEFAULT);
+    ub->size = 4;
 
     UCX_TEST_BEGIN
 
@@ -360,10 +364,21 @@
     UCX_TEST_ASSERT(ucx_buffer_getc(b) == EOF,
         "out of bounds read does not return EOF");
     UCX_TEST_ASSERT(b->pos == 16, "wrong position after out of bounds read");
+    
+    int uc[5];
+    for(int i=0;i<5;i++) {
+        uc[i] = ucx_buffer_getc(ub);
+    }
+    UCX_TEST_ASSERT(uc[0] == 127, "wrong unsigned value(0)");
+    UCX_TEST_ASSERT(uc[1] == 128, "wrong unsigned value(0)");
+    UCX_TEST_ASSERT(uc[2] == 129, "wrong unsigned value(0)");
+    UCX_TEST_ASSERT(uc[3] == 130, "wrong unsigned value(0)");
+    UCX_TEST_ASSERT(uc[4] == EOF, "EOF not set after last ub read");
 
     UCX_TEST_END
 
     ucx_buffer_free(b);
+    ucx_buffer_free(ub);
     free(buffer);
 }
 

mercurial