]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/method-self-arg-trait.rs
29d100beb064f7c2161b9f69f59c2a044fb6e3b2
[rust.git] / src / test / run-pass / method-self-arg-trait.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 // Test method calls with self as an argument
12
13 static mut COUNT: u64 = 1;
14
15 struct Foo;
16
17 impl Copy for Foo {}
18
19 trait Bar : Sized {
20     fn foo1(&self);
21     fn foo2(self);
22     fn foo3(self: Box<Self>);
23
24     fn bar1(&self) {
25         unsafe { COUNT *= 7; }
26     }
27     fn bar2(self) {
28         unsafe { COUNT *= 11; }
29     }
30     fn bar3(self: Box<Self>) {
31         unsafe { COUNT *= 13; }
32     }
33 }
34
35 impl Bar for Foo {
36     fn foo1(&self) {
37         unsafe { COUNT *= 2; }
38     }
39
40     fn foo2(self) {
41         unsafe { COUNT *= 3; }
42     }
43
44     fn foo3(self: Box<Foo>) {
45         unsafe { COUNT *= 5; }
46     }
47 }
48
49 impl Foo {
50     fn baz(self) {
51         unsafe { COUNT *= 17; }
52         // Test internal call.
53         Bar::foo1(&self);
54         Bar::foo2(self);
55         Bar::foo3(box self);
56
57         Bar::bar1(&self);
58         Bar::bar2(self);
59         Bar::bar3(box self);
60     }
61 }
62
63 fn main() {
64     let x = Foo;
65     // Test external call.
66     Bar::foo1(&x);
67     Bar::foo2(x);
68     Bar::foo3(box x);
69
70     Bar::bar1(&x);
71     Bar::bar2(x);
72     Bar::bar3(box x);
73
74     x.baz();
75
76     unsafe { assert!(COUNT == 2u64*2*3*3*5*5*7*7*11*11*13*13*17); }
77 }