Added trait Rateable.
This commit is contained in:
14
.gitlab-ci.yml
Normal file
14
.gitlab-ci.yml
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
image: "rust:latest"
|
||||||
|
|
||||||
|
variables:
|
||||||
|
CARGO_HOME: $CI_PROJECT_DIR/cargo
|
||||||
|
|
||||||
|
test:cargo:
|
||||||
|
script:
|
||||||
|
- rustc --version && cargo --version
|
||||||
|
- time cargo test --verbose --jobs 1 --release
|
||||||
|
|
||||||
|
cache:
|
||||||
|
paths:
|
||||||
|
- target/
|
||||||
|
- cargo/
|
||||||
28
src/lib.rs
28
src/lib.rs
@@ -10,6 +10,24 @@ fn expected(a: f32, b: f32, n: f32) -> f32 {
|
|||||||
1.0 / (1.0 + 10.0_f32.powf((b - a) / n))
|
1.0 / (1.0 + 10.0_f32.powf((b - a) / n))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
trait Rateable {
|
||||||
|
fn rating(&mut self) -> &mut f32;
|
||||||
|
|
||||||
|
fn record_match(&mut self, other: &mut Self, score: f32) {
|
||||||
|
let r_a = *self.rating();
|
||||||
|
let r_b = *other.rating();
|
||||||
|
|
||||||
|
*self.rating() = rating(r_a, r_b, score, 400.0, 32.0);
|
||||||
|
*other.rating() = rating(r_b, r_a, 1.0 - score, 400.0, 32.0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Rateable for f32 {
|
||||||
|
fn rating(&mut self) -> &mut f32 {
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
@@ -24,4 +42,14 @@ mod tests {
|
|||||||
|
|
||||||
assert_eq!(r, 1600.0);
|
assert_eq!(r, 1600.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_rateable() {
|
||||||
|
let mut a: f32 = 1600.0;
|
||||||
|
let mut b: f32 = 1600.0;
|
||||||
|
|
||||||
|
a.record_match(&mut b, 1.0);
|
||||||
|
|
||||||
|
assert_eq!(a, 1616.0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user