Some tests.
This commit is contained in:
parent
b652ddb99f
commit
0260fa50c0
4 changed files with 78 additions and 6 deletions
|
@ -9,7 +9,7 @@ pub fn compute_flow(
|
||||||
source: &Address,
|
source: &Address,
|
||||||
sink: &Address,
|
sink: &Address,
|
||||||
edges: &HashMap<Address, Vec<Edge>>,
|
edges: &HashMap<Address, Vec<Edge>>,
|
||||||
) -> String {
|
) -> (U256, Vec<Edge>) {
|
||||||
let mut adjacencies = Adjacencies::new(edges);
|
let mut adjacencies = Adjacencies::new(edges);
|
||||||
let mut used_edges: HashMap<Node, HashMap<Node, U256>> = HashMap::new();
|
let mut used_edges: HashMap<Node, HashMap<Node, U256>> = HashMap::new();
|
||||||
|
|
||||||
|
@ -52,7 +52,7 @@ pub fn compute_flow(
|
||||||
extract_transfers(source, sink, &flow, used_edges)
|
extract_transfers(source, sink, &flow, used_edges)
|
||||||
};
|
};
|
||||||
println!("Num transfers: {}", transfers.len());
|
println!("Num transfers: {}", transfers.len());
|
||||||
flow.to_string()
|
(flow, transfers)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn augmenting_path(
|
fn augmenting_path(
|
||||||
|
@ -165,3 +165,69 @@ fn next_full_capacity_edge(
|
||||||
}
|
}
|
||||||
panic!();
|
panic!();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
fn addresses() -> (Address, Address, Address, Address, Address, Address) {
|
||||||
|
(
|
||||||
|
Address::from("0x11C7e86fF693e9032A0F41711b5581a04b26Be2E"),
|
||||||
|
Address::from("0x22cEDde51198D1773590311E2A340DC06B24cB37"),
|
||||||
|
Address::from("0x33cEDde51198D1773590311E2A340DC06B24cB37"),
|
||||||
|
Address::from("0x447EDde51198D1773590311E2A340DC06B24cB37"),
|
||||||
|
Address::from("0x55c16ce62d26fd51582a646e2e30a3267b1e6d7e"),
|
||||||
|
Address::from("0x66c16ce62d26fd51582a646e2e30a3267b1e6d7e"),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
fn build_edges(input: Vec<Edge>) -> HashMap<Address, Vec<Edge>> {
|
||||||
|
let mut output: HashMap<Address, Vec<Edge>> = HashMap::new();
|
||||||
|
for e in input {
|
||||||
|
output.entry(e.from).or_default().push(e);
|
||||||
|
}
|
||||||
|
output
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn direct() {
|
||||||
|
let (a, b, t, ..) = addresses();
|
||||||
|
let edges = build_edges(vec![
|
||||||
|
Edge{from: a, to: b, token: t, capacity: U256::from(10)}
|
||||||
|
]);
|
||||||
|
let flow = compute_flow(&a, &b, &edges);
|
||||||
|
assert_eq!(flow, (U256::from(10), edges[&a].clone()));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn one_hop() {
|
||||||
|
let (a, b, c, t1, t2, ..) = addresses();
|
||||||
|
let edges = build_edges(vec![
|
||||||
|
Edge{from: a, to: b, token: t1, capacity: U256::from(10)},
|
||||||
|
Edge{from: b, to: c, token: t2, capacity: U256::from(8)},
|
||||||
|
]);
|
||||||
|
let flow = compute_flow(&a, &c, &edges);
|
||||||
|
assert_eq!(flow, (U256::from(8), vec![
|
||||||
|
Edge{from: a, to: b, token: t1, capacity: U256::from(8)},
|
||||||
|
Edge{from: b, to: c, token: t2, capacity: U256::from(8)},
|
||||||
|
]));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn diamond() {
|
||||||
|
let (a, b, c, d, t1, t2) = addresses();
|
||||||
|
let edges = build_edges(vec![
|
||||||
|
Edge{from: a, to: b, token: t1, capacity: U256::from(10)},
|
||||||
|
Edge{from: a, to: c, token: t2, capacity: U256::from(7)},
|
||||||
|
Edge{from: b, to: d, token: t2, capacity: U256::from(9)},
|
||||||
|
Edge{from: c, to: d, token: t1, capacity: U256::from(8)},
|
||||||
|
]);
|
||||||
|
let mut flow = compute_flow(&a, &d, &edges);
|
||||||
|
flow.1.sort();
|
||||||
|
assert_eq!(flow, (U256::from(16), vec![
|
||||||
|
Edge{from: a, to: b, token: t1, capacity: U256::from(9)},
|
||||||
|
Edge{from: a, to: c, token: t2, capacity: U256::from(7)},
|
||||||
|
Edge{from: b, to: d, token: t2, capacity: U256::from(9)},
|
||||||
|
Edge{from: c, to: d, token: t1, capacity: U256::from(7)},
|
||||||
|
]));
|
||||||
|
}
|
||||||
|
}
|
|
@ -69,7 +69,7 @@ fn handle_connection(
|
||||||
);
|
);
|
||||||
println!("Computed flow");
|
println!("Computed flow");
|
||||||
// TODO error handling
|
// TODO error handling
|
||||||
socket.write_all(jsonrpc_result(request.id, json::JsonValue::from(flow)).as_bytes())?;
|
socket.write_all(jsonrpc_result(request.id, json::JsonValue::from(flow.0.to_string())).as_bytes())?;
|
||||||
}
|
}
|
||||||
"cancel" => {}
|
"cancel" => {}
|
||||||
"update_edges" => {}
|
"update_edges" => {}
|
||||||
|
|
|
@ -1,8 +1,14 @@
|
||||||
use std::fmt::{Display, Formatter};
|
use std::fmt::{Display, Formatter, Debug};
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, Default, Hash, Eq, PartialEq, PartialOrd)]
|
#[derive(Clone, Copy, Default, Hash, Eq, PartialEq, Ord, PartialOrd)]
|
||||||
pub struct Address([u8; 20]);
|
pub struct Address([u8; 20]);
|
||||||
|
|
||||||
|
impl Debug for Address {
|
||||||
|
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
|
||||||
|
write!(f, "{}", self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl From<[u8; 20]> for Address {
|
impl From<[u8; 20]> for Address {
|
||||||
fn from(item: [u8; 20]) -> Self {
|
fn from(item: [u8; 20]) -> Self {
|
||||||
Address(item)
|
Address(item)
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use crate::types::Address;
|
use crate::types::Address;
|
||||||
use crate::types::U256;
|
use crate::types::U256;
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, Default, Hash, PartialEq, PartialOrd)]
|
#[derive(Clone, Copy, Debug, Default, Hash, PartialEq, Ord, PartialOrd)]
|
||||||
pub struct Edge {
|
pub struct Edge {
|
||||||
pub from: Address,
|
pub from: Address,
|
||||||
pub to: Address,
|
pub to: Address,
|
||||||
|
|
Loading…
Reference in a new issue