Skip to content

Commit 3926e25

Browse files
committed
added lesson practices
1 parent 65f146e commit 3926e25

7 files changed

Lines changed: 199 additions & 53 deletions

File tree

drone-lab/src/controller/flight_controller.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
1-
use crate::model::Drone;
2-
1+
use crate::model::{Drone, Location};
32
pub struct FlightController;
43

54
impl FlightController {
6-
pub fn check_status(drone: &Drone) -> DroneStatus {
5+
pub fn check_status<'a>(drone: &'a Drone<'a>) -> DroneStatus<'a> {
76
if !drone.is_alive {
87
return DroneStatus::Offline;
98
}
109
if drone.energy_level < 30.0 {
1110
return DroneStatus::LowBattery(drone.energy_level);
1211
}
13-
if drone.location.x > 800.0 || drone.location.y < 800.0 || drone.location.z < 800.0 {
14-
return DroneStatus::LowBattery(drone.location.x);
12+
if drone.location.x > 800.0 || drone.location.y > 800.0 || drone.location.z > 800.0 {
13+
return DroneStatus::OutOffRange(drone.location);
1514
}
1615

1716
DroneStatus::Fine
1817
}
1918
}
20-
21-
pub enum DroneStatus {
22-
OutOffRange,
19+
#[derive(Debug, PartialEq)]
20+
pub enum DroneStatus<'a> {
21+
OutOffRange(Location<'a>),
2322
Offline,
2423
LowBattery(f32),
2524
Fine,

drone-lab/src/controller/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ mod drone_repository;
22
mod flight_controller;
33

44
pub use drone_repository::*;
5+
pub use flight_controller::*;

drone-lab/src/model/location.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
#[derive(Debug, Copy, Clone)]
1+
#[derive(Debug, Copy, Clone, PartialEq)]
22
pub struct Location<'a> {
33
pub caption: &'a str, // 2nci Kat Güney Kanadı
44
pub x: f32,
55
pub y: f32,
66
pub z: f32,
7-
}
7+
}

drone-lab/src/tests.rs renamed to drone-lab/src/tests/drone_repository_tests.rs

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,43 @@
1-
#[cfg(test)]
2-
mod tests {
3-
4-
use crate::controller::*;
5-
use crate::model::*;
6-
7-
#[test]
8-
fn should_drone_save_returns_ok_test() {
9-
let temperature_drone = Drone {
10-
id: 1145,
11-
model: "UHD Drone TX-19",
12-
energy_level: 100.0,
13-
location: Location {
14-
x: 0.0,
15-
y: 0.0,
16-
z: 0.0,
17-
caption: "Kuzey Blogu",
18-
},
19-
is_alive: true,
20-
};
21-
let actual = DroneRepository::save(temperature_drone);
22-
assert!(actual.is_ok());
23-
}
24-
25-
#[test]
26-
fn should_wrong_drone_id_returns_validation_error() {
27-
let discovery_drone = Drone {
28-
id: 0,
29-
model: "DIS Drone TX-258",
30-
energy_level: 9.0,
31-
location: Location {
32-
x: 450.0,
33-
y: 500.0,
34-
z: 801.0,
35-
caption: "Sahra II Bölgesi",
36-
},
37-
is_alive: true,
38-
};
39-
let actual = DroneRepository::save(discovery_drone);
40-
assert!(actual.is_err());
41-
assert_eq!(actual.err(), Some(SaveValidationError::InvalidDroneId));
42-
}
43-
}
1+
#[cfg(test)]
2+
mod tests {
3+
4+
use crate::controller::*;
5+
use crate::model::*;
6+
7+
#[test]
8+
fn should_drone_save_returns_ok_test() {
9+
let temperature_drone = Drone {
10+
id: 1145,
11+
model: "UHD Drone TX-19",
12+
energy_level: 100.0,
13+
location: Location {
14+
x: 0.0,
15+
y: 0.0,
16+
z: 0.0,
17+
caption: "Kuzey Blogu",
18+
},
19+
is_alive: true,
20+
};
21+
let actual = DroneRepository::save(temperature_drone);
22+
assert!(actual.is_ok());
23+
}
24+
25+
#[test]
26+
fn should_wrong_drone_id_returns_validation_error() {
27+
let discovery_drone = Drone {
28+
id: 0,
29+
model: "DIS Drone TX-258",
30+
energy_level: 9.0,
31+
location: Location {
32+
x: 450.0,
33+
y: 500.0,
34+
z: 801.0,
35+
caption: "Sahra II Bölgesi",
36+
},
37+
is_alive: true,
38+
};
39+
let actual = DroneRepository::save(discovery_drone);
40+
assert!(actual.is_err());
41+
assert_eq!(actual.err(), Some(SaveValidationError::InvalidDroneId));
42+
}
43+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#[cfg(test)]
2+
mod tests {
3+
4+
use crate::controller::*;
5+
use crate::model::*;
6+
7+
#[test]
8+
fn should_return_offline_test() {
9+
let discovery_drone = Drone {
10+
id: 0,
11+
model: "DIS Drone TX-258",
12+
energy_level: 9.0,
13+
location: Location {
14+
x: 450.0,
15+
y: 500.0,
16+
z: 801.0,
17+
caption: "Sahra II Bölgesi",
18+
},
19+
is_alive: false,
20+
};
21+
let actual = FlightController::check_status(&discovery_drone);
22+
assert_eq!(actual, DroneStatus::Offline);
23+
}
24+
25+
#[test]
26+
fn should_return_low_battery_test() {
27+
let energy_level = 25.0;
28+
let discovery_drone = Drone {
29+
id: 0,
30+
model: "DIS Drone TX-258",
31+
energy_level,
32+
location: Location {
33+
x: 450.0,
34+
y: 500.0,
35+
z: 801.0,
36+
caption: "Sahra II Bölgesi",
37+
},
38+
is_alive: true,
39+
};
40+
let actual = FlightController::check_status(&discovery_drone);
41+
assert_eq!(actual, DroneStatus::LowBattery(energy_level));
42+
}
43+
44+
#[test]
45+
fn should_return_out_off_range_test() {
46+
let location = Location {
47+
x: 450.0,
48+
y: 500.0,
49+
z: 801.0,
50+
caption: "Sahra II Bölgesi",
51+
};
52+
let discovery_drone = Drone {
53+
id: 0,
54+
model: "DIS Drone TX-258",
55+
energy_level: 55.0,
56+
location,
57+
is_alive: true,
58+
};
59+
let actual = FlightController::check_status(&discovery_drone);
60+
assert_eq!(actual, DroneStatus::OutOffRange(location));
61+
}
62+
63+
#[test]
64+
fn should_return_fine_test() {
65+
let location = Location {
66+
x: 450.0,
67+
y: 500.0,
68+
z: 750.0,
69+
caption: "Sahra II Bölgesi",
70+
};
71+
let discovery_drone = Drone {
72+
id: 19,
73+
model: "DIS Drone TX-258",
74+
energy_level: 80.0,
75+
location,
76+
is_alive: true,
77+
};
78+
let actual = FlightController::check_status(&discovery_drone);
79+
assert_eq!(actual, DroneStatus::Fine);
80+
}
81+
}

drone-lab/src/tests/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
mod drone_repository_tests;
2+
mod flight_controller_tests;
3+
mod simulation_loader_tests;
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#[cfg(test)]
2+
mod tests {
3+
use crate::model::{Drone, Location};
4+
5+
struct Simulation<'a> {
6+
drones: Vec<Drone<'a>>,
7+
}
8+
impl<'a> Simulation<'a> {
9+
fn new() -> Self {
10+
Simulation { drones: Vec::new() }
11+
}
12+
fn load(&mut self, drone_count: i32) -> bool {
13+
for i in 0..drone_count {
14+
self.drones.push(Drone {
15+
id: 1,
16+
energy_level: 100.0,
17+
model: "MODEL X",
18+
is_alive: true,
19+
location: Location {
20+
x: 0.0,
21+
y: 0.0,
22+
z: 0.0,
23+
caption: "",
24+
},
25+
})
26+
}
27+
true
28+
}
29+
fn get_count(&self) -> usize {
30+
self.drones.len()
31+
}
32+
fn get_random(&self) -> Drone {
33+
self.drones[0]
34+
}
35+
}
36+
37+
#[test]
38+
fn should_allocate_one_drone_test() {
39+
let mut simulation = Simulation::new();
40+
let actual = simulation.load(1);
41+
let expected = true;
42+
assert_eq!(actual, expected);
43+
}
44+
45+
#[test]
46+
fn should_allocate_ten_drone_returns_ten_test() {
47+
let mut simulation = Simulation::new();
48+
let _ = simulation.load(10);
49+
let actual = simulation.get_count();
50+
assert_eq!(actual, 10);
51+
}
52+
53+
// #[test]
54+
// fn should_any_drone_in_any_location_test() {
55+
// let mut simulation = Simulation::new();
56+
// let _ = simulation.load(10);
57+
// let any_drone: Drone = simulation.get_random();
58+
// assert!(any_drone.location.x > 0.0);
59+
// // assert!(any_drone.location.y > 0.0);
60+
// // assert!(any_drone.location.z > 0.0);
61+
// }
62+
}

0 commit comments

Comments
 (0)