]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/unboxed-closures-drop.rs
rustdoc: Replace no-pretty-expanded with pretty-expanded
[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 // pretty-expanded FIXME #23616
15
16 #![feature(unboxed_closures)]
17
18 static mut DROP_COUNT: uint = 0;
19
20 fn drop_count() -> uint {
21     unsafe {
22         DROP_COUNT
23     }
24 }
25
26 struct Droppable {
27     x: int,
28 }
29
30 impl Droppable {
31     fn new() -> Droppable {
32         Droppable {
33             x: 1
34         }
35     }
36 }
37
38 impl Drop for Droppable {
39     fn drop(&mut self) {
40         unsafe {
41             DROP_COUNT += 1
42         }
43     }
44 }
45
46 fn a<F:Fn(int, int) -> int>(f: F) -> int {
47     f(1, 2)
48 }
49
50 fn b<F:FnMut(int, int) -> int>(mut f: F) -> int {
51     f(3, 4)
52 }
53
54 fn c<F:FnOnce(int, int) -> int>(f: F) -> int {
55     f(5, 6)
56 }
57
58 fn test_fn() {
59     {
60         a(move |a: int, b| { a + b });
61     }
62     assert_eq!(drop_count(), 0);
63
64     {
65         let z = &Droppable::new();
66         a(move |a: int, b| { z; a + b });
67         assert_eq!(drop_count(), 0);
68     }
69     assert_eq!(drop_count(), 1);
70
71     {
72         let z = &Droppable::new();
73         let zz = &Droppable::new();
74         a(move |a: int, b| { z; zz; a + b });
75         assert_eq!(drop_count(), 1);
76     }
77     assert_eq!(drop_count(), 3);
78 }
79
80 fn test_fn_mut() {
81     {
82         b(move |a: int, b| { a + b });
83     }
84     assert_eq!(drop_count(), 3);
85
86     {
87         let z = &Droppable::new();
88         b(move |a: int, b| { z; a + b });
89         assert_eq!(drop_count(), 3);
90     }
91     assert_eq!(drop_count(), 4);
92
93     {
94         let z = &Droppable::new();
95         let zz = &Droppable::new();
96         b(move |a: int, b| { z; zz; a + b });
97         assert_eq!(drop_count(), 4);
98     }
99     assert_eq!(drop_count(), 6);
100 }
101
102 fn test_fn_once() {
103     {
104         c(move |a: int, b| { a + b });
105     }
106     assert_eq!(drop_count(), 6);
107
108     {
109         let z = Droppable::new();
110         c(move |a: int, b| { z; a + b });
111         assert_eq!(drop_count(), 7);
112     }
113     assert_eq!(drop_count(), 7);
114
115     {
116         let z = Droppable::new();
117         let zz = Droppable::new();
118         c(move |a: int, b| { z; zz; a + b });
119         assert_eq!(drop_count(), 9);
120     }
121     assert_eq!(drop_count(), 9);
122 }
123
124 fn main() {
125     test_fn();
126     test_fn_mut();
127     test_fn_once();
128 }