flag_test.tpl (2224B)
1 package bitutil_test 2 3 import ( 4 "strings" 5 "testing" 6 7 "codeberg.org/gruf/go-bytes" 8 ) 9 10 {{ range $idx, $size := . }} 11 12 func TestFlags{{ $size.Size }}Get(t *testing.T) { 13 var mask, flags bitutil.Flags{{ $size.Size }} 14 15 {{ range $idx := $size.Bits }} 16 17 mask = bitutil.Flags{{ $size.Size }}(1) << {{ $idx }} 18 19 flags = 0 20 21 flags |= mask 22 if !flags.Get({{ $idx }}) { 23 t.Error("failed .Get() set Flags{{ $size.Size }} bit at index {{ $idx }}") 24 } 25 26 flags = ^bitutil.Flags{{ $size.Size }}(0) 27 28 flags &= ^mask 29 if flags.Get({{ $idx }}) { 30 t.Error("failed .Get() unset Flags{{ $size.Size }} bit at index {{ $idx }}") 31 } 32 33 flags = 0 34 35 flags |= mask 36 if !flags.Get{{ $idx }}() { 37 t.Error("failed .Get{{ $idx }}() set Flags{{ $size.Size }} bit at index {{ $idx }}") 38 } 39 40 flags = ^bitutil.Flags{{ $size.Size }}(0) 41 42 flags &= ^mask 43 if flags.Get{{ $idx }}() { 44 t.Error("failed .Get{{ $idx }}() unset Flags{{ $size.Size }} bit at index {{ $idx }}") 45 } 46 47 {{ end }} 48 } 49 50 func TestFlags{{ $size.Size }}Set(t *testing.T) { 51 var mask, flags bitutil.Flags{{ $size.Size }} 52 53 {{ range $idx := $size.Bits }} 54 55 mask = bitutil.Flags{{ $size.Size }}(1) << {{ $idx }} 56 57 flags = 0 58 59 flags = flags.Set({{ $idx }}) 60 if flags & mask == 0 { 61 t.Error("failed .Set() Flags{{ $size.Size }} bit at index {{ $idx }}") 62 } 63 64 flags = 0 65 66 flags = flags.Set{{ $idx }}() 67 if flags & mask == 0 { 68 t.Error("failed .Set{{ $idx }}() Flags{{ $size.Size }} bit at index {{ $idx }}") 69 } 70 71 {{ end }} 72 } 73 74 func TestFlags{{ $size.Size }}Unset(t *testing.T) { 75 var mask, flags bitutil.Flags{{ $size.Size }} 76 77 {{ range $idx := $size.Bits }} 78 79 mask = bitutil.Flags{{ $size.Size }}(1) << {{ $idx }} 80 81 flags = ^bitutil.Flags{{ $size.Size }}(0) 82 83 flags = flags.Unset({{ $idx }}) 84 if flags & mask != 0 { 85 t.Error("failed .Unset() Flags{{ $size.Size }} bit at index {{ $idx }}") 86 } 87 88 flags = ^bitutil.Flags{{ $size.Size }}(0) 89 90 flags = flags.Unset{{ $idx }}() 91 if flags & mask != 0 { 92 t.Error("failed .Unset{{ $idx }}() Flags{{ $size.Size }} bit at index {{ $idx }}") 93 } 94 95 {{ end }} 96 } 97 98 {{ end }}