enum, test, test3

This commit is contained in:
zhihanz 2020-05-03 22:14:35 -07:00
parent 9c559d9965
commit d8429301db
7 changed files with 32 additions and 15 deletions

View File

@ -1,11 +1,13 @@
// enums1.rs // enums1.rs
// Make me compile! Execute `rustlings hint enums1` for hints! // Make me compile! Execute `rustlings hint enums1` for hints!
// I AM NOT DONE
#[derive(Debug)] #[derive(Debug)]
enum Message { enum Message {
// TODO: define a few types of messages as used below Quit,
Echo,
Move,
ChangeColor,
} }
fn main() { fn main() {

View File

@ -1,11 +1,13 @@
// enums2.rs // enums2.rs
// Make me compile! Execute `rustlings hint enums2` for hints! // Make me compile! Execute `rustlings hint enums2` for hints!
// I AM NOT DONE
#[derive(Debug)] #[derive(Debug)]
enum Message { enum Message {
// TODO: define the different variants used below Quit,
Echo(String),// a tuple with one elem
Move{x: i32, y: i32},
ChangeColor(u8, u8, u8),
} }
impl Message { impl Message {

View File

@ -1,10 +1,13 @@
// enums3.rs // enums3.rs
// Address all the TODOs to make the tests pass! // Address all the TODOs to make the tests pass!
// I AM NOT DONE
#[derive(Debug)]
enum Message { enum Message {
// TODO: implement the message variant types based on their usage below Quit,
Echo(String),// a tuple with one elem
Move{x: u8, y: u8},
ChangeColor(u8, u8, u8),
} }
struct Point { struct Point {
@ -37,6 +40,12 @@ impl State {
fn process(&mut self, message: Message) { fn process(&mut self, message: Message) {
// TODO: create a match expression to process the different message variants // TODO: create a match expression to process the different message variants
match message {
Message::Quit => {self.quit()}
Message::Move{x, y} => {self.move_position(Point{x: x, y : y})}
Message::Echo(string) => {self.echo(string)}
Message::ChangeColor(r,g,b) => {self.change_color((r,g,b))}
}
} }
} }

View File

@ -7,7 +7,6 @@
// we expect to get when we call `times_two` with a negative number. // we expect to get when we call `times_two` with a negative number.
// No hints, you can do this :) // No hints, you can do this :)
// I AM NOT DONE
pub fn times_two(num: i32) -> i32 { pub fn times_two(num: i32) -> i32 {
num * 2 num * 2
@ -19,11 +18,11 @@ mod tests {
#[test] #[test]
fn returns_twice_of_positive_numbers() { fn returns_twice_of_positive_numbers() {
assert_eq!(times_two(4), ???); assert_eq!(times_two(4), 8);
} }
#[test] #[test]
fn returns_twice_of_negative_numbers() { fn returns_twice_of_negative_numbers() {
// TODO write an assert for `times_two(-4)` assert_eq!(times_two(-4), -8);
} }
} }

View File

@ -6,12 +6,11 @@
// This test has a problem with it -- make the test compile! Make the test // This test has a problem with it -- make the test compile! Make the test
// pass! Make the test fail! Execute `rustlings hint tests1` for hints :) // pass! Make the test fail! Execute `rustlings hint tests1` for hints :)
// I AM NOT DONE
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
#[test] #[test]
fn you_can_assert() { fn you_can_assert() {
assert!(); assert!(!(1==9));
} }
} }

View File

@ -2,12 +2,11 @@
// This test has a problem with it -- make the test compile! Make the test // This test has a problem with it -- make the test compile! Make the test
// pass! Make the test fail! Execute `rustlings hint tests2` for hints :) // pass! Make the test fail! Execute `rustlings hint tests2` for hints :)
// I AM NOT DONE
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
#[test] #[test]
fn you_can_assert_eq() { fn you_can_assert_eq() {
assert_eq!(); assert_eq!(1,1);
} }
} }

View File

@ -4,7 +4,6 @@
// we expect to get when we call `is_even(5)`. // we expect to get when we call `is_even(5)`.
// Execute `rustlings hint tests3` for hints :) // Execute `rustlings hint tests3` for hints :)
// I AM NOT DONE
pub fn is_even(num: i32) -> bool { pub fn is_even(num: i32) -> bool {
num % 2 == 0 num % 2 == 0
@ -16,6 +15,14 @@ mod tests {
#[test] #[test]
fn is_true_when_even() { fn is_true_when_even() {
assert!(); assert!(!is_even(5));
}
#[test]
fn is_even_works() ->Result<(), String> {
if !is_even(1<<10) {
Err(String::from("1<<10"))
} else {
Ok(())
}
} }
} }