Skip to content

Commit fcde4cb

Browse files
committed
Fix FlatFat wrap-around bug and add test
1 parent 6038b12 commit fcde4cb

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

rust/src/reactive/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ where
3232
}
3333
}
3434
fn inverted(&self) -> bool {
35-
self.front > self.back
35+
self.front >= self.back
3636
}
3737
fn resize(&mut self, capacity: usize) {
3838
let leaves = self.fat.leaves();
@@ -69,7 +69,7 @@ where
6969
fn push(&mut self, v: Value) {
7070
self.fat.update([(self.back, v)].iter().cloned());
7171
self.size += 1;
72-
self.back += 1;
72+
self.back = (self.back + 1) % self.fat.capacity;
7373
if self.size > (3 * self.fat.capacity) / 4 {
7474
self.resize(self.fat.capacity * 2);
7575
}
@@ -79,7 +79,7 @@ where
7979
self.fat
8080
.update([(self.front, Value::identity())].iter().cloned());
8181
self.size -= 1;
82-
self.front += 1;
82+
self.front = (self.front + 1) % self.fat.capacity;
8383
if self.size <= self.fat.capacity / 4 && self.size > 0 {
8484
self.resize(self.fat.capacity / 2);
8585
}

rust/tests/fifo_window.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,25 @@ where
126126
window.pop();
127127
}
128128

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));
141+
}
142+
129143
test_matrix! {
130144
test1 => [ recalc::ReCalc, soe::SoE, reactive::Reactive, two_stacks::TwoStacks ],
131145
test2 => [ recalc::ReCalc, soe::SoE, reactive::Reactive, two_stacks::TwoStacks ],
132146
test3 => [ recalc::ReCalc, reactive::Reactive, two_stacks::TwoStacks ],
133147
test4 => [ recalc::ReCalc, soe::SoE, reactive::Reactive, two_stacks::TwoStacks ],
134-
test5 => [ 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 ]
135150
}

0 commit comments

Comments
 (0)