diff options
author | Alberto Sartori <alberto.sartori@huawei.com> | 2024-06-24 15:49:36 +0200 |
---|---|---|
committer | Klaus Aehlig <klaus.aehlig@huawei.com> | 2024-06-24 15:49:36 +0200 |
commit | f87ad41f72ca4465a0c5b4ba9fd36a7b09e4d4f4 (patch) | |
tree | 24d4a3a5975df2046714c6bc114d5d050bdc61fc /test/rust-from-c | |
download | rules-rust-f87ad41f72ca4465a0c5b4ba9fd36a7b09e4d4f4.tar.gz |
Initial commit
Co-authored-by: Klaus Aehlig <klaus.aehlig@huawei.com>
Diffstat (limited to 'test/rust-from-c')
-rw-r--r-- | test/rust-from-c/TARGETS | 23 | ||||
-rw-r--r-- | test/rust-from-c/check_ints.sh | 5 | ||||
-rw-r--r-- | test/rust-from-c/foo/TARGETS | 15 | ||||
-rw-r--r-- | test/rust-from-c/foo/bar.rs | 3 | ||||
-rw-r--r-- | test/rust-from-c/foo/foo.h | 2 | ||||
-rw-r--r-- | test/rust-from-c/foo/foo.rs | 6 | ||||
-rw-r--r-- | test/rust-from-c/foo/foo_test.rs | 11 | ||||
-rw-r--r-- | test/rust-from-c/main.c | 14 |
8 files changed, 79 insertions, 0 deletions
diff --git a/test/rust-from-c/TARGETS b/test/rust-from-c/TARGETS new file mode 100644 index 0000000..1cb8019 --- /dev/null +++ b/test/rust-from-c/TARGETS @@ -0,0 +1,23 @@ +{ "main": + { "type": ["@", "rules-cc", "CC", "binary"] + , "pure C": ["true"] + , "name": ["main"] + , "srcs": ["main.c"] + , "private-deps": [["./", "foo", "foo"]] + } +, "test_foo": + { "type": ["rules/rust", "test"] + , "name": ["test_foo"] + , "crate_root": ["foo/foo_test.rs"] + , "stage": ["test_foo"] + , "deps": [["./", "foo", "foo"]] + } +, "check_ints": + { "type": ["@", "rules-cc", "shell/test", "script"] + , "name": ["check_ints"] + , "test": ["check_ints.sh"] + , "deps": ["main"] + } +, "TESTS": + {"type": "install", "deps": ["check_ints", "test_foo"], "tainted": ["test"]} +} diff --git a/test/rust-from-c/check_ints.sh b/test/rust-from-c/check_ints.sh new file mode 100644 index 0000000..00780c3 --- /dev/null +++ b/test/rust-from-c/check_ints.sh @@ -0,0 +1,5 @@ +set -e + +for i in `seq 1 42`; do + ./main -$i | grep " -$i is $i" +done diff --git a/test/rust-from-c/foo/TARGETS b/test/rust-from-c/foo/TARGETS new file mode 100644 index 0000000..17c66ba --- /dev/null +++ b/test/rust-from-c/foo/TARGETS @@ -0,0 +1,15 @@ +{ "foo": + { "type": ["rules/rust", "library"] + , "name": ["foo"] + , "crate_root": ["foo.rs"] + , "c_hdrs": ["foo.h"] + , "stage": ["foo"] + , "deps": ["bar"] + } +, "bar": + { "type": ["rules/rust", "library"] + , "name": ["bar"] + , "crate_root": ["bar.rs"] + , "stage": ["bar"] + } +} diff --git a/test/rust-from-c/foo/bar.rs b/test/rust-from-c/foo/bar.rs new file mode 100644 index 0000000..61e68f4 --- /dev/null +++ b/test/rust-from-c/foo/bar.rs @@ -0,0 +1,3 @@ +pub fn bar(x: i32) -> i32 { + return x.abs(); +}
\ No newline at end of file diff --git a/test/rust-from-c/foo/foo.h b/test/rust-from-c/foo/foo.h new file mode 100644 index 0000000..88deb2e --- /dev/null +++ b/test/rust-from-c/foo/foo.h @@ -0,0 +1,2 @@ +#pragma once +int foo(int); diff --git a/test/rust-from-c/foo/foo.rs b/test/rust-from-c/foo/foo.rs new file mode 100644 index 0000000..8735b67 --- /dev/null +++ b/test/rust-from-c/foo/foo.rs @@ -0,0 +1,6 @@ +extern crate bar; + +#[no_mangle] +pub extern "C" fn foo(x: i32) -> i32 { + return bar::bar(x); +} diff --git a/test/rust-from-c/foo/foo_test.rs b/test/rust-from-c/foo/foo_test.rs new file mode 100644 index 0000000..8b5de77 --- /dev/null +++ b/test/rust-from-c/foo/foo_test.rs @@ -0,0 +1,11 @@ +extern crate foo; + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_foo(){ + assert_eq!(foo::foo(-7),7); + } +}
\ No newline at end of file diff --git a/test/rust-from-c/main.c b/test/rust-from-c/main.c new file mode 100644 index 0000000..0f69281 --- /dev/null +++ b/test/rust-from-c/main.c @@ -0,0 +1,14 @@ +#include <stdio.h> +#include <stdlib.h> + +int foo(int); + +int main(int argc, char **argv) { + if (argc < 2) { + fprintf(stderr, "Please provide one number as argument\n"); + exit(1); + } + int x = atoi(argv[1]); + printf("absolute value of %d is %d\n", x, foo(x)); + return 0; +} |