Add fixes for powmod and sorting of polynomials #17
2 changed files with 66 additions and 9 deletions
|
|
@ -138,4 +138,58 @@ mod tests {
|
||||||
assert_eq!(json!(result), expected);
|
assert_eq!(json!(result), expected);
|
||||||
//assert_eq!(BASE64_STANDARD.encode(product), "MoAAAAAAAAAAAAAAAAAAAA==");
|
//assert_eq!(BASE64_STANDARD.encode(product), "MoAAAAAAAAAAAAAAAAAAAA==");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_poly_sorting_02() {
|
||||||
|
let json1 = json!(
|
||||||
|
{"polys": [
|
||||||
|
[
|
||||||
|
"AQAAAAAAAAAAAAAAAAAAAA==", // 0x01
|
||||||
|
"AgAAAAAAAAAAAAAAAAAAAA==", // 0x02
|
||||||
|
"AwAAAAAAAAAAAAAAAAAAAA==" // 0x03
|
||||||
|
],
|
||||||
|
[
|
||||||
|
"AQAAAAAAAAAAAAAAAAAAAA==", // 0x01
|
||||||
|
"AgAAAAAAAAAAAAAAAAAAAA==", // 0x02
|
||||||
|
"BAAAAAAAAAAAAAAAAAAAAA==" // 0x04
|
||||||
|
],
|
||||||
|
[
|
||||||
|
"AQAAAAAAAAAAAAAAAAAAAA==", // 0x01
|
||||||
|
"AgAAAAAAAAAAAAAAAAAAAA==" // 0x02
|
||||||
|
],
|
||||||
|
[
|
||||||
|
"AQAAAAAAAAAAAAAAAAAAAA==", // 0x01
|
||||||
|
"AwAAAAAAAAAAAAAAAAAAAA==" // 0x03
|
||||||
|
]
|
||||||
|
],});
|
||||||
|
|
||||||
|
let expected = json!([
|
||||||
|
[
|
||||||
|
"WereNoStrangersToLoveA==",
|
||||||
|
"YouKnowTheRulesAAAAAAA==",
|
||||||
|
"AndSoDoIAAAAAAAAAAAAAA=="
|
||||||
|
],
|
||||||
|
[
|
||||||
|
"NeverGonnaMakeYouCryAA==",
|
||||||
|
"NeverGonnaSayGoodbyeAA==",
|
||||||
|
"NeverGonnaTellALieAAAA==",
|
||||||
|
"AndHurtYouAAAAAAAAAAAA=="
|
||||||
|
],
|
||||||
|
[
|
||||||
|
"NeverGonnaGiveYouUpAAA==",
|
||||||
|
"NeverGonnaLetYouDownAA==",
|
||||||
|
"NeverGonnaRunAroundAAA==",
|
||||||
|
"AndDesertYouAAAAAAAAAA=="
|
||||||
|
]
|
||||||
|
]);
|
||||||
|
|
||||||
|
let sorted_array = gfpoly_sort(&json1).unwrap();
|
||||||
|
let mut result: Vec<Vec<String>> = vec![];
|
||||||
|
for poly in sorted_array {
|
||||||
|
result.push(poly.to_c_array());
|
||||||
|
}
|
||||||
|
|
||||||
|
assert_eq!(json!(result), expected);
|
||||||
|
//assert_eq!(BASE64_STANDARD.encode(product), "MoAAAAAAAAAAAAAAAAAAAA==");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -324,11 +324,12 @@ impl PartialEq for Polynomial {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PartialOrd for Polynomial {
|
impl PartialOrd for Polynomial {
|
||||||
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
|
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
||||||
if self.polynomial.len() != other.polynomial.len() {
|
match self.polynomial.len().cmp(&other.polynomial.len()) {
|
||||||
return Some(self.polynomial.len().cmp(&other.polynomial.len()));
|
Ordering::Equal => {
|
||||||
} else {
|
for (field_a, field_b) in
|
||||||
for (field_a, field_b) in self.as_ref().iter().rev().zip(other.as_ref().iter().rev()) {
|
self.as_ref().iter().rev().zip(other.as_ref().iter().rev())
|
||||||
|
{
|
||||||
match field_a.cmp(field_b) {
|
match field_a.cmp(field_b) {
|
||||||
std::cmp::Ordering::Equal => continue,
|
std::cmp::Ordering::Equal => continue,
|
||||||
other => return Some(other.reverse()),
|
other => return Some(other.reverse()),
|
||||||
|
|
@ -336,6 +337,8 @@ impl PartialOrd for Polynomial {
|
||||||
}
|
}
|
||||||
Some(Ordering::Equal)
|
Some(Ordering::Equal)
|
||||||
}
|
}
|
||||||
|
other => Some(other),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue