• Stars
    star
    125
  • Rank 286,335 (Top 6 %)
  • Language
    Rust
  • License
    Apache License 2.0
  • Created about 9 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Automatic testing of FFI bindings for Rust

ctest

Build Status Documentation

Automated testing of FFI bindings in Rust. This repository is intended to validate the *-sys crates that can be found on crates.io to ensure that the APIs in Rust match the APIs defined in C.

Example

Unfortunately the usage today is a little wonky, but to use this library, first, create a new Cargo project in your repo:

$ cargo new --bin systest

Then, edit systest/Cargo.toml to add these dependencies:

[package]
# ...
build = "build.rs"

[dependencies]
mylib-sys = { path = "../mylib-sys" }
libc = "0.2"

[build-dependencies]
ctest = "0.2"

Next, add a build script to systest/build.rs:

extern crate ctest;

fn main() {
    let mut cfg = ctest::TestGenerator::new();

    // Include the header files where the C APIs are defined
    cfg.header("foo.h")
       .header("bar.h");

    // Include the directory where the header files are defined
    cfg.include("path/to/include");

    // Generate the tests, passing the path to the `*-sys` library as well as
    // the module to generate.
    cfg.generate("../mylib-sys/lib.rs", "all.rs");
}

Next, add this to src/main.rs

#![allow(bad_style)]

extern crate mylib_sys;
extern crate libc;

use libc::*;
use mylib_sys::*;

include!(concat!(env!("OUT_DIR"), "/all.rs"));

And you're good to go! To run the tests execute cargo run in the systest directory, and everything should be kicked into action!

How it works

This library will parse the *-sys crate to learn about all extern fn definitions within. It will then generate a test suite to ensure that all function function signatures, constant values, struct layout/alignment, type size/alignment, etc, all match their C equivalent.

The generated tests come in two forms. One is a Rust file which contains the main function (hence the include! above), and another is a C file which is compiled as part of the build script. The C file is what includes all headers and returns information about the C side of things (which is validated in Rust).

A large amount of configuration can be applied to how the C file is generated, you can browse the documentation.

Projects using ctest

License

This project is licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in ctest by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

More Repositories

1

cargo-asm

cargo subcommand showing the assembly or llvm-ir generated for Rust code
Rust
1,045
star
2

jemallocator

Rust allocator using jemalloc as a backend
Rust
378
star
3

static_vector

A dynamically-resizable vector with fixed capacity and embedded storage
C++
156
star
4

slice_deque

A contiguous-in-memory double-ended queue that derefs into a slice
Rust
142
star
5

scattered

C++ Scattered Containers
C++
61
star
6

mimallocator

Rust
54
star
7

bitwise

Portable high-level bitwise manipulation algorithms
Rust
46
star
8

bitintr

Portable Bitwise Manipulation Intrinsics
Rust
36
star
9

cpp_skeleton

C++ Projekt Skeleton
CMake
19
star
10

is_sorted

Is an Iterator sorted?
Rust
18
star
11

nmp

Non-blocking message passing (a C++14 MPI wrapper)
C++
18
star
12

lbm-rs

Lattice-Boltzmann Method implementation in Rust
Rust
14
star
13

ndtree

nd-tree data structures and algorithms
C++
14
star
14

sleef-sys

Rust binding for SLEEF: SIMD Library for Evaluating Elementary Functions
Rust
12
star
15

aobench

Ambient Occlusion Benchmark in Rust (multi-threaded and explicitly vectorized)
C++
9
star
16

match_cfg

Convenience macro for defining items depending on large number of #[cfg]s
Rust
8
star
17

hm3

Hierarchical Multiphysics Multiscale methods
C++
7
star
18

simple-executor

A simple async executor for learning purposes
Rust
5
star
19

arithmetic_type

Implementation of an arithmetic type in C++
C++
5
star
20

cffi-panic

Error handling in Rust->C->Rust for C APIs taking callbacks
Rust
4
star
21

glfw

GLFW C++ wrapper with SFML-like event polling.
C++
4
star
22

hom3

A laboratory for High-Order Multiphysics Multiscale Methods
C++
3
star
23

is_utf8

Functions for ASCII and UTF-8 validation for byte slices
Rust
3
star
24

ampi

Asynchronous Message Passing Interface
Rust
3
star
25

tsc

Time stamp counter based timer
Rust
2
star
26

glibm

A pure Rust math library
Rust
2
star
27

linux-rs

Raw FFI Bindings to the Linux kernel APIs
Shell
1
star
28

typed_arch

Portably-typed std::arch intrinsics
Rust
1
star
29

toys

Toy projects
C++
1
star
30

stdsimd_portable

experimenting with building portable simd intrinsics on top of stdsimd
Rust
1
star