github.com/scottcagno/storage@v1.8.0/pkg/generic/cache/lru_test.go (about)

     1  package cache
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  	"testing"
     7  )
     8  
     9  type t_key int
    10  type t_val string
    11  
    12  type tentry struct {
    13  	key  t_key
    14  	val  t_val
    15  	want t_val
    16  	isok bool
    17  }
    18  
    19  func makeNEntries(n int) []tentry {
    20  	var tentries []tentry
    21  	for i := 0; i < n; i++ {
    22  		key := t_key(i)
    23  		val := t_val(fmt.Sprintf("value-%0.6d", i))
    24  		tentries = append(tentries, tentry{
    25  			key:  key,
    26  			val:  val,
    27  			want: val,
    28  			isok: true,
    29  		})
    30  	}
    31  	return tentries
    32  }
    33  
    34  func TestLRU_Set(t *testing.T) {
    35  	tests := makeNEntries(64)
    36  	for n, tt := range tests {
    37  		t.Run("LRU_Set", func(t *testing.T) {
    38  			l := NewLRU[t_key, t_val](128)
    39  			got, got1 := l.Set(tt.key, tt.val)
    40  			if n > 0 {
    41  				if !reflect.DeepEqual(got, tt.want) {
    42  					t.Errorf("Del() got = %v, want %v", got, tt.want)
    43  				}
    44  				if got1 != tt.isok {
    45  					t.Errorf("Del() got1 = %v, want %v", got1, tt.isok)
    46  				}
    47  			}
    48  		})
    49  	}
    50  }
    51  
    52  func TestLRU_Get(t *testing.T) {
    53  	tests := makeNEntries(64)
    54  	for _, tt := range tests {
    55  		t.Run("LRU_Get", func(t *testing.T) {
    56  			l := NewLRU[t_key, t_val](128)
    57  			got, got1 := l.Get(tt.key)
    58  			if !reflect.DeepEqual(got, tt.want) {
    59  				t.Errorf("Del() got = %v, want %v", got, tt.want)
    60  			}
    61  			if got1 != tt.isok {
    62  				t.Errorf("Del() got1 = %v, want %v", got1, tt.isok)
    63  			}
    64  		})
    65  	}
    66  }
    67  
    68  func TestLRU_Del(t *testing.T) {
    69  	tests := makeNEntries(64)
    70  	for _, tt := range tests {
    71  		t.Run("LRU_Del", func(t *testing.T) {
    72  			l := NewLRU[t_key, t_val](128)
    73  			got, got1 := l.Del(tt.key)
    74  			if !reflect.DeepEqual(got, tt.want) {
    75  				t.Errorf("Del() got = %v, want %v", got, tt.want)
    76  			}
    77  			if got1 != tt.isok {
    78  				t.Errorf("Del() got1 = %v, want %v", got1, tt.isok)
    79  			}
    80  		})
    81  	}
    82  }