Your browser sucks, JavaScript disabled! Gunnar's console needs JavaScript and WebAssembly. Git over SSH on port 2222 does not.

nordisk/kleene

crates/kleene-graph/tests/rounds_upto.rs

//! The rounds stopped at a limit (`reach_rounds_upto`, `components_rounds_upto`): at most
//! that many exchanges, what was found so far, and without a limit the rounds' own answer.

use kleene_graph::{
    BfsState, Graph, Split, SplitKey, components_rounds, components_rounds_upto, reach_rounds,
    reach_rounds_upto,
};
use kleene_sparse::{BoolOrAnd, CpuBackend, CpuDevice};

type B = CpuBackend;

/// A path 0 - 1 - ... - n-1, both ways: a hash split crosses at nearly every step.
fn path(n: u32) -> (Vec<u32>, Vec<u32>) {
    let (mut s, mut d) = (Vec::new(), Vec::new());
    for v in 0..n - 1 {
        s.extend([v, v + 1]);
        d.extend([v + 1, v]);
    }
    (s, d)
}

fn graphs(split: &Split<u8>, device: &CpuDevice) -> (Vec<Graph<B>>, Vec<BfsState<B>>) {
    let gs: Vec<Graph<B>> = split
        .parts()
        .iter()
        .map(|p| Graph::new(p.inner().clone(), device).unwrap())
        .collect();
    let st = gs
        .iter()
        .map(|g| BfsState::new(g.vertices(), device))
        .collect();
    (gs, st)
}

#[test]
fn a_search_stops_at_the_limit_with_what_it_found() {
    let device = CpuDevice::new(2);
    let n = 200;
    let (s, d) = path(n);
    let split = Split::<u8>::new(n as usize, &s, &d, None, &SplitKey::Hash(4), BoolOrAnd).unwrap();
    let from = split.ids().from_original(0).unwrap();
    let (mut gs, mut st) = graphs(&split, &device);
    let full = reach_rounds(&split, &mut gs, &[from], &mut st, &device).unwrap();
    assert!(full.exchanges.exchanges > 10);
    let all = full.ids(split.ids());
    assert_eq!(all.len(), n as usize);
    for cap in [0u32, 1, 5, 10] {
        let (mut gs, mut st) = graphs(&split, &device);
        let r = reach_rounds_upto(&split, &mut gs, &[from], &mut st, cap, &device).unwrap();
        assert_eq!(r.exchanges.exchanges, cap, "cap {cap}");
        let got = r.ids(split.ids());
        assert!(got.len() < all.len(), "cap {cap}");
        assert!(got.iter().all(|i| all.binary_search(i).is_ok()));
    }
    let (mut gs, mut st) = graphs(&split, &device);
    let r = reach_rounds_upto(&split, &mut gs, &[from], &mut st, u32::MAX, &device).unwrap();
    assert_eq!(r.ids(split.ids()), all);
    assert_eq!(r.exchanges, full.exchanges);
}

#[test]
fn the_components_stop_at_the_limit_with_the_names_so_far() {
    let device = CpuDevice::new(2);
    let n = 200;
    let (s, d) = path(n);
    let key = SplitKey::Hash(4);
    let out = Split::<u8>::new(n as usize, &s, &d, None, &key, BoolOrAnd).unwrap();
    let inn = Split::<u8>::new(n as usize, &d, &s, None, &key, BoolOrAnd).unwrap();
    let full = components_rounds::<B>(&out, &inn, &device).unwrap();
    assert!(full.of.iter().all(|&c| c == 0), "one component named 0");
    assert!(full.exchanges.exchanges > 10);
    for cap in [1u32, 5] {
        let r = components_rounds_upto::<B>(&out, &inn, cap, &device).unwrap();
        assert_eq!(r.exchanges.exchanges, cap, "cap {cap}");
        assert!(r.of.iter().any(|&c| c != 0), "cap {cap}: not done");
    }
    let r = components_rounds_upto::<B>(&out, &inn, u32::MAX, &device).unwrap();
    assert_eq!(r.of, full.of);
    assert_eq!(r.exchanges, full.exchanges);
}