Skip to content

Commit 1e6da7b

Browse files
committed
Add another test case for #2288
Added a test case for #2288. It's xfailed for now, pending #2364
1 parent 1460ea4 commit 1e6da7b

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed
+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// xfail-test
2+
// xfail-fast
3+
use std;
4+
import std::map::*;
5+
6+
enum cat_type { tuxedo, tabby, tortoiseshell }
7+
8+
// Very silly -- this just returns the value of the name field
9+
// for any int value that's less than the meows field
10+
11+
// ok: T should be in scope when resolving the iface ref for map
12+
class cat<T: copy> implements map<int, T> {
13+
priv {
14+
// Yes, you can have negative meows
15+
let mut meows : int;
16+
fn meow() {
17+
self.meows += 1;
18+
#error("Meow %d", self.meows);
19+
if self.meows % 5 == 0 {
20+
self.how_hungry += 1;
21+
}
22+
}
23+
}
24+
25+
let mut how_hungry : int;
26+
let name : T;
27+
28+
new(in_x : int, in_y : int, in_name: T)
29+
{ self.meows = in_x; self.how_hungry = in_y; self.name = in_name; }
30+
31+
fn speak() { self.meow(); }
32+
33+
fn eat() -> bool {
34+
if self.how_hungry > 0 {
35+
#error("OM NOM NOM");
36+
self.how_hungry -= 2;
37+
ret true;
38+
}
39+
else {
40+
#error("Not hungry!");
41+
ret false;
42+
}
43+
}
44+
45+
fn size() -> uint { self.meows as uint }
46+
fn insert(&&k: int, &&_v: T) -> bool {
47+
self.meows += k;
48+
true
49+
}
50+
fn contains_key(&&k: int) -> bool { k <= self.meows }
51+
52+
fn get(&&k:int) -> T { alt self.find(k) {
53+
some(v) { v }
54+
none { fail "epic fail"; }
55+
}
56+
}
57+
fn find(&&k:int) -> option<T> { if k <= self.meows {
58+
some(self.name)
59+
}
60+
else { none }
61+
}
62+
63+
fn remove(&&k:int) -> option<T> {
64+
alt self.find(k) {
65+
some(x) {
66+
self.meows -= k; some(x)
67+
}
68+
none { none }
69+
}
70+
}
71+
72+
fn each(f: fn(&&int, &&T) -> bool) {
73+
let mut n = int::abs(self.meows);
74+
while n > 0 {
75+
if !f(n, self.name) { break; }
76+
n -= 1;
77+
}
78+
}
79+
80+
fn each_key(&&f: fn(&&int) -> bool) {
81+
for self.each {|k, _v| if !f(k) { break; } cont;};
82+
}
83+
fn each_value(&&f: fn(&&T) -> bool) {
84+
for self.each {|_k, v| if !f(v) { break; } cont;};
85+
}
86+
}
87+
88+
89+
fn main() {
90+
let nyan : cat<str> = cat(0, 2, "nyan");
91+
uint::range(1u, 5u) {|_i| nyan.speak(); }
92+
assert(nyan.find(1) == some("nyan"));
93+
assert(nyan.find(10) == none);
94+
let spotty : cat<cat_type> = cat(2, 57, tuxedo);
95+
uint::range(0u, 6u) {|_i| spotty.speak(); }
96+
assert(spotty.size() == 8u);
97+
assert(spotty.contains_key(2));
98+
assert(spotty.get(3) == tuxedo);
99+
}

0 commit comments

Comments
 (0)