Browse Source

Improve exercise `changes`

pull/695/head
Augusto 4 years ago
parent
commit
d3ad5b3dc7
  1. 61
      rust/tests/changes_test/src/main.rs
  2. 41
      subjects/changes/README.md

61
rust/tests/changes_test/src/main.rs

@ -1,41 +1,54 @@
/*
## error types
## changes
### Instructions
Make this code compile
Imagine you are working in a software to control smart lights in a
house. You have access to an array of all the lights in a house.
Define the associated function `new` to the data structure `Light`
which creates a new light with the alias passed in the arguments and
a brightness of 0.
Define the function `change_brightness` that receives a Vec of lights,
an alias and a u8 value and sets the u8 value as the new brightness of the light
identified by the alias in the Vec of lights.
*/
use changes::*;
fn main() {
let mut a = String::from("Hello");
println!("the value of `a` before changing {}", a);
add_excitement(&mut a);
println!("The value of `a` after changing {}", a);
// bedroom
let mut lights = vec![
Light::new("living_room"),
Light::new("bedroom"),
Light::new("rest_room"),
];
println!("brightness = {}", lights[0].brightness);
change_brightness(&mut lights, "living_room", 200);
println!("new brightness = {}", lights[0].brightness);
}
// fn add_excitement(s: &mut String) {
// s.push_str("!");
// }
#[test]
fn test_ascii() {
let mut expected = "hello".to_string();
add_excitement(&mut expected);
assert_eq!("hello!", &expected);
let mut expected = "go on".to_string();
add_excitement(&mut expected);
assert_eq!("go on!", &expected);
fn test_unexistente_alias() {
let mut lights = Vec::new();
for i in 0..5 {
let alias = format!("light-{}", i);
lights.push(Light::new(&alias));
}
let copy = lights.clone();
change_brightness(&mut lights, "light-6", 100);
assert_eq!(copy, lights);
}
#[test]
fn test_unicode() {
let mut expected = "↕".to_string();
add_excitement(&mut expected);
assert_eq!(expected, "↕!");
fn test_alias() {
let mut lights = Vec::new();
for i in 0..5 {
let alias = format!("light-{}", i);
lights.push(Light::new(&alias));
}
let alias = "light-3";
change_brightness(&mut lights, alias, 100);
assert_eq!(lights[3].brightness, 100);
}

41
subjects/changes/README.md

@ -2,12 +2,27 @@
### Instructions
Define the function `add_excitement` that add the exclamation point at the end of every sentenced that is passed to the function without returning anything but just modifying the input.
Imagine you are working in a software to control smart lights in a house. You have access to an array of all the lights in a house.
### Expected Functions
Define the associated function `new` to the data structure `Light` which creates a new light with the alias passed in the arguments and a brightness of 0.
Define the function `change_brightness` that receives a Vec of lights, an alias and a u8 value and sets the u8 value as the new brightness of the light identified by the alias in the Vec of lights.
### Expected Functions and Structure
```rust
fn add_excitement(s: &) {
#[derive(Debug, Eq, PartialEq, Clone)]
pub struct Light {
pub alias: String,
pub brightness: u8,
}
impl Light {
pub fn new(alias: &str) -> Self {
}
}
pub fn change_brightness(lights: &mut Vec<Light>, alias: &str, value: u8) {
}
```
@ -17,13 +32,15 @@ Here is an incomplete program to test your function
```rust
fn main() {
let mut a = String::from("Hello");
println!("the value of `a` before changing {}", a);
add_excitement(a);
println!("The value of `a` after changing {}", a);
// bedroom
let mut lights = vec![
Light::new("living_room"),
Light::new("bedroom"),
Light::new("rest_room"),
];
println!("brightness = {}", lights[0].brightness);
change_brightness(&mut lights, "living_room", 200);
println!("new brightness = {}", lights[0].brightness);
}
```
@ -31,7 +48,7 @@ And its expected output
```console
student@ubuntu:~/[[ROOT]]/test$ cargo run
the value of `a` before changing Hello
The value of `a` after changing Hello!
brightness = 0
new brightness = 200
student@ubuntu:~/[[ROOT]]/test$
```

Loading…
Cancel
Save