Решение на Dungeons and Compilers от Константин Константинов

Обратно към всички решения

Към профила на Константин Константинов

Резултати

  • 8 точки от тестове
  • 0 бонус точки
  • 8 точки общо
  • 6 успешни тест(а)
  • 9 неуспешни тест(а)

Код

use std::{collections::HashMap, io::BufRead};
#[derive(Debug)]
pub enum Errors {
DuplicateRoom(String),
UnknownRoom(String),
IoError(std::io::Error),
LineParseError { line_number: usize },
DirectionParseError(String),
}
#[derive(Clone, Copy, Eq, PartialEq, Hash)]
pub enum Direction {
North,
South,
East,
West,
}
impl Direction {
pub fn reverse_direction(&self) -> Direction {
match self {
Self::North => Self::South,
Self::South => Self::North,
Self::East => Self::West,
Self::West => Self::East
}
}
}
pub struct Room {
pub name: String,
}
pub struct Dungeon {
rooms: HashMap<String, Room>,
links: HashMap<(String, Direction), String>,
}
impl Dungeon {
pub fn new() -> Self {
Dungeon {
rooms : HashMap::new(),
links : HashMap::new()
}
}
pub fn add_room(&mut self, name: &str) -> Result<(), Errors> {
match self.rooms.contains_key(name) {
true => Err(Errors::DuplicateRoom(name.to_string())),
false => {
self.rooms.insert(name.to_string(), Room {name : name.to_string()});
Ok(())
}
}
}
pub fn get_room(&self, room_name: &str) -> Result<&Room, Errors> {
match self.rooms.get(room_name) {
None => Err(Errors::UnknownRoom(room_name.to_string())),
Some(room) => Ok(room)
}
}
pub fn set_link(&mut self, room_name: &str, direction: Direction, other_room_name: &str) -> Result<(), Errors> {
self.get_room(room_name)?;
self.get_room(other_room_name)?;
self.links.insert((room_name.to_string(), direction),other_room_name.to_string());
self.links.insert((other_room_name.to_string(), direction.reverse_direction()),room_name.to_string());
Ok(())
}
pub fn get_next_room(&self, room_name: &str, direction: Direction) -> Result<Option<&Room>, Errors> {
self.get_room(room_name)?;
match self.links.get(&(room_name.to_string(), direction)){
Some(room) => Ok(Some(self.get_room(room)?)),
None => Ok(None)
}
}
pub fn from_reader<B: BufRead>(reader: B) -> Result<Self, Errors> {
todo!()
}
pub fn find_path(&self, start_room_name: &str, end_room_name: &str) -> Result<Option<Vec<&Room>>, Errors> {
todo!()
}
}
#[cfg(test)]
mod tests {
use crate::{Direction, Dungeon};
#[test]
fn test1() {
let mut dungeon = Dungeon::new();
dungeon.add_room("Entrance").unwrap();
dungeon.add_room("Hallway").unwrap();
dungeon.set_link("Entrance", Direction::East, "Hallway").unwrap();
assert_eq!(dungeon.get_room("Hallway").unwrap().name, "Hallway");
assert_eq!(dungeon.get_next_room("Hallway", Direction::West).unwrap().unwrap().name, "Entrance");
}
#[test]
fn test2()
{
let mut dungeon = Dungeon::new();
dungeon.add_room("Entrance").unwrap();
dungeon.add_room("Hallway").unwrap();
dungeon.add_room("Magic Lab").unwrap();
dungeon.set_link("Entrance", Direction::East, "Hallway").unwrap();
dungeon.set_link("Hallway", Direction::West, "Magic Lab").unwrap();
assert_eq!(dungeon.get_next_room("Entrance", Direction::East).unwrap().unwrap().name, "Hallway");
assert_eq!(dungeon.get_next_room("Hallway", Direction::West).unwrap().unwrap().name, "Magic Lab");
}
}

Лог от изпълнението

Compiling solution v0.1.0 (/tmp/d20220116-3533338-otof74/solution)
warning: unused variable: `reader`
  --> src/lib.rs:79:36
   |
79 |     pub fn from_reader<B: BufRead>(reader: B) -> Result<Self, Errors> {
   |                                    ^^^^^^ help: if this is intentional, prefix it with an underscore: `_reader`
   |
   = note: `#[warn(unused_variables)]` on by default

warning: unused variable: `start_room_name`
  --> src/lib.rs:83:29
   |
83 |     pub fn find_path(&self, start_room_name: &str, end_room_name: &str) -> Result<Option<Vec<&Room>>, Errors> {
   |                             ^^^^^^^^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_start_room_name`

warning: unused variable: `end_room_name`
  --> src/lib.rs:83:52
   |
83 |     pub fn find_path(&self, start_room_name: &str, end_room_name: &str) -> Result<Option<Vec<&Room>>, Errors> {
   |                                                    ^^^^^^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_end_room_name`

warning: `solution` (lib) generated 3 warnings
    Finished test [unoptimized + debuginfo] target(s) in 3.38s
     Running tests/solution_test.rs (target/debug/deps/solution_test-2e292b23ac75572c)

running 15 tests
test solution_test::test_adding_rooms_1 ... ok
test solution_test::test_adding_rooms_2 ... ok
test solution_test::test_cyrillic_room_names ... ok
test solution_test::test_finding_a_direct_path ... FAILED
test solution_test::test_finding_a_reflexive_path ... FAILED
test solution_test::test_finding_an_indirect_path ... FAILED
test solution_test::test_finding_no_path ... FAILED
test solution_test::test_invalid_parsing ... FAILED
test solution_test::test_io_error ... FAILED
test solution_test::test_overwriting_a_room_link ... ok
test solution_test::test_parsing_cyrillic_rooms ... FAILED
test solution_test::test_parsing_no_rooms_or_links ... FAILED
test solution_test::test_parsing_rooms ... FAILED
test solution_test::test_room_errors ... ok
test solution_test::test_room_links ... ok

failures:

---- solution_test::test_finding_a_direct_path stdout ----
thread '<unnamed>' panicked at 'not yet implemented', src/lib.rs:84:9
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
thread 'main' panicked at 'called `Option::unwrap()` on a `None` value', tests/solution_test.rs:306:5

---- solution_test::test_finding_a_reflexive_path stdout ----
thread '<unnamed>' panicked at 'not yet implemented', src/lib.rs:84:9
thread 'main' panicked at 'called `Option::unwrap()` on a `None` value', tests/solution_test.rs:354:5

---- solution_test::test_finding_an_indirect_path stdout ----
thread '<unnamed>' panicked at 'not yet implemented', src/lib.rs:84:9
thread 'main' panicked at 'called `Option::unwrap()` on a `None` value', tests/solution_test.rs:320:5

---- solution_test::test_finding_no_path stdout ----
thread '<unnamed>' panicked at 'not yet implemented', src/lib.rs:84:9
thread 'main' panicked at 'called `Option::unwrap()` on a `None` value', tests/solution_test.rs:372:5

---- solution_test::test_invalid_parsing stdout ----
thread 'main' panicked at 'not yet implemented', /tmp/d20220116-3533338-otof74/solution/src/lib.rs:80:9

---- solution_test::test_io_error stdout ----
thread '<unnamed>' panicked at 'not yet implemented', /tmp/d20220116-3533338-otof74/solution/src/lib.rs:80:9
thread 'main' panicked at 'called `Option::unwrap()` on a `None` value', tests/solution_test.rs:194:5

---- solution_test::test_parsing_cyrillic_rooms stdout ----
thread 'main' panicked at 'not yet implemented', /tmp/d20220116-3533338-otof74/solution/src/lib.rs:80:9

---- solution_test::test_parsing_no_rooms_or_links stdout ----
thread 'main' panicked at 'not yet implemented', /tmp/d20220116-3533338-otof74/solution/src/lib.rs:80:9

---- solution_test::test_parsing_rooms stdout ----
thread 'main' panicked at 'not yet implemented', /tmp/d20220116-3533338-otof74/solution/src/lib.rs:80:9


failures:
    solution_test::test_finding_a_direct_path
    solution_test::test_finding_a_reflexive_path
    solution_test::test_finding_an_indirect_path
    solution_test::test_finding_no_path
    solution_test::test_invalid_parsing
    solution_test::test_io_error
    solution_test::test_parsing_cyrillic_rooms
    solution_test::test_parsing_no_rooms_or_links
    solution_test::test_parsing_rooms

test result: FAILED. 6 passed; 9 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

error: test failed, to rerun pass '--test solution_test'

История (2 версии и 1 коментар)

Константин качи първо решение на 11.01.2022 14:15 (преди над 3 години)

Константин качи решение на 11.01.2022 15:49 (преди над 3 години)

-use std::{collections::HashMap};
+use std::{collections::HashMap, io::BufRead};
#[derive(Debug)]
pub enum Errors {
DuplicateRoom(String),
UnknownRoom(String),
IoError(std::io::Error),
LineParseError { line_number: usize },
DirectionParseError(String),
}
#[derive(Clone, Copy, Eq, PartialEq, Hash)]
pub enum Direction {
North,
South,
East,
West,
}
impl Direction {
pub fn reverse_direction(&self) -> Direction {
match self {
Self::North => Self::South,
Self::South => Self::North,
Self::East => Self::West,
Self::West => Self::East
}
}
}
pub struct Room {
pub name: String,
}
pub struct Dungeon {
rooms: HashMap<String, Room>,
links: HashMap<(String, Direction), String>,
}
impl Dungeon {
pub fn new() -> Self {
Dungeon {
rooms : HashMap::new(),
links : HashMap::new()
}
}
pub fn add_room(&mut self, name: &str) -> Result<(), Errors> {
match self.rooms.contains_key(name) {
true => Err(Errors::DuplicateRoom(name.to_string())),
false => {
self.rooms.insert(name.to_string(), Room {name : name.to_string()});
Ok(())
}
}
}
pub fn get_room(&self, room_name: &str) -> Result<&Room, Errors> {
match self.rooms.get(room_name) {
None => Err(Errors::UnknownRoom(room_name.to_string())),
Some(room) => Ok(room)
}
}
pub fn set_link(&mut self, room_name: &str, direction: Direction, other_room_name: &str) -> Result<(), Errors> {
self.get_room(room_name)?;
self.get_room(other_room_name)?;
self.links.insert((room_name.to_string(), direction),other_room_name.to_string());
self.links.insert((other_room_name.to_string(), direction.reverse_direction()),room_name.to_string());
Ok(())
}
pub fn get_next_room(&self, room_name: &str, direction: Direction) -> Result<Option<&Room>, Errors> {
self.get_room(room_name)?;
match self.links.get(&(room_name.to_string(), direction)){
Some(room) => Ok(Some(self.get_room(room)?)),
None => Ok(None)
}
}
+ pub fn from_reader<B: BufRead>(reader: B) -> Result<Self, Errors> {
+ todo!()
+ }
+
+ pub fn find_path(&self, start_room_name: &str, end_room_name: &str) -> Result<Option<Vec<&Room>>, Errors> {
+ todo!()
+ }
+
}
#[cfg(test)]
mod tests {
use crate::{Direction, Dungeon};
#[test]
fn test1() {
let mut dungeon = Dungeon::new();
dungeon.add_room("Entrance").unwrap();
dungeon.add_room("Hallway").unwrap();
dungeon.set_link("Entrance", Direction::East, "Hallway").unwrap();
assert_eq!(dungeon.get_room("Hallway").unwrap().name, "Hallway");
assert_eq!(dungeon.get_next_room("Hallway", Direction::West).unwrap().unwrap().name, "Entrance");
}
#[test]
fn test2()
{
let mut dungeon = Dungeon::new();
dungeon.add_room("Entrance").unwrap();
dungeon.add_room("Hallway").unwrap();
dungeon.add_room("Magic Lab").unwrap();
dungeon.set_link("Entrance", Direction::East, "Hallway").unwrap();
dungeon.set_link("Hallway", Direction::West, "Magic Lab").unwrap();
assert_eq!(dungeon.get_next_room("Entrance", Direction::East).unwrap().unwrap().name, "Hallway");
assert_eq!(dungeon.get_next_room("Hallway", Direction::West).unwrap().unwrap().name, "Magic Lab");
}
}