11use rand:: Rng ;
2- use swag:: reactive:: * ;
3- use swag:: recalc:: * ;
4- use swag:: soe:: * ;
5- use swag:: two_stacks:: * ;
62use swag:: * ;
73
84mod common;
95use common:: * ;
106
7+ /// Macro for generating test cases for different algorithms.
8+ macro_rules! test_matrix {
9+ {
10+ $(
11+ $name: ident => [ $( $module: ident:: $algorithm: ident) ,* ]
12+ ) ,*
13+ } => {
14+ $(
15+ mod $name {
16+ $(
17+ #[ test]
18+ fn $module( ) {
19+ super :: $name:: <swag:: $module:: $algorithm<_, _>>( ) ;
20+ }
21+ ) *
22+ }
23+ ) *
24+ }
25+ }
26+
1127/// Basic test for integer sums.
1228fn test1 < Window > ( )
1329where
@@ -34,21 +50,21 @@ where
3450 assert_eq ! ( window. query( ) , Int ( 5 ) ) ;
3551}
3652
37- fn generate ( ) -> Vec < Int > {
53+ fn synthesize ( size : usize ) -> Vec < Int > {
3854 let mut rng = rand:: thread_rng ( ) ;
39- ( 0 ..1000 )
55+ ( 0 ..size )
4056 . map ( |_| rng. gen_range ( 1 , 5 ) )
4157 . map ( Int )
4258 . collect :: < Vec < _ > > ( )
4359}
4460
45- /// Tries to aggregate the sum of 1000 randomly generated integers.
61+ /// Tries to aggregate the sum of 1K randomly generated integers.
4662fn test2 < Window > ( )
4763where
4864 Window : FifoWindow < Int , Sum > ,
4965{
50- let values = generate ( ) ;
51- let sum: i32 = values. iter ( ) . fold ( 0 , |acc, Int ( x) | acc + x) ;
66+ let values = synthesize ( 1_000 ) ;
67+ let sum = values. iter ( ) . fold ( 0 , |acc, Int ( x) | acc + x) ;
5268 let mut window = Window :: new ( ) ;
5369 for v in values. clone ( ) {
5470 window. push ( v) ;
@@ -60,13 +76,13 @@ where
6076 assert_eq ! ( window. query( ) , Int ( 0 ) ) ;
6177}
6278
63- /// Tries to find the maximum value out 1000 randomly generated integers.
79+ /// Tries to find the maximum value out 1K randomly generated integers.
6480fn test3 < Window > ( )
6581where
6682 Window : FifoWindow < Int , Max > ,
6783{
6884 let mut window = Window :: new ( ) ;
69- let values = generate ( ) ;
85+ let values = synthesize ( 1_000 ) ;
7086 let max = values. iter ( ) . map ( |Int ( x) | * x) . max ( ) . unwrap ( ) ;
7187 for v in values. clone ( ) {
7288 window. push ( v) ;
@@ -75,60 +91,60 @@ where
7591 for _ in values {
7692 window. pop ( ) ;
7793 }
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 > > ( ) ;
94+ assert_eq ! ( window. query( ) , Int ( std:: i64 :: MIN ) ) ;
9995}
10096
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 > > ( ) ;
97+ /// Fills a window with 1K elements and pushes/pops/queries 1K times.
98+ fn test4 < Window > ( )
99+ where
100+ Window : FifoWindow < Int , Sum > ,
101+ {
102+ let mut window = Window :: new ( ) ;
103+ let values = synthesize ( 1_000 ) ;
104+ let sum = values. iter ( ) . fold ( 0 , |acc, Int ( x) | acc + x) ;
105+ for v in values. clone ( ) {
106+ window. push ( v) ;
107+ }
108+ for v in values {
109+ window. push ( v) ;
110+ window. pop ( ) ;
111+ window. query ( ) ;
112+ assert_eq ! ( window. query( ) , Int ( sum) ) ;
113+ }
119114}
120115
121- #[ test]
122- fn test1_reactive ( ) {
123- test1 :: < Reactive < Int , Sum > > ( ) ;
116+ /// Pops more elements from a window than what it contains.
117+ fn test5 < Window > ( )
118+ where
119+ Window : FifoWindow < Int , Sum > ,
120+ {
121+ let mut window = Window :: new ( ) ;
122+ window. push ( Int ( 0 ) ) ;
123+ window. push ( Int ( 0 ) ) ;
124+ window. pop ( ) ;
125+ window. pop ( ) ;
126+ window. pop ( ) ;
124127}
125128
126- #[ test]
127- fn test2_reactive ( ) {
128- test2 :: < Reactive < Int , Sum > > ( ) ;
129+ /// Pops more elements from a window than what it contains.
130+ fn test6 < Window > ( )
131+ where
132+ Window : FifoWindow < Int , Sum > ,
133+ {
134+ let mut window = Window :: new ( ) ;
135+ window. push ( Int ( 1 ) ) ;
136+ window. push ( Int ( 2 ) ) ;
137+ window. push ( Int ( 3 ) ) ;
138+ window. pop ( ) ;
139+ window. push ( Int ( 4 ) ) ;
140+ window. push ( Int ( 5 ) ) ;
129141}
130142
131- #[ test]
132- fn test3_reactive ( ) {
133- test3 :: < Reactive < Int , Max > > ( ) ;
143+ test_matrix ! {
144+ test1 => [ recalc:: ReCalc , soe:: SoE , reactive:: Reactive , two_stacks:: TwoStacks ] ,
145+ test2 => [ recalc:: ReCalc , soe:: SoE , reactive:: Reactive , two_stacks:: TwoStacks ] ,
146+ test3 => [ recalc:: ReCalc , reactive:: Reactive , two_stacks:: TwoStacks ] ,
147+ test4 => [ recalc:: ReCalc , soe:: SoE , reactive:: Reactive , two_stacks:: TwoStacks ] ,
148+ test5 => [ recalc:: ReCalc , soe:: SoE , reactive:: Reactive , two_stacks:: TwoStacks ] ,
149+ test6 => [ recalc:: ReCalc , soe:: SoE , reactive:: Reactive , two_stacks:: TwoStacks ]
134150}
0 commit comments