Курс във Факултета по Математика и Информатика към
СУ
Color parsing
Краен срок
03.11.2022 17:00
Точки
4
Срокът за предаване на решения е отминал
usesolution::*;
#[test]
fntest_parsing_hsv(){
letparsed=Color::from_str("hsv(300,50%,70%)");
ifletColor::HSV{hue,saturation,value}=parsed{
assert_eq!(hue,300);
assert_eq!(saturation,50);
assert_eq!(value,70);
}else{
panic!("Not the expected result");
}
}
#[test]
fntest_parsing_hex_rgb(){
letparsed=Color::from_str("#abcdef");
ifletColor::RGB{red,green,blue}=parsed{
assert_eq!(red,0xAB);
assert_eq!(green,0xCD);
assert_eq!(blue,0xEF);
}else{
panic!("Not the expected result");
}
}
На предното домашно печатахме цветове, сега ще вървим в обратната посока -- парсене. Имате низ, искате да извадите цвят. Тъй като това е предизвикателство, няма да ви дадем цялата информация, която ви трябва за да се справите, но може да намерите помощни средства в стандартната документация, примерно:
Очакваме Color::from_str("#ff00ff") да се изпарси до Color::RGB { red: 255, green: 0, blue: 255 } примерно. Очакваме Color::from_str("hsv(360,100%,50%)") да извади Color::HSV { hue: 360, saturation: 100, value: 50 }. Може да приемете, че винаги ще ви даваме валиден вход (т.е. няма нужда да мислите за error handling за момента, panic!-вайте ако се налага), няма да има интервали между запетайките, винаги шестнадесетичната репрезентация на RGB ще бъде с малки букви.
Compiling solution v0.1.0 (/tmp/d20221103-1221210-btp2wl/solution)
Finished test [unoptimized + debuginfo] target(s) in 0.84s
Running tests/solution_test.rs (target/debug/deps/solution_test-867d7317a91c4f07)
running 2 tests
test solution_test::test_parsing_hsv ... ok
test solution_test::test_parsing_hex_rgb ... ok
test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
Compiling solution v0.1.0 (/tmp/d20221103-1221210-a5mjqv/solution)
Finished test [unoptimized + debuginfo] target(s) in 0.64s
Running tests/solution_test.rs (target/debug/deps/solution_test-867d7317a91c4f07)
running 2 tests
test solution_test::test_parsing_hex_rgb ... ok
test solution_test::test_parsing_hsv ... ok
test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
Compiling solution v0.1.0 (/tmp/d20221103-1221210-1o8r2pf/solution)
Finished test [unoptimized + debuginfo] target(s) in 0.61s
Running tests/solution_test.rs (target/debug/deps/solution_test-867d7317a91c4f07)
running 2 tests
test solution_test::test_parsing_hex_rgb ... ok
test solution_test::test_parsing_hsv ... ok
test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
Compiling solution v0.1.0 (/tmp/d20221103-1221210-1qpub55/solution)
Finished test [unoptimized + debuginfo] target(s) in 0.70s
Running tests/solution_test.rs (target/debug/deps/solution_test-867d7317a91c4f07)
running 2 tests
test solution_test::test_parsing_hex_rgb ... ok
test solution_test::test_parsing_hsv ... ok
test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
Compiling solution v0.1.0 (/tmp/d20221103-1221210-1vyrdse/solution)
Finished test [unoptimized + debuginfo] target(s) in 0.57s
Running tests/solution_test.rs (target/debug/deps/solution_test-867d7317a91c4f07)
running 2 tests
test solution_test::test_parsing_hex_rgb ... ok
test solution_test::test_parsing_hsv ... ok
test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
Compiling solution v0.1.0 (/tmp/d20221103-1221210-8w6gh3/solution)
Finished test [unoptimized + debuginfo] target(s) in 0.64s
Running tests/solution_test.rs (target/debug/deps/solution_test-867d7317a91c4f07)
running 2 tests
test solution_test::test_parsing_hsv ... ok
test solution_test::test_parsing_hex_rgb ... ok
test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
Compiling solution v0.1.0 (/tmp/d20221103-1221210-6wc28z/solution)
Finished test [unoptimized + debuginfo] target(s) in 0.56s
Running tests/solution_test.rs (target/debug/deps/solution_test-867d7317a91c4f07)
running 2 tests
test solution_test::test_parsing_hex_rgb ... ok
test solution_test::test_parsing_hsv ... ok
test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
Compiling solution v0.1.0 (/tmp/d20221103-1221210-17z1bln/solution)
Finished test [unoptimized + debuginfo] target(s) in 0.69s
Running tests/solution_test.rs (target/debug/deps/solution_test-867d7317a91c4f07)
running 2 tests
test solution_test::test_parsing_hex_rgb ... ok
test solution_test::test_parsing_hsv ... ok
test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
Compiling solution v0.1.0 (/tmp/d20221103-1221210-18u23ku/solution)
Finished test [unoptimized + debuginfo] target(s) in 0.62s
Running tests/solution_test.rs (target/debug/deps/solution_test-867d7317a91c4f07)
running 2 tests
test solution_test::test_parsing_hex_rgb ... ok
test solution_test::test_parsing_hsv ... ok
test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
_=>panic!("Cannot unwrap non-hsv type to (u16, u8, u8)")
}
}
}
#[cfg(test)]
modtests{
usecrate::Color;
#[test]
fnbasic_rgb_test(){
letres=Color::from_str("#000000");
lett=res.unwrap_rgb();
assert_eq!(t,(0,0,0));
}
#[test]
fnbasic_hsv_test(){
letres=Color::from_str("hsv(10,10%,10%)");
lett=res.unwrap_hsv();
assert_eq!(t,(10,10,10));
}
#[test]
fnhard_rgb_test(){
letres=Color::from_str("#198235");
lett=res.unwrap_rgb();
assert_eq!(t,(25,130,53));
}
#[test]
fnhard_hsv_test(){
letres=Color::from_str("hsv(100,100%,99%)");
lett=res.unwrap_hsv();
assert_eq!(t,(100,100,99));
}
#[test]
#[should_panic(expected="Invalid input")]
fnpanic_on_invalid_input(){
let_=Color::from_str("!(Tests are fun)");
}
#[test]
#[should_panic(expected="called `Result::unwrap()` on an `Err` value: ParseIntError { kind: PosOverflow }")]
fnpanic_on_overflow_input(){
let_=Color::from_str("hsv(400,400%,400%)");
}
#[test]
fnstolen_tests(){
letrgb=Color::from_str("#ff00ff");
lethsv=Color::from_str("hsv(360,100%,50%)");
letpair_rgb=rgb.unwrap_rgb();
letpair_hsv=hsv.unwrap_hsv();
assert_eq!(pair_hsv,(360,100,50));
assert_eq!(pair_rgb,(255,0,255));
}
}
Compiling solution v0.1.0 (/tmp/d20221103-1221210-1k02pvr/solution)
Finished test [unoptimized + debuginfo] target(s) in 0.56s
Running tests/solution_test.rs (target/debug/deps/solution_test-867d7317a91c4f07)
running 2 tests
test solution_test::test_parsing_hex_rgb ... ok
test solution_test::test_parsing_hsv ... ok
test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
Compiling solution v0.1.0 (/tmp/d20221103-1221210-15r4fug/solution)
Finished test [unoptimized + debuginfo] target(s) in 0.64s
Running tests/solution_test.rs (target/debug/deps/solution_test-867d7317a91c4f07)
running 2 tests
test solution_test::test_parsing_hex_rgb ... ok
test solution_test::test_parsing_hsv ... ok
test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
Compiling solution v0.1.0 (/tmp/d20221103-1221210-czkeze/solution)
Finished test [unoptimized + debuginfo] target(s) in 0.68s
Running tests/solution_test.rs (target/debug/deps/solution_test-867d7317a91c4f07)
running 2 tests
test solution_test::test_parsing_hex_rgb ... ok
test solution_test::test_parsing_hsv ... ok
test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
Compiling solution v0.1.0 (/tmp/d20221103-1221210-1oehwkn/solution)
Finished test [unoptimized + debuginfo] target(s) in 0.67s
Running tests/solution_test.rs (target/debug/deps/solution_test-867d7317a91c4f07)
running 2 tests
test solution_test::test_parsing_hex_rgb ... ok
test solution_test::test_parsing_hsv ... ok
test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
Compiling solution v0.1.0 (/tmp/d20221103-1221210-1mg66t4/solution)
Finished test [unoptimized + debuginfo] target(s) in 0.63s
Running tests/solution_test.rs (target/debug/deps/solution_test-867d7317a91c4f07)
running 2 tests
test solution_test::test_parsing_hex_rgb ... ok
test solution_test::test_parsing_hsv ... ok
test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
_=>panic!("Not found converting between given input: &str and Color !"),
};
res
}
}
pubfnadd(left:usize,right:usize)->usize{
left+right
}
#[cfg(test)]
modtests{
usesuper::*;
#[test]
fnit_works(){
letresult=add(2,2);
assert_eq!(result,4);
}
#[test]
fnbasic_test(){
leta=Color::from_str("#ff0010");
letb=match&a{
Color::RGB{red,green,blue}=>(*red,*green,*blue),
_=>(0,0,0),
};
assert_eq!(b,(255,0,16));
leta=Color::from_str("hsv(360,100%,50%)");
letb=match&a{
Color::HSV{
hue,
saturation,
value,
}=>(*hue,*saturation,*value),
_=>(0,0,0),
};
assert_eq!(b,(360,100,50));
}
#[test]
fnedge_cases_test(){
leta=Color::from_str("#000000");
letb=match&a{
Color::RGB{red,green,blue}=>(*red,*green,*blue),
_=>(1,1,1),
};
assert_eq!(b,(0,0,0));
leta=Color::from_str("#ffffff");
letb=match&a{
Color::RGB{red,green,blue}=>(*red,*green,*blue),
_=>(0,0,0),
};
assert_eq!(b,(255,255,255));
leta=Color::from_str("hsv(0,0%,0%)");
letb=match&a{
Color::HSV{
hue,
saturation,
value,
}=>(*hue,*saturation,*value),
_=>(1,1,1),
};
assert_eq!(b,(0,0,0));
leta=Color::from_str("hsv(360,100%,100%)");
letb=match&a{
Color::HSV{
hue,
saturation,
value,
}=>(*hue,*saturation,*value),
_=>(0,0,0),
};
assert_eq!(b,(360,100,100));
}
#[test]
fnrandom_test(){
leta=Color::from_str("hsv(261,69%,43%)");
letb=match&a{
Color::HSV{
hue,
saturation,
value,
}=>(*hue,*saturation,*value),
_=>(0,0,0),
};
assert_eq!(b,(261,69,43));
leta=Color::from_str("#597cfd");
letb=match&a{
Color::RGB{red,green,blue}=>(*red,*green,*blue),
_=>(0,0,0),
};
assert_eq!(b,(89,124,253));
}
#[test]
fnjust_to_know_test(){
//let a = Color::from_str("hsv(360,256%,0%)"); //panic /w mes: "number too large to fit in target type"
//let b = Color::from_str("sv(360,255%,0%)"); //panic /w mes: "Not found converting between given input: &str and Color ! "
//let c = Color::from_str("hsv(-360,255%,0%)"); //panic /w mes: "invalid digit found in string "
assert_eq!(1,1);
}
}
Compiling solution v0.1.0 (/tmp/d20221103-1221210-1ypdw7w/solution)
warning: unused import: `process::Output`
--> src/lib.rs:1:11
|
1 | use std::{process::Output, str::FromStr};
| ^^^^^^^^^^^^^^^
|
= note: `#[warn(unused_imports)]` on by default
warning: variable does not need to be mutable
--> src/lib.rs:44:21
|
44 | let mut t = input
| ----^
| |
| help: remove this `mut`
|
= note: `#[warn(unused_mut)]` on by default
warning: `solution` (lib) generated 2 warnings
Finished test [unoptimized + debuginfo] target(s) in 0.64s
Running tests/solution_test.rs (target/debug/deps/solution_test-867d7317a91c4f07)
running 2 tests
test solution_test::test_parsing_hex_rgb ... ok
test solution_test::test_parsing_hsv ... ok
test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
Compiling solution v0.1.0 (/tmp/d20221103-1221210-1klpg4t/solution)
Finished test [unoptimized + debuginfo] target(s) in 0.64s
Running tests/solution_test.rs (target/debug/deps/solution_test-867d7317a91c4f07)
running 2 tests
test solution_test::test_parsing_hex_rgb ... ok
test solution_test::test_parsing_hsv ... ok
test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
Compiling solution v0.1.0 (/tmp/d20221103-1221210-x8mbhe/solution)
warning: unused variable: `coma1`
--> src/lib.rs:81:18
|
81 | let (coma1 , data_without_hue) = &data_without_hue_and_coma.split_at(1);
| ^^^^^ help: if this is intentional, prefix it with an underscore: `_coma1`
|
= note: `#[warn(unused_variables)]` on by default
warning: unused variable: `coma2`
--> src/lib.rs:86:18
|
86 | let (coma2, data_without_saturation) = &data_without_saturation_and_coma.split_at(2);
| ^^^^^ help: if this is intentional, prefix it with an underscore: `_coma2`
warning: `solution` (lib) generated 2 warnings
Finished test [unoptimized + debuginfo] target(s) in 0.64s
Running tests/solution_test.rs (target/debug/deps/solution_test-867d7317a91c4f07)
running 2 tests
test solution_test::test_parsing_hex_rgb ... ok
test solution_test::test_parsing_hsv ... ok
test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
panic!("Cannot find a conversion from \"{}\" to Color",input)
}
}
}
Compiling solution v0.1.0 (/tmp/d20221103-1221210-60dsis/solution)
Finished test [unoptimized + debuginfo] target(s) in 0.62s
Running tests/solution_test.rs (target/debug/deps/solution_test-867d7317a91c4f07)
running 2 tests
test solution_test::test_parsing_hex_rgb ... ok
test solution_test::test_parsing_hsv ... ok
test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
Compiling solution v0.1.0 (/tmp/d20221103-1221210-1mba4t2/solution)
Finished test [unoptimized + debuginfo] target(s) in 0.62s
Running tests/solution_test.rs (target/debug/deps/solution_test-867d7317a91c4f07)
running 2 tests
test solution_test::test_parsing_hex_rgb ... ok
test solution_test::test_parsing_hsv ... ok
test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
//казано ни е, че входа е коректен, но все пак да си тествам default case-а на match в from_str()
fntest11(){
Color::from_str("🙂");
}
Compiling solution v0.1.0 (/tmp/d20221103-1221210-1wy61q6/solution)
Finished test [unoptimized + debuginfo] target(s) in 0.64s
Running tests/solution_test.rs (target/debug/deps/solution_test-867d7317a91c4f07)
running 2 tests
test solution_test::test_parsing_hex_rgb ... ok
test solution_test::test_parsing_hsv ... ok
test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s