1//  Copyright (c) 2017 Couchbase, Inc.
2//  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
3//  except in compliance with the License. You may obtain a copy of the License at
4//    http://www.apache.org/licenses/LICENSE-2.0
5//  Unless required by applicable law or agreed to in writing, software distributed under the
6//  License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
7//  either express or implied. See the License for the specific language governing permissions
8//  and limitations under the License.
9
10package util
11
12import (
13	"strconv"
14	"testing"
15)
16
17type testCache struct {
18	value int
19}
20
21func TestCache(t *testing.T) {
22	var names []string
23
24	names = make([]string, 50)
25	c := NewGenCache(100)
26
27	// Add and Get
28	for i := 1; i <= 50; i++ {
29		v := testCache{value: i}
30		id := strconv.Itoa(i)
31		names[i-1] = id
32
33		c.Add(v, id, nil)
34		s := c.Size()
35		if s != i {
36			t.Errorf("Add test: expected %v elements, got %v", i, s)
37		}
38		vi := c.Get(id, nil)
39		if vi == nil {
40			t.Errorf("Add test: expected to find %v, got nothing", id)
41		}
42		v1, ok := vi.(testCache)
43		if !ok {
44			t.Errorf("Add test: invalid element type for %v", id)
45		}
46		if v1.value != i {
47			t.Errorf("Add test: was expecting %v, read back %v", i, v1.value)
48		}
49	}
50
51	// Delete
52	sz := 49
53	tgt := "25"
54	r := c.Delete(tgt, nil)
55	if !r {
56		t.Errorf("Delete test: element not deleted %v", tgt)
57	}
58	s := c.Size()
59	if s != sz {
60		t.Errorf("Delete test: expected %v elements, got %v", sz, s)
61	}
62	r = c.Delete(tgt, nil)
63	if r {
64		t.Errorf("Delete test: deleted element deleted again %v", tgt)
65	}
66	s = c.Size()
67	if s != sz {
68		t.Errorf("Delete test: expected %v elements, got %v", sz, s)
69	}
70
71	// Update
72	id := "50"
73	v := testCache{value: 51}
74	c.Add(v, id, nil)
75	s = c.Size()
76	if s != sz {
77		t.Errorf("Update test: expected %v elements, got %v", sz, s)
78	}
79	vi := c.Get(id, nil)
80	if vi == nil {
81		t.Errorf("Update test: expected to find %v, got nothing", id)
82	}
83	v1, ok := vi.(testCache)
84	if !ok {
85		t.Errorf("Update test: invalid element type for %v", id)
86	}
87	if v1.value != 51 {
88		t.Errorf("Update test: was expecting %v, read back %v", 51, v1.value)
89	}
90
91	// Names, Foreach
92	n := c.Names()
93	s = len(n)
94	if s != sz {
95		t.Errorf("Foreach test: expected %v elements, got %v", sz, s)
96	}
97
98	for i := 0; i < sz; i++ {
99		iName, err := strconv.Atoi(n[i])
100
101		if err != nil || iName < 1 || iName > 50 {
102			t.Errorf("Update test: element name %v is not valid %v %v", n[i], i, n)
103		} else if names[iName-1] == "" {
104			t.Errorf("Update test: element name %v is duplicate", n[i])
105		} else {
106			names[iName-1] = ""
107		}
108		v := c.Get(n[i], nil)
109		if v == nil {
110			t.Errorf("Foreach test: expected to find %v, got nothing", n[i])
111		}
112	}
113
114	c.SetLimit(sz)
115}
116