vitess.io/vitess@v0.16.2/go/history/history_test.go (about) 1 /* 2 Copyright 2019 The Vitess Authors. 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 agreedto 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 history 18 19 import ( 20 "testing" 21 ) 22 23 func TestHistory(t *testing.T) { 24 q := New(4) 25 26 i := 0 27 for ; i < 2; i++ { 28 q.Add(i) 29 } 30 want := []int{1, 0} 31 records := q.Records() 32 if want, got := len(want), len(records); want != got { 33 t.Errorf("len(records): want %v, got %v. records: %+v", want, got, q) 34 } 35 for i, record := range records { 36 if record != want[i] { 37 t.Errorf("record doesn't match: want %v, got %v", want[i], record) 38 } 39 } 40 41 for ; i < 6; i++ { 42 q.Add(i) 43 } 44 45 want = []int{5, 4, 3, 2} 46 records = q.Records() 47 if want, got := len(want), len(records); want != got { 48 t.Errorf("len(records): want %v, got %v. records: %+v", want, got, q) 49 } 50 for i, record := range records { 51 if record != want[i] { 52 t.Errorf("record doesn't match: want %v, got %v", want[i], record) 53 } 54 } 55 } 56 57 func TestLatest(t *testing.T) { 58 h := New(4) 59 60 // Add first value. 61 h.Add(mod10(1)) 62 if got, want := int(h.Records()[0].(mod10)), 1; got != want { 63 t.Errorf("h.Records()[0] = %v, want %v", got, want) 64 } 65 if got, want := int(h.Latest().(mod10)), 1; got != want { 66 t.Errorf("h.Latest() = %v, want %v", got, want) 67 } 68 69 // Add value that isn't a "duplicate". 70 h.Add(mod10(2)) 71 if got, want := int(h.Records()[0].(mod10)), 2; got != want { 72 t.Errorf("h.Records()[0] = %v, want %v", got, want) 73 } 74 if got, want := int(h.Latest().(mod10)), 2; got != want { 75 t.Errorf("h.Latest() = %v, want %v", got, want) 76 } 77 78 // Add value that IS a "duplicate". 79 h.Add(mod10(12)) 80 // Records()[0] doesn't change. 81 if got, want := int(h.Records()[0].(mod10)), 2; got != want { 82 t.Errorf("h.Records()[0] = %v, want %v", got, want) 83 } 84 // Latest() does change. 85 if got, want := int(h.Latest().(mod10)), 12; got != want { 86 t.Errorf("h.Latest() = %v, want %v", got, want) 87 } 88 } 89 90 type duplic int 91 92 func (d duplic) IsDuplicate(other any) bool { 93 return d == other 94 } 95 96 func TestIsEquivalent(t *testing.T) { 97 q := New(4) 98 q.Add(duplic(0)) 99 q.Add(duplic(0)) 100 if got, want := len(q.Records()), 1; got != want { 101 t.Errorf("len(q.Records())=%v, want %v", got, want) 102 } 103 } 104 105 type mod10 int 106 107 func (m mod10) IsDuplicate(other any) bool { 108 return m%10 == other.(mod10)%10 109 }