forked from autumnai/collenchyma
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test/tensor: add several compile tests for SharedTensor API
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
1 parent
f76ced4
commit df02d48
Showing
8 changed files
with
109 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|