Skip to content

Commit e383ae5

Browse files
committed
Add tests
1 parent 9188d35 commit e383ae5

12 files changed

Lines changed: 90 additions & 7 deletions

‎rules/memory/src_until_cxx23.cpp‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,4 @@
66

77
void f1(char *a0, std::size_t a1) { return std::declare_no_pointers(a0, a1); }
88

9-
void f2(char *a0, std::size_t a1) {
10-
return std::undeclare_no_pointers(a0, a1);
11-
}
9+
void f2(char *a0, std::size_t a1) { return std::undeclare_no_pointers(a0, a1); }

‎rules/memory/tgt_refcount.rs‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33

44
use libcc2rs::*;
55

6-
fn f1(_a0: Ptr<i8>, _a1: usize) {}
6+
fn f1(a0: Ptr<i8>, a1: usize) {
7+
()
8+
}
79

8-
fn f2(_a0: Ptr<i8>, _a1: usize) {}
10+
fn f2(a0: Ptr<i8>, a1: usize) {
11+
()
12+
}

‎rules/memory/tgt_unsafe.rs‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
// Copyright (c) 2022-present INESC-ID.
22
// Distributed under the MIT license that can be found in the LICENSE file.
33

4-
unsafe fn f1(_a0: *mut libc::c_char, _a1: usize) {}
4+
unsafe fn f1(a0: *mut libc::c_char, a1: usize) {
5+
()
6+
}
57

6-
unsafe fn f2(_a0: *mut libc::c_char, _a1: usize) {}
8+
unsafe fn f2(a0: *mut libc::c_char, a1: usize) {
9+
()
10+
}

‎tests/unit/08_unique_array.cpp‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// ADDITIONAL_COMPILE_FLAGS: -std=c++20
12
#include <cassert>
23
#include <memory>
34

@@ -9,5 +10,10 @@ int main() {
910
g_ptr[0] = 13;
1011
g_ptr[1] = 14;
1112
assert(g[0] + g[1] == 27);
13+
auto buf = std::make_unique<char[]>(16);
14+
std::declare_no_pointers(buf.get(), 16);
15+
buf[0] = 'x';
16+
std::undeclare_no_pointers(buf.get(), 16);
17+
assert(buf[0] == 'x');
1218
return 0;
1319
}

‎tests/unit/elidable_iterator_copy_cxx11.cpp‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,8 @@ int main() {
1010
std::vector<int>::iterator it = std::find(v.begin(), v.end(), 7);
1111
assert(it != v.end());
1212
assert(*it == 7);
13+
std::random_shuffle(v.begin(), v.end());
14+
std::sort(v.begin(), v.end());
15+
assert(v[0] == 4 && v[1] == 7);
1316
return 0;
1417
}

‎tests/unit/out/refcount/08_unique_array.rs‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ fn main_0() -> i32 {
2424
+ g.as_ref().unwrap().borrow()[(1_usize) as usize])
2525
== 27)
2626
);
27+
let mut buf: Option<Value<Box<[i8]>>> = Some(Rc::new(RefCell::new(
28+
(0..16_usize).map(|_| <i8>::default()).collect::<Box<[_]>>(),
29+
)));
30+
();
31+
buf.as_ref().unwrap().borrow_mut()[(0_usize) as usize] = ('x' as i8);
32+
();
33+
assert!(((buf.as_ref().unwrap().borrow()[(0_usize) as usize] as i32) == (('x' as i8) as i32)));
2734
return 0;
2835
}
2936
pub fn __cpp2rust_init_globals() {}

‎tests/unit/out/refcount/elidable_iterator_copy_cxx11.rs‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,21 @@ fn main_0() -> i32 {
3434
};
3535
assert!(it != (v.as_pointer() as Ptr<i32>).to_end());
3636
assert!(((it.read()) == 7));
37+
{
38+
let count = (v.as_pointer() as Ptr<i32>).to_end().get_offset()
39+
- (v.as_pointer() as Ptr<i32>).get_offset();
40+
let random = std::hash::RandomState::new();
41+
for i in (1..count).rev() {
42+
let j = std::hash::BuildHasher::hash_one(&random, i) as usize % (i + 1);
43+
let x = (v.as_pointer() as Ptr<i32>).offset(i);
44+
let y = (v.as_pointer() as Ptr<i32>).offset(j);
45+
let tmp = x.read();
46+
x.write(y.read());
47+
y.write(tmp);
48+
}
49+
};
50+
(v.as_pointer() as Ptr<i32>).sort((v.as_pointer() as Ptr<i32>).to_end().get_offset());
51+
assert!(({ (*v.borrow())[0_usize] } == 4) && ({ (*v.borrow())[1_usize] } == 7));
3752
return 0;
3853
}
3954
pub fn __cpp2rust_init_globals() {}

‎tests/unit/out/refcount/string.rs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ fn main_0() -> i32 {
2929
Ptr::<i8>::from_string_literal(b"hello")
3030
.with_c_str(|__s| (*s1.borrow())[..(*s1.borrow()).len().saturating_sub(1)] == *__s)
3131
);
32+
assert!(
33+
Ptr::<i8>::from_string_literal(b"world")
34+
.with_c_str(|__s| (*s1.borrow())[..(*s1.borrow()).len().saturating_sub(1)] != *__s)
35+
);
36+
assert!(
37+
!(Ptr::<i8>::from_string_literal(b"hello")
38+
.with_c_str(|__s| (*s1.borrow())[..(*s1.borrow()).len().saturating_sub(1)] != *__s))
39+
);
3240
let mut p1: Ptr<i8> = (s1.as_pointer() as Ptr<i8>);
3341
assert!((((elem!(p1, 0).read()) as i32) == (('h' as i8) as i32)));
3442
assert!((((elem!(p1, 1).read()) as i32) == (('e' as i8) as i32)));

‎tests/unit/out/unsafe/08_unique_array.rs‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@ unsafe fn main_0() -> i32 {
2323
(*g_ptr.offset((0) as isize)) = 13;
2424
(*g_ptr.offset((1) as isize)) = 14;
2525
assert!((((g.as_mut().unwrap()[(0_usize)]) + (g.as_mut().unwrap()[(1_usize)])) == (27)));
26+
let mut buf: Option<Box<[libc::c_char]>> = Some(
27+
(0..16_usize)
28+
.map(|_| <libc::c_char>::default())
29+
.collect::<Box<[_]>>(),
30+
);
31+
();
32+
buf.as_mut().unwrap()[(0_usize)] = ('x' as libc::c_char);
33+
();
34+
assert!(((buf.as_mut().unwrap()[(0_usize)] as i32) == (('x' as libc::c_char) as i32)));
2635
return 0;
2736
}
2837
pub unsafe fn __cpp2rust_init_globals() {}

‎tests/unit/out/unsafe/elidable_iterator_copy_cxx11.rs‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,18 @@ unsafe fn main_0() -> i32 {
3131
};
3232
assert!(it != v.as_mut_ptr().add(v.len()));
3333
assert!(((*it) == (7)));
34+
{
35+
let len = v.as_mut_ptr().add(v.len()).offset_from(v.as_mut_ptr()) as usize;
36+
let slice = ::std::slice::from_raw_parts_mut(v.as_mut_ptr(), len);
37+
for i in (1..len).rev() {
38+
slice.swap(i, libc::rand() as usize % (i + 1));
39+
}
40+
};
41+
{
42+
let len = v.as_mut_ptr().add(v.len()).offset_from(v.as_mut_ptr()) as usize;
43+
::std::slice::from_raw_parts_mut(v.as_mut_ptr(), len).sort()
44+
};
45+
assert!(((v[(0_usize)]) == (4)) && ((v[(1_usize)]) == (7)));
3446
return 0;
3547
}
3648
pub unsafe fn __cpp2rust_init_globals() {}

0 commit comments

Comments
 (0)