]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/unboxed-closures-drop.rs
rollup merge of #17355 : gamazeps/issue17210
[rust.git] / src / test / run-pass / unboxed-closures-drop.rs
1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // A battery of tests to ensure destructors of unboxed closure environments
12 // run at the right times.
13
14 #![feature(overloaded_calls, unboxed_closures)]
15
16 static mut DROP_COUNT: uint = 0;
17
18 fn drop_count() -> uint {
19     unsafe {
20         DROP_COUNT
21     }
22 }
23
24 struct Droppable {
25     x: int,
26 }
27
28 impl Droppable {
29     fn new() -> Droppable {
30         Droppable {
31             x: 1
32         }
33     }
34 }
35
36 impl Drop for Droppable {
37     fn drop(&mut self) {
38         unsafe {
39             DROP_COUNT += 1
40         }
41     }
42 }
43
44 fn a<F:Fn(int, int) -> int>(f: F) -> int {
45     f(1, 2)
46 }
47
48 fn b<F:FnMut(int, int) -> int>(mut f: F) -> int {
49     f(3, 4)
50 }
51
52 fn c<F:FnOnce(int, int) -> int>(f: F) -> int {
53     f(5, 6)
54 }
55
56 fn test_fn() {
57     {
58         a(|&: a: int, b| { a + b });
59     }
60     assert_eq!(drop_count(), 0);
61
62     {
63         let z = &Droppable::new();
64         a(|&: a: int, b| { z; a + b });
65         assert_eq!(drop_count(), 0);
66     }
67     assert_eq!(drop_count(), 1);
68
69     {
70         let z = &Droppable::new();
71         let zz = &Droppable::new();
72         a(|&: a: int, b| { z; zz; a + b });
73         assert_eq!(drop_count(), 1);
74     }
75     assert_eq!(drop_count(), 3);
76 }
77
78 fn test_fn_mut() {
79     {
80         b(|&mut: a: int, b| { a + b });
81     }
82     assert_eq!(drop_count(), 3);
83
84     {
85         let z = &Droppable::new();
86         b(|&mut: a: int, b| { z; a + b });
87         assert_eq!(drop_count(), 3);
88     }
89     assert_eq!(drop_count(), 4);
90
91     {
92         let z = &Droppable::new();
93         let zz = &Droppable::new();
94         b(|&mut: a: int, b| { z; zz; a + b });
95         assert_eq!(drop_count(), 4);
96     }
97     assert_eq!(drop_count(), 6);
98 }
99
100 fn test_fn_once() {
101     {
102         c(|: a: int, b| { a + b });
103     }
104     assert_eq!(drop_count(), 6);
105
106     {
107         let z = Droppable::new();
108         c(|: a: int, b| { z; a + b });
109         assert_eq!(drop_count(), 7);
110     }
111     assert_eq!(drop_count(), 7);
112
113     {
114         let z = Droppable::new();
115         let zz = Droppable::new();
116         c(|: a: int, b| { z; zz; a + b });
117         assert_eq!(drop_count(), 9);
118     }
119     assert_eq!(drop_count(), 9);
120 }
121
122 fn main() {
123     test_fn();
124     test_fn_mut();
125     test_fn_once();
126 }
127