github.com/maypok86/otter@v1.2.1/stats_test.go (about)

     1  // Copyright (c) 2024 Alexey Mayshev. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package otter
    16  
    17  import (
    18  	"math"
    19  	"testing"
    20  )
    21  
    22  func TestStats(t *testing.T) {
    23  	var s Stats
    24  	if s.Ratio() != 0.0 {
    25  		t.Fatalf("not valid hit ratio. want 0.0, got %.2f", s.Ratio())
    26  	}
    27  
    28  	expected := int64(math.MaxInt64)
    29  
    30  	s = Stats{
    31  		hits:         math.MaxInt64,
    32  		misses:       math.MaxInt64,
    33  		rejectedSets: math.MaxInt64,
    34  		evictedCount: math.MaxInt64,
    35  		evictedCost:  math.MaxInt64,
    36  	}
    37  
    38  	if s.Hits() != expected {
    39  		t.Fatalf("not valid hits. want %d, got %d", expected, s.Hits())
    40  	}
    41  
    42  	if s.Misses() != expected {
    43  		t.Fatalf("not valid misses. want %d, got %d", expected, s.Misses())
    44  	}
    45  
    46  	if s.Ratio() != 1.0 {
    47  		t.Fatalf("not valid hit ratio. want 1.0, got %.2f", s.Ratio())
    48  	}
    49  
    50  	if s.RejectedSets() != expected {
    51  		t.Fatalf("not valid rejected sets. want %d, got %d", expected, s.RejectedSets())
    52  	}
    53  
    54  	if s.EvictedCount() != expected {
    55  		t.Fatalf("not valid evicted count. want %d, got %d", expected, s.EvictedCount())
    56  	}
    57  
    58  	if s.EvictedCost() != expected {
    59  		t.Fatalf("not valid evicted cost. want %d, got %d", expected, s.EvictedCost())
    60  	}
    61  }