github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/golang/groupcache/lru/lru_test.go (about)

     1  /*
     2  Copyright 2013 Google Inc.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8       http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package lru
    18  
    19  import (
    20  	"testing"
    21  )
    22  
    23  type simpleStruct struct {
    24  	int
    25  	string
    26  }
    27  
    28  type complexStruct struct {
    29  	int
    30  	simpleStruct
    31  }
    32  
    33  var getTests = []struct {
    34  	name       string
    35  	keyToAdd   interface{}
    36  	keyToGet   interface{}
    37  	expectedOk bool
    38  }{
    39  	{"string_hit", "myKey", "myKey", true},
    40  	{"string_miss", "myKey", "nonsense", false},
    41  	{"simple_struct_hit", simpleStruct{1, "two"}, simpleStruct{1, "two"}, true},
    42  	{"simeple_struct_miss", simpleStruct{1, "two"}, simpleStruct{0, "noway"}, false},
    43  	{"complex_struct_hit", complexStruct{1, simpleStruct{2, "three"}},
    44  		complexStruct{1, simpleStruct{2, "three"}}, true},
    45  }
    46  
    47  func TestGet(t *testing.T) {
    48  	for _, tt := range getTests {
    49  		lru := New(0)
    50  		lru.Add(tt.keyToAdd, 1234)
    51  		val, ok := lru.Get(tt.keyToGet)
    52  		if ok != tt.expectedOk {
    53  			t.Fatalf("%s: cache hit = %v; want %v", tt.name, ok, !ok)
    54  		} else if ok && val != 1234 {
    55  			t.Fatalf("%s expected get to return 1234 but got %v", tt.name, val)
    56  		}
    57  	}
    58  }
    59  
    60  func TestRemove(t *testing.T) {
    61  	lru := New(0)
    62  	lru.Add("myKey", 1234)
    63  	if val, ok := lru.Get("myKey"); !ok {
    64  		t.Fatal("TestRemove returned no match")
    65  	} else if val != 1234 {
    66  		t.Fatalf("TestRemove failed.  Expected %d, got %v", 1234, val)
    67  	}
    68  
    69  	lru.Remove("myKey")
    70  	if _, ok := lru.Get("myKey"); ok {
    71  		t.Fatal("TestRemove returned a removed entry")
    72  	}
    73  }