OpenTally/tests/tests_impl/convert.rs

58 lines
2.0 KiB
Rust

/* OpenTally: Open-source election vote counting
* Copyright © 2021 Lee Yingtong Li (RunasSudo)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use opentally::election::Election;
use opentally::numbers::Rational;
use opentally::parser;
use opentally::writer;
use std::fs;
#[test]
fn ers97_wd_conversions() {
let blt_input = fs::read_to_string("tests/data/ers97_wd.blt").expect("IO Error");
let election: Election<Rational> = parser::blt::parse_path("tests/data/ers97_wd.blt").expect("Parse Error");
// Check BLT
let mut buf: Vec<u8> = Vec::new();
writer::blt::write(election.clone(), &mut buf);
assert_eq!(blt_input, std::str::from_utf8(&buf).expect("UTF-8 Error"));
// Check BIN
buf.clear();
writer::bin::write(election.clone(), &mut buf);
let election2: Election<Rational> = parser::bin::parse_bytes(&buf);
buf.clear();
writer::blt::write(election2, &mut buf);
assert_eq!(blt_input, std::str::from_utf8(&buf).expect("UTF-8 Error"));
// Check CSP
buf.clear();
writer::csp::write(election.clone(), &mut buf);
let mut election2: Election<Rational> = parser::csp::parse_reader(&buf[..]);
election2.seats = election.seats;
election2.name = election.name;
election2.withdrawn_candidates = election.withdrawn_candidates.clone();
buf.clear();
writer::blt::write(election2, &mut buf);
assert_eq!(blt_input, std::str::from_utf8(&buf).expect("UTF-8 Error"));
}