github.com/cockroachdb/pebble@v1.1.1-0.20240513155919-3622ade60459/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  				"BatchJustRefreshed",
    30  				func() bool { return f.BatchJustRefreshed() },
    31  				func() { f = f.EnableBatchJustRefreshed() },
    32  				func() { f = f.DisableBatchJustRefreshed() },
    33  			},
    34  		}
    35  		ref := make([]bool, len(flags))
    36  		checkCombination(t, 0, flags, ref)
    37  	})
    38  	t.Run("SeekLTFlags", func(t *testing.T) {
    39  		f := SeekLTFlagsNone
    40  		flags := []flag{
    41  			{
    42  				"RelativeSeek",
    43  				func() bool { return f.RelativeSeek() },
    44  				func() { f = f.EnableRelativeSeek() },
    45  				func() { f = f.DisableRelativeSeek() },
    46  			},
    47  		}
    48  		ref := make([]bool, len(flags))
    49  		checkCombination(t, 0, flags, ref)
    50  	})
    51  }
    52  
    53  type flag struct {
    54  	label string
    55  	pred  func() bool
    56  	set   func()
    57  	unset func()
    58  }
    59  
    60  func checkCombination(t *testing.T, i int, flags []flag, ref []bool) {
    61  	if i >= len(ref) {
    62  		// Verify that ref matches the flag predicates.
    63  		for j := 0; j < i; j++ {
    64  			if got := flags[j].pred(); ref[j] != got {
    65  				t.Errorf("%s() = %t, want %t", flags[j].label, got, ref[j])
    66  			}
    67  		}
    68  		return
    69  	}
    70  
    71  	// flag i remains unset.
    72  	t.Run(fmt.Sprintf("%s begin unset", flags[i].label), func(t *testing.T) {
    73  		checkCombination(t, i+1, flags, ref)
    74  	})
    75  
    76  	// set flag i
    77  	ref[i] = true
    78  	flags[i].set()
    79  	t.Run(fmt.Sprintf("%s set", flags[i].label), func(t *testing.T) {
    80  		checkCombination(t, i+1, flags, ref)
    81  	})
    82  
    83  	// unset flag i
    84  	ref[i] = false
    85  	flags[i].unset()
    86  	t.Run(fmt.Sprintf("%s unset", flags[i].label), func(t *testing.T) {
    87  		checkCombination(t, i+1, flags, ref)
    88  	})
    89  }