github.com/zuoyebang/bitalostable@v1.0.1-0.20240229032404-e3b99a834294/internal/base/iterator_test.go (about) 1 // Copyright 2022 The LevelDB-Go and Pebble Authors. All rights reserved. Use 2 // of this source code is governed by a BSD-style license that can be found in 3 // the LICENSE file. 4 5 package base 6 7 import ( 8 "fmt" 9 "testing" 10 ) 11 12 func TestFlags(t *testing.T) { 13 t.Run("SeekGEFlags", func(t *testing.T) { 14 f := SeekGEFlagsNone 15 flags := []flag{ 16 { 17 "TrySeekUsingNext", 18 func() bool { return f.TrySeekUsingNext() }, 19 func() { f = f.EnableTrySeekUsingNext() }, 20 func() { f = f.DisableTrySeekUsingNext() }, 21 }, 22 { 23 "RelativeSeek", 24 func() bool { return f.RelativeSeek() }, 25 func() { f = f.EnableRelativeSeek() }, 26 func() { f = f.DisableRelativeSeek() }, 27 }, 28 } 29 ref := make([]bool, len(flags)) 30 checkCombination(t, 0, flags, ref) 31 }) 32 t.Run("SeekLTFlags", func(t *testing.T) { 33 f := SeekLTFlagsNone 34 flags := []flag{ 35 { 36 "RelativeSeek", 37 func() bool { return f.RelativeSeek() }, 38 func() { f = f.EnableRelativeSeek() }, 39 func() { f = f.DisableRelativeSeek() }, 40 }, 41 } 42 ref := make([]bool, len(flags)) 43 checkCombination(t, 0, flags, ref) 44 }) 45 } 46 47 type flag struct { 48 label string 49 pred func() bool 50 set func() 51 unset func() 52 } 53 54 func checkCombination(t *testing.T, i int, flags []flag, ref []bool) { 55 if i >= len(ref) { 56 // Verify that ref matches the flag predicates. 57 for j := 0; j < i; j++ { 58 if got := flags[j].pred(); ref[j] != got { 59 t.Errorf("%s() = %t, want %t", flags[j].label, got, ref[j]) 60 } 61 } 62 return 63 } 64 65 // flag i remains unset. 66 t.Run(fmt.Sprintf("%s begin unset", flags[i].label), func(t *testing.T) { 67 checkCombination(t, i+1, flags, ref) 68 }) 69 70 // set flag i 71 ref[i] = true 72 flags[i].set() 73 t.Run(fmt.Sprintf("%s set", flags[i].label), func(t *testing.T) { 74 checkCombination(t, i+1, flags, ref) 75 }) 76 77 // unset flag i 78 ref[i] = false 79 flags[i].unset() 80 t.Run(fmt.Sprintf("%s unset", flags[i].label), func(t *testing.T) { 81 checkCombination(t, i+1, flags, ref) 82 }) 83 }