11
2+ using System . Collections . Generic ;
23using BitFaster . Caching . Lfu ;
34using FluentAssertions ;
4- using System . Collections . Generic ;
5- using System . Runtime . Intrinsics . X86 ;
65using Xunit ;
76
87namespace BitFaster . Caching . UnitTests . Lfu
@@ -23,9 +22,24 @@ public abstract class CmSketchTestBase<I> where I : struct, IsaProbe
2322
2423 public CmSketchTestBase ( )
2524 {
26- SkipAvxIfNotSupported ( ) ;
25+ Intrinsics . SkipAvxIfNotSupported < I > ( ) ;
26+ }
27+
28+ [ SkippableFact ]
29+ public void Repro ( )
30+ {
31+ sketch = new CmSketch < int , I > ( 1_048_576 , EqualityComparer < int > . Default ) ;
32+
33+ for ( int i = 0 ; i < 1_048_576 ; i ++ )
34+ {
35+ if ( i % 3 == 0 )
36+ {
37+ sketch . Increment ( i ) ;
38+ }
39+ }
2740 }
2841
42+
2943 [ SkippableFact ]
3044 public void WhenCapacityIsZeroDefaultsSelected ( )
3145 {
@@ -100,10 +114,48 @@ public void WhenClearedCountIsReset()
100114 sketch . EstimateFrequency ( 2 ) . Should ( ) . Be ( 0 ) ;
101115 }
102116
103- private static void SkipAvxIfNotSupported ( )
117+ [ SkippableFact ]
118+ public void HeavyHitters ( )
104119 {
105- // when we are trying to test Avx2, skip the test if it's not supported
106- Skip . If ( typeof ( I ) == typeof ( DetectIsa ) && ! Avx2 . IsSupported ) ;
120+ for ( int i = 100 ; i < 100_000 ; i ++ )
121+ {
122+ sketch . Increment ( i ) ;
123+ }
124+ for ( int i = 0 ; i < 10 ; i += 2 )
125+ {
126+ for ( int j = 0 ; j < i ; j ++ )
127+ {
128+ sketch . Increment ( i ) ;
129+ }
130+ }
131+
132+ // A perfect popularity count yields an array [0, 0, 2, 0, 4, 0, 6, 0, 8, 0]
133+ int [ ] popularity = new int [ 10 ] ;
134+
135+ for ( int i = 0 ; i < 10 ; i ++ )
136+ {
137+ popularity [ i ] = sketch . EstimateFrequency ( i ) ;
138+ }
139+
140+ for ( int i = 0 ; i < popularity . Length ; i ++ )
141+ {
142+ if ( ( i == 0 ) || ( i == 1 ) || ( i == 3 ) || ( i == 5 ) || ( i == 7 ) || ( i == 9 ) )
143+ {
144+ popularity [ i ] . Should ( ) . BeLessThanOrEqualTo ( popularity [ 2 ] ) ;
145+ }
146+ else if ( i == 2 )
147+ {
148+ popularity [ 2 ] . Should ( ) . BeLessThanOrEqualTo ( popularity [ 4 ] ) ;
149+ }
150+ else if ( i == 4 )
151+ {
152+ popularity [ 4 ] . Should ( ) . BeLessThanOrEqualTo ( popularity [ 6 ] ) ;
153+ }
154+ else if ( i == 6 )
155+ {
156+ popularity [ 6 ] . Should ( ) . BeLessThanOrEqualTo ( popularity [ 8 ] ) ;
157+ }
158+ }
107159 }
108160 }
109161}
0 commit comments