github.com/aaabigfish/gopkg@v1.1.0/cache/lrucache/lrucache_test.go (about)

     1  package lrucache
     2  
     3  import (
     4  	"container/list"
     5  	"testing"
     6  )
     7  
     8  type Elem struct {
     9  	key   int
    10  	value string
    11  }
    12  
    13  func Test_New(t *testing.T) {
    14  	lc := New(5)
    15  	if lc.Len() != 0 {
    16  		t.Error("case 1 failed")
    17  	}
    18  }
    19  
    20  func Test_Put(t *testing.T) {
    21  	lc := New(0)
    22  	lc.Put(1, "1")
    23  	if lc.Len() != 0 {
    24  		t.Error("case 1.1 failed")
    25  	}
    26  
    27  	lc = New(5)
    28  	lc.Put(1, "1")
    29  	lc.Put(2, "2")
    30  	lc.Put(1, "3")
    31  	if lc.Len() != 2 {
    32  		t.Error("case 2.1 failed")
    33  	}
    34  
    35  	l := list.New()
    36  	l.PushBack(&Elem{1, "3"})
    37  	l.PushBack(&Elem{2, "2"})
    38  
    39  	e := l.Front()
    40  	for c := lc.Front(); c != nil; c = c.Next() {
    41  		v := e.Value.(*Elem)
    42  		if c.Key.(int) != v.key {
    43  			t.Error("case 2.2 failed: ", c.Key.(int), v.key)
    44  		}
    45  		if c.Value.(string) != v.value {
    46  			t.Error("case 2.3 failed: ", c.Value.(string), v.value)
    47  		}
    48  		e = e.Next()
    49  	}
    50  
    51  	lc.Put(3, "4")
    52  	lc.Put(4, "5")
    53  	lc.Put(5, "6")
    54  	lc.Put(2, "7")
    55  	if lc.Len() != 5 {
    56  		t.Error("case 3.1 failed")
    57  	}
    58  
    59  	l = list.New()
    60  	l.PushBack(&Elem{2, "7"})
    61  	l.PushBack(&Elem{5, "6"})
    62  	l.PushBack(&Elem{4, "5"})
    63  	l.PushBack(&Elem{3, "4"})
    64  	l.PushBack(&Elem{1, "3"})
    65  
    66  	rl := list.New()
    67  	rl.PushBack(&Elem{1, "3"})
    68  	rl.PushBack(&Elem{3, "4"})
    69  	rl.PushBack(&Elem{4, "5"})
    70  	rl.PushBack(&Elem{5, "6"})
    71  	rl.PushBack(&Elem{2, "7"})
    72  
    73  	e = l.Front()
    74  	for c := lc.Front(); c != nil; c = c.Next() {
    75  		v := e.Value.(*Elem)
    76  		if c.Key.(int) != v.key {
    77  			t.Error("case 3.2 failed: ", c.Key.(int), v.key)
    78  		}
    79  		if c.Value.(string) != v.value {
    80  			t.Error("case 3.3 failed: ", c.Value.(string), v.value)
    81  		}
    82  		e = e.Next()
    83  	}
    84  
    85  	e = rl.Front()
    86  	for c := lc.Back(); c != nil; c = c.Prev() {
    87  		v := e.Value.(*Elem)
    88  		if c.Key.(int) != v.key {
    89  			t.Error("case 3.4 failed: ", c.Key.(int), v.key)
    90  		}
    91  		if c.Value.(string) != v.value {
    92  			t.Error("case 3.5 failed: ", c.Value.(string), v.value)
    93  		}
    94  		e = e.Next()
    95  	}
    96  
    97  	lc.Put(6, "8")
    98  	if lc.Len() != 5 {
    99  		t.Error("case 4.1 failed")
   100  	}
   101  
   102  	l = list.New()
   103  	l.PushBack(&Elem{6, "8"})
   104  	l.PushBack(&Elem{2, "7"})
   105  	l.PushBack(&Elem{5, "6"})
   106  	l.PushBack(&Elem{4, "5"})
   107  	l.PushBack(&Elem{3, "4"})
   108  
   109  	e = l.Front()
   110  	for c := lc.Front(); c != nil; c = c.Next() {
   111  		v := e.Value.(*Elem)
   112  		if c.Key.(int) != v.key {
   113  			t.Error("case 4.2 failed: ", c.Key.(int), v.key)
   114  		}
   115  		if c.Value.(string) != v.value {
   116  			t.Error("case 4.3 failed: ", c.Value.(string), v.value)
   117  		}
   118  		e = e.Next()
   119  	}
   120  }
   121  
   122  func Test_Get(t *testing.T) {
   123  	lc := New(2)
   124  	lc.Put(1, "1")
   125  	lc.Put(2, "2")
   126  	if v, _ := lc.Get(1); v != "1" {
   127  		t.Error("case 1.1 failed")
   128  	}
   129  	lc.Put(3, "3")
   130  	if lc.Len() != 2 {
   131  		t.Error("case 1.2 failed")
   132  	}
   133  
   134  	l := list.New()
   135  	l.PushBack(&Elem{3, "3"})
   136  	l.PushBack(&Elem{1, "1"})
   137  
   138  	e := l.Front()
   139  	for c := lc.Front(); c != nil; c = c.Next() {
   140  		v := e.Value.(*Elem)
   141  		if c.Key.(int) != v.key {
   142  			t.Error("case 1.3 failed: ", c.Key.(int), v.key)
   143  		}
   144  		if c.Value.(string) != v.value {
   145  			t.Error("case 1.4 failed: ", c.Value.(string), v.value)
   146  		}
   147  		e = e.Next()
   148  	}
   149  }
   150  
   151  func Test_Delete(t *testing.T) {
   152  	lc := New(5)
   153  	lc.Put(3, "4")
   154  	lc.Put(4, "5")
   155  	lc.Put(5, "6")
   156  	lc.Put(2, "7")
   157  	lc.Put(6, "8")
   158  	lc.Delete(5)
   159  
   160  	l := list.New()
   161  	l.PushBack(&Elem{6, "8"})
   162  	l.PushBack(&Elem{2, "7"})
   163  	l.PushBack(&Elem{4, "5"})
   164  	l.PushBack(&Elem{3, "4"})
   165  	if lc.Len() != 4 {
   166  		t.Error("case 1.1 failed")
   167  	}
   168  
   169  	e := l.Front()
   170  	for c := lc.Front(); c != nil; c = c.Next() {
   171  		v := e.Value.(*Elem)
   172  		if c.Key.(int) != v.key {
   173  			t.Error("case 1.2 failed: ", c.Key.(int), v.key)
   174  		}
   175  		if c.Value.(string) != v.value {
   176  			t.Error("case 1.3 failed: ", c.Value.(string), v.value)
   177  		}
   178  		e = e.Next()
   179  	}
   180  
   181  	lc.Delete(6)
   182  
   183  	l = list.New()
   184  	l.PushBack(&Elem{2, "7"})
   185  	l.PushBack(&Elem{4, "5"})
   186  	l.PushBack(&Elem{3, "4"})
   187  	if lc.Len() != 3 {
   188  		t.Error("case 2.1 failed")
   189  	}
   190  
   191  	e = l.Front()
   192  	for c := lc.Front(); c != nil; c = c.Next() {
   193  		v := e.Value.(*Elem)
   194  		if c.Key.(int) != v.key {
   195  			t.Error("case 2.2 failed: ", c.Key.(int), v.key)
   196  		}
   197  		if c.Value.(string) != v.value {
   198  			t.Error("case 2.3 failed: ", c.Value.(string), v.value)
   199  		}
   200  		e = e.Next()
   201  	}
   202  
   203  	lc.Delete(3)
   204  
   205  	l = list.New()
   206  	l.PushBack(&Elem{2, "7"})
   207  	l.PushBack(&Elem{4, "5"})
   208  	if lc.Len() != 2 {
   209  		t.Error("case 3.1 failed")
   210  	}
   211  
   212  	e = l.Front()
   213  	for c := lc.Front(); c != nil; c = c.Next() {
   214  		v := e.Value.(*Elem)
   215  		if c.Key.(int) != v.key {
   216  			t.Error("case 3.2 failed: ", c.Key.(int), v.key)
   217  		}
   218  		if c.Value.(string) != v.value {
   219  			t.Error("case 3.3 failed: ", c.Value.(string), v.value)
   220  		}
   221  		e = e.Next()
   222  	}
   223  }
   224  
   225  func Test_Range(t *testing.T) {
   226  	lc := New(5)
   227  	lc.Put(3, "4")
   228  	lc.Put(4, "5")
   229  	lc.Put(5, "6")
   230  	lc.Put(2, "7")
   231  	lc.Put(6, "8")
   232  
   233  	l := list.New()
   234  	l.PushBack(&Elem{6, "8"})
   235  	l.PushBack(&Elem{2, "7"})
   236  	l.PushBack(&Elem{5, "6"})
   237  	l.PushBack(&Elem{4, "5"})
   238  	l.PushBack(&Elem{3, "4"})
   239  
   240  	e := l.Front()
   241  	lc.Range(
   242  		func(key, value interface{}) bool {
   243  			v := e.Value.(*Elem)
   244  			if key.(int) != v.key {
   245  				t.Error("case 1.1 failed: ", key.(int), v.key)
   246  			}
   247  			if value.(string) != v.value {
   248  				t.Error("case 1.2 failed: ", value.(string), v.value)
   249  			}
   250  			e = e.Next()
   251  			return true
   252  		})
   253  
   254  	if e != nil {
   255  		t.Error("case 1.3 failed: ", e.Value)
   256  	}
   257  }