1 /* |
|
2 * |
|
3 */ |
|
4 |
|
5 #include "memstream_tests.h" |
|
6 |
|
7 UCX_TEST_IMPLEMENT(test_ucx_memseektell) { |
|
8 char *buffer = malloc(16); |
|
9 memset(buffer, 32, 7); |
|
10 buffer[7] = 0; |
|
11 |
|
12 UcxMemstream *m = ucx_memopen(buffer, 16); |
|
13 int r; |
|
14 |
|
15 UCX_TEST_BEGIN |
|
16 |
|
17 r = ucx_memseek(m, 5, SEEK_SET); |
|
18 UCX_TEST_ASSERT(r == 0, "seek SET+5 failed"); |
|
19 UCX_TEST_ASSERT(ucx_memtell(m) == 5, "seek SET+5 set wrong position"); |
|
20 |
|
21 r = ucx_memseek(m, 20, SEEK_SET); |
|
22 UCX_TEST_ASSERT(r != 0, "seek beyond bounds shall fail"); |
|
23 UCX_TEST_ASSERT(ucx_memtell(m) == 5, |
|
24 "failed seek shall leave pos unchanged"); |
|
25 |
|
26 r = ucx_memseek(m, 5, SEEK_CUR); |
|
27 UCX_TEST_ASSERT(r == 0, "seek CUR+5 failed"); |
|
28 UCX_TEST_ASSERT(ucx_memtell(m) == 10, "seek CUR+5 set wrong position"); |
|
29 |
|
30 r = ucx_memseek(m, 10, SEEK_CUR); |
|
31 UCX_TEST_ASSERT(r != 0, "seek CUR beyond bounds shall fail"); |
|
32 UCX_TEST_ASSERT(ucx_memtell(m) == 10, |
|
33 "failed seek shall leave pos unchanged"); |
|
34 |
|
35 r = ucx_memseek(m, -5, SEEK_END); |
|
36 UCX_TEST_ASSERT(r == 0, "seek END-5 failed"); |
|
37 UCX_TEST_ASSERT(ucx_memtell(m) == 2, "seek END-5 set wrong position"); |
|
38 |
|
39 r = ucx_memseek(m, -10, SEEK_END); |
|
40 UCX_TEST_ASSERT(r != 0, "seek END beyond bounds shall fail"); |
|
41 UCX_TEST_ASSERT(ucx_memtell(m) == 2, |
|
42 "failed seek shall leave pos unchanged"); |
|
43 |
|
44 UCX_TEST_END |
|
45 |
|
46 ucx_memclose(m); |
|
47 free(buffer); |
|
48 } |
|
49 |
|
50 UCX_TEST_IMPLEMENT(test_ucx_memputc) { |
|
51 char *buffer = malloc(16); |
|
52 memset(buffer, 32, 16); |
|
53 |
|
54 UcxMemstream *m = ucx_memopen(buffer, 16); |
|
55 int r; |
|
56 |
|
57 UCX_TEST_BEGIN |
|
58 |
|
59 ucx_memputc(m, 48); ucx_memputc(m, 48); ucx_memputc(m, 48); |
|
60 UCX_TEST_ASSERT(ucx_memtell(m) == 3, "pos wrong after first 3 puts"); |
|
61 ucx_memseek(m, 10, SEEK_CUR); |
|
62 ucx_memputc(m, 48); ucx_memputc(m, 48); ucx_memputc(m, 48); |
|
63 UCX_TEST_ASSERT(ucx_memtell(m) == 16, "pos wrong after last 3 puts"); |
|
64 UCX_TEST_ASSERT(ucx_memeof(m), "eof not set"); |
|
65 UCX_TEST_ASSERT(ucx_memputc(m, 48) == EOF, "put shall return EOF on memof"); |
|
66 UCX_TEST_ASSERT(memcmp(buffer, "000 000", 16) == 0, |
|
67 "buffer contains incorrect content"); |
|
68 |
|
69 UCX_TEST_END |
|
70 |
|
71 ucx_memclose(m); |
|
72 free(buffer); |
|
73 } |
|
74 |
|
75 UCX_TEST_IMPLEMENT(test_ucx_memgetc) { |
|
76 char *buffer = malloc(16); |
|
77 memset(buffer, 32, 8); |
|
78 for (int i = 8; i < 16 ; i++) { |
|
79 buffer[i] = 40+i; |
|
80 } |
|
81 |
|
82 UcxMemstream *m = ucx_memopen(buffer, 16); |
|
83 int r; |
|
84 |
|
85 UCX_TEST_BEGIN |
|
86 |
|
87 char rb[16]; |
|
88 for (int i = 0 ; i < 16 ; i++) { |
|
89 UCX_TEST_ASSERT(ucx_memtell(m) == i, "pos wrong during read loop"); |
|
90 UCX_TEST_ASSERT(!ucx_memeof(m), |
|
91 "EOF shall not be set during read loop"); |
|
92 rb[i] = ucx_memgetc(m); |
|
93 } |
|
94 UCX_TEST_ASSERT(ucx_memtell(m) == 16, "pos wrong after read loop"); |
|
95 UCX_TEST_ASSERT(ucx_memeof(m), "EOF not set"); |
|
96 UCX_TEST_ASSERT(memcmp(rb, " 01234567", 16) == 0, |
|
97 "read data incorrect"); |
|
98 |
|
99 UCX_TEST_END |
|
100 |
|
101 ucx_memclose(m); |
|
102 free(buffer); |
|
103 } |
|
104 |
|
105 UCX_TEST_IMPLEMENT(test_ucx_memwrite) { |
|
106 char *buffer = malloc(16); |
|
107 memset(buffer, 32, 8); |
|
108 for (int i = 8; i < 16 ; i++) { |
|
109 buffer[i] = 40+i; |
|
110 } |
|
111 |
|
112 UcxMemstream *m = ucx_memopen(buffer, 16); |
|
113 int r; |
|
114 |
|
115 UCX_TEST_BEGIN |
|
116 |
|
117 char* teststring = "this is way too much"; |
|
118 r = ucx_memwrite(teststring, 1, 20, m); |
|
119 UCX_TEST_ASSERT(r == 16, "string not correctly trimed"); |
|
120 UCX_TEST_ASSERT(memcmp(buffer, teststring, 16) == 0, |
|
121 "buffer data incorrect"); |
|
122 UCX_TEST_ASSERT(ucx_memeof(m), "eof shall be set"); |
|
123 |
|
124 ucx_memseek(m, 8, SEEK_SET); |
|
125 r = ucx_memwrite("not", 1, 3, m); |
|
126 UCX_TEST_ASSERT(r == 3, "three bytes should be replace"); |
|
127 UCX_TEST_ASSERT(memcmp(buffer, "this is not too much", 16) == 0, |
|
128 "modified buffer is incorrect"); |
|
129 |
|
130 char* threebytestring = " t h r e e "; |
|
131 memset(buffer, 49, 16); |
|
132 ucx_memseek(m, 0, SEEK_SET); |
|
133 r = ucx_memwrite(threebytestring, 3, 6, m); |
|
134 UCX_TEST_ASSERT(r == 15, "three byte string not correctly trimed"); |
|
135 UCX_TEST_ASSERT(ucx_memtell(m) == 15, |
|
136 "position after write of three byte string incorrect"); |
|
137 UCX_TEST_ASSERT(!ucx_memeof(m), "eof shall not be set"); |
|
138 UCX_TEST_ASSERT(memcmp(buffer, " t h r e e1", 16) == 0, |
|
139 "bufer is incorrect after three byte string has been written"); |
|
140 |
|
141 UCX_TEST_END |
|
142 |
|
143 ucx_memclose(m); |
|
144 free(buffer); |
|
145 } |
|
146 |
|
147 UCX_TEST_IMPLEMENT(test_ucx_memread) { |
|
148 char *buffer = malloc(16); |
|
149 memset(buffer, 56, 8); |
|
150 for (int i = 8; i < 16 ; i++) { |
|
151 buffer[i] = 40+i; |
|
152 } |
|
153 |
|
154 UcxMemstream *m = ucx_memopen(buffer, 16); |
|
155 int r; |
|
156 |
|
157 UCX_TEST_BEGIN |
|
158 |
|
159 char rb[16]; |
|
160 memset(rb, 32, 16); |
|
161 |
|
162 ucx_memseek(m, 8, SEEK_SET); |
|
163 r = ucx_memread(rb, 1, 16, m); |
|
164 UCX_TEST_ASSERT(r == 8, "read did not stop at buffer end"); |
|
165 UCX_TEST_ASSERT(memcmp(rb, "01234567 ", 16) == 0, |
|
166 "buffer incorrect after first read"); |
|
167 UCX_TEST_ASSERT(ucx_memeof(m), "eof shall be set"); |
|
168 |
|
169 ucx_memseek(m, 0, SEEK_SET); |
|
170 r = ucx_memread(rb+8, 1, 8, m); |
|
171 UCX_TEST_ASSERT(r == 8, "read did not read the specified amount of bytes"); |
|
172 UCX_TEST_ASSERT(memcmp(rb, "0123456788888888", 16) == 0, |
|
173 "buffer incorrect after second read"); |
|
174 |
|
175 ucx_memseek(m, 0, SEEK_SET); |
|
176 r = ucx_memread(rb, 3, 6, m); |
|
177 UCX_TEST_ASSERT(r == 15, |
|
178 "three byte read did not read the desired amount of bytes"); |
|
179 UCX_TEST_ASSERT(memcmp(rb, "8888888801234568", 16) == 0, |
|
180 "buffer incorrect after three byte read"); |
|
181 |
|
182 UCX_TEST_END |
|
183 |
|
184 ucx_memclose(m); |
|
185 free(buffer); |
|
186 } |
|