github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/pkg/util/bitflag_test.go (about)

     1  // Copyright 2020 PingCAP, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package util
    15  
    16  import (
    17  	"testing"
    18  
    19  	"github.com/stretchr/testify/require"
    20  )
    21  
    22  const (
    23  	FlagA Flag = 1 << Flag(iota)
    24  	FlagB
    25  	FlagC
    26  	FlagD
    27  )
    28  
    29  func TestExample(t *testing.T) {
    30  	var flag Flag
    31  
    32  	flag.Add(FlagA)
    33  	flag.Add(FlagB, FlagC)
    34  	flag.Add(FlagC, FlagD)
    35  	flag.Clear()
    36  	flag.Add(FlagA, FlagB, FlagC, FlagD)
    37  	flag.Remove(FlagA)
    38  	flag.Remove(FlagB, FlagC)
    39  
    40  	require.False(t, flag.HasAll(FlagA))
    41  	require.False(t, flag.HasAll(FlagB))
    42  	require.False(t, flag.HasAll(FlagC))
    43  	require.True(t, flag.HasAll(FlagD))
    44  }
    45  
    46  func TestAdd(t *testing.T) {
    47  	var flag Flag
    48  
    49  	flag.Add(FlagA)
    50  	flag.Add(FlagB)
    51  	require.True(t, flag.HasAll(FlagA, FlagB))
    52  
    53  	flag.Add(FlagA, FlagB)
    54  	require.True(t, flag.HasAll(FlagA, FlagB))
    55  }
    56  
    57  func TestRemove(t *testing.T) {
    58  	var flag Flag
    59  
    60  	flag.Add(FlagA, FlagB, FlagC)
    61  	require.True(t, flag.HasAll(FlagA, FlagB, FlagC))
    62  
    63  	flag.Remove(FlagB)
    64  	require.False(t, flag.HasAll(FlagB))
    65  	require.True(t, flag.HasAll(FlagA, FlagC))
    66  }
    67  
    68  func TestClear(t *testing.T) {
    69  	var flag Flag
    70  
    71  	flag.Add(FlagA, FlagB, FlagC)
    72  	require.True(t, flag.HasAll(FlagA, FlagB, FlagC))
    73  
    74  	flag.Clear()
    75  	require.False(t, flag.HasOne(FlagA, FlagB, FlagC))
    76  }