diff options
Diffstat (limited to 'nix-test/src')
-rw-r--r-- | nix-test/src/lib.rs | 33 | ||||
-rw-r--r-- | nix-test/src/sizes.c | 23 |
2 files changed, 48 insertions, 8 deletions
diff --git a/nix-test/src/lib.rs b/nix-test/src/lib.rs index 6cd3a1f4..26d7cd4d 100644 --- a/nix-test/src/lib.rs +++ b/nix-test/src/lib.rs @@ -6,24 +6,41 @@ use std::ffi::CString; use libc::{c_int}; mod ffi { - use libc::{c_int, c_char}; + use libc::{c_int, c_char, size_t}; #[link(name = "nixtest", kind = "static")] extern { pub fn assert_errno_eq(errno: *const c_char) -> c_int; + pub fn size_of(ty: *const c_char) -> size_t; } } -pub fn assert_errno_eq(err: &str, val: c_int) { +pub fn assert_errno_eq(name: &str, actual: c_int) { unsafe { - let name = CString::new(err).unwrap(); - let actual = ffi::assert_errno_eq(name.as_ptr()); + let cstr = CString::new(name).unwrap(); + let expect = ffi::assert_errno_eq(cstr.as_ptr()); - assert!(actual > 0); + assert!(expect > 0, "undefined errno {}", name); - if val != actual { - panic!("incorrect value for errno {}; got={}; expected={}", - err, val, actual); + if actual != expect { + panic!("incorrect value for errno {}; expect={}; actual={}", + name, expect, actual); + } + } +} + +pub fn assert_size_of<T>(name: &str) { + use std::mem; + + unsafe { + let cstr = CString::new(name).unwrap(); + let expect = ffi::size_of(cstr.as_ptr()) as usize; + + assert!(expect > 0, "undefined type {}", name); + + if mem::size_of::<T>() != expect { + panic!("incorrectly sized type; expect={}; actual={}", + expect, mem::size_of::<T>()); } } } diff --git a/nix-test/src/sizes.c b/nix-test/src/sizes.c new file mode 100644 index 00000000..24c46fd2 --- /dev/null +++ b/nix-test/src/sizes.c @@ -0,0 +1,23 @@ +#include "sys/uio.h" + +#define SIZE_OF_T(TYPE) \ + do { \ + if (0 == strcmp(type, #TYPE)) { \ + return sizeof(TYPE); \ + } \ + } while (0) + +#define SIZE_OF_S(TYPE) \ + do { \ + if (0 == strcmp(type, #TYPE)) { \ + return sizeof(struct TYPE); \ + } \ + } while (0) + +size_t +size_of(const char* type) { + // sys/uio + SIZE_OF_S(iovec); + + return 0; +} |