authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-02-07 16:07:12+01:00
committergravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-02-07 16:07:12+01:00
log7a58ec81ecfbf64baabdf883a458ca7296a34fc7
treea45974fb822ef1fc890e6649a881a18c3b6892e5
parentbe02616c869c5fe089bc9a80ea44817df2f5c2ba

std: Add a few tests for mmap/munmap


1 files changed, 98 insertions(+), 0 deletions(-)

lib/std/os/test.zig+98
...@@ -256,3 +256,101 @@ test "memfd_create" {...@@ -256,3 +256,101 @@ test "memfd_create" {
256 expect(bytes_read == 4);256 expect(bytes_read == 4);
257 expect(mem.eql(u8, buf[0..4], "test"));257 expect(mem.eql(u8, buf[0..4], "test"));
258}258}
259
260test "mmap" {
261 if (builtin.os == .windows)
262 return error.SkipZigTest;
263
264 // Simple mmap() call with non page-aligned size
265 {
266 const data = try os.mmap(
267 null,
268 1234,
269 os.PROT_READ | os.PROT_WRITE,
270 os.MAP_ANONYMOUS | os.MAP_PRIVATE,
271 -1,
272 0,
273 );
274 defer os.munmap(data);
275
276 testing.expectEqual(@as(usize, 1234), data.len);
277
278 // By definition the data returned by mmap is zero-filled
279 std.mem.set(u8, data[0 .. data.len - 1], 0x55);
280 testing.expect(mem.indexOfScalar(u8, data, 0).? == 1234 - 1);
281 }
282
283 const test_out_file = "os_tmp_test";
284 // Must be a multiple of 4096 so that the test works with mmap2
285 const alloc_size = 8 * 4096;
286
287 // Create a file used for testing mmap() calls with a file descriptor
288 {
289 const file = try fs.cwd().createFile(test_out_file, .{});
290 defer file.close();
291
292 var out_stream = file.outStream();
293 const stream = &out_stream.stream;
294
295 var i: u32 = 0;
296 while (i < alloc_size / @sizeOf(u32)) : (i += 1) {
297 try stream.writeIntNative(u32, i);
298 }
299 }
300
301 // Map the whole file
302 {
303 const file = try fs.cwd().createFile(test_out_file, .{
304 .read = true,
305 .truncate = false,
306 });
307 defer file.close();
308
309 const data = try os.mmap(
310 null,
311 alloc_size,
312 os.PROT_READ,
313 os.MAP_PRIVATE,
314 file.handle,
315 0,
316 );
317 defer os.munmap(data);
318
319 var mem_stream = io.SliceInStream.init(data);
320 const stream = &mem_stream.stream;
321
322 var i: u32 = 0;
323 while (i < alloc_size / @sizeOf(u32)) : (i += 1) {
324 testing.expectEqual(i, try stream.readIntNative(u32));
325 }
326 }
327
328 // Map the upper half of the file
329 {
330 const file = try fs.cwd().createFile(test_out_file, .{
331 .read = true,
332 .truncate = false,
333 });
334 defer file.close();
335
336 const data = try os.mmap(
337 null,
338 alloc_size,
339 os.PROT_READ,
340 os.MAP_PRIVATE,
341 file.handle,
342 alloc_size / 2,
343 );
344 defer os.munmap(data);
345
346 var mem_stream = io.SliceInStream.init(data);
347 const stream = &mem_stream.stream;
348
349 var i: u32 = alloc_size / 2 / @sizeOf(u32);
350 while (i < alloc_size / @sizeOf(u32)) : (i += 1) {
351 testing.expectEqual(i, try stream.readIntNative(u32));
352 }
353 }
354
355 try fs.cwd().deleteFile(test_out_file);
356}