github.com/pingcap/ticdc@v0.0.0-20220526033649-485a10ef2652/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  	"github.com/pingcap/check"
    18  	"github.com/pingcap/ticdc/pkg/util/testleak"
    19  )
    20  
    21  const (
    22  	FlagA Flag = 1 << Flag(iota)
    23  	FlagB
    24  	FlagC
    25  	FlagD
    26  )
    27  
    28  type bitFlagSuite struct{}
    29  
    30  var _ = check.Suite(&bitFlagSuite{})
    31  
    32  func (b *bitFlagSuite) TestExample(c *check.C) {
    33  	defer testleak.AfterTest(c)()
    34  	var flag Flag
    35  
    36  	flag.Add(FlagA)
    37  	flag.Add(FlagB, FlagC)
    38  	flag.Add(FlagC, FlagD)
    39  	flag.Clear()
    40  	flag.Add(FlagA, FlagB, FlagC, FlagD)
    41  	flag.Remove(FlagA)
    42  	flag.Remove(FlagB, FlagC)
    43  
    44  	c.Assert(flag.HasAll(FlagA), check.IsFalse)
    45  	c.Assert(flag.HasAll(FlagB), check.IsFalse)
    46  	c.Assert(flag.HasAll(FlagC), check.IsFalse)
    47  	c.Assert(flag.HasAll(FlagD), check.IsTrue)
    48  }
    49  
    50  func (b *bitFlagSuite) TestAdd(c *check.C) {
    51  	defer testleak.AfterTest(c)()
    52  	var flag Flag
    53  
    54  	flag.Add(FlagA)
    55  	flag.Add(FlagB)
    56  	c.Check(flag.HasAll(FlagA, FlagB), check.IsTrue)
    57  
    58  	flag.Add(FlagA, FlagB)
    59  	c.Check(flag.HasAll(FlagA, FlagB), check.IsTrue)
    60  }
    61  
    62  func (b *bitFlagSuite) TestRemove(c *check.C) {
    63  	defer testleak.AfterTest(c)()
    64  	var flag Flag
    65  
    66  	flag.Add(FlagA, FlagB, FlagC)
    67  	c.Check(flag.HasAll(FlagA, FlagB, FlagC), check.IsTrue)
    68  
    69  	flag.Remove(FlagB)
    70  	c.Check(flag.HasAll(FlagB), check.IsFalse)
    71  	c.Check(flag.HasAll(FlagA, FlagC), check.IsTrue)
    72  }
    73  
    74  func (b *bitFlagSuite) TestClear(c *check.C) {
    75  	defer testleak.AfterTest(c)()
    76  	var flag Flag
    77  
    78  	flag.Add(FlagA, FlagB, FlagC)
    79  	c.Check(flag.HasAll(FlagA, FlagB, FlagC), check.IsTrue)
    80  
    81  	flag.Clear()
    82  	c.Check(flag.HasOne(FlagA, FlagB, FlagC), check.IsFalse)
    83  }