forked from dgrunwald/rust-cpython
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_sharedref.rs
148 lines (133 loc) · 4.45 KB
/
test_sharedref.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
use cpython::{py_class, GILGuard, Python};
py_class!(class Owner |py| {
@shared data string: String;
});
fn prepare_env() -> (GILGuard, Owner) {
let gil = Python::acquire_gil();
let owner = {
let py = gil.python();
Owner::create_instance(py, "new".to_owned()).unwrap()
};
(gil, owner)
}
#[test]
fn test_leaked_borrow() {
let (gil, owner) = prepare_env();
let py = gil.python();
let leaked = owner.string(py).leak_immutable();
let leaked_ref = unsafe { leaked.try_borrow(py) }.unwrap();
assert_eq!(*leaked_ref, "new");
}
#[test]
fn test_leaked_borrow_mut() {
let (gil, owner) = prepare_env();
let py = gil.python();
let leaked = owner.string(py).leak_immutable();
let mut leaked_iter = unsafe { leaked.map(py, |s| s.chars()) };
let mut leaked_ref = unsafe { leaked_iter.try_borrow_mut(py) }.unwrap();
assert_eq!(leaked_ref.next(), Some('n'));
assert_eq!(leaked_ref.next(), Some('e'));
assert_eq!(leaked_ref.next(), Some('w'));
assert_eq!(leaked_ref.next(), None);
}
#[test]
fn test_leaked_borrow_after_mut() {
let (gil, owner) = prepare_env();
let py = gil.python();
let leaked = owner.string(py).leak_immutable();
owner.string(py).borrow_mut().clear();
assert!(unsafe { leaked.try_borrow(py) }.is_err());
}
#[test]
fn test_leaked_borrow_mut_after_mut() {
let (gil, owner) = prepare_env();
let py = gil.python();
let leaked = owner.string(py).leak_immutable();
let mut leaked_iter = unsafe { leaked.map(py, |s| s.chars()) };
owner.string(py).borrow_mut().clear();
assert!(unsafe { leaked_iter.try_borrow_mut(py) }.is_err());
}
#[test]
#[should_panic(expected = "map() over invalidated leaked reference")]
fn test_leaked_map_after_mut() {
let (gil, owner) = prepare_env();
let py = gil.python();
let leaked = owner.string(py).leak_immutable();
owner.string(py).borrow_mut().clear();
let _leaked_iter = unsafe { leaked.map(py, |s| s.chars()) };
}
#[test]
fn test_try_borrow_mut_while_leaked_ref() {
let (gil, owner) = prepare_env();
let py = gil.python();
assert!(owner.string(py).try_borrow_mut().is_ok());
let leaked = owner.string(py).leak_immutable();
{
let _leaked_ref = unsafe { leaked.try_borrow(py) }.unwrap();
assert!(owner.string(py).try_borrow_mut().is_err());
{
let _leaked_ref2 = unsafe { leaked.try_borrow(py) }.unwrap();
assert!(owner.string(py).try_borrow_mut().is_err());
}
assert!(owner.string(py).try_borrow_mut().is_err());
}
assert!(owner.string(py).try_borrow_mut().is_ok());
}
#[test]
fn test_try_borrow_mut_while_leaked_ref_mut() {
let (gil, owner) = prepare_env();
let py = gil.python();
assert!(owner.string(py).try_borrow_mut().is_ok());
let leaked = owner.string(py).leak_immutable();
let mut leaked_iter = unsafe { leaked.map(py, |s| s.chars()) };
{
let _leaked_ref = unsafe { leaked_iter.try_borrow_mut(py) }.unwrap();
assert!(owner.string(py).try_borrow_mut().is_err());
}
assert!(owner.string(py).try_borrow_mut().is_ok());
}
#[test]
fn test_try_leak_while_borrow_mut() {
let (gil, owner) = prepare_env();
let py = gil.python();
let _mut_ref = owner.string(py).borrow_mut();
assert!(owner.string(py).try_leak_immutable().is_err());
}
#[test]
#[should_panic(expected = "already mutably borrowed")]
fn test_leak_while_borrow_mut() {
let (gil, owner) = prepare_env();
let py = gil.python();
let _mut_ref = owner.string(py).borrow_mut();
owner.string(py).leak_immutable();
}
#[test]
fn test_try_borrow_mut_while_borrow() {
let (gil, owner) = prepare_env();
let py = gil.python();
let _ref = owner.string(py).borrow();
assert!(owner.string(py).try_borrow_mut().is_err());
}
#[test]
#[should_panic(expected = "already borrowed")]
fn test_borrow_mut_while_borrow() {
let (gil, owner) = prepare_env();
let py = gil.python();
let _ref = owner.string(py).borrow();
owner.string(py).borrow_mut();
}
#[test]
fn test_try_borrow_while_borrow_mut() {
let (gil, owner) = prepare_env();
let py = gil.python();
let _mut_ref = owner.string(py).borrow_mut();
assert!(owner.string(py).try_borrow().is_err());
}
#[test]
#[should_panic(expected = "already mutably borrowed")]
fn test_borrow_while_borrow_mut() {
let (gil, owner) = prepare_env();
let py = gil.python();
let _mut_ref = owner.string(py).borrow_mut();
owner.string(py).borrow();
}