Move phragmen benchmarks out of Staking (#3588)

* Move phragmen benches to.. phragmen.

* Move some basic phragmen tests to.. phragmen.

* Line-width

* Add phragmen equ implementation as flot

* Add phragmen equ implementation as flot

* Add mock and test file.
This commit is contained in:
Kian Paimani
2019-09-13 08:41:33 +02:00
committed by GitHub
parent 7b87cd206b
commit a6b5d1d155
10 changed files with 661 additions and 374 deletions
+135
View File
@@ -0,0 +1,135 @@
// Copyright 2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
//! Tests for phragmen.
#![cfg(test)]
use crate::mock::*;
use crate::{elect, ACCURACY, PhragmenResult};
use support::assert_eq_uvec;
#[test]
fn float_phragmen_poc_works() {
let candidates = vec![1, 2, 3];
let voters = vec![
(10, vec![1, 2]),
(20, vec![1, 3]),
(30, vec![2, 3]),
];
let stake_of = create_stake_of(&[(10, 10), (20, 20), (30, 30), (1, 0), (2, 0), (3, 0)]);
let mut phragmen_result = elect_float(2, 2, candidates, voters, &stake_of, false).unwrap();
let winners = phragmen_result.clone().winners;
let assignments = phragmen_result.clone().assignments;
assert_eq_uvec!(winners, vec![2, 3]);
assert_eq_uvec!(
assignments,
vec![
(10, vec![(2, 1.0)]),
(20, vec![(3, 1.0)]),
(30, vec![(2, 0.5), (3, 0.5)]),
]
);
let mut support_map = build_support_map(&mut phragmen_result, &stake_of);
assert_eq!(
support_map.get(&2).unwrap(),
&_Support { own: 0.0, total: 25.0, others: vec![(10u64, 10.0), (30u64, 15.0)]}
);
assert_eq!(
support_map.get(&3).unwrap(),
&_Support { own: 0.0, total: 35.0, others: vec![(20u64, 20.0), (30u64, 15.0)]}
);
equalize_float(phragmen_result.assignments, &mut support_map, 0.0, 2, stake_of);
assert_eq!(
support_map.get(&2).unwrap(),
&_Support { own: 0.0, total: 30.0, others: vec![(10u64, 10.0), (30u64, 20.0)]}
);
assert_eq!(
support_map.get(&3).unwrap(),
&_Support { own: 0.0, total: 30.0, others: vec![(20u64, 20.0), (30u64, 10.0)]}
);
}
#[test]
fn phragmen_poc_works() {
let candidates = vec![1, 2, 3];
let voters = vec![
(10, vec![1, 2]),
(20, vec![1, 3]),
(30, vec![2, 3]),
];
let PhragmenResult { winners, assignments } = elect::<_, _, _, TestCurrencyToVote>(
2,
2,
candidates,
voters,
create_stake_of(&[(10, 10), (20, 20), (30, 30)]),
false,
).unwrap();
assert_eq_uvec!(winners, vec![2, 3]);
assert_eq_uvec!(
assignments,
vec![
(10, vec![(2, ACCURACY)]),
(20, vec![(3, ACCURACY)]),
(30, vec![(2, ACCURACY/2), (3, ACCURACY/2)]),
]
);
}
#[test]
fn phragmen_poc_2_works() {
let candidates = vec![10, 20, 30];
let voters = vec![
(2, vec![10, 20, 30]),
(4, vec![10, 20, 40]),
];
let stake_of = create_stake_of(&[
(10, 1000),
(20, 1000),
(30, 1000),
(40, 1000),
(2, 500),
(4, 500),
]);
run_and_compare(candidates, voters, stake_of, 2, 2, true);
}
#[test]
fn phragmen_poc_3_works() {
let candidates = vec![10, 20, 30];
let voters = vec![
(2, vec![10, 20, 30]),
(4, vec![10, 20, 40]),
];
let stake_of = create_stake_of(&[
(10, 1000),
(20, 1000),
(30, 1000),
(2, 50),
(4, 1000),
]);
run_and_compare(candidates, voters, stake_of, 2, 2, true);
}