github.com/outcaste-io/ristretto@v0.2.3/sim/sim_test.go (about) 1 /* 2 * Copyright 2019 Dgraph Labs, Inc. and Contributors 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 sim 18 19 import ( 20 "bytes" 21 "compress/gzip" 22 "os" 23 "testing" 24 ) 25 26 func TestZipfian(t *testing.T) { 27 s := NewZipfian(1.5, 1, 100) 28 m := make(map[uint64]uint64, 100) 29 for i := 0; i < 100; i++ { 30 k, err := s() 31 if err != nil { 32 t.Fatal(err) 33 } 34 m[k]++ 35 } 36 if len(m) == 0 || len(m) == 100 { 37 t.Fatal("zipfian not skewed") 38 } 39 } 40 41 func TestUniform(t *testing.T) { 42 s := NewUniform(100) 43 for i := 0; i < 100; i++ { 44 if _, err := s(); err != nil { 45 t.Fatal(err) 46 } 47 } 48 } 49 50 func TestParseLIRS(t *testing.T) { 51 s := NewReader(ParseLIRS, bytes.NewReader([]byte{ 52 '0', '\n', 53 '1', '\r', '\n', 54 '2', '\r', '\n', 55 })) 56 for i := uint64(0); i < 3; i++ { 57 v, err := s() 58 if err != nil { 59 t.Fatal(err) 60 } 61 if v != i { 62 t.Fatal("value mismatch") 63 } 64 } 65 } 66 67 func TestReadLIRS(t *testing.T) { 68 f, err := os.Open("./gli.lirs.gz") 69 if err != nil { 70 t.Fatal(err) 71 } 72 r, err := gzip.NewReader(f) 73 if err != nil { 74 t.Fatal(err) 75 } 76 s := NewReader(ParseLIRS, r) 77 for i := uint64(0); i < 100; i++ { 78 if _, err = s(); err != nil { 79 t.Fatal(err) 80 } 81 } 82 } 83 84 func TestParseARC(t *testing.T) { 85 s := NewReader(ParseARC, bytes.NewReader([]byte{ 86 '1', '2', '7', ' ', '6', '4', ' ', '0', ' ', '0', '\r', '\n', 87 '1', '9', '1', ' ', '3', '6', ' ', '0', ' ', '0', '\r', '\n', 88 })) 89 for i := uint64(0); i < 100; i++ { 90 v, err := s() 91 if err != nil { 92 t.Fatal(err) 93 } 94 if v != 127+i { 95 t.Fatal("value mismatch") 96 } 97 } 98 } 99 100 func TestCollection(t *testing.T) { 101 s := NewUniform(100) 102 c := Collection(s, 100) 103 if len(c) != 100 { 104 t.Fatal("collection not full") 105 } 106 } 107 108 func TestStringCollection(t *testing.T) { 109 s := NewUniform(100) 110 c := StringCollection(s, 100) 111 if len(c) != 100 { 112 t.Fatal("string collection not full") 113 } 114 }