2012-12-10 19:32:48 -06:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2012-05-22 12:54:12 -05:00
|
|
|
// xfail-test
|
2012-05-03 11:17:58 -05:00
|
|
|
// xfail-fast
|
2012-05-22 12:54:12 -05:00
|
|
|
|
2012-09-11 19:46:20 -05:00
|
|
|
extern mod std;
|
2013-02-01 01:13:36 -06:00
|
|
|
use std::oldmap::*;
|
2012-05-03 11:17:58 -05:00
|
|
|
|
2012-06-22 20:19:35 -05:00
|
|
|
class cat : map<int, bool> {
|
2012-05-03 11:17:58 -05:00
|
|
|
priv {
|
|
|
|
// Yes, you can have negative meows
|
|
|
|
let mut meows : int;
|
|
|
|
fn meow() {
|
|
|
|
self.meows += 1;
|
2012-08-22 19:24:52 -05:00
|
|
|
error!("Meow %d", self.meows);
|
2012-05-03 11:17:58 -05:00
|
|
|
if self.meows % 5 == 0 {
|
|
|
|
self.how_hungry += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut how_hungry : int;
|
|
|
|
let name : str;
|
|
|
|
|
|
|
|
new(in_x : int, in_y : int, in_name: str)
|
|
|
|
{ self.meows = in_x; self.how_hungry = in_y; self.name = in_name; }
|
|
|
|
|
|
|
|
fn speak() { self.meow(); }
|
|
|
|
|
|
|
|
fn eat() -> bool {
|
|
|
|
if self.how_hungry > 0 {
|
2012-08-22 19:24:52 -05:00
|
|
|
error!("OM NOM NOM");
|
2012-05-03 11:17:58 -05:00
|
|
|
self.how_hungry -= 2;
|
2012-08-01 19:30:05 -05:00
|
|
|
return true;
|
2012-05-03 11:17:58 -05:00
|
|
|
}
|
|
|
|
else {
|
2012-08-22 19:24:52 -05:00
|
|
|
error!("Not hungry!");
|
2012-08-01 19:30:05 -05:00
|
|
|
return false;
|
2012-05-03 11:17:58 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn size() -> uint { self.meows as uint }
|
2012-06-08 16:35:11 -05:00
|
|
|
fn insert(+k: int, +v: bool) -> bool {
|
2012-05-03 11:17:58 -05:00
|
|
|
if v { self.meows += k; } else { self.meows -= k; };
|
|
|
|
true
|
|
|
|
}
|
|
|
|
fn contains_key(&&k: int) -> bool { k <= self.meows }
|
|
|
|
fn get(&&k:int) -> bool { k <= self.meows }
|
2012-06-27 21:58:08 -05:00
|
|
|
fn [](&&k:int) -> bool { k <= self.meows }
|
2013-01-25 13:31:45 -06:00
|
|
|
fn find(&&k:int) -> Option<bool> { Some(self.get(k)) }
|
|
|
|
fn remove(&&k:int) -> Option<bool> { self.meows -= k; Some(true) }
|
2012-05-03 11:17:58 -05:00
|
|
|
fn each(f: fn(&&int, &&bool) -> bool) {
|
|
|
|
let mut n = int::abs(self.meows);
|
|
|
|
while n > 0 {
|
|
|
|
if !f(n, true) { break; }
|
|
|
|
n -= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn each_key(&&f: fn(&&int) -> bool) {
|
2012-07-09 16:37:48 -05:00
|
|
|
for self.each |k, _v| { if !f(k) { break; } again;};
|
2012-05-03 11:17:58 -05:00
|
|
|
}
|
|
|
|
fn each_value(&&f: fn(&&bool) -> bool) {
|
2012-07-09 16:37:48 -05:00
|
|
|
for self.each |_k, v| { if !f(v) { break; } again;};
|
2012-05-03 11:17:58 -05:00
|
|
|
}
|
2012-07-09 16:06:29 -05:00
|
|
|
fn clear() { }
|
2012-05-03 11:17:58 -05:00
|
|
|
}
|
|
|
|
|
2013-02-01 21:43:17 -06:00
|
|
|
pub fn main() {
|
2012-05-03 11:17:58 -05:00
|
|
|
let nyan : cat = cat(0, 2, "nyan");
|
2012-06-30 18:19:07 -05:00
|
|
|
for uint::range(1u, 5u) |_i| { nyan.speak(); }
|
2012-05-03 11:17:58 -05:00
|
|
|
// cat returns true if uint input is greater than
|
|
|
|
// the number of meows so far
|
|
|
|
assert(nyan.get(1));
|
|
|
|
assert(!nyan.get(10));
|
|
|
|
}
|