Sat, 21 Dec 2013 12:31:31 +0100
Added allocator to mempool struct + fixed suncc.mk
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
1 | /* |
103
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
3 | * |
103
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
4 | * Copyright 2013 Olaf Wintermann. All rights reserved. |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
5 | * |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
6 | * Redistribution and use in source and binary forms, with or without |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
7 | * modification, are permitted provided that the following conditions are met: |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
8 | * |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
9 | * 1. Redistributions of source code must retain the above copyright |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
10 | * notice, this list of conditions and the following disclaimer. |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
11 | * |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
12 | * 2. Redistributions in binary form must reproduce the above copyright |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
13 | * notice, this list of conditions and the following disclaimer in the |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
14 | * documentation and/or other materials provided with the distribution. |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
15 | * |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
95
diff
changeset
|
26 | * POSSIBILITY OF SUCH DAMAGE. |
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
27 | */ |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
28 | |
60
abae4669fba7
renamed UcxMemstream to UcxBuffer
Mike Becker <universe@uap-core.de>
parents:
58
diff
changeset
|
29 | #include "buffer_tests.h" |
140
15f871f50bfd
completed documentation + changed API for buffer/stream generic copy functions
Mike Becker <universe@uap-core.de>
parents:
134
diff
changeset
|
30 | #include "ucx/utils.h" |
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
31 | |
134
4d320dc3a7af
documented test.h and removed duplicated implement/declare macros for UCX_TEST
Mike Becker <universe@uap-core.de>
parents:
103
diff
changeset
|
32 | UCX_TEST(test_ucx_buffer_seektell) { |
76 | 33 | UcxBuffer *b = ucx_buffer_new(NULL, 32, UCX_BUFFER_DEFAULT); |
34 | b->size = 16; // less than capacity | |
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
35 | int r; |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
36 | |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
37 | UCX_TEST_BEGIN |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
38 | |
60
abae4669fba7
renamed UcxMemstream to UcxBuffer
Mike Becker <universe@uap-core.de>
parents:
58
diff
changeset
|
39 | r = ucx_buffer_seek(b, 5, SEEK_SET); |
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
40 | UCX_TEST_ASSERT(r == 0, "seek SET+5 failed"); |
63
1d3500806565
discarded fascism from buffer
Mike Becker <universe@uap-core.de>
parents:
62
diff
changeset
|
41 | UCX_TEST_ASSERT(b->pos == 5, "seek SET+5 set wrong position"); |
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
42 | |
60
abae4669fba7
renamed UcxMemstream to UcxBuffer
Mike Becker <universe@uap-core.de>
parents:
58
diff
changeset
|
43 | r = ucx_buffer_seek(b, 20, SEEK_SET); |
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
44 | UCX_TEST_ASSERT(r != 0, "seek beyond bounds shall fail"); |
63
1d3500806565
discarded fascism from buffer
Mike Becker <universe@uap-core.de>
parents:
62
diff
changeset
|
45 | UCX_TEST_ASSERT(b->pos == 5, |
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
46 | "failed seek shall leave pos unchanged"); |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
47 | |
60
abae4669fba7
renamed UcxMemstream to UcxBuffer
Mike Becker <universe@uap-core.de>
parents:
58
diff
changeset
|
48 | r = ucx_buffer_seek(b, 5, SEEK_CUR); |
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
49 | UCX_TEST_ASSERT(r == 0, "seek CUR+5 failed"); |
63
1d3500806565
discarded fascism from buffer
Mike Becker <universe@uap-core.de>
parents:
62
diff
changeset
|
50 | UCX_TEST_ASSERT(b->pos == 10, "seek CUR+5 set wrong position"); |
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
51 | |
60
abae4669fba7
renamed UcxMemstream to UcxBuffer
Mike Becker <universe@uap-core.de>
parents:
58
diff
changeset
|
52 | r = ucx_buffer_seek(b, 10, SEEK_CUR); |
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
53 | UCX_TEST_ASSERT(r != 0, "seek CUR beyond bounds shall fail"); |
63
1d3500806565
discarded fascism from buffer
Mike Becker <universe@uap-core.de>
parents:
62
diff
changeset
|
54 | UCX_TEST_ASSERT(b->pos == 10, |
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
55 | "failed seek shall leave pos unchanged"); |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
56 | |
60
abae4669fba7
renamed UcxMemstream to UcxBuffer
Mike Becker <universe@uap-core.de>
parents:
58
diff
changeset
|
57 | r = ucx_buffer_seek(b, -5, SEEK_END); |
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
58 | UCX_TEST_ASSERT(r == 0, "seek END-5 failed"); |
76 | 59 | UCX_TEST_ASSERT(b->pos == 11, "seek END-5 set wrong position"); |
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
60 | |
76 | 61 | r = ucx_buffer_seek(b, -20, SEEK_END); |
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
62 | UCX_TEST_ASSERT(r != 0, "seek END beyond bounds shall fail"); |
76 | 63 | UCX_TEST_ASSERT(b->pos == 11, |
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
64 | "failed seek shall leave pos unchanged"); |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
65 | |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
66 | UCX_TEST_END |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
67 | |
60
abae4669fba7
renamed UcxMemstream to UcxBuffer
Mike Becker <universe@uap-core.de>
parents:
58
diff
changeset
|
68 | ucx_buffer_free(b); |
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
69 | } |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
70 | |
134
4d320dc3a7af
documented test.h and removed duplicated implement/declare macros for UCX_TEST
Mike Becker <universe@uap-core.de>
parents:
103
diff
changeset
|
71 | UCX_TEST(test_ucx_buffer_putc) { |
69
fb59270b1de3
made the code work with VC++ compiler (use make CONF=windows)
Mike Becker <universe@uap-core.de>
parents:
64
diff
changeset
|
72 | char *buffer = (char*) malloc(16); |
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
73 | memset(buffer, 32, 16); |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
74 | |
61
fb07a0ab9a17
added flag field to buffer
Mike Becker <universe@uap-core.de>
parents:
60
diff
changeset
|
75 | UcxBuffer *b = ucx_buffer_new(buffer, 16, UCX_BUFFER_DEFAULT); |
76 | 76 | b->size = b->capacity; |
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
77 | |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
78 | UCX_TEST_BEGIN |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
79 | |
76 | 80 | ucx_buffer_putc(b, '0'); |
81 | ucx_buffer_putc(b, '0'); | |
82 | ucx_buffer_putc(b, '0'); | |
83 | ||
63
1d3500806565
discarded fascism from buffer
Mike Becker <universe@uap-core.de>
parents:
62
diff
changeset
|
84 | UCX_TEST_ASSERT(b->pos == 3, "pos wrong after first 3 puts"); |
60
abae4669fba7
renamed UcxMemstream to UcxBuffer
Mike Becker <universe@uap-core.de>
parents:
58
diff
changeset
|
85 | ucx_buffer_seek(b, 10, SEEK_CUR); |
76 | 86 | |
87 | ucx_buffer_putc(b, '0'); | |
88 | ucx_buffer_putc(b, '0'); | |
89 | ucx_buffer_putc(b, '0'); | |
90 | ||
63
1d3500806565
discarded fascism from buffer
Mike Becker <universe@uap-core.de>
parents:
62
diff
changeset
|
91 | UCX_TEST_ASSERT(b->pos == 16, "pos wrong after last 3 puts"); |
60
abae4669fba7
renamed UcxMemstream to UcxBuffer
Mike Becker <universe@uap-core.de>
parents:
58
diff
changeset
|
92 | UCX_TEST_ASSERT(ucx_buffer_eof(b), "eof not set"); |
62 | 93 | UCX_TEST_ASSERT(ucx_buffer_putc(b, 48) == EOF, |
94 | "put shall return EOF when buffer is full"); | |
76 | 95 | |
96 | ucx_buffer_seek(b, 3, SEEK_SET); | |
97 | ucx_buffer_putc(b, 'a'); | |
98 | ucx_buffer_putc(b, 'b'); | |
99 | ucx_buffer_putc(b, 'c'); | |
100 | ||
101 | UCX_TEST_ASSERT(b->size == 16, "wrong size after seek and puts"); | |
102 | UCX_TEST_ASSERT(memcmp(buffer, "000abc 000", 16) == 0, | |
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
103 | "buffer contains incorrect content"); |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
104 | |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
105 | UCX_TEST_END |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
106 | |
60
abae4669fba7
renamed UcxMemstream to UcxBuffer
Mike Becker <universe@uap-core.de>
parents:
58
diff
changeset
|
107 | ucx_buffer_free(b); |
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
108 | free(buffer); |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
109 | } |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
110 | |
134
4d320dc3a7af
documented test.h and removed duplicated implement/declare macros for UCX_TEST
Mike Becker <universe@uap-core.de>
parents:
103
diff
changeset
|
111 | UCX_TEST(test_ucx_buffer_putc_ax) { |
76 | 112 | UcxBuffer *b = ucx_buffer_new(NULL, 2, UCX_BUFFER_AUTOEXTEND); |
113 | ||
114 | UCX_TEST_BEGIN | |
115 | ||
116 | ucx_buffer_putc(b, '0'); | |
117 | ucx_buffer_putc(b, '1'); | |
118 | ||
119 | UCX_TEST_ASSERT(b->pos == 2, "pos wrong after first 2 puts"); | |
120 | UCX_TEST_ASSERT(b->capacity == 2, "buffer erroneously extended"); | |
121 | ||
122 | ucx_buffer_putc(b, 'a'); | |
123 | ||
124 | UCX_TEST_ASSERT(b->pos == 3, "pos wrong after 1 put"); | |
125 | UCX_TEST_ASSERT(b->capacity == 4, "buffer not properly extended"); | |
126 | UCX_TEST_ASSERT(b->size == 3, "wrong buffer size"); | |
127 | ||
128 | UCX_TEST_ASSERT(b->space[2] == 'a', "wrong content"); | |
129 | ||
130 | UCX_TEST_END | |
131 | ||
147
1aa598f36872
added printf for UcxBuffer + fixed memory leaks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
140
diff
changeset
|
132 | ucx_buffer_free(b); |
76 | 133 | } |
134 | ||
134
4d320dc3a7af
documented test.h and removed duplicated implement/declare macros for UCX_TEST
Mike Becker <universe@uap-core.de>
parents:
103
diff
changeset
|
135 | UCX_TEST(test_ucx_buffer_getc) { |
69
fb59270b1de3
made the code work with VC++ compiler (use make CONF=windows)
Mike Becker <universe@uap-core.de>
parents:
64
diff
changeset
|
136 | char *buffer = (char*) malloc(16); |
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
137 | memset(buffer, 32, 8); |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
138 | for (int i = 8; i < 16 ; i++) { |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
139 | buffer[i] = 40+i; |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
140 | } |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
141 | |
61
fb07a0ab9a17
added flag field to buffer
Mike Becker <universe@uap-core.de>
parents:
60
diff
changeset
|
142 | UcxBuffer *b = ucx_buffer_new(buffer, 16, UCX_BUFFER_DEFAULT); |
76 | 143 | b->size = b->capacity; |
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
144 | |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
145 | UCX_TEST_BEGIN |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
146 | |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
147 | char rb[16]; |
95 | 148 | for (size_t i = 0 ; i < 16 ; i++) { |
63
1d3500806565
discarded fascism from buffer
Mike Becker <universe@uap-core.de>
parents:
62
diff
changeset
|
149 | UCX_TEST_ASSERT(b->pos == i, "pos wrong during read loop"); |
60
abae4669fba7
renamed UcxMemstream to UcxBuffer
Mike Becker <universe@uap-core.de>
parents:
58
diff
changeset
|
150 | UCX_TEST_ASSERT(!ucx_buffer_eof(b), |
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
151 | "EOF shall not be set during read loop"); |
60
abae4669fba7
renamed UcxMemstream to UcxBuffer
Mike Becker <universe@uap-core.de>
parents:
58
diff
changeset
|
152 | rb[i] = ucx_buffer_getc(b); |
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
153 | } |
63
1d3500806565
discarded fascism from buffer
Mike Becker <universe@uap-core.de>
parents:
62
diff
changeset
|
154 | UCX_TEST_ASSERT(b->pos == 16, "pos wrong after read loop"); |
60
abae4669fba7
renamed UcxMemstream to UcxBuffer
Mike Becker <universe@uap-core.de>
parents:
58
diff
changeset
|
155 | UCX_TEST_ASSERT(ucx_buffer_eof(b), "EOF not set"); |
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
156 | UCX_TEST_ASSERT(memcmp(rb, " 01234567", 16) == 0, |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
157 | "read data incorrect"); |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
158 | |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
159 | UCX_TEST_END |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
160 | |
60
abae4669fba7
renamed UcxMemstream to UcxBuffer
Mike Becker <universe@uap-core.de>
parents:
58
diff
changeset
|
161 | ucx_buffer_free(b); |
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
162 | free(buffer); |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
163 | } |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
164 | |
134
4d320dc3a7af
documented test.h and removed duplicated implement/declare macros for UCX_TEST
Mike Becker <universe@uap-core.de>
parents:
103
diff
changeset
|
165 | UCX_TEST(test_ucx_buffer_write) { |
69
fb59270b1de3
made the code work with VC++ compiler (use make CONF=windows)
Mike Becker <universe@uap-core.de>
parents:
64
diff
changeset
|
166 | char *buffer = (char*) malloc(16); |
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
167 | memset(buffer, 32, 8); |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
168 | for (int i = 8; i < 16 ; i++) { |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
169 | buffer[i] = 40+i; |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
170 | } |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
171 | |
61
fb07a0ab9a17
added flag field to buffer
Mike Becker <universe@uap-core.de>
parents:
60
diff
changeset
|
172 | UcxBuffer *b = ucx_buffer_new(buffer, 16, UCX_BUFFER_DEFAULT); |
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
173 | int r; |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
174 | |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
175 | UCX_TEST_BEGIN |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
176 | |
69
fb59270b1de3
made the code work with VC++ compiler (use make CONF=windows)
Mike Becker <universe@uap-core.de>
parents:
64
diff
changeset
|
177 | const char* teststring = "this is way too much"; |
fb59270b1de3
made the code work with VC++ compiler (use make CONF=windows)
Mike Becker <universe@uap-core.de>
parents:
64
diff
changeset
|
178 | r = ucx_buffer_write((void*)teststring, 1, 20, b); |
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
179 | UCX_TEST_ASSERT(r == 16, "string not correctly trimed"); |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
180 | UCX_TEST_ASSERT(memcmp(buffer, teststring, 16) == 0, |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
181 | "buffer data incorrect"); |
60
abae4669fba7
renamed UcxMemstream to UcxBuffer
Mike Becker <universe@uap-core.de>
parents:
58
diff
changeset
|
182 | UCX_TEST_ASSERT(ucx_buffer_eof(b), "eof shall be set"); |
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
183 | |
60
abae4669fba7
renamed UcxMemstream to UcxBuffer
Mike Becker <universe@uap-core.de>
parents:
58
diff
changeset
|
184 | ucx_buffer_seek(b, 8, SEEK_SET); |
71
303dabadff1c
made the code work with g++ without errors (but warnings)
Mike Becker <universe@uap-core.de>
parents:
69
diff
changeset
|
185 | r = ucx_buffer_write((void*)"not", 1, 3, b); |
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
186 | UCX_TEST_ASSERT(r == 3, "three bytes should be replace"); |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
187 | UCX_TEST_ASSERT(memcmp(buffer, "this is not too much", 16) == 0, |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
188 | "modified buffer is incorrect"); |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
189 | |
69
fb59270b1de3
made the code work with VC++ compiler (use make CONF=windows)
Mike Becker <universe@uap-core.de>
parents:
64
diff
changeset
|
190 | const char* threebytestring = " t h r e e "; |
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
191 | memset(buffer, 49, 16); |
60
abae4669fba7
renamed UcxMemstream to UcxBuffer
Mike Becker <universe@uap-core.de>
parents:
58
diff
changeset
|
192 | ucx_buffer_seek(b, 0, SEEK_SET); |
69
fb59270b1de3
made the code work with VC++ compiler (use make CONF=windows)
Mike Becker <universe@uap-core.de>
parents:
64
diff
changeset
|
193 | r = ucx_buffer_write((void*)threebytestring, 3, 6, b); |
76 | 194 | UCX_TEST_ASSERT(r == 5, "three byte string not correctly trimed"); |
63
1d3500806565
discarded fascism from buffer
Mike Becker <universe@uap-core.de>
parents:
62
diff
changeset
|
195 | UCX_TEST_ASSERT(b->pos == 15, |
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
196 | "position after write of three byte string incorrect"); |
60
abae4669fba7
renamed UcxMemstream to UcxBuffer
Mike Becker <universe@uap-core.de>
parents:
58
diff
changeset
|
197 | UCX_TEST_ASSERT(!ucx_buffer_eof(b), "eof shall not be set"); |
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
198 | UCX_TEST_ASSERT(memcmp(buffer, " t h r e e1", 16) == 0, |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
199 | "bufer is incorrect after three byte string has been written"); |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
200 | |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
201 | UCX_TEST_END |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
202 | |
60
abae4669fba7
renamed UcxMemstream to UcxBuffer
Mike Becker <universe@uap-core.de>
parents:
58
diff
changeset
|
203 | ucx_buffer_free(b); |
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
204 | free(buffer); |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
205 | } |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
206 | |
134
4d320dc3a7af
documented test.h and removed duplicated implement/declare macros for UCX_TEST
Mike Becker <universe@uap-core.de>
parents:
103
diff
changeset
|
207 | UCX_TEST(test_ucx_buffer_write_ax) { |
76 | 208 | char *buffer = (char*) malloc(16); |
64
16590c9c497c
added autoextend feature to buffer
Mike Becker <universe@uap-core.de>
parents:
63
diff
changeset
|
209 | |
16590c9c497c
added autoextend feature to buffer
Mike Becker <universe@uap-core.de>
parents:
63
diff
changeset
|
210 | UcxBuffer *b = ucx_buffer_new(buffer, 16, |
16590c9c497c
added autoextend feature to buffer
Mike Becker <universe@uap-core.de>
parents:
63
diff
changeset
|
211 | UCX_BUFFER_AUTOEXTEND | UCX_BUFFER_AUTOFREE); |
16590c9c497c
added autoextend feature to buffer
Mike Becker <universe@uap-core.de>
parents:
63
diff
changeset
|
212 | int r; |
16590c9c497c
added autoextend feature to buffer
Mike Becker <universe@uap-core.de>
parents:
63
diff
changeset
|
213 | |
16590c9c497c
added autoextend feature to buffer
Mike Becker <universe@uap-core.de>
parents:
63
diff
changeset
|
214 | UCX_TEST_BEGIN |
16590c9c497c
added autoextend feature to buffer
Mike Becker <universe@uap-core.de>
parents:
63
diff
changeset
|
215 | |
69
fb59270b1de3
made the code work with VC++ compiler (use make CONF=windows)
Mike Becker <universe@uap-core.de>
parents:
64
diff
changeset
|
216 | const char* teststring = "this is way too much"; |
fb59270b1de3
made the code work with VC++ compiler (use make CONF=windows)
Mike Becker <universe@uap-core.de>
parents:
64
diff
changeset
|
217 | r = ucx_buffer_write((void*)teststring, 1, 20, b); |
fb59270b1de3
made the code work with VC++ compiler (use make CONF=windows)
Mike Becker <universe@uap-core.de>
parents:
64
diff
changeset
|
218 | buffer = (char*) b->space; /*autoextend enabled, we MUST retrieve pointer*/ |
64
16590c9c497c
added autoextend feature to buffer
Mike Becker <universe@uap-core.de>
parents:
63
diff
changeset
|
219 | UCX_TEST_ASSERT(r == 20, "not all characters written"); |
76 | 220 | UCX_TEST_ASSERT(b->capacity == 32, "buffer not properly extended"); |
64
16590c9c497c
added autoextend feature to buffer
Mike Becker <universe@uap-core.de>
parents:
63
diff
changeset
|
221 | UCX_TEST_ASSERT(b->pos == 20, "position incorrect"); |
16590c9c497c
added autoextend feature to buffer
Mike Becker <universe@uap-core.de>
parents:
63
diff
changeset
|
222 | UCX_TEST_ASSERT(memcmp(buffer, |
16590c9c497c
added autoextend feature to buffer
Mike Becker <universe@uap-core.de>
parents:
63
diff
changeset
|
223 | "this is way too much\0\0\0\0\0\0\0\0\0\0\0\0", 32) == 0, |
16590c9c497c
added autoextend feature to buffer
Mike Becker <universe@uap-core.de>
parents:
63
diff
changeset
|
224 | "incorrect buffer content"); |
16590c9c497c
added autoextend feature to buffer
Mike Becker <universe@uap-core.de>
parents:
63
diff
changeset
|
225 | |
16590c9c497c
added autoextend feature to buffer
Mike Becker <universe@uap-core.de>
parents:
63
diff
changeset
|
226 | UCX_TEST_END |
16590c9c497c
added autoextend feature to buffer
Mike Becker <universe@uap-core.de>
parents:
63
diff
changeset
|
227 | |
16590c9c497c
added autoextend feature to buffer
Mike Becker <universe@uap-core.de>
parents:
63
diff
changeset
|
228 | ucx_buffer_free(b); |
16590c9c497c
added autoextend feature to buffer
Mike Becker <universe@uap-core.de>
parents:
63
diff
changeset
|
229 | } |
16590c9c497c
added autoextend feature to buffer
Mike Becker <universe@uap-core.de>
parents:
63
diff
changeset
|
230 | |
134
4d320dc3a7af
documented test.h and removed duplicated implement/declare macros for UCX_TEST
Mike Becker <universe@uap-core.de>
parents:
103
diff
changeset
|
231 | UCX_TEST(test_ucx_buffer_read) { |
76 | 232 | UcxBuffer *b = ucx_buffer_new(NULL, 8, UCX_BUFFER_AUTOFREE); |
233 | ||
234 | char buf[32]; | |
235 | memset(buf, 'X', 32); | |
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
236 | int r; |
76 | 237 | |
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
238 | UCX_TEST_BEGIN |
76 | 239 | |
240 | ucx_buffer_write("01234567", 1, 8, b); | |
241 | UCX_TEST_ASSERT(b->pos == 8, "buffer not correctly filled"); | |
242 | b->pos = 0; | |
243 | ||
244 | r = ucx_buffer_read(buf, 1, 2, b); | |
245 | UCX_TEST_ASSERT(r == 2, "wrong number of bytes read"); | |
246 | UCX_TEST_ASSERT(buf[0] == '0' && buf[1] == '1' && buf[2] == 'X', | |
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
247 | "buffer incorrect after first read"); |
76 | 248 | |
249 | r = ucx_buffer_read(buf + 2, 1, 8, b); | |
250 | UCX_TEST_ASSERT(r == 6, "wrong number of bytes read(2)"); | |
251 | UCX_TEST_ASSERT(memcmp(buf, "01234567XX", 10) == 0, | |
252 | "buffer incorrect after second read"); | |
253 | ||
254 | memset(buf, 'X', 32); | |
60
abae4669fba7
renamed UcxMemstream to UcxBuffer
Mike Becker <universe@uap-core.de>
parents:
58
diff
changeset
|
255 | ucx_buffer_seek(b, 0, SEEK_SET); |
76 | 256 | r = ucx_buffer_read(buf, 3, 3, b); |
257 | ||
258 | UCX_TEST_ASSERT(r == 2, "wrong number of blocks read"); | |
259 | UCX_TEST_ASSERT(memcmp(buf, "012345XX", 8) == 0, | |
260 | "buffer incorrect after three byte read"); | |
261 | ||
262 | ||
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
263 | UCX_TEST_END |
76 | 264 | |
147
1aa598f36872
added printf for UcxBuffer + fixed memory leaks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
140
diff
changeset
|
265 | ucx_buffer_free(b); |
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
266 | } |
62 | 267 | |
134
4d320dc3a7af
documented test.h and removed duplicated implement/declare macros for UCX_TEST
Mike Becker <universe@uap-core.de>
parents:
103
diff
changeset
|
268 | UCX_TEST(test_ucx_buffer_extract) { |
69
fb59270b1de3
made the code work with VC++ compiler (use make CONF=windows)
Mike Becker <universe@uap-core.de>
parents:
64
diff
changeset
|
269 | char *buffer = (char*) malloc(16); |
62 | 270 | strcpy(buffer, "this is a test!"); |
271 | ||
76 | 272 | UcxBuffer *src = ucx_buffer_new(buffer, 16, UCX_BUFFER_AUTOFREE); |
273 | src->size = 15; | |
274 | UcxBuffer *dst = ucx_buffer_extract(src, 5, 5, UCX_BUFFER_DEFAULT); | |
62 | 275 | |
276 | UCX_TEST_BEGIN | |
76 | 277 | UCX_TEST_ASSERT(dst != NULL, "ucx_buffer_extract returned NULL"); |
278 | ||
63
1d3500806565
discarded fascism from buffer
Mike Becker <universe@uap-core.de>
parents:
62
diff
changeset
|
279 | UCX_TEST_ASSERT((dst->flags & UCX_BUFFER_AUTOFREE) == UCX_BUFFER_AUTOFREE, |
62 | 280 | "autofree flag shall be enforced"); |
63
1d3500806565
discarded fascism from buffer
Mike Becker <universe@uap-core.de>
parents:
62
diff
changeset
|
281 | UCX_TEST_ASSERT(dst->size == 5, "wrong size for new buffer"); |
62 | 282 | char rb[5]; |
283 | ucx_buffer_read(rb, 1, 5, dst); | |
284 | UCX_TEST_ASSERT(memcmp(rb, "is a ", 5) == 0, | |
285 | "new buffer has incorrect content"); | |
286 | ||
287 | UCX_TEST_ASSERT(ucx_buffer_extract(dst, 3, 3, UCX_BUFFER_DEFAULT) == NULL, | |
288 | "extract shall fail on invalid bounds"); | |
289 | ||
290 | UCX_TEST_END | |
291 | ||
292 | ucx_buffer_free(dst); | |
293 | ucx_buffer_free(src); | |
294 | } | |
76 | 295 | |
140
15f871f50bfd
completed documentation + changed API for buffer/stream generic copy functions
Mike Becker <universe@uap-core.de>
parents:
134
diff
changeset
|
296 | UCX_TEST(test_ucx_stream_copy) { |
76 | 297 | UcxBuffer *b1 = ucx_buffer_new(NULL, 64, UCX_BUFFER_DEFAULT); |
298 | UcxBuffer *b2 = ucx_buffer_new(NULL, 2, UCX_BUFFER_AUTOEXTEND); | |
299 | ||
300 | UCX_TEST_BEGIN | |
301 | ||
302 | ucx_buffer_write("01234567", 1, 8, b1); | |
303 | ucx_buffer_write("abcdefgh", 1, 8, b1); | |
304 | UCX_TEST_ASSERT(b1->size == 16, "failed to fill buffer b1"); | |
305 | ucx_buffer_seek(b1, 0, SEEK_SET); | |
306 | ||
140
15f871f50bfd
completed documentation + changed API for buffer/stream generic copy functions
Mike Becker <universe@uap-core.de>
parents:
134
diff
changeset
|
307 | size_t ncp = ucx_stream_hcopy(b1, b2, ucx_buffer_read, ucx_buffer_write); |
76 | 308 | UCX_TEST_ASSERT(ncp == 16, "wrong number of copied bytes"); |
309 | UCX_TEST_ASSERT(b2->size == 16, "b2 has wrong size"); | |
310 | UCX_TEST_ASSERT(memcmp(b1->space, b2->space, 16) == 0, | |
311 | "b1 and b2 have not the same content"); | |
312 | ||
313 | memset(b2->space, 0, b2->capacity); | |
314 | b2->pos = 0; | |
315 | b2->size = 0; | |
316 | ucx_buffer_seek(b1, 0, SEEK_SET); | |
317 | ||
318 | FILE *file = tmpfile(); | |
319 | UCX_TEST_ASSERT(file, "test file cannot be opened, test aborted"); | |
320 | ||
140
15f871f50bfd
completed documentation + changed API for buffer/stream generic copy functions
Mike Becker <universe@uap-core.de>
parents:
134
diff
changeset
|
321 | ncp = ucx_stream_hcopy(b1, file, ucx_buffer_read, fwrite); |
76 | 322 | UCX_TEST_ASSERT(ncp == 16, "copied wrong number of bytes to file"); |
323 | ||
324 | fseek(file, 0, SEEK_SET); | |
325 | ||
140
15f871f50bfd
completed documentation + changed API for buffer/stream generic copy functions
Mike Becker <universe@uap-core.de>
parents:
134
diff
changeset
|
326 | ncp = ucx_stream_hcopy(file, b2, fread, ucx_buffer_write); |
76 | 327 | UCX_TEST_ASSERT(ncp == 16, "copied wrong number of bytes from file"); |
328 | ||
329 | UCX_TEST_ASSERT(memcmp(b1->space, b2->space, 16) == 0, | |
330 | "b1 and b2 content mismatch"); | |
331 | ||
332 | fclose(file); | |
140
15f871f50bfd
completed documentation + changed API for buffer/stream generic copy functions
Mike Becker <universe@uap-core.de>
parents:
134
diff
changeset
|
333 | |
15f871f50bfd
completed documentation + changed API for buffer/stream generic copy functions
Mike Becker <universe@uap-core.de>
parents:
134
diff
changeset
|
334 | ucx_buffer_clear(b1); |
15f871f50bfd
completed documentation + changed API for buffer/stream generic copy functions
Mike Becker <universe@uap-core.de>
parents:
134
diff
changeset
|
335 | ucx_buffer_seek(b2, 0, SEEK_SET); |
15f871f50bfd
completed documentation + changed API for buffer/stream generic copy functions
Mike Becker <universe@uap-core.de>
parents:
134
diff
changeset
|
336 | ncp = ucx_stream_ncopy(b2, b1, ucx_buffer_read, ucx_buffer_write, 8); |
15f871f50bfd
completed documentation + changed API for buffer/stream generic copy functions
Mike Becker <universe@uap-core.de>
parents:
134
diff
changeset
|
337 | UCX_TEST_ASSERT(ncp == 8, "copied wrong number of bytes with ncopy"); |
15f871f50bfd
completed documentation + changed API for buffer/stream generic copy functions
Mike Becker <universe@uap-core.de>
parents:
134
diff
changeset
|
338 | UCX_TEST_ASSERT(memcmp(b1->space, "01234567\0\0\0\0\0\0\0\0", 16) == 0, |
15f871f50bfd
completed documentation + changed API for buffer/stream generic copy functions
Mike Becker <universe@uap-core.de>
parents:
134
diff
changeset
|
339 | "content wrong after ncopy"); |
76 | 340 | |
341 | UCX_TEST_END | |
342 | ||
343 | ucx_buffer_free(b1); | |
344 | ucx_buffer_free(b2); | |
345 | } |