-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.rs
184 lines (157 loc) · 3.28 KB
/
lib.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#[macro_use]
extern crate adhesion;
#[test]
fn most_basic() {
contract! {
fn asdf() {
body {
()
}
}
}
asdf();
}
#[test]
fn ordering_doesnt_matter() {
contract! {
fn sqrt(x: f64) -> f64 {
body {
x.sqrt()
}
post(y) {
assert!(x == y * y, "wat!");
}
}
}
sqrt(25_f64);
}
#[test]
fn can_omit_post_param() {
contract! {
fn omitted() {
post {
}
}
}
omitted();
contract! {
fn not_omitted() {
post(stuff) {
}
}
}
not_omitted();
contract! {
fn no_macro_clash() {
post(def) { // default name of post param in macro
}
}
}
no_macro_clash();
}
#[test]
fn attributes() {
contract! {
#[cold]
fn before() {
body {}
}
}
before();
contract! {
fn after() {
#![cold]
body {}
}
}
after();
}
#[test]
#[allow(unused)]
fn mutability() {
contract! {
#[allow(unused)]
fn mutable(mut i_am_mutable: String) -> String {
body {
"wut".to_string()
}
}
}
contract! {
#[allow(unused)]
fn mutable_multiple1(mut stuff: u32, mut things: u64) {}
}
contract! {
#[allow(unused)]
fn mutable_mixed_first(mut stuff: String, things: bool) {}
}
contract! {
#[allow(unused)]
fn mutable_mixed_second(stuff: f64, things: ()) {}
}
}
#[test]
fn structs() {
struct TestStruct {
var: u32
}
impl TestStruct {
contract! {
fn eat(self) -> u32 {
body {
self.var
}
}
}
contract! {
fn change(&mut self, value: u32) {
body {
self.var = value;
}
}
}
contract! {
fn check_out(&self) -> u32 {
body {
self.var
}
}
}
}
assert!(TestStruct { var: 4 }.eat() == 4, "var not properly extracted from TestStruct");
{
let mut t = TestStruct { var: 4 };
t.change(26);
assert!(t.var == 26, "var not mutated in TestStruct");
}
{
let t = TestStruct { var: 3 };
assert!(t.check_out() == 3, "var not properly read from TestStruct");
assert!(t.eat() == 3, "var not properly extracted from TestStruct");
}
}
#[test]
fn generics() {
contract! {
fn add_together1<T: ::std::ops::Add>(left: T, right: T) -> <T as std::ops::Add>::Output {
pre {}
body {
left + right
}
post (def) {}
invariant {}
}
}
assert!(add_together1(2, 4) == 6, "add impl broken (!?)");
contract! {
fn add_together2<T>(left: T, right: T) -> T::Output where T: ::std::ops::Add {
pre {}
body {
left + right
}
post (def) {}
invariant {}
}
}
assert!(add_together2(2, 4) == 6, "add impl broken (!?)");
}