From 2efb0f568b688d78d60a3635705a95e68fec3352 Mon Sep 17 00:00:00 2001 From: Frithjof Winkelmann Date: Wed, 9 Oct 2024 12:17:07 +0200 Subject: [PATCH] More tests --- .github/workflows/develop.yml | 2 - backend-rust/Cargo.lock | 89 ++++++++++++++++++++++++++++++++- backend-rust/Cargo.toml | 1 + backend-rust/src/search.rs | 4 +- backend-rust/src/search_test.rs | 40 ++++++++++++++- 5 files changed, 129 insertions(+), 7 deletions(-) diff --git a/.github/workflows/develop.yml b/.github/workflows/develop.yml index 761f98e..899f9d6 100644 --- a/.github/workflows/develop.yml +++ b/.github/workflows/develop.yml @@ -1,5 +1,3 @@ -# Github action that runs rust clippy, unit tests, tries to compile the code and if successful, builds a docker image and pushes it to dockerhub. After that, it deploys the image to a remote server using SSH. - name: develop on: diff --git a/backend-rust/Cargo.lock b/backend-rust/Cargo.lock index 3893294..247ad9d 100644 --- a/backend-rust/Cargo.lock +++ b/backend-rust/Cargo.lock @@ -194,6 +194,7 @@ dependencies = [ "image", "ndarray", "num-traits", + "proptest", "quick-xml", "rocket", "serde", @@ -226,6 +227,21 @@ version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "383d29d513d8764dcdc42ea295d979eb99c3c9f00607b3692cf68a431f7dca72" +[[package]] +name = "bit-set" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0700ddab506f33b20a03b13996eccd309a48e5ff77d0d95926aa0210fb4e95f1" +dependencies = [ + "bit-vec", +] + +[[package]] +name = "bit-vec" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "349f9b6a179ed607305526ca489b34ad0a41aed5f7980fa90eb03160b69598fb" + [[package]] name = "bit_field" version = "0.10.2" @@ -948,7 +964,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f79afb8cbee2ef20f59ccd477a218c12a93943d075b492015ecb1bb81f8ee904" dependencies = [ "byteorder-lite", - "quick-error", + "quick-error 2.0.1", ] [[package]] @@ -1073,6 +1089,12 @@ dependencies = [ "once_cell", ] +[[package]] +name = "libm" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ec2a862134d2a7d32d7983ddcdd1c4923530833c9f2ea1a44fc5fa473989058" + [[package]] name = "linux-raw-sys" version = "0.4.14" @@ -1325,6 +1347,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" dependencies = [ "autocfg", + "libm", ] [[package]] @@ -1552,6 +1575,26 @@ dependencies = [ "syn", ] +[[package]] +name = "proptest" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b4c2511913b88df1637da85cc8d96ec8e43a3f8bb8ccb71ee1ac240d6f3df58d" +dependencies = [ + "bit-set", + "bit-vec", + "bitflags 2.6.0", + "lazy_static", + "num-traits", + "rand", + "rand_chacha", + "rand_xorshift", + "regex-syntax 0.8.4", + "rusty-fork", + "tempfile", + "unarray", +] + [[package]] name = "qoi" version = "0.4.1" @@ -1561,6 +1604,12 @@ dependencies = [ "bytemuck", ] +[[package]] +name = "quick-error" +version = "1.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0" + [[package]] name = "quick-error" version = "2.0.1" @@ -1615,6 +1664,15 @@ dependencies = [ "getrandom", ] +[[package]] +name = "rand_xorshift" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d25bf25ec5ae4a3f1b92f929810509a2f53d7dca2f50b794ff57e3face536c8f" +dependencies = [ + "rand_core", +] + [[package]] name = "rav1e" version = "0.7.1" @@ -1659,7 +1717,7 @@ dependencies = [ "avif-serialize", "imgref", "loop9", - "quick-error", + "quick-error 2.0.1", "rav1e", "rgb", ] @@ -1928,6 +1986,18 @@ version = "1.0.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "955d28af4278de8121b7ebeb796b6a45735dc01436d898801014aced2773a3d6" +[[package]] +name = "rusty-fork" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb3dcc6e454c328bb824492db107ab7c0ae8fcffe4ad210136ef014458c1bc4f" +dependencies = [ + "fnv", + "quick-error 1.2.3", + "tempfile", + "wait-timeout", +] + [[package]] name = "ryu" version = "1.0.18" @@ -2406,6 +2476,12 @@ dependencies = [ "serde", ] +[[package]] +name = "unarray" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eaea85b334db583fe3274d12b4cd1880032beab409c0d774be044d4480ab9a94" + [[package]] name = "uncased" version = "0.9.10" @@ -2463,6 +2539,15 @@ version = "0.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" +[[package]] +name = "wait-timeout" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f200f5b12eb75f8c1ed65abd4b2db8a6e1b138a20de009dacee265a2498f3f6" +dependencies = [ + "libc", +] + [[package]] name = "walkdir" version = "2.5.0" diff --git a/backend-rust/Cargo.toml b/backend-rust/Cargo.toml index 5f2e080..5b355cd 100644 --- a/backend-rust/Cargo.toml +++ b/backend-rust/Cargo.toml @@ -15,6 +15,7 @@ serde = { version = "1.0.210", features = ["derive", "serde_derive"] } [dev-dependencies] approx = "0.5.1" +proptest = "1.5.0" criterion = { version = "0.5", features = ["html_reports"] } [[bench]] diff --git a/backend-rust/src/search.rs b/backend-rust/src/search.rs index 9536994..d838b90 100644 --- a/backend-rust/src/search.rs +++ b/backend-rust/src/search.rs @@ -175,13 +175,13 @@ impl SearchState { } } -struct EffectiveGlide { +pub struct EffectiveGlide { #[allow(dead_code)] speed: f32, glide_ratio: f32, } -fn get_effective_glide_ratio( +pub fn get_effective_glide_ratio( effective_wind_angle: f32, wind_speed: f32, trim_speed: f32, diff --git a/backend-rust/src/search_test.rs b/backend-rust/src/search_test.rs index a26da02..6dcb28b 100644 --- a/backend-rust/src/search_test.rs +++ b/backend-rust/src/search_test.rs @@ -1,4 +1,42 @@ -use super::{search_from_point, SearchQuery}; +use core::f32; + +use super::{get_effective_glide_ratio, search_from_point, SearchQuery}; + +use approx::assert_relative_eq; +use proptest::{prop_assert_eq, prop_compose, proptest}; + +prop_compose! { + fn speed_wind_speed(max_speed: f32) + (speed in 10.0..max_speed, wind_speed in -max_speed..max_speed) -> (f32, f32) { + (speed, wind_speed.min(speed - 0.1)) + } +} + +proptest! { + #[test] + fn get_effective_glide_ratio_no_wind(glide_ratio in 0.05..0.5f32, speed in 10.0..50.0f32) { + let result = get_effective_glide_ratio(0.0, 0.0, speed, glide_ratio); + + prop_assert_eq!(result.speed, speed); + prop_assert_eq!(result.glide_ratio, glide_ratio); + } + + #[test] + fn get_effective_glide_ratio_wind(glide_ratio in 0.05..0.5f32, (speed, wind_speed) in speed_wind_speed(50.0)) { + let result = get_effective_glide_ratio(f32::consts::PI, wind_speed, speed, glide_ratio); + + prop_assert_eq!(result.speed, speed - wind_speed); + prop_assert_eq!(result.glide_ratio, glide_ratio/((speed - wind_speed)/speed)); + } + + #[test] + fn get_effective_glide_ratio_side_wind(glide_ratio in 0.05..0.5f32, speed in 10.0..50.0f32) { + let result = get_effective_glide_ratio(f32::consts::PI/2.0, speed/2.0f32.sqrt(), speed, glide_ratio); + + assert_relative_eq!(result.speed, speed/2.0f32.sqrt(), max_relative = 0.01); + assert_relative_eq!(result.glide_ratio, glide_ratio*2.0f32.sqrt(), max_relative = 0.01); + } +} #[test] fn test_search() {