aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRunasSudo <runassudo@yingtongli.me>2021-10-26 00:55:42 +1100
committerRunasSudo <runassudo@yingtongli.me>2021-10-26 00:55:42 +1100
commit0c97755813f58de1d96ba8797be63c22d2b3c6d8 (patch)
tree97f0e702e7c618224858e05fddba44e70046057e
parentbe8a6e83fc2f4518fc6163e414932dd6a23a321e (diff)
Add CSP parsing tests
-rw-r--r--src/parser/csp.rs32
1 files changed, 32 insertions, 0 deletions
diff --git a/src/parser/csp.rs b/src/parser/csp.rs
index 1d6e405..4451c21 100644
--- a/src/parser/csp.rs
+++ b/src/parser/csp.rs
@@ -142,3 +142,35 @@ pub fn parse_reader<R: Read, N: Number>(reader: R, require_1: bool, require_sequ
constraints: None,
});
}
+
+#[test]
+fn csp_formal() {
+ let csp_data = "A,B,C\n1,2,3";
+ let election = parse_reader::<_, crate::numbers::Rational>(csp_data.as_bytes(), false, false, false).unwrap();
+ assert_eq!(election.ballots.first().unwrap().preferences, vec![vec![0], vec![1], vec![2]]);
+
+ let csp_data = "A,B,C\n2,3,4";
+ let election = parse_reader::<_, crate::numbers::Rational>(csp_data.as_bytes(), false, false, false).unwrap();
+ assert_eq!(election.ballots.first().unwrap().preferences, vec![vec![0], vec![1], vec![2]]);
+}
+
+#[test]
+fn csp_no1() {
+ let csp_data = "A,B,C\n2,3,4";
+ let election = parse_reader::<_, crate::numbers::Rational>(csp_data.as_bytes(), true, false, false).unwrap();
+ assert_eq!(election.ballots.first().unwrap().preferences.len(), 0);
+}
+
+#[test]
+fn csp_skipped_preference() {
+ let csp_data = "A,B,C\n1,3,4";
+ let election = parse_reader::<_, crate::numbers::Rational>(csp_data.as_bytes(), false, true, false).unwrap();
+ assert_eq!(election.ballots.first().unwrap().preferences, vec![vec![0]]);
+}
+
+#[test]
+fn csp_duplicate_preference() {
+ let csp_data = "A,B,C\n1,2,2";
+ let election = parse_reader::<_, crate::numbers::Rational>(csp_data.as_bytes(), false, false, true).unwrap();
+ assert_eq!(election.ballots.first().unwrap().preferences, vec![vec![0]]);
+}
Contact (issues, pull requests, etc.) at git@yingtongli.me. Generated by cgit.