Skip to content

Commit 318472b

Browse files
committed
test
1 parent 1397f99 commit 318472b

File tree

2 files changed

+129
-0
lines changed

2 files changed

+129
-0
lines changed

src/test/auxiliary/method_self_arg.rs

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+
#![crate_type = "lib"]
12+
13+
static mut COUNT: u64 = 1;
14+
15+
pub fn get_count() -> u64 { unsafe { COUNT } }
16+
pub fn reset_count() { unsafe { COUNT = 1; } }
17+
18+
pub struct Foo;
19+
20+
impl Foo {
21+
pub fn foo(self, x: &Foo) {
22+
unsafe { COUNT *= 2; }
23+
// Test internal call.
24+
Foo::bar(&self);
25+
Foo::bar(x);
26+
27+
Foo::baz(self);
28+
Foo::baz(*x);
29+
30+
Foo::qux(box self);
31+
Foo::qux(box *x);
32+
}
33+
34+
pub fn bar(&self) {
35+
unsafe { COUNT *= 3; }
36+
}
37+
38+
pub fn baz(self) {
39+
unsafe { COUNT *= 5; }
40+
}
41+
42+
pub fn qux(self: Box<Foo>) {
43+
unsafe { COUNT *= 7; }
44+
}
45+
46+
pub fn run_trait(self) {
47+
unsafe { COUNT *= 17; }
48+
// Test internal call.
49+
Bar::foo1(&self);
50+
Bar::foo2(self);
51+
Bar::foo3(box self);
52+
53+
Bar::bar1(&self);
54+
Bar::bar2(self);
55+
Bar::bar3(box self);
56+
}
57+
}
58+
59+
pub trait Bar {
60+
fn foo1(&self);
61+
fn foo2(self);
62+
fn foo3(self: Box<Self>);
63+
64+
fn bar1(&self) {
65+
unsafe { COUNT *= 7; }
66+
}
67+
fn bar2(self) {
68+
unsafe { COUNT *= 11; }
69+
}
70+
fn bar3(self: Box<Self>) {
71+
unsafe { COUNT *= 13; }
72+
}
73+
}
74+
75+
impl Bar for Foo {
76+
fn foo1(&self) {
77+
unsafe { COUNT *= 2; }
78+
}
79+
80+
fn foo2(self) {
81+
unsafe { COUNT *= 3; }
82+
}
83+
84+
fn foo3(self: Box<Foo>) {
85+
unsafe { COUNT *= 5; }
86+
}
87+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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 (cross-crate)
12+
13+
// aux-build:method_self_arg.rs
14+
extern crate method_self_arg;
15+
use method_self_arg::{Foo, Bar};
16+
17+
fn main() {
18+
let x = Foo;
19+
// Test external call.
20+
Foo::bar(&x);
21+
Foo::baz(x);
22+
Foo::qux(box x);
23+
24+
x.foo(&x);
25+
26+
assert!(method_self_arg::get_count() == 2u64*3*3*3*5*5*5*7*7*7);
27+
28+
method_self_arg::reset_count();
29+
// Test external call.
30+
Bar::foo1(&x);
31+
Bar::foo2(x);
32+
Bar::foo3(box x);
33+
34+
Bar::bar1(&x);
35+
Bar::bar2(x);
36+
Bar::bar3(box x);
37+
38+
x.run_trait();
39+
40+
println!("{}, {}", method_self_arg::get_count(), 2u64*2*3*3*5*5*7*7*11*11*13*13*17);
41+
assert!(method_self_arg::get_count() == 2u64*2*3*3*5*5*7*7*11*11*13*13*17);
42+
}

0 commit comments

Comments
 (0)