Skip to content

Commit 92a41f8

Browse files
committed
Add unit tests for normalized
The `semantic::Policy::normalized` function is recursive and has nested processing of `Threshold`s. It is not trivial to fully grok. The function is already tested elsewhere in the unit tests but we don't have simple single purpose tests for it. Add various unit tests for `normalized`, these serve as documentation of the function as much as anything.
1 parent 9507538 commit 92a41f8

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

src/policy/semantic.rs

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,4 +1042,86 @@ mod tests {
10421042
}));
10431043
assert_eq!(count, 17);
10441044
}
1045+
1046+
fn four_arbitrary_keys(
1047+
) -> (Arc<StringPolicy>, Arc<StringPolicy>, Arc<StringPolicy>, Arc<StringPolicy>) {
1048+
let a = Arc::new(StringPolicy::from_str("pk(A)").unwrap());
1049+
let b = Arc::new(StringPolicy::from_str("pk(B)").unwrap());
1050+
let c = Arc::new(StringPolicy::from_str("pk(C)").unwrap());
1051+
let d = Arc::new(StringPolicy::from_str("pk(D)").unwrap());
1052+
1053+
(a, b, c, d)
1054+
}
1055+
1056+
#[test]
1057+
fn normalize_nested_and() {
1058+
let (a, b, c, d) = four_arbitrary_keys();
1059+
1060+
let thresh0 = StringPolicy::Threshold(2, vec![a.clone(), b.clone()]);
1061+
let thresh1 = StringPolicy::Threshold(2, vec![c.clone(), d.clone()]);
1062+
1063+
let policy = StringPolicy::Threshold(2, vec![thresh0.into(), thresh1.into()]);
1064+
let got = policy.normalized();
1065+
1066+
let want = StringPolicy::Threshold(4, vec![a, b, c, d]);
1067+
1068+
assert_eq!(got, want)
1069+
}
1070+
1071+
#[test]
1072+
fn normalize_nested_or() {
1073+
let (a, b, c, d) = four_arbitrary_keys();
1074+
1075+
let thresh0 = StringPolicy::Threshold(1, vec![a.clone(), b.clone()]);
1076+
let thresh1 = StringPolicy::Threshold(1, vec![c.clone(), d.clone()]);
1077+
1078+
let policy = StringPolicy::Threshold(1, vec![thresh0.into(), thresh1.into()]);
1079+
let got = policy.normalized();
1080+
1081+
let want = StringPolicy::Threshold(1, vec![a, b, c, d]);
1082+
1083+
assert_eq!(got, want)
1084+
}
1085+
1086+
#[test]
1087+
fn normalize_2_of_5_containing_2_unsatisfiable_1_trivial() {
1088+
let (a, b, _, _) = four_arbitrary_keys();
1089+
let u = Arc::new(StringPolicy::Unsatisfiable);
1090+
let t = Arc::new(StringPolicy::Trivial);
1091+
1092+
// (2,5)-thresh with only 2 satisfiable, 1 trivial
1093+
let policy =
1094+
StringPolicy::Threshold(2, vec![a.clone(), b.clone(), u.clone(), u.clone(), t]);
1095+
let got = policy.normalized();
1096+
let want = StringPolicy::Threshold(1, vec![a, b]);
1097+
1098+
assert_eq!(got, want)
1099+
}
1100+
1101+
#[test]
1102+
fn normalize_2_of_5_containing_3_unsatisfiable() {
1103+
let (a, b, _, _) = four_arbitrary_keys();
1104+
let u = Arc::new(StringPolicy::Unsatisfiable);
1105+
1106+
// (2,5)-thresh with only 2 satisfiable
1107+
let policy =
1108+
StringPolicy::Threshold(2, vec![a.clone(), b.clone(), u.clone(), u.clone(), u.clone()]);
1109+
let got = policy.normalized();
1110+
let want = StringPolicy::Threshold(2, vec![a, b]);
1111+
1112+
assert_eq!(got, want)
1113+
}
1114+
1115+
#[test]
1116+
fn normalize_2_of_3_containing_2_unsatisfiable() {
1117+
let (a, _, _, _) = four_arbitrary_keys();
1118+
let u = Arc::new(StringPolicy::Unsatisfiable);
1119+
1120+
// (2,3)-thresh with only 2 satisfiable
1121+
let policy = StringPolicy::Threshold(2, vec![a.clone(), u.clone(), u.clone()]);
1122+
let got = policy.normalized();
1123+
let want = StringPolicy::Unsatisfiable;
1124+
1125+
assert_eq!(got, want)
1126+
}
10451127
}

0 commit comments

Comments
 (0)