Commit 9f22f06b

9f22f06bc9b9Merged

Richard Ramsden <[email protected]> · 6 months ago · 10 files · +10 added

Description

test(ffi): add five regression tests for the FFI surface

Each test covers one feature so a future regression points at the specific piece that broke. Tests use .rb.expected files (not the CRuby-comparison path) since CRuby has no ffi_func.

ffi_libc_libm_basic cos / sqrt / pow / strlen / getpid ffi_const ffi_const inlined as Module::NAME ffi_buffer_reader ffi_buffer + ffi_read_u32/i32 round-trips ffi_ptr_nil :ptr == nil reads NULL correctly (BSS buffer) ffi_void_return :void calls in expression position

Details

Commit
9f22f06bc9b9
Parent
e9eec312f2ab

test/ffi_buffer_reader.rbaddedView file

1+module Buf
2+ ffi_buffer :scratch, 16
3+ ffi_read_u32 :u32_at_0, 0
4+ ffi_read_u32 :u32_at_4, 4
5+ ffi_read_u32 :u32_at_8, 8
6+ ffi_read_i32 :i32_at_0, 0
7+end
8+
9+# `ffi_buffer` lives in BSS so it's zero-initialised at load. Reading
10+# the buffer back through every reader kind at every offset proves the
11+# storage + read-pointer arithmetic line up.
12+puts Buf.u32_at_0(Buf.scratch)
13+puts Buf.u32_at_4(Buf.scratch)
14+puts Buf.u32_at_8(Buf.scratch)
15+puts Buf.i32_at_0(Buf.scratch)
16+
17+# A second buffer is laid out next to the first; each gets its own
18+# slot, so reading from one doesn't accidentally see the other's bytes.
19+module Other
20+ ffi_buffer :scratch, 16
21+ ffi_read_u32 :u32, 0
22+end
23+puts Other.u32(Other.scratch)