package shroom_internals import ( "testing" ) func TestCache(t *testing.T) { cache := DataCache{} for i := 0; i < 2*MAX_CACHE_POINTS; i++ { cache.Add(&Datapoint{Time: uint64(i)}) if len(cache.data) > MAX_CACHE_POINTS { t.Errorf("cache expanded past limit at %d, len = %d \n", i, len(cache.data)) } if (i % 100) == 0 { for j := 0; j < 3*MAX_CACHE_POINTS; j += 25 { result := cache.ReadSince(uint64(j)) if j > (i - MAX_CACHE_POINTS) { if result == nil { t.Errorf("read was nil instead of an array at %d, %d\n", i, j) } else { expected := i - j + 1 if expected < 0 { expected = 0 } if len(result) != expected { t.Errorf("invalid read len %d != %d\n", len(result), expected) } if expected < 0 { for k, v := range result { if v.Time != uint64(k+j) { t.Errorf("incorrect time %d != %d\n", v.Time, j+k) } } } } } else { if result != nil { t.Errorf("read should have been nil at %d, %d %v\n", i, j, result) } } } } } } func BenchmarkCacheAdd(b *testing.B) { cache := DataCache{} dp := Datapoint{} for n := 0; n < b.N; n++ { cache.Add(&dp) } } func BenchmarkCacheRead(b *testing.B) { cache := DataCache{} for i := 0; i < int(1.3*MAX_CACHE_POINTS); i++ { cache.Add(&Datapoint{Time: uint64(i)}) } for n := 0; n < b.N; n++ { cache.ReadSince(MAX_CACHE_POINTS) } }