github.com/blend/go-sdk@v1.20220411.3/bitflag/bitflag_test.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package bitflag
     9  
    10  import (
    11  	"testing"
    12  
    13  	"github.com/blend/go-sdk/assert"
    14  )
    15  
    16  func TestCombine(t *testing.T) {
    17  	assert := assert.New(t)
    18  	three := Combine(1, 2)
    19  	assert.Equal(3, three)
    20  }
    21  
    22  func TestAny(t *testing.T) {
    23  	assert := assert.New(t)
    24  
    25  	var one Bitflag = 1 << 0
    26  	var two Bitflag = 1 << 1
    27  	var four Bitflag = 1 << 2
    28  	var eight Bitflag = 1 << 3
    29  	var sixteen Bitflag = 1 << 4
    30  
    31  	masterFlag := Combine(one, two, four, eight)
    32  	checkFlag := Combine(one, sixteen)
    33  	assert.True(masterFlag.Any(checkFlag))
    34  	assert.False(masterFlag.Any(1 << 5))
    35  }
    36  
    37  func TestBitFlagAll(t *testing.T) {
    38  	assert := assert.New(t)
    39  
    40  	var one Bitflag = 1 << 0
    41  	var two Bitflag = 1 << 1
    42  	var four Bitflag = 1 << 2
    43  	var eight Bitflag = 1 << 3
    44  	var sixteen Bitflag = 1 << 4
    45  
    46  	masterFlag := Combine(one, two, four, eight)
    47  	checkValidFlag := Combine(one, two)
    48  	checkInvalidFlag := Combine(one, sixteen)
    49  	assert.True(masterFlag.All(checkValidFlag))
    50  	assert.False(masterFlag.All(checkInvalidFlag))
    51  }
    52  
    53  func TestBitFlagSet(t *testing.T) {
    54  	assert := assert.New(t)
    55  
    56  	var zero Bitflag
    57  	var four Bitflag = 1 << 4
    58  	assert.Equal(16, zero.Set(1<<4))
    59  	assert.Equal(16, four.Set(1<<4))
    60  }
    61  
    62  func TestBitFlagZero(t *testing.T) {
    63  	assert := assert.New(t)
    64  
    65  	var zero Bitflag
    66  	var one Bitflag = 1 << 0
    67  	var four Bitflag = 1 << 4
    68  
    69  	flagSet := Combine(one, four)
    70  	assert.Equal(0, zero.Unset(1<<4))
    71  	assert.Equal(0, four.Unset(1<<4))
    72  	assert.Equal(16, flagSet.Unset(one))
    73  }