3 */ |
3 */ |
4 |
4 |
5 #include "buffer_tests.h" |
5 #include "buffer_tests.h" |
6 |
6 |
7 UCX_TEST_IMPLEMENT(test_ucx_buffer_seektell) { |
7 UCX_TEST_IMPLEMENT(test_ucx_buffer_seektell) { |
8 char *buffer = malloc(16); |
8 char *buffer = (char*) malloc(16); |
9 memset(buffer, 32, 7); |
9 memset(buffer, 32, 7); |
10 buffer[7] = 0; |
10 buffer[7] = 0; |
11 |
11 |
12 UcxBuffer *b = ucx_buffer_new(buffer, 16, UCX_BUFFER_DEFAULT); |
12 UcxBuffer *b = ucx_buffer_new(buffer, 16, UCX_BUFFER_DEFAULT); |
13 int r; |
13 int r; |
46 ucx_buffer_free(b); |
46 ucx_buffer_free(b); |
47 free(buffer); |
47 free(buffer); |
48 } |
48 } |
49 |
49 |
50 UCX_TEST_IMPLEMENT(test_ucx_buffer_putc) { |
50 UCX_TEST_IMPLEMENT(test_ucx_buffer_putc) { |
51 char *buffer = malloc(16); |
51 char *buffer = (char*) malloc(16); |
52 memset(buffer, 32, 16); |
52 memset(buffer, 32, 16); |
53 |
53 |
54 UcxBuffer *b = ucx_buffer_new(buffer, 16, UCX_BUFFER_DEFAULT); |
54 UcxBuffer *b = ucx_buffer_new(buffer, 16, UCX_BUFFER_DEFAULT); |
55 int r; |
55 int r; |
56 |
56 |
72 ucx_buffer_free(b); |
72 ucx_buffer_free(b); |
73 free(buffer); |
73 free(buffer); |
74 } |
74 } |
75 |
75 |
76 UCX_TEST_IMPLEMENT(test_ucx_buffer_getc) { |
76 UCX_TEST_IMPLEMENT(test_ucx_buffer_getc) { |
77 char *buffer = malloc(16); |
77 char *buffer = (char*) malloc(16); |
78 memset(buffer, 32, 8); |
78 memset(buffer, 32, 8); |
79 for (int i = 8; i < 16 ; i++) { |
79 for (int i = 8; i < 16 ; i++) { |
80 buffer[i] = 40+i; |
80 buffer[i] = 40+i; |
81 } |
81 } |
82 |
82 |
102 ucx_buffer_free(b); |
102 ucx_buffer_free(b); |
103 free(buffer); |
103 free(buffer); |
104 } |
104 } |
105 |
105 |
106 UCX_TEST_IMPLEMENT(test_ucx_buffer_write) { |
106 UCX_TEST_IMPLEMENT(test_ucx_buffer_write) { |
107 char *buffer = malloc(16); |
107 char *buffer = (char*) malloc(16); |
108 memset(buffer, 32, 8); |
108 memset(buffer, 32, 8); |
109 for (int i = 8; i < 16 ; i++) { |
109 for (int i = 8; i < 16 ; i++) { |
110 buffer[i] = 40+i; |
110 buffer[i] = 40+i; |
111 } |
111 } |
112 |
112 |
113 UcxBuffer *b = ucx_buffer_new(buffer, 16, UCX_BUFFER_DEFAULT); |
113 UcxBuffer *b = ucx_buffer_new(buffer, 16, UCX_BUFFER_DEFAULT); |
114 int r; |
114 int r; |
115 |
115 |
116 UCX_TEST_BEGIN |
116 UCX_TEST_BEGIN |
117 |
117 |
118 char* teststring = "this is way too much"; |
118 const char* teststring = "this is way too much"; |
119 r = ucx_buffer_write(teststring, 1, 20, b); |
119 r = ucx_buffer_write((void*)teststring, 1, 20, b); |
120 UCX_TEST_ASSERT(r == 16, "string not correctly trimed"); |
120 UCX_TEST_ASSERT(r == 16, "string not correctly trimed"); |
121 UCX_TEST_ASSERT(memcmp(buffer, teststring, 16) == 0, |
121 UCX_TEST_ASSERT(memcmp(buffer, teststring, 16) == 0, |
122 "buffer data incorrect"); |
122 "buffer data incorrect"); |
123 UCX_TEST_ASSERT(ucx_buffer_eof(b), "eof shall be set"); |
123 UCX_TEST_ASSERT(ucx_buffer_eof(b), "eof shall be set"); |
124 |
124 |
126 r = ucx_buffer_write("not", 1, 3, b); |
126 r = ucx_buffer_write("not", 1, 3, b); |
127 UCX_TEST_ASSERT(r == 3, "three bytes should be replace"); |
127 UCX_TEST_ASSERT(r == 3, "three bytes should be replace"); |
128 UCX_TEST_ASSERT(memcmp(buffer, "this is not too much", 16) == 0, |
128 UCX_TEST_ASSERT(memcmp(buffer, "this is not too much", 16) == 0, |
129 "modified buffer is incorrect"); |
129 "modified buffer is incorrect"); |
130 |
130 |
131 char* threebytestring = " t h r e e "; |
131 const char* threebytestring = " t h r e e "; |
132 memset(buffer, 49, 16); |
132 memset(buffer, 49, 16); |
133 ucx_buffer_seek(b, 0, SEEK_SET); |
133 ucx_buffer_seek(b, 0, SEEK_SET); |
134 r = ucx_buffer_write(threebytestring, 3, 6, b); |
134 r = ucx_buffer_write((void*)threebytestring, 3, 6, b); |
135 UCX_TEST_ASSERT(r == 15, "three byte string not correctly trimed"); |
135 UCX_TEST_ASSERT(r == 15, "three byte string not correctly trimed"); |
136 UCX_TEST_ASSERT(b->pos == 15, |
136 UCX_TEST_ASSERT(b->pos == 15, |
137 "position after write of three byte string incorrect"); |
137 "position after write of three byte string incorrect"); |
138 UCX_TEST_ASSERT(!ucx_buffer_eof(b), "eof shall not be set"); |
138 UCX_TEST_ASSERT(!ucx_buffer_eof(b), "eof shall not be set"); |
139 UCX_TEST_ASSERT(memcmp(buffer, " t h r e e1", 16) == 0, |
139 UCX_TEST_ASSERT(memcmp(buffer, " t h r e e1", 16) == 0, |
144 ucx_buffer_free(b); |
144 ucx_buffer_free(b); |
145 free(buffer); |
145 free(buffer); |
146 } |
146 } |
147 |
147 |
148 UCX_TEST_IMPLEMENT(test_ucx_buffer_write_ax) { |
148 UCX_TEST_IMPLEMENT(test_ucx_buffer_write_ax) { |
149 char *buffer = malloc(4); |
149 char *buffer = (char*) malloc(4); |
150 |
150 |
151 UcxBuffer *b = ucx_buffer_new(buffer, 16, |
151 UcxBuffer *b = ucx_buffer_new(buffer, 16, |
152 UCX_BUFFER_AUTOEXTEND | UCX_BUFFER_AUTOFREE); |
152 UCX_BUFFER_AUTOEXTEND | UCX_BUFFER_AUTOFREE); |
153 int r; |
153 int r; |
154 |
154 |
155 UCX_TEST_BEGIN |
155 UCX_TEST_BEGIN |
156 |
156 |
157 char* teststring = "this is way too much"; |
157 const char* teststring = "this is way too much"; |
158 r = ucx_buffer_write(teststring, 1, 20, b); |
158 r = ucx_buffer_write((void*)teststring, 1, 20, b); |
159 buffer = b->space; /* autoextend is enabled, so we MUST retrieve pointer */ |
159 buffer = (char*) b->space; /*autoextend enabled, we MUST retrieve pointer*/ |
160 UCX_TEST_ASSERT(r == 20, "not all characters written"); |
160 UCX_TEST_ASSERT(r == 20, "not all characters written"); |
161 UCX_TEST_ASSERT(b->size == 32, "buffer not properly extended"); |
161 UCX_TEST_ASSERT(b->size == 32, "buffer not properly extended"); |
162 UCX_TEST_ASSERT(b->pos == 20, "position incorrect"); |
162 UCX_TEST_ASSERT(b->pos == 20, "position incorrect"); |
163 UCX_TEST_ASSERT(memcmp(buffer, |
163 UCX_TEST_ASSERT(memcmp(buffer, |
164 "this is way too much\0\0\0\0\0\0\0\0\0\0\0\0", 32) == 0, |
164 "this is way too much\0\0\0\0\0\0\0\0\0\0\0\0", 32) == 0, |
209 ucx_buffer_free(b); |
209 ucx_buffer_free(b); |
210 free(buffer); |
210 free(buffer); |
211 } |
211 } |
212 |
212 |
213 UCX_TEST_IMPLEMENT(test_ucx_buffer_extract) { |
213 UCX_TEST_IMPLEMENT(test_ucx_buffer_extract) { |
214 char *buffer = malloc(16); |
214 char *buffer = (char*) malloc(16); |
215 strcpy(buffer, "this is a test!"); |
215 strcpy(buffer, "this is a test!"); |
216 |
216 |
217 UcxBuffer *src = ucx_buffer_new(buffer, 16, UCX_BUFFER_AUTOFREE), |
217 UcxBuffer *src = ucx_buffer_new(buffer, 16, UCX_BUFFER_AUTOFREE), |
218 *dst = ucx_buffer_extract(src, 5, 5, UCX_BUFFER_DEFAULT); |
218 *dst = ucx_buffer_extract(src, 5, 5, UCX_BUFFER_DEFAULT); |
219 |
219 |