Skip to content

Commit 42535c7

Browse files
committed
Add operator new/delete test
1 parent 4910525 commit 42535c7

3 files changed

Lines changed: 80 additions & 0 deletions

File tree

‎tests/unit/operator_new_delete.cpp‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <cassert>
2+
#include <new>
3+
4+
int main() {
5+
auto a = static_cast<int*>(::operator new(sizeof(int)));
6+
*a = 42;
7+
assert(*a == 42);
8+
::operator delete(a);
9+
10+
auto arr = static_cast<int*>(::operator new[](sizeof(int) * 2));
11+
arr[0] = 0;
12+
arr[1] = 1;
13+
assert(arr[0] + arr[1] == 1);
14+
::operator delete[](arr);
15+
16+
return 0;
17+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
extern crate libcc2rs;
2+
use libcc2rs::*;
3+
use std::cell::RefCell;
4+
use std::collections::BTreeMap;
5+
use std::io::prelude::*;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::AsFd;
8+
use std::rc::{Rc, Weak};
9+
pub fn main() {
10+
__cpp2rust_init_globals();
11+
std::process::exit(main_0());
12+
}
13+
fn main_0() -> i32 {
14+
let a: Value<Ptr<i32>> = Rc::new(RefCell::new(
15+
libcc2rs::malloc_refcount(::std::mem::size_of::<i32>()).reinterpret_cast::<i32>(),
16+
));
17+
(*a.borrow()).write(42);
18+
assert!((((*a.borrow()).read()) == 42));
19+
libcc2rs::free_refcount(((*a.borrow()).clone() as Ptr<i32>).to_any());
20+
let arr: Value<Ptr<i32>> = Rc::new(RefCell::new(
21+
libcc2rs::malloc_refcount((::std::mem::size_of::<i32>() as usize).wrapping_mul(2_usize))
22+
.reinterpret_cast::<i32>(),
23+
));
24+
(*arr.borrow()).offset((0) as isize).write(0);
25+
(*arr.borrow()).offset((1) as isize).write(1);
26+
assert!(
27+
((((*arr.borrow()).offset((0) as isize).read())
28+
+ ((*arr.borrow()).offset((1) as isize).read()))
29+
== 1)
30+
);
31+
libcc2rs::free_refcount(((*arr.borrow()).clone() as Ptr<i32>).to_any());
32+
return 0;
33+
}
34+
pub fn __cpp2rust_init_globals() {}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
extern crate libc;
2+
use libc::*;
3+
extern crate libcc2rs;
4+
use libcc2rs::*;
5+
use std::collections::BTreeMap;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
8+
use std::rc::Rc;
9+
pub fn main() {
10+
unsafe {
11+
__cpp2rust_init_globals();
12+
std::process::exit(main_0() as i32);
13+
}
14+
}
15+
unsafe fn main_0() -> i32 {
16+
let mut a: *mut i32 = (libcc2rs::malloc_unsafe(::std::mem::size_of::<i32>()) as *mut i32);
17+
(*a) = 42;
18+
assert!(((*a) == (42)));
19+
libcc2rs::free_unsafe((a as *mut i32 as *mut ::libc::c_void));
20+
let mut arr: *mut i32 =
21+
(libcc2rs::malloc_unsafe((::std::mem::size_of::<i32>() as usize).wrapping_mul(2_usize))
22+
as *mut i32);
23+
(*arr.offset((0) as isize)) = 0;
24+
(*arr.offset((1) as isize)) = 1;
25+
assert!((((*arr.offset((0) as isize)) + (*arr.offset((1) as isize))) == (1)));
26+
libcc2rs::free_unsafe((arr as *mut i32 as *mut ::libc::c_void));
27+
return 0;
28+
}
29+
pub unsafe fn __cpp2rust_init_globals() {}

0 commit comments

Comments
 (0)