86 lines
2.7 KiB
Rust
86 lines
2.7 KiB
Rust
use pokerfreak::game::{Choice, Decision};
|
|
use pokerfreak::json::{parse, Value};
|
|
use pokerfreak::wire::encode::encode;
|
|
|
|
fn parsed(d: &Decision) -> Vec<(String, Value)> {
|
|
match parse(&encode(d)).expect("valid json") {
|
|
Value::Object(fields) => fields,
|
|
other => panic!("{other:?}"),
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn contract_example() {
|
|
let d = Decision::new(Choice::Call, 0.72, "top pair, good price");
|
|
assert_eq!(
|
|
encode(&d),
|
|
r#"{"action":"CALL","size":null,"confidence":0.72,"reason":"top pair, good price"}"#
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn key_order_is_fixed() {
|
|
let keys: Vec<String> =
|
|
parsed(&Decision::new(Choice::Fold, 0.5, "x")).into_iter().map(|(k, _)| k).collect();
|
|
assert_eq!(keys, vec!["action", "size", "confidence", "reason"]);
|
|
}
|
|
|
|
#[test]
|
|
fn every_action_name() {
|
|
for (choice, name) in [
|
|
(Choice::Fold, "FOLD"),
|
|
(Choice::Check, "CHECK"),
|
|
(Choice::Call, "CALL"),
|
|
(Choice::Raise { to: 3.0 }, "RAISE"),
|
|
(Choice::Abstain, "ABSTAIN"),
|
|
] {
|
|
let fields = parsed(&Decision::new(choice, 0.5, "r"));
|
|
assert_eq!(fields[0].1, Value::String(name.into()));
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn only_raise_has_a_size() {
|
|
for choice in [Choice::Fold, Choice::Check, Choice::Call, Choice::Abstain] {
|
|
assert_eq!(parsed(&Decision::new(choice, 0.5, "r"))[1].1, Value::Null);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn sizes_round_trip_exactly() {
|
|
for size in [0.5, 1.15, 4.6, 20.0, 0.1, 2.4, 1234.5678, 0.30000000000000004] {
|
|
let fields = parsed(&Decision::new(Choice::Raise { to: size }, 0.9, "r"));
|
|
assert_eq!(fields[1].1, Value::Number(size), "{size}");
|
|
}
|
|
assert!(encode(&Decision::new(Choice::Raise { to: 1.15 }, 0.9, "r")).contains(r#""size":1.15,"#));
|
|
assert!(encode(&Decision::new(Choice::Raise { to: 4.6 }, 0.9, "r")).contains(r#""size":4.6,"#));
|
|
}
|
|
|
|
#[test]
|
|
fn non_finite_size_becomes_null() {
|
|
assert_eq!(parsed(&Decision::new(Choice::Raise { to: f64::NAN }, 0.9, "r"))[1].1, Value::Null);
|
|
}
|
|
|
|
#[test]
|
|
fn confidence_is_rounded_to_three_decimals_and_in_range() {
|
|
let conf = |c: f64| match &parsed(&Decision::new(Choice::Call, c, "r"))[2].1 {
|
|
Value::Number(n) => *n,
|
|
other => panic!("{other:?}"),
|
|
};
|
|
assert_eq!(conf(0.123456), 0.123);
|
|
assert_eq!(conf(0.9996), 1.0);
|
|
assert_eq!(conf(0.0), 0.0);
|
|
assert_eq!(conf(1.0), 1.0);
|
|
assert_eq!(conf(7.0), 1.0);
|
|
assert_eq!(conf(-3.0), 0.0);
|
|
assert_eq!(conf(f64::NAN), 0.0);
|
|
assert_eq!(conf(0.6666666), 0.667);
|
|
}
|
|
|
|
#[test]
|
|
fn reason_is_escaped_and_single_line() {
|
|
let d = Decision::abstain("bad \"x\"\nline2\\");
|
|
assert!(!encode(&d).contains('\n'));
|
|
assert_eq!(parsed(&d)[3].1, Value::String("bad \"x\"\nline2\\".into()));
|
|
}
|