Skip to content

Commit

Permalink
test/tensor: add several compile tests for SharedTensor API
Browse files Browse the repository at this point in the history
Implementation of SharedTensor uses `unsafe` to extend lifetime of memory
references that are returned by read/write family of methods. Those tests
verify that attempts to create dangling pointers or otherwise misuse API
fail at compile time.
  • Loading branch information
alexandermorozov committed Apr 18, 2016
1 parent f76ced4 commit df02d48
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ num = "0.1"
lazy_static = "0.1.15"

clippy = { version = "0.0.27", optional = true }
compiletest_rs = { version = "0.1", optional = true }

[dev-dependencies]
rand = "0.3"
Expand All @@ -35,7 +36,7 @@ opencl = []
unstable_alloc = [] # faster but unstable memory allocation on native machines

dev = []
unstable = [] # for travis-cargo
unstable = ["compiletest_rs"] # for travis-cargo
travis = ["native"]
lint = ["clippy"]

Expand Down
13 changes: 13 additions & 0 deletions tests/compile-fail/drop_live_memory.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
extern crate collenchyma;
use collenchyma::prelude::*;

fn main() {
let ntv = Native::new();
let dev = ntv.new_device(ntv.hardwares()).unwrap();

let x = &mut SharedTensor::<f32>::new(&10).unwrap();
let m = x.write_only(&dev).unwrap();
x.drop_device(&dev);
//~^ ERROR error: cannot borrow `*x` as mutable more than once at a time
}

16 changes: 16 additions & 0 deletions tests/compile-fail/leak_read_reference.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
extern crate collenchyma;
use collenchyma::prelude::*;

fn main() {
let ntv = Native::new();
let dev = ntv.new_device(ntv.hardwares()).unwrap();

let mem = {
let x = &mut SharedTensor::<f32>::new(&10).unwrap();
//~^ ERROR error: borrowed value does not live long enough
x.write_only(&dev).unwrap();
let m = x.read(&dev).unwrap();
m
};
}

15 changes: 15 additions & 0 deletions tests/compile-fail/leak_write_reference.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
extern crate collenchyma;
use collenchyma::prelude::*;

fn main() {
let ntv = Native::new();
let dev = ntv.new_device(ntv.hardwares()).unwrap();

let mem = {
let x = &mut SharedTensor::<f32>::new(&10).unwrap();
//~^ ERROR error: borrowed value does not live long enough
let m = x.write_only(&dev).unwrap();
m
};
}

13 changes: 13 additions & 0 deletions tests/compile-fail/read_write_borrows.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
extern crate collenchyma;
use collenchyma::prelude::*;

fn main() {
let ntv = Native::new();
let dev = ntv.new_device(ntv.hardwares()).unwrap();

let x = &mut SharedTensor::<f32>::new(&10).unwrap();
let m1 = x.write_only(&dev).unwrap();
let m2 = x.read(&dev).unwrap();
//~^ ERROR cannot borrow `*x` as immutable because it is also borrowed as mutable
}

13 changes: 13 additions & 0 deletions tests/compile-fail/two_write_borrows.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
extern crate collenchyma;
use collenchyma::prelude::*;

fn main() {
let ntv = Native::new();
let dev = ntv.new_device(ntv.hardwares()).unwrap();

let x = &mut SharedTensor::<f32>::new(&10).unwrap();
let m1 = x.write_only(&dev).unwrap();
let m2 = x.write_only(&dev).unwrap();
//~^ ERROR error: cannot borrow `*x` as mutable more than once at a time
}

22 changes: 22 additions & 0 deletions tests/compiletests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#![cfg(feature = "compiletest_rs")]
extern crate compiletest_rs as compiletest;

use std::path::PathBuf;

fn run_mode(mode: &'static str) {
let mut config = compiletest::default_config();
let cfg_mode = mode.parse().ok().expect("Invalid mode");

config.target_rustcflags = Some("-L target/debug/ -L target/debug/deps/"
.to_owned());
config.mode = cfg_mode;
config.src_base = PathBuf::from(format!("tests/{}", mode));

compiletest::run_tests(&config);
}

#[test]
fn compile_test() {
run_mode("compile-fail");
run_mode("run-pass");
}
15 changes: 15 additions & 0 deletions tests/run-pass/multiple_read_only_borrows.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
extern crate collenchyma;
use collenchyma::prelude::*;

fn main() {
let ntv = Native::new();
let dev = ntv.new_device(ntv.hardwares()).unwrap();

let x = &mut SharedTensor::<f32>::new(&10).unwrap();
x.write_only(&dev).unwrap();

let m1 = x.read(&dev);
let m2 = x.read(&dev);
let m3 = x.read(&dev);
}

0 comments on commit df02d48

Please sign in to comment.