From f49185d32c1727f0cb28971d55c3ad05d14ab2a3 Mon Sep 17 00:00:00 2001 From: Matthew Gumport Date: Wed, 10 Apr 2024 13:49:10 -0700 Subject: [PATCH] fix tinylfu test flake The TinyLFU test was failing for changes unrelated to anything in the tinyufo crate. This changes the test to stop that! What was occurring was expected behavior, so the test is what's changing instead of any internal logic. What was happening were hash collisions in the bloom filter used by the estimator. Instead of asserting that we'd always start counting from 0, we now allow for this in the test by `get()`ing first, then comparing relative values. For the final comparisons, we check that values are greater-than or equal-to their lower bound as determined by the number of `incr()`s we called on their keys. --- .bleep | 2 +- tinyufo/src/estimation.rs | 29 +++++++++++++++++------------ 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/.bleep b/.bleep index 8a5222b..80d7b14 100644 --- a/.bleep +++ b/.bleep @@ -1 +1 @@ -802590d7d04730c3e93f9c2335175990eee1fb92 \ No newline at end of file +d87a50338cb6483a3b0cbfe73d3b0491a336b7a2 \ No newline at end of file diff --git a/tinyufo/src/estimation.rs b/tinyufo/src/estimation.rs index 18c2d4f..6f165ac 100644 --- a/tinyufo/src/estimation.rs +++ b/tinyufo/src/estimation.rs @@ -183,20 +183,25 @@ mod tests { assert_eq!(tiny.incr(1), 2); assert_eq!(tiny.get(1), 2); - assert_eq!(tiny.get(2), 0); - assert_eq!(tiny.incr(2), 1); - assert_eq!(tiny.incr(2), 2); - assert_eq!(tiny.get(2), 2); + // Might have hash collisions for the others, need to + // get() before can assert on the incr() value. + let two = tiny.get(2); + assert_eq!(tiny.incr(2), two + 1); + assert_eq!(tiny.incr(2), two + 2); + assert_eq!(tiny.get(2), two + 2); - assert_eq!(tiny.incr(3), 1); - assert_eq!(tiny.incr(3), 2); - assert_eq!(tiny.incr(3), 3); - assert_eq!(tiny.incr(3), 4); + let three = tiny.get(3); + assert_eq!(tiny.incr(3), three + 1); + assert_eq!(tiny.incr(3), three + 2); + assert_eq!(tiny.incr(3), three + 3); + assert_eq!(tiny.incr(3), three + 4); - // 8 incr(), now reset + // 8 incr(), now resets on next incr + // can only assert they are greater than or equal + // to the incr() we do per key. - assert_eq!(tiny.incr(3), 3); - assert_eq!(tiny.incr(1), 2); - assert_eq!(tiny.incr(2), 2); + assert!(tiny.incr(3) >= 3); // had 4, reset to 2, added another. + assert!(tiny.incr(1) >= 2); // had 2, reset to 1, added another. + assert!(tiny.incr(2) >= 2); // had 2, reset to 1, added another. } }