1 | /* |
---|
2 | * Copyright (c) 2016 Chris Johns <chrisj@rtems.org>. All rights reserved. |
---|
3 | * |
---|
4 | * The license and distribution terms for this file may be |
---|
5 | * found in the file LICENSE in this distribution or at |
---|
6 | * http://www.rtems.org/license/LICENSE. |
---|
7 | */ |
---|
8 | |
---|
9 | #ifdef HAVE_CONFIG_H |
---|
10 | #include "config.h" |
---|
11 | #endif |
---|
12 | |
---|
13 | #include "tmacros.h" |
---|
14 | |
---|
15 | #include <fcntl.h> |
---|
16 | #include <sys/types.h> |
---|
17 | #include <unistd.h> |
---|
18 | #include <stdlib.h> |
---|
19 | #include <stdio.h> |
---|
20 | |
---|
21 | #include <rtems/rtl/rtl.h> |
---|
22 | #include <rtems/rtl/rtl-obj-cache.h> |
---|
23 | #include <rtems/rtl/rtl-trace.h> |
---|
24 | |
---|
25 | #include "dl-cache.h" |
---|
26 | |
---|
27 | #define CACHE_SIZE (2048) |
---|
28 | #define CACHE_SIZE_TOO_BIG (-1) |
---|
29 | #define CACHE_BUFFER_SIZE (CACHE_SIZE * 4) |
---|
30 | |
---|
31 | static uint8_t* contents; |
---|
32 | |
---|
33 | static const char *const filename = "/dl-test"; |
---|
34 | |
---|
35 | static void dl_cache_create_file(void) |
---|
36 | { |
---|
37 | uint16_t* p; |
---|
38 | int fd; |
---|
39 | int i; |
---|
40 | rtems_test_assert((contents = malloc(CACHE_BUFFER_SIZE)) != NULL); |
---|
41 | memset(contents, 0, CACHE_BUFFER_SIZE); |
---|
42 | p = (uint16_t*) contents; |
---|
43 | for (i = 0; i < (CACHE_BUFFER_SIZE / sizeof(uint16_t)); ++i) |
---|
44 | *p++ = i; |
---|
45 | rtems_test_assert((fd = open(filename, |
---|
46 | O_WRONLY | O_TRUNC | O_CREAT, |
---|
47 | S_IRUSR | S_IWUSR)) >= 0); |
---|
48 | rtems_test_assert(write(fd, contents, CACHE_BUFFER_SIZE) == CACHE_BUFFER_SIZE); |
---|
49 | rtems_test_assert(close(fd) >= 0); |
---|
50 | } |
---|
51 | |
---|
52 | static bool dl_cache_check(void* buffer, off_t offset, size_t length) |
---|
53 | { |
---|
54 | uint16_t* b; |
---|
55 | uint16_t* c; |
---|
56 | int i; |
---|
57 | b = buffer; |
---|
58 | c = (uint16_t*) (contents + offset); |
---|
59 | printf("cache: buffer: "); |
---|
60 | for (i = 0; i < 4; ++i) |
---|
61 | printf("%04x/%04x ", b[i], c[i]); |
---|
62 | printf("\n"); |
---|
63 | return memcmp(buffer, contents + offset, length) == 0; |
---|
64 | } |
---|
65 | |
---|
66 | static off_t dl_cache_buffer_offset(rtems_rtl_obj_cache_t* cache, void* buffer) |
---|
67 | { |
---|
68 | return (off_t) (((uint8_t*) buffer) - ((uint8_t*) cache->buffer)); |
---|
69 | } |
---|
70 | |
---|
71 | static void dl_init_rtl(void) |
---|
72 | { |
---|
73 | /* |
---|
74 | * Check the RTL object is created and can be locked and unlocked. |
---|
75 | */ |
---|
76 | rtems_test_assert(rtems_rtl_data () == NULL); |
---|
77 | rtems_test_assert(rtems_rtl_lock () != NULL); |
---|
78 | rtems_test_assert(rtems_rtl_unlock () == true); |
---|
79 | rtems_test_assert(rtems_rtl_data () != NULL); |
---|
80 | rtems_rtl_trace_set_mask(RTEMS_RTL_TRACE_ALL | RTEMS_RTL_TRACE_CACHE); |
---|
81 | } |
---|
82 | |
---|
83 | int dl_cache_test(void) |
---|
84 | { |
---|
85 | rtems_rtl_obj_cache_t cache; |
---|
86 | int fd; |
---|
87 | void* buffer; |
---|
88 | off_t offset_in; |
---|
89 | off_t offset; |
---|
90 | size_t length_in; |
---|
91 | size_t length; |
---|
92 | |
---|
93 | /* |
---|
94 | * Make sure the RTL can initialise. |
---|
95 | */ |
---|
96 | dl_init_rtl(); |
---|
97 | |
---|
98 | /* |
---|
99 | * Create the file to test the cache with. |
---|
100 | */ |
---|
101 | dl_cache_create_file(); |
---|
102 | |
---|
103 | /* |
---|
104 | * Check the too big error is handled. |
---|
105 | */ |
---|
106 | printf ("cache create with large size\n"); |
---|
107 | rtems_test_assert(rtems_rtl_obj_cache_open(&cache, |
---|
108 | CACHE_SIZE_TOO_BIG) == false); |
---|
109 | |
---|
110 | /* |
---|
111 | * Create a cache. |
---|
112 | */ |
---|
113 | printf ("cache create\n"); |
---|
114 | rtems_test_assert(rtems_rtl_obj_cache_open(&cache, |
---|
115 | CACHE_SIZE) == true); |
---|
116 | rtems_test_assert(cache.fd == -1); |
---|
117 | rtems_test_assert(cache.file_size == 0); |
---|
118 | rtems_test_assert(cache.size == CACHE_SIZE); |
---|
119 | rtems_test_assert(cache.buffer != NULL); |
---|
120 | |
---|
121 | /* |
---|
122 | * Open the file to use with the cache tests. |
---|
123 | */ |
---|
124 | printf ("cache file open\n"); |
---|
125 | rtems_test_assert((fd = open(filename, O_RDONLY)) >= 0); |
---|
126 | |
---|
127 | buffer = NULL; offset_in = 0; length_in = length = CACHE_SIZE / 2; |
---|
128 | printf("cache read: in: offset=%d length=%d\n", (int) offset_in, (int) length); |
---|
129 | rtems_test_assert(rtems_rtl_obj_cache_read(&cache, |
---|
130 | fd, |
---|
131 | offset_in, |
---|
132 | &buffer, |
---|
133 | &length) == true); |
---|
134 | offset = dl_cache_buffer_offset(&cache, buffer); |
---|
135 | printf("cache read: out: offset=%d length=%d\n", (int) offset, (int) length); |
---|
136 | rtems_test_assert(offset == offset_in); |
---|
137 | rtems_test_assert(length == length_in); |
---|
138 | rtems_test_assert(dl_cache_check(buffer, (int) offset_in, length) == true); |
---|
139 | |
---|
140 | buffer = NULL; offset_in = 0; length_in = length = CACHE_SIZE; |
---|
141 | printf("cache read: in: offset=%d length=%d\n", (int) offset_in, (int) length); |
---|
142 | rtems_test_assert(rtems_rtl_obj_cache_read(&cache, |
---|
143 | fd, |
---|
144 | offset_in, |
---|
145 | &buffer, |
---|
146 | &length) == true); |
---|
147 | offset = dl_cache_buffer_offset(&cache, buffer); |
---|
148 | printf("cache read: out: offset=%d length=%d\n", (int) offset, (int) length); |
---|
149 | rtems_test_assert(offset == offset_in); |
---|
150 | rtems_test_assert(length == length_in); |
---|
151 | rtems_test_assert(dl_cache_check(buffer, (int) offset_in, length) == true); |
---|
152 | |
---|
153 | buffer = NULL; offset_in = CACHE_SIZE / 2; length_in = length = CACHE_SIZE / 2; |
---|
154 | printf("cache read: in: offset=%d length=%d\n", (int) offset_in, (int) length); |
---|
155 | rtems_test_assert(rtems_rtl_obj_cache_read(&cache, |
---|
156 | fd, |
---|
157 | offset_in, |
---|
158 | &buffer, |
---|
159 | &length) == true); |
---|
160 | offset = dl_cache_buffer_offset(&cache, buffer); |
---|
161 | printf("cache read: out: offset=%d length=%d\n", (int) offset, (int) length); |
---|
162 | rtems_test_assert(offset == offset_in); |
---|
163 | rtems_test_assert(length == length_in); |
---|
164 | rtems_test_assert(dl_cache_check(buffer, (int) offset_in, length) == true); |
---|
165 | |
---|
166 | buffer = NULL; offset_in = CACHE_BUFFER_SIZE - CACHE_SIZE; length_in = length = CACHE_SIZE; |
---|
167 | printf("cache read: in: offset=%d length=%d\n", (int) offset_in, (int) length); |
---|
168 | rtems_test_assert(rtems_rtl_obj_cache_read(&cache, |
---|
169 | fd, |
---|
170 | offset_in, |
---|
171 | &buffer, |
---|
172 | &length) == true); |
---|
173 | offset = dl_cache_buffer_offset(&cache, buffer); |
---|
174 | printf("cache read: out: offset=%d length=%d\n", (int) offset, (int) length); |
---|
175 | rtems_test_assert(offset == 0); |
---|
176 | rtems_test_assert(length == length_in); |
---|
177 | rtems_test_assert(dl_cache_check(buffer, (int) offset_in, length) == true); |
---|
178 | |
---|
179 | buffer = NULL; offset_in = CACHE_BUFFER_SIZE - (CACHE_SIZE / 2); length_in = length = CACHE_SIZE / 2; |
---|
180 | printf("cache read: in: offset=%d length=%d\n", (int) offset_in, (int) length); |
---|
181 | rtems_test_assert(rtems_rtl_obj_cache_read(&cache, |
---|
182 | fd, |
---|
183 | offset_in, |
---|
184 | &buffer, |
---|
185 | &length) == true); |
---|
186 | offset = dl_cache_buffer_offset(&cache, buffer); |
---|
187 | printf("cache read: out: offset=%d length=%d\n", (int) offset, (int) length); |
---|
188 | rtems_test_assert(offset == (CACHE_SIZE / 2)); |
---|
189 | rtems_test_assert(length == length_in); |
---|
190 | rtems_test_assert(dl_cache_check(buffer, (int) offset_in, length) == true); |
---|
191 | |
---|
192 | buffer = NULL; offset_in = 0; length_in = length = CACHE_SIZE / 4; |
---|
193 | printf("cache read: in: offset=%d length=%d\n", (int) offset_in, (int) length); |
---|
194 | rtems_test_assert(rtems_rtl_obj_cache_read(&cache, |
---|
195 | fd, |
---|
196 | offset_in, |
---|
197 | &buffer, |
---|
198 | &length) == true); |
---|
199 | offset = dl_cache_buffer_offset(&cache, buffer); |
---|
200 | printf("cache read: out: offset=%d length=%d\n", (int) offset, (int) length); |
---|
201 | rtems_test_assert(offset == offset_in); |
---|
202 | rtems_test_assert(length == length_in); |
---|
203 | rtems_test_assert(dl_cache_check(buffer, (int) offset_in, length) == true); |
---|
204 | |
---|
205 | buffer = NULL; offset_in = 0; length_in = length = CACHE_SIZE / 8; |
---|
206 | printf("cache read: in: offset=%d length=%d\n", (int) offset_in, (int) length); |
---|
207 | rtems_test_assert(rtems_rtl_obj_cache_read(&cache, |
---|
208 | fd, |
---|
209 | offset_in, |
---|
210 | &buffer, |
---|
211 | &length) == true); |
---|
212 | offset = dl_cache_buffer_offset(&cache, buffer); |
---|
213 | printf("cache read: out: offset=%d length=%d\n", (int) offset, (int) length); |
---|
214 | rtems_test_assert(offset == offset_in); |
---|
215 | rtems_test_assert(length == length_in); |
---|
216 | rtems_test_assert(dl_cache_check(buffer, (int) offset_in, length) == true); |
---|
217 | |
---|
218 | buffer = NULL; offset_in = 0; length_in = length = CACHE_SIZE; |
---|
219 | printf("cache read: in: offset=%d length=%d\n", (int) offset_in, (int) length); |
---|
220 | rtems_test_assert(rtems_rtl_obj_cache_read(&cache, |
---|
221 | fd, |
---|
222 | offset_in, |
---|
223 | &buffer, |
---|
224 | &length) == true); |
---|
225 | offset = dl_cache_buffer_offset(&cache, buffer); |
---|
226 | printf("cache read: out: offset=%d length=%d\n", (int) offset, (int) length); |
---|
227 | rtems_test_assert(offset == offset_in); |
---|
228 | rtems_test_assert(length == length_in); |
---|
229 | rtems_test_assert(dl_cache_check(buffer, (int) offset_in, length) == true); |
---|
230 | |
---|
231 | buffer = NULL; offset_in = CACHE_BUFFER_SIZE - 40; length_in = length = 16; |
---|
232 | printf("cache read: in: offset=%d length=%d\n", (int) offset_in, (int) length); |
---|
233 | rtems_test_assert(rtems_rtl_obj_cache_read(&cache, |
---|
234 | fd, |
---|
235 | offset_in, |
---|
236 | &buffer, |
---|
237 | &length) == true); |
---|
238 | offset = dl_cache_buffer_offset(&cache, buffer); |
---|
239 | printf("cache read: out: offset=%d length=%d\n", (int) offset, (int) length); |
---|
240 | rtems_test_assert(length == length_in); |
---|
241 | rtems_test_assert(dl_cache_check(buffer, (int) offset_in, length) == true); |
---|
242 | |
---|
243 | buffer = NULL; offset_in = CACHE_BUFFER_SIZE - 40; length_in = length = 40; |
---|
244 | printf("cache read: in: offset=%d length=%d\n", (int) offset_in, (int) length); |
---|
245 | rtems_test_assert(rtems_rtl_obj_cache_read(&cache, |
---|
246 | fd, |
---|
247 | offset_in, |
---|
248 | &buffer, |
---|
249 | &length) == true); |
---|
250 | offset = dl_cache_buffer_offset(&cache, buffer); |
---|
251 | printf("cache read: out: offset=%d length=%d\n", (int) offset, (int) length); |
---|
252 | rtems_test_assert(length == length_in); |
---|
253 | rtems_test_assert(dl_cache_check(buffer, (int) offset_in, length) == true); |
---|
254 | |
---|
255 | buffer = NULL; offset_in = CACHE_BUFFER_SIZE - 40; length_in = length = 80; |
---|
256 | printf("cache read: in: offset=%d length=%d\n", (int) offset_in, (int) length); |
---|
257 | rtems_test_assert(rtems_rtl_obj_cache_read(&cache, |
---|
258 | fd, |
---|
259 | offset_in, |
---|
260 | &buffer, |
---|
261 | &length) == true); |
---|
262 | offset = dl_cache_buffer_offset(&cache, buffer); |
---|
263 | printf("cache read: out: offset=%d length=%d\n", (int) offset, (int) length); |
---|
264 | rtems_test_assert(length == 40); |
---|
265 | rtems_test_assert(dl_cache_check(buffer, (int) offset_in, length) == true); |
---|
266 | |
---|
267 | buffer = NULL; offset_in = CACHE_BUFFER_SIZE - CACHE_SIZE + 80; |
---|
268 | length_in = length = 20; |
---|
269 | printf("cache read: in: offset=%d length=%d\n", (int) offset_in, (int) length); |
---|
270 | rtems_test_assert(rtems_rtl_obj_cache_read(&cache, |
---|
271 | fd, |
---|
272 | offset_in, |
---|
273 | &buffer, |
---|
274 | &length) == true); |
---|
275 | offset = dl_cache_buffer_offset(&cache, buffer); |
---|
276 | printf("cache read: out: offset=%d length=%d\n", (int) offset, (int) length); |
---|
277 | rtems_test_assert(length == length_in); |
---|
278 | rtems_test_assert(dl_cache_check(buffer, (int) offset_in, length) == true); |
---|
279 | |
---|
280 | buffer = NULL; offset_in = CACHE_BUFFER_SIZE - 40; |
---|
281 | length_in = length = 40; |
---|
282 | printf("cache read: in: offset=%d length=%d\n", (int) offset_in, (int) length); |
---|
283 | rtems_test_assert(rtems_rtl_obj_cache_read(&cache, |
---|
284 | fd, |
---|
285 | offset_in, |
---|
286 | &buffer, |
---|
287 | &length) == true); |
---|
288 | offset = dl_cache_buffer_offset(&cache, buffer); |
---|
289 | printf("cache read: out: offset=%d length=%d\n", (int) offset, (int) length); |
---|
290 | rtems_test_assert(length == length_in); |
---|
291 | rtems_test_assert(dl_cache_check(buffer, (int) offset_in, length) == true); |
---|
292 | |
---|
293 | rtems_rtl_obj_cache_close(&cache); |
---|
294 | |
---|
295 | return 0; |
---|
296 | } |
---|