Skip to content

Commit 078e517

Browse files
committed
Refactor and add new tests
1 parent dc14300 commit 078e517

File tree

3 files changed

+246
-135
lines changed

3 files changed

+246
-135
lines changed

rust/tests/common.rs

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
use alga::general::AbstractGroup;
2+
use alga::general::AbstractLoop;
3+
use alga::general::AbstractMagma;
4+
use alga::general::AbstractMonoid;
5+
use alga::general::AbstractQuasigroup;
6+
use alga::general::AbstractSemigroup;
7+
use alga::general::Identity;
8+
use alga::general::Operator;
9+
use alga::general::TwoSidedInverse;
10+
11+
/// Abstract Algebra Lattice:
12+
/// Borrowed from https://docs.rs/alga/0.9.3/alga/general/index.html
13+
///
14+
/// AbstractMagma
15+
/// |
16+
/// _______/ \______
17+
/// / \
18+
/// divisibility associativity
19+
/// | |
20+
/// V V
21+
/// AbstractQuasigroup AbstractSemigroup
22+
/// | |
23+
/// identity identity
24+
/// | |
25+
/// V V
26+
/// AbstractLoop AbstractMonoid
27+
/// | |
28+
/// associativity invertibility
29+
/// \______ _______/
30+
/// \ /
31+
/// |
32+
/// V
33+
/// AbstractGroup
34+
/// |
35+
/// commutativity
36+
/// |
37+
/// V
38+
/// AbstractGroupAbelian
39+
40+
/// An integer value
41+
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
42+
pub struct Int(pub i32);
43+
44+
/// Binary operator for calculating the arithmetic sum.
45+
/// Has the following properties:
46+
/// * Invertibility
47+
/// * Associativity
48+
/// * Commutativity
49+
#[derive(Copy, Clone)]
50+
pub struct Sum;
51+
52+
impl Operator for Sum {
53+
fn operator_token() -> Sum {
54+
Sum
55+
}
56+
}
57+
58+
impl Identity<Sum> for Int {
59+
fn identity() -> Int {
60+
Int(0)
61+
}
62+
}
63+
64+
impl AbstractMagma<Sum> for Int {
65+
fn operate(&self, other: &Self) -> Self {
66+
Int(self.0 + other.0)
67+
}
68+
}
69+
70+
impl TwoSidedInverse<Sum> for Int {
71+
fn two_sided_inverse(&self) -> Int {
72+
Int(-self.0)
73+
}
74+
}
75+
76+
impl AbstractSemigroup<Sum> for Int {}
77+
impl AbstractMonoid<Sum> for Int {}
78+
impl AbstractQuasigroup<Sum> for Int {}
79+
impl AbstractLoop<Sum> for Int {}
80+
impl AbstractGroup<Sum> for Int {}
81+
82+
/// Binary operator for calculating the maximum Int.
83+
/// Has the following properties:
84+
/// * Associativity
85+
/// * Commutativity
86+
#[derive(Copy, Clone)]
87+
pub(crate) struct Max;
88+
89+
impl Operator for Max {
90+
fn operator_token() -> Max {
91+
Max
92+
}
93+
}
94+
95+
impl Identity<Max> for Int {
96+
fn identity() -> Int {
97+
Int(std::i32::MIN)
98+
}
99+
}
100+
101+
impl AbstractMagma<Max> for Int {
102+
fn operate(&self, other: &Self) -> Self {
103+
if self.0 > other.0 {
104+
*self
105+
} else {
106+
*other
107+
}
108+
}
109+
}
110+
111+
impl AbstractSemigroup<Max> for Int {}
112+
impl AbstractMonoid<Max> for Int {}

rust/tests/fifo-window.rs

Lines changed: 0 additions & 135 deletions
This file was deleted.

rust/tests/fifo_window.rs

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
use rand::Rng;
2+
use swag::reactive::*;
3+
use swag::recalc::*;
4+
use swag::soe::*;
5+
use swag::two_stacks::*;
6+
use swag::*;
7+
8+
mod common;
9+
use common::*;
10+
11+
/// Basic test for integer sums.
12+
fn test1<Window>()
13+
where
14+
Window: FifoWindow<Int, Sum>,
15+
{
16+
let mut window = Window::new();
17+
18+
assert_eq!(window.query(), Int(0));
19+
20+
window.push(Int(1));
21+
22+
assert_eq!(window.query(), Int(1));
23+
24+
window.push(Int(2));
25+
26+
assert_eq!(window.query(), Int(3));
27+
28+
window.push(Int(3));
29+
30+
assert_eq!(window.query(), Int(6));
31+
32+
window.pop();
33+
34+
assert_eq!(window.query(), Int(5));
35+
}
36+
37+
fn generate() -> Vec<Int> {
38+
let mut rng = rand::thread_rng();
39+
(0..1000)
40+
.map(|_| rng.gen_range(1, 5))
41+
.map(Int)
42+
.collect::<Vec<_>>()
43+
}
44+
45+
/// Tries to aggregate the sum of 1000 randomly generated integers.
46+
fn test2<Window>()
47+
where
48+
Window: FifoWindow<Int, Sum>,
49+
{
50+
let values = generate();
51+
let sum: i32 = values.iter().fold(0, |acc, Int(x)| acc + x);
52+
let mut window = Window::new();
53+
for v in values.clone() {
54+
window.push(v);
55+
}
56+
assert_eq!(window.query(), Int(sum));
57+
for _ in values {
58+
window.pop();
59+
}
60+
assert_eq!(window.query(), Int(0));
61+
}
62+
63+
/// Tries to find the maximum value out 1000 randomly generated integers.
64+
fn test3<Window>()
65+
where
66+
Window: FifoWindow<Int, Max>,
67+
{
68+
let mut window = Window::new();
69+
let values = generate();
70+
let max = values.iter().map(|Int(x)| *x).max().unwrap();
71+
for v in values.clone() {
72+
window.push(v);
73+
}
74+
assert_eq!(window.query(), Int(max));
75+
for _ in values {
76+
window.pop();
77+
}
78+
assert_eq!(window.query(), Int(std::i32::MIN));
79+
}
80+
81+
#[test]
82+
fn test1_recalc() {
83+
test1::<ReCalc<Int, Sum>>();
84+
}
85+
86+
#[test]
87+
fn test2_recalc() {
88+
test2::<ReCalc<Int, Sum>>();
89+
}
90+
91+
#[test]
92+
fn test3_recalc() {
93+
test3::<ReCalc<Int, Max>>();
94+
}
95+
96+
#[test]
97+
fn test1_soe() {
98+
test1::<SoE<Int, Sum>>();
99+
}
100+
101+
#[test]
102+
fn test2_soe() {
103+
test2::<SoE<Int, Sum>>();
104+
}
105+
106+
#[test]
107+
fn test1_two_stacks() {
108+
test1::<TwoStacks<Int, Sum>>();
109+
}
110+
111+
#[test]
112+
fn test2_two_stacks() {
113+
test2::<TwoStacks<Int, Sum>>();
114+
}
115+
116+
#[test]
117+
fn test3_two_stacks() {
118+
test3::<TwoStacks<Int, Max>>();
119+
}
120+
121+
#[test]
122+
fn test1_reactive() {
123+
test1::<Reactive<Int, Sum>>();
124+
}
125+
126+
#[test]
127+
fn test2_reactive() {
128+
test2::<Reactive<Int, Sum>>();
129+
}
130+
131+
#[test]
132+
fn test3_reactive() {
133+
test3::<Reactive<Int, Max>>();
134+
}

0 commit comments

Comments
 (0)