diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 2343274..35ed887 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -1,29 +1,29 @@ name: Go -on: [ push, workflow_dispatch ] +on: [push, workflow_dispatch] jobs: golangcilint: - name: Golangci Lint + name: golangci-lint runs-on: ubuntu-latest steps: - name: Checkout code - uses: actions/checkout@v2 + uses: actions/checkout@v3 - name: golangci-lint - uses: golangci/golangci-lint-action@v2 + uses: golangci/golangci-lint-action@v3 with: - version: v1.41 + version: v1.51 test: strategy: matrix: - go-version: [ 1.16.x ] - name: Tests + go-version: [1.18.x, 1.19.x, 1.20.x] + name: Unit tests runs-on: ubuntu-latest steps: - name: Checkout code - uses: actions/checkout@v2 + uses: actions/checkout@v3 - name: Go install - uses: actions/setup-go@v2 + uses: actions/setup-go@v3 with: go-version: ${{ matrix.go-version }} - name: Run tests diff --git a/.golangci.yml b/.golangci.yml index 91aedc9..888090c 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,72 +1,46 @@ run: modules-download-mode: readonly # Do not update dependencies. - linters-settings: - gocritic: - enabled-tags: - - diagnostic - - experimental - - opinionated - - performance - - style - disabled-checks: - - dupImport # https://github.com/go-critic/go-critic/issues/845 - gomnd: - settings: - mnd: - # don't include the "operation" and "assign" - checks: - - argument - - case - - condition - - return - lll: - line-length: 160 - maligned: - suggest-new: true - misspell: - locale: US - staticcheck: - go: "1.16" - gosimple: - go: "1.16" - stylecheck: - go: "1.16" - unused: - go: "1.16" - + varnamelen: + min-name-length: 2 linters: - # please, do not use `enable-all`: it's deprecated and will be removed soon. - # inverted configuration with `enable-all` and `disable` is not scalable during updates of golangci-lint disable-all: true enable: # Default linters - - deadcode - errcheck - gosimple - govet - ineffassign - staticcheck - - structcheck - typecheck - unused - - varcheck # Additional linters + - asasalint - asciicheck + - bidichk - bodyclose + - containedctx + - contextcheck - cyclop + - decorder - depguard - dogsled - dupl + - dupword - durationcheck + - errchkjson + - errname - errorlint + - execinquery - exhaustive - # - exhaustivestruct + - exhaustruct - exportloopref - forbidigo - forcetypeassert - funlen - gci + # - ginkgolinter # Will be enabled in 1.51. + # - gocheckcompilerdirectives # Will be enabled in 1.51. - gochecknoglobals - gochecknoinits - gocognit @@ -78,47 +52,79 @@ linters: - goerr113 - gofmt - gofumpt - - goheader # Configure it + - goheader - goimports - gomnd - gomoddirectives - gomodguard - goprintffuncname - gosec + - grouper - importas + - interfacebloat + # - ireturn # Does not support generics, see https://github.com/butuzov/ireturn/issues/39. - lll + - loggercheck + - maintidx - makezero - misspell + # - musttag # Will be enabled in 1.51. - nakedret - nestif + - nilerr + - nilnil - nlreturn - noctx - - nolintlint + # - nolintlint # Let's keed nolintlint if we decide to add it. + # - nonamedreturns # This linter does not make any sense. + - nosprintfhostport - paralleltest - prealloc - predeclared - promlinter + - reassign - revive - rowserrcheck - sqlclosecheck - stylecheck - tagliatelle + - tenv + - testableexamples - testpackage - thelper - tparallel - unconvert - unparam + - usestdlibvars + - varnamelen - wastedassign - whitespace - wrapcheck - wsl - issues: exclude-rules: - path: _test\.go linters: - - gocyclo - - funlen - - goerr113 - - dupl - - exhaustivestruct + - dupl + - funlen + - dupword + - path: doc\.go + linters: + - lll + - path: constraints\/constraints_test\.go + linters: + - gocritic + - wsl + - gochecknoglobals + - varnamelen + - gosec + - cyclop + - unconvert + - paralleltest + - tparallel + - unused + +# golangci.com configuration +# https://github.com/golangci/golangci/wiki/Configuration +service: + golangci-lint-version: 1.51.x # use the fixed version to not introduce new linters unexpectedly diff --git a/README.md b/README.md index 3c9c5c1..7c75c9f 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,8 @@ [![Codecov](https://codecov.io/gh/aohorodnyk/binflags/branch/main/graph/badge.svg?token=61SCDABJJ0)](https://codecov.io/gh/aohorodnyk/binflags) ![Test](https://github.com/aohorodnyk/binflags/workflows/Test/badge.svg) ![Lint](https://github.com/aohorodnyk/binflags/workflows/Lint/badge.svg) ## Golang 1.18 -If you use Golang version 1.18 and above, I'll suggest to use [binflags](https://pkg.go.dev/github.com/aohorodnyk/stl/collections/binflags) package from [STL](https://pkg.go.dev/github.com/aohorodnyk/stl) library. + +The package was built to use Go 1.18 with generics. If you need older version, see please the version [v0.0.3](https://github.com/aohorodnyk/binflags/releases/tag/v0.0.3). ## Motivation There are number of needs to implement bitset for various types, and some "Big Flags" implementation through map or array of INTs. @@ -36,7 +37,7 @@ There are known limitations: ### Examples in unit tests Full list of examples can be found in `*_test.go` files. This library has coverage ~100%, and I'm gonna keep the level. -### Check has flag in base types +### Check has flag in base types ```go package main diff --git a/array_int16.go b/array_int16.go deleted file mode 100644 index dc8aedd..0000000 --- a/array_int16.go +++ /dev/null @@ -1,34 +0,0 @@ -package binflags - -func HasFlagArrayInt16(flags []int16, flag uint64) bool { - if len(flags) == 0 { - return false - } - - idx, bit := flagExt(flag, FlagMaxInt16) - if len(flags) <= int(idx) { - return false - } - - return HasFlagInt16(flags[idx], bit) -} - -func SetFlagArrayInt16(flags []int16, flag uint64, set bool) error { - if flags == nil { - return ErrorFlagsArrayNil(ErrorMsgFlagsArrayNil) - } - - if HasFlagArrayInt16(flags, flag) == set { - return nil - } - - idx, bit := flagExt(flag, FlagMaxInt16) - if len(flags) <= int(idx) { - return ErrorOutOfRange(ErrorMsgOutOfRange) - } - - var err error - flags[idx], err = SetFlagInt16(flags[idx], bit, set) - - return err -} diff --git a/array_int16_test.go b/array_int16_test.go deleted file mode 100644 index ff49ef3..0000000 --- a/array_int16_test.go +++ /dev/null @@ -1,226 +0,0 @@ -package binflags_test - -import ( - "reflect" - "testing" - - "github.com/aohorodnyk/binflags" -) - -func TestHasFlagArrayInt16(t *testing.T) { - t.Parallel() - - for _, prov := range providerHasFlagArrayInt16() { - prov := prov - t.Run(prov.name, func(t *testing.T) { - t.Parallel() - - actual := binflags.HasFlagArrayInt16(prov.flags, prov.flag) - - if prov.expected != actual { - t.Fatalf("Unexpected result.\nExpected: %t\nActual: %t", prov.expected, actual) - } - }) - } -} - -func TestSetFlagArrayInt16(t *testing.T) { - t.Parallel() - - for _, prov := range providerSetFlagArrayInt16() { - prov := prov - t.Run(prov.name, func(t *testing.T) { - t.Parallel() - - err := binflags.SetFlagArrayInt16(prov.flags, prov.flag, prov.set) - - if !reflect.DeepEqual(prov.err, err) { - t.Fatalf("Unexpected error.\nExpected: %T(%v)\nActual: %T(%v)", prov.err, prov.err, err, err) - } - - if !reflect.DeepEqual(prov.expected, prov.flags) { - t.Fatalf("Unexpected flags.\nExpected: %+v\nActual: %+v", prov.expected, prov.flags) - } - }) - } -} - -type providerTypeHasFlagArrayInt16 struct { - name string - flags []int16 - flag uint64 - expected bool -} - -type providerTypeSetFlagArrayInt16 struct { - name string - flags []int16 - flag uint64 - set bool - expected []int16 - err error -} - -func providerHasFlagArrayInt16() []providerTypeHasFlagArrayInt16 { - return []providerTypeHasFlagArrayInt16{ - { - name: "Flags nil, flag 0", - flags: nil, - flag: 0, - expected: false, - }, - { - name: "Flags [115, 12, 105], flag 36", - flags: []int16{115, 12, 105}, - flag: 36, - expected: false, - }, - { - name: "Flags [115], flag 5", - flags: []int16{115}, - flag: 5, - expected: true, - }, - { - name: "Flags [0, 0, 0, 0], flag 0", - flags: []int16{0, 0, 0, 0}, - flag: 0, - expected: false, - }, - { - name: "Flags [0, 0, 0, 0], flag 8", - flags: []int16{0, 0, 0, 0}, - flag: 8, - expected: false, - }, - { - name: "Flags [0, 0, 1, 0], flag 32", - flags: []int16{0, 0, 1, 0}, - flag: 32, - expected: true, - }, - { - name: "Flags [0, 0, 9, 0], flag 35", - flags: []int16{0, 0, 9, 0}, - flag: 35, - expected: true, - }, - { - name: "Flags [0, 0, 217, 0], flag 39", - flags: []int16{0, 0, 217, 0}, - flag: 39, - expected: true, - }, - { - name: "Flags [0, 0, 217, 0], flag 38", - flags: []int16{0, 0, 217, 0}, - flag: 38, - expected: true, - }, - { - name: "Flags [0, 0, 217, 0], flag 37", - flags: []int16{0, 0, 217, 0}, - flag: 37, - expected: false, - }, - { - name: "Flags [0, 0, 217, 0], flag 40", - flags: []int16{0, 0, 217, 0}, - flag: 40, - expected: false, - }, - } -} - -func providerSetFlagArrayInt16() []providerTypeSetFlagArrayInt16 { - return []providerTypeSetFlagArrayInt16{ - { - name: "Flags [0], flag 0, set", - flags: []int16{0}, - flag: 0, - set: true, - expected: []int16{1}, - err: nil, - }, - { - name: "Flags [1], flag 6, set", - flags: []int16{1}, - flag: 6, - set: true, - expected: []int16{65}, - err: nil, - }, - { - name: "Flags [65], flag 6, unset", - flags: []int16{65}, - flag: 6, - set: false, - expected: []int16{1}, - err: nil, - }, - { - name: "Flags [65], flag 12, set", - flags: []int16{65}, - flag: 12, - set: true, - expected: []int16{4161}, - err: nil, - }, - { - name: "Flags [65], flag 15, set", - flags: []int16{65}, - flag: 15, - set: true, - expected: []int16{-32703}, - err: nil, - }, - { - name: "Flags [65, 0], flag 16, set", - flags: []int16{65, 0}, - flag: 16, - set: true, - expected: []int16{65, 1}, - err: nil, - }, - { - name: "Flags [65, 0], flag 31, set", - flags: []int16{65, 0}, - flag: 31, - set: true, - expected: []int16{65, -32768}, - err: nil, - }, - { - name: "Flags [65, 0, 1235, 724, 635], flag 50, set", - flags: []int16{65, 0, 1235, 724, 635}, - flag: 50, - set: true, - expected: []int16{65, 0, 1235, 724, 635}, - err: nil, - }, - { - name: "Flags [65, 0, 1235, 724, 635], flag 50, unset", - flags: []int16{65, 0, 1235, 724, 635}, - flag: 50, - set: false, - expected: []int16{65, 0, 1235, 720, 635}, - err: nil, - }, - { - name: "Flags [0], flag 16, set", - flags: []int16{0}, - flag: 16, - set: true, - expected: []int16{0}, - err: binflags.ErrorOutOfRange(binflags.ErrorMsgOutOfRange), - }, - { - name: "Flags nil, flag 0, unset", - flags: nil, - flag: 0, - set: false, - expected: nil, - err: binflags.ErrorFlagsArrayNil(binflags.ErrorMsgFlagsArrayNil), - }, - } -} diff --git a/array_int32.go b/array_int32.go deleted file mode 100644 index 28172c7..0000000 --- a/array_int32.go +++ /dev/null @@ -1,34 +0,0 @@ -package binflags - -func HasFlagArrayInt32(flags []int32, flag uint64) bool { - if len(flags) == 0 { - return false - } - - idx, bit := flagExt(flag, FlagMaxInt32) - if len(flags) <= int(idx) { - return false - } - - return HasFlagInt32(flags[idx], bit) -} - -func SetFlagArrayInt32(flags []int32, flag uint64, set bool) error { - if flags == nil { - return ErrorFlagsArrayNil(ErrorMsgFlagsArrayNil) - } - - if HasFlagArrayInt32(flags, flag) == set { - return nil - } - - idx, bit := flagExt(flag, FlagMaxInt32) - if len(flags) <= int(idx) { - return ErrorOutOfRange(ErrorMsgOutOfRange) - } - - var err error - flags[idx], err = SetFlagInt32(flags[idx], bit, set) - - return err -} diff --git a/array_int32_test.go b/array_int32_test.go deleted file mode 100644 index ae357a3..0000000 --- a/array_int32_test.go +++ /dev/null @@ -1,238 +0,0 @@ -package binflags_test - -import ( - "reflect" - "testing" - - "github.com/aohorodnyk/binflags" -) - -func TestHasFlagArrayInt32(t *testing.T) { - t.Parallel() - - for _, prov := range providerHasFlagArrayInt32() { - prov := prov - t.Run(prov.name, func(t *testing.T) { - t.Parallel() - - actual := binflags.HasFlagArrayInt32(prov.flags, prov.flag) - - if prov.expected != actual { - t.Fatalf("Unexpected result.\nExpected: %t\nActual: %t", prov.expected, actual) - } - }) - } -} - -func TestSetFlagArrayInt32(t *testing.T) { - t.Parallel() - - for _, prov := range providerSetFlagArrayInt32() { - prov := prov - t.Run(prov.name, func(t *testing.T) { - t.Parallel() - - err := binflags.SetFlagArrayInt32(prov.flags, prov.flag, prov.set) - - if !reflect.DeepEqual(prov.err, err) { - t.Fatalf("Unexpected error.\nExpected: %T(%v)\nActual: %T(%v)", prov.err, prov.err, err, err) - } - - if !reflect.DeepEqual(prov.expected, prov.flags) { - t.Fatalf("Unexpected flags.\nExpected: %+v\nActual: %+v", prov.expected, prov.flags) - } - }) - } -} - -type providerTypeHasFlagArrayInt32 struct { - name string - flags []int32 - flag uint64 - expected bool -} - -type providerTypeSetFlagArrayInt32 struct { - name string - flags []int32 - flag uint64 - set bool - expected []int32 - err error -} - -func providerHasFlagArrayInt32() []providerTypeHasFlagArrayInt32 { - return []providerTypeHasFlagArrayInt32{ - { - name: "Flags nil, flag 0", - flags: nil, - flag: 0, - expected: false, - }, - { - name: "Flags [115, 12, 105], flag 36", - flags: []int32{115, 12, 105}, - flag: 36, - expected: false, - }, - { - name: "Flags [115], flag 5", - flags: []int32{115}, - flag: 5, - expected: true, - }, - { - name: "Flags [0,0,0,0], flag 0", - flags: []int32{0, 0, 0, 0}, - flag: 0, - expected: false, - }, - { - name: "Flags [0,0,0,0], flag 8", - flags: []int32{0, 0, 0, 0}, - flag: 8, - expected: false, - }, - { - name: "Flags {-2147483583, 1, 0, 0}, flag 31", - flags: []int32{-2147483583, 1, 0, 0}, - flag: 31, - expected: true, - }, - { - name: "Flags [0, 1, 0, 0], flag 32", - flags: []int32{0, 1, 0, 0}, - flag: 32, - expected: true, - }, - { - name: "Flags [0, 0, 1, 0], flag 64", - flags: []int32{0, 0, 1, 0}, - flag: 64, - expected: true, - }, - { - name: "Flags [0, 0, 9, 0], flag 67", - flags: []int32{0, 0, 9, 0}, - flag: 67, - expected: true, - }, - { - name: "Flags [0, 0, 217, 0], flag 71", - flags: []int32{0, 0, 217, 0}, - flag: 71, - expected: true, - }, - { - name: "Flags [0, 0, 217, 0], flag 70", - flags: []int32{0, 0, 217, 0}, - flag: 70, - expected: true, - }, - { - name: "Flags [0, 0, 217, 0], flag 69", - flags: []int32{0, 0, 217, 0}, - flag: 69, - expected: false, - }, - { - name: "Flags [0, 0, 217, 0], flag 72", - flags: []int32{0, 0, 217, 0}, - flag: 72, - expected: false, - }, - } -} - -func providerSetFlagArrayInt32() []providerTypeSetFlagArrayInt32 { - return []providerTypeSetFlagArrayInt32{ - { - name: "Flags [0], flag 0, set", - flags: []int32{0}, - flag: 0, - set: true, - expected: []int32{1}, - err: nil, - }, - { - name: "Flags [1], flag 6, set", - flags: []int32{1}, - flag: 6, - set: true, - expected: []int32{65}, - err: nil, - }, - { - name: "Flags [65], flag 6, unset", - flags: []int32{65}, - flag: 6, - set: false, - expected: []int32{1}, - err: nil, - }, - { - name: "Flags [65], flag 12, set", - flags: []int32{65}, - flag: 12, - set: true, - expected: []int32{4161}, - err: nil, - }, - { - name: "Flags [65], flag 31, set", - flags: []int32{65}, - flag: 31, - set: true, - expected: []int32{-2147483583}, - err: nil, - }, - { - name: "Flags [65, 0], flag 32, set", - flags: []int32{65, 0}, - flag: 32, - set: true, - expected: []int32{65, 1}, - err: nil, - }, - { - name: "Flags [65, 0], flag 63, set", - flags: []int32{65, 0}, - flag: 63, - set: true, - expected: []int32{65, -2147483648}, - err: nil, - }, - { - name: "Flags [65, 0, 1235, 724, 635], flag 68, set", - flags: []int32{65, 0, 1235, 724, 635}, - flag: 68, - set: true, - expected: []int32{65, 0, 1235, 724, 635}, - err: nil, - }, - { - name: "Flags [65, 0, 1235, 724, 635], flag 68, unset", - flags: []int32{65, 0, 1235, 724, 635}, - flag: 68, - set: false, - expected: []int32{65, 0, 1219, 724, 635}, - err: nil, - }, - { - name: "Flags [0], flag 32, set", - flags: []int32{0}, - flag: 32, - set: true, - expected: []int32{0}, - err: binflags.ErrorOutOfRange(binflags.ErrorMsgOutOfRange), - }, - { - name: "Flags nil, flag 0, unset", - flags: nil, - flag: 0, - set: false, - expected: nil, - err: binflags.ErrorFlagsArrayNil(binflags.ErrorMsgFlagsArrayNil), - }, - } -} diff --git a/array_int64.go b/array_int64.go deleted file mode 100644 index 71cbc35..0000000 --- a/array_int64.go +++ /dev/null @@ -1,34 +0,0 @@ -package binflags - -func HasFlagArrayInt64(flags []int64, flag uint64) bool { - if len(flags) == 0 { - return false - } - - idx, bit := flagExt(flag, FlagMaxInt64) - if len(flags) <= int(idx) { - return false - } - - return HasFlagInt64(flags[idx], bit) -} - -func SetFlagArrayInt64(flags []int64, flag uint64, set bool) error { - if flags == nil { - return ErrorFlagsArrayNil(ErrorMsgFlagsArrayNil) - } - - if HasFlagArrayInt64(flags, flag) == set { - return nil - } - - idx, bit := flagExt(flag, FlagMaxInt64) - if len(flags) <= int(idx) { - return ErrorOutOfRange(ErrorMsgOutOfRange) - } - - var err error - flags[idx], err = SetFlagInt64(flags[idx], bit, set) - - return err -} diff --git a/array_int64_test.go b/array_int64_test.go deleted file mode 100644 index 3734187..0000000 --- a/array_int64_test.go +++ /dev/null @@ -1,238 +0,0 @@ -package binflags_test - -import ( - "reflect" - "testing" - - "github.com/aohorodnyk/binflags" -) - -func TestHasFlagArrayInt64(t *testing.T) { - t.Parallel() - - for _, prov := range providerHasFlagArrayInt64() { - prov := prov - t.Run(prov.name, func(t *testing.T) { - t.Parallel() - - actual := binflags.HasFlagArrayInt64(prov.flags, prov.flag) - - if prov.expected != actual { - t.Fatalf("Unexpected result.\nExpected: %t\nActual: %t", prov.expected, actual) - } - }) - } -} - -func TestSetFlagArrayInt64(t *testing.T) { - t.Parallel() - - for _, prov := range providerSetFlagArrayInt64() { - prov := prov - t.Run(prov.name, func(t *testing.T) { - t.Parallel() - - err := binflags.SetFlagArrayInt64(prov.flags, prov.flag, prov.set) - - if !reflect.DeepEqual(prov.err, err) { - t.Fatalf("Unexpected error.\nExpected: %T(%v)\nActual: %T(%v)", prov.err, prov.err, err, err) - } - - if !reflect.DeepEqual(prov.expected, prov.flags) { - t.Fatalf("Unexpected flags.\nExpected: %+v\nActual: %+v", prov.expected, prov.flags) - } - }) - } -} - -type providerTypeHasFlagArrayInt64 struct { - name string - flags []int64 - flag uint64 - expected bool -} - -type providerTypeSetFlagArrayInt64 struct { - name string - flags []int64 - flag uint64 - set bool - expected []int64 - err error -} - -func providerHasFlagArrayInt64() []providerTypeHasFlagArrayInt64 { - return []providerTypeHasFlagArrayInt64{ - { - name: "Flags nil, flag 0", - flags: nil, - flag: 0, - expected: false, - }, - { - name: "Flags [115, 12, 105], flag 36", - flags: []int64{115, 12, 105}, - flag: 36, - expected: false, - }, - { - name: "Flags [115], flag 5", - flags: []int64{115}, - flag: 5, - expected: true, - }, - { - name: "Flags [0, 0, 0, 0], flag 0", - flags: []int64{0, 0, 0, 0}, - flag: 0, - expected: false, - }, - { - name: "Flags [0, 0, 0, 0], flag 8", - flags: []int64{0, 0, 0, 0}, - flag: 8, - expected: false, - }, - { - name: "Flags {-9223372036854775808, 1, 0, 0}, flag 63", - flags: []int64{-9223372036854775808, 1, 0, 0}, - flag: 63, - expected: true, - }, - { - name: "Flags [0, 1, 0, 0], flag 64", - flags: []int64{0, 1, 0, 0}, - flag: 64, - expected: true, - }, - { - name: "Flags [0, 0, 1, 0], flag 128", - flags: []int64{0, 0, 1, 0}, - flag: 128, - expected: true, - }, - { - name: "Flags [0, 0, 9, 0], flag 131", - flags: []int64{0, 0, 9, 0}, - flag: 131, - expected: true, - }, - { - name: "Flags [0, 0, 217, 0], flag 135", - flags: []int64{0, 0, 217, 0}, - flag: 135, - expected: true, - }, - { - name: "Flags [0, 0, 217, 0], flag 134", - flags: []int64{0, 0, 217, 0}, - flag: 134, - expected: true, - }, - { - name: "Flags [0, 0, 217, 0], flag 69", - flags: []int64{0, 0, 217, 0}, - flag: 69, - expected: false, - }, - { - name: "Flags [0, 0, 217, 0], flag 72", - flags: []int64{0, 0, 217, 0}, - flag: 72, - expected: false, - }, - } -} - -func providerSetFlagArrayInt64() []providerTypeSetFlagArrayInt64 { - return []providerTypeSetFlagArrayInt64{ - { - name: "Flags [0], flag 0, set", - flags: []int64{0}, - flag: 0, - set: true, - expected: []int64{1}, - err: nil, - }, - { - name: "Flags [1], flag 6, set", - flags: []int64{1}, - flag: 6, - set: true, - expected: []int64{65}, - err: nil, - }, - { - name: "Flags [65], flag 6, unset", - flags: []int64{65}, - flag: 6, - set: false, - expected: []int64{1}, - err: nil, - }, - { - name: "Flags [65], flag 12, set", - flags: []int64{65}, - flag: 12, - set: true, - expected: []int64{4161}, - err: nil, - }, - { - name: "Flags [65], flag 63, set", - flags: []int64{65}, - flag: 63, - set: true, - expected: []int64{-9223372036854775743}, - err: nil, - }, - { - name: "Flags [65, 0], flag 64, set", - flags: []int64{65, 0}, - flag: 64, - set: true, - expected: []int64{65, 1}, - err: nil, - }, - { - name: "Flags [65, 0], flag 127, set", - flags: []int64{65, 0}, - flag: 127, - set: true, - expected: []int64{65, -9223372036854775808}, - err: nil, - }, - { - name: "Flags [65, 0, 1235, 724, 635], flag 132, set", - flags: []int64{65, 0, 1235, 724, 635}, - flag: 132, - set: true, - expected: []int64{65, 0, 1235, 724, 635}, - err: nil, - }, - { - name: "Flags [65, 0, 1235, 724, 635], flag 132, unset", - flags: []int64{65, 0, 1235, 724, 635}, - flag: 132, - set: false, - expected: []int64{65, 0, 1219, 724, 635}, - err: nil, - }, - { - name: "Flags [0], flag 64, set", - flags: []int64{0}, - flag: 64, - set: true, - expected: []int64{0}, - err: binflags.ErrorOutOfRange(binflags.ErrorMsgOutOfRange), - }, - { - name: "Flags nil, flag 0, unset", - flags: nil, - flag: 0, - set: false, - expected: nil, - err: binflags.ErrorFlagsArrayNil(binflags.ErrorMsgFlagsArrayNil), - }, - } -} diff --git a/array_int8.go b/array_int8.go deleted file mode 100644 index 80aaa3c..0000000 --- a/array_int8.go +++ /dev/null @@ -1,34 +0,0 @@ -package binflags - -func HasFlagArrayInt8(flags []int8, flag uint64) bool { - if len(flags) == 0 { - return false - } - - idx, bit := flagExt(flag, FlagMaxInt8) - if len(flags) <= int(idx) { - return false - } - - return HasFlagInt8(flags[idx], bit) -} - -func SetFlagArrayInt8(flags []int8, flag uint64, set bool) error { - if flags == nil { - return ErrorFlagsArrayNil(ErrorMsgFlagsArrayNil) - } - - if HasFlagArrayInt8(flags, flag) == set { - return nil - } - - idx, bit := flagExt(flag, FlagMaxInt8) - if len(flags) <= int(idx) { - return ErrorOutOfRange(ErrorMsgOutOfRange) - } - - var err error - flags[idx], err = SetFlagInt8(flags[idx], bit, set) - - return err -} diff --git a/array_int8_test.go b/array_int8_test.go deleted file mode 100644 index c4394d3..0000000 --- a/array_int8_test.go +++ /dev/null @@ -1,182 +0,0 @@ -package binflags_test - -import ( - "reflect" - "testing" - - "github.com/aohorodnyk/binflags" -) - -func TestHasFlagArrayInt8(t *testing.T) { - t.Parallel() - - for _, prov := range providerHasFlagArrayInt8() { - prov := prov - t.Run(prov.name, func(t *testing.T) { - t.Parallel() - - actual := binflags.HasFlagArrayInt8(prov.flags, prov.flag) - - if prov.expected != actual { - t.Fatalf("Unexpected result.\nExpected: %t\nActual: %t", prov.expected, actual) - } - }) - } -} - -func TestSetFlagArrayInt8(t *testing.T) { - t.Parallel() - - for _, prov := range providerSetFlagArrayInt8() { - prov := prov - t.Run(prov.name, func(t *testing.T) { - t.Parallel() - - err := binflags.SetFlagArrayInt8(prov.flags, prov.flag, prov.set) - - if !reflect.DeepEqual(prov.err, err) { - t.Fatalf("Unexpected error.\nExpected: %T(%v)\nActual: %T(%v)", prov.err, prov.err, err, err) - } - - if !reflect.DeepEqual(prov.expected, prov.flags) { - t.Fatalf("Unexpected flags.\nExpected: %+v\nActual: %+v", prov.expected, prov.flags) - } - }) - } -} - -type providerTypeHasFlagArrayInt8 struct { - name string - flags []int8 - flag uint64 - expected bool -} - -type providerTypeSetFlagArrayInt8 struct { - name string - flags []int8 - flag uint64 - set bool - expected []int8 - err error -} - -func providerHasFlagArrayInt8() []providerTypeHasFlagArrayInt8 { - return []providerTypeHasFlagArrayInt8{ - { - name: "Flags nil, flag 0", - flags: nil, - flag: 0, - expected: false, - }, - { - name: "Flags [115, 12, 105], flag 36", - flags: []int8{115, 12, 105}, - flag: 36, - expected: false, - }, - { - name: "Flags [115], flag 5", - flags: []int8{115}, - flag: 5, - expected: true, - }, - { - name: "Flags [0, 0, 0, 0], flag 0", - flags: []int8{0, 0, 0, 0}, - flag: 0, - expected: false, - }, - { - name: "Flags [0, 0, 0, 0], flag 8", - flags: []int8{0, 0, 0, 0}, - flag: 8, - expected: false, - }, - { - name: "Flags [0, 0, 1, 0], flag 16", - flags: []int8{0, 0, 1, 0}, - flag: 16, - expected: true, - }, - { - name: "Flags [0, 0, 9, 0], flag 19", - flags: []int8{0, 0, 9, 0}, - flag: 19, - expected: true, - }, - { - name: "Flags {0, 0, -39, 0}, flag 19", - flags: []int8{0, 0, -39, 0}, - flag: 23, - expected: true, - }, - { - name: "Flags {0, 0, -39, 0}, flag 24", - flags: []int8{0, 0, -39, 0}, - flag: 24, - expected: false, - }, - } -} - -func providerSetFlagArrayInt8() []providerTypeSetFlagArrayInt8 { - return []providerTypeSetFlagArrayInt8{ - { - name: "Flags [0], flag 0, set", - flags: []int8{0}, - flag: 0, - set: true, - expected: []int8{1}, - err: nil, - }, - { - name: "Flags [1], flag 6, set", - flags: []int8{1}, - flag: 6, - set: true, - expected: []int8{65}, - err: nil, - }, - { - name: "Flags [65, 1, 0, 83], flag 30, set", - flags: []int8{65, 1, 0, 83}, - flag: 30, - set: true, - expected: []int8{65, 1, 0, 83}, - err: nil, - }, - { - name: "Flags [65, 1, 0, 83], flag 30, unset", - flags: []int8{65, 1, 0, 83}, - flag: 30, - set: false, - expected: []int8{65, 1, 0, 19}, - err: nil, - }, - { - name: "Flags [0, 0], flag 7, set", - flags: []int8{0, 0}, - flag: 7, - set: true, - expected: []int8{-128, 0}, - err: nil, - }, - { - name: "Flags [0], flag 8, set", - flags: []int8{0}, - flag: 8, - set: true, - expected: []int8{0}, - err: binflags.ErrorOutOfRange(binflags.ErrorMsgOutOfRange), - }, - { - name: "Flags nil, flag 0, unset", - flags: nil, - flag: 0, - set: false, - expected: nil, - err: binflags.ErrorFlagsArrayNil(binflags.ErrorMsgFlagsArrayNil), - }, - } -} diff --git a/array_uint16.go b/array_uint16.go deleted file mode 100644 index 96f7929..0000000 --- a/array_uint16.go +++ /dev/null @@ -1,34 +0,0 @@ -package binflags - -func HasFlagArrayUint16(flags []uint16, flag uint64) bool { - if len(flags) == 0 { - return false - } - - idx, bit := flagExt(flag, FlagMaxInt16) - if len(flags) <= int(idx) { - return false - } - - return HasFlagUint16(flags[idx], bit) -} - -func SetFlagArrayUint16(flags []uint16, flag uint64, set bool) error { - if flags == nil { - return ErrorFlagsArrayNil(ErrorMsgFlagsArrayNil) - } - - if HasFlagArrayUint16(flags, flag) == set { - return nil - } - - idx, bit := flagExt(flag, FlagMaxInt16) - if len(flags) <= int(idx) { - return ErrorOutOfRange(ErrorMsgOutOfRange) - } - - var err error - flags[idx], err = SetFlagUint16(flags[idx], bit, set) - - return err -} diff --git a/array_uint16_test.go b/array_uint16_test.go deleted file mode 100644 index 7e35681..0000000 --- a/array_uint16_test.go +++ /dev/null @@ -1,226 +0,0 @@ -package binflags_test - -import ( - "reflect" - "testing" - - "github.com/aohorodnyk/binflags" -) - -func TestHasFlagArrayUint16(t *testing.T) { - t.Parallel() - - for _, prov := range providerHasFlagArrayUint16() { - prov := prov - t.Run(prov.name, func(t *testing.T) { - t.Parallel() - - actual := binflags.HasFlagArrayUint16(prov.flags, prov.flag) - - if prov.expected != actual { - t.Fatalf("Unexpected result.\nExpected: %t\nActual: %t", prov.expected, actual) - } - }) - } -} - -func TestSetFlagArrayUint16(t *testing.T) { - t.Parallel() - - for _, prov := range providerSetFlagArrayUint16() { - prov := prov - t.Run(prov.name, func(t *testing.T) { - t.Parallel() - - err := binflags.SetFlagArrayUint16(prov.flags, prov.flag, prov.set) - - if !reflect.DeepEqual(prov.err, err) { - t.Fatalf("Unexpected error.\nExpected: %T(%v)\nActual: %T(%v)", prov.err, prov.err, err, err) - } - - if !reflect.DeepEqual(prov.expected, prov.flags) { - t.Fatalf("Unexpected flags.\nExpected: %+v\nActual: %+v", prov.expected, prov.flags) - } - }) - } -} - -type providerTypeHasFlagArrayUint16 struct { - name string - flags []uint16 - flag uint64 - expected bool -} - -type providerTypeSetFlagArrayUint16 struct { - name string - flags []uint16 - flag uint64 - set bool - expected []uint16 - err error -} - -func providerHasFlagArrayUint16() []providerTypeHasFlagArrayUint16 { - return []providerTypeHasFlagArrayUint16{ - { - name: "Flags nil, flag 0", - flags: nil, - flag: 0, - expected: false, - }, - { - name: "Flags [115, 12, 105], flag 36", - flags: []uint16{115, 12, 105}, - flag: 36, - expected: false, - }, - { - name: "Flags [115], flag 5", - flags: []uint16{115}, - flag: 5, - expected: true, - }, - { - name: "Flags [0, 0, 0, 0], flag 0", - flags: []uint16{0, 0, 0, 0}, - flag: 0, - expected: false, - }, - { - name: "Flags [0, 0, 0, 0], flag 8", - flags: []uint16{0, 0, 0, 0}, - flag: 8, - expected: false, - }, - { - name: "Flags [0, 0, 1, 0], flag 32", - flags: []uint16{0, 0, 1, 0}, - flag: 32, - expected: true, - }, - { - name: "Flags [0, 0, 9, 0], flag 35", - flags: []uint16{0, 0, 9, 0}, - flag: 35, - expected: true, - }, - { - name: "Flags [0, 0, 217, 0], flag 39", - flags: []uint16{0, 0, 217, 0}, - flag: 39, - expected: true, - }, - { - name: "Flags [0, 0, 217, 0], flag 38", - flags: []uint16{0, 0, 217, 0}, - flag: 38, - expected: true, - }, - { - name: "Flags [0, 0, 217, 0], flag 37", - flags: []uint16{0, 0, 217, 0}, - flag: 37, - expected: false, - }, - { - name: "Flags [0, 0, 217, 0], flag 40", - flags: []uint16{0, 0, 217, 0}, - flag: 40, - expected: false, - }, - } -} - -func providerSetFlagArrayUint16() []providerTypeSetFlagArrayUint16 { - return []providerTypeSetFlagArrayUint16{ - { - name: "Flags [0], flag 0, set", - flags: []uint16{0}, - flag: 0, - set: true, - expected: []uint16{1}, - err: nil, - }, - { - name: "Flags [1], flag 6, set", - flags: []uint16{1}, - flag: 6, - set: true, - expected: []uint16{65}, - err: nil, - }, - { - name: "Flags [65], flag 6, unset", - flags: []uint16{65}, - flag: 6, - set: false, - expected: []uint16{1}, - err: nil, - }, - { - name: "Flags [65], flag 12, set", - flags: []uint16{65}, - flag: 12, - set: true, - expected: []uint16{4161}, - err: nil, - }, - { - name: "Flags [65], flag 15, set", - flags: []uint16{65}, - flag: 15, - set: true, - expected: []uint16{32833}, - err: nil, - }, - { - name: "Flags [65, 0], flag 16, set", - flags: []uint16{65, 0}, - flag: 16, - set: true, - expected: []uint16{65, 1}, - err: nil, - }, - { - name: "Flags [65, 0], flag 31, set", - flags: []uint16{65, 0}, - flag: 31, - set: true, - expected: []uint16{65, 32768}, - err: nil, - }, - { - name: "Flags [65, 0, 1235, 724, 635], flag 50, set", - flags: []uint16{65, 0, 1235, 724, 635}, - flag: 50, - set: true, - expected: []uint16{65, 0, 1235, 724, 635}, - err: nil, - }, - { - name: "Flags [65, 0, 1235, 724, 635], flag 50, unset", - flags: []uint16{65, 0, 1235, 724, 635}, - flag: 50, - set: false, - expected: []uint16{65, 0, 1235, 720, 635}, - err: nil, - }, - { - name: "Flags [0], flag 16, set", - flags: []uint16{0}, - flag: 16, - set: true, - expected: []uint16{0}, - err: binflags.ErrorOutOfRange(binflags.ErrorMsgOutOfRange), - }, - { - name: "Flags nil, flag 0, unset", - flags: nil, - flag: 0, - set: false, - expected: nil, - err: binflags.ErrorFlagsArrayNil(binflags.ErrorMsgFlagsArrayNil), - }, - } -} diff --git a/array_uint32.go b/array_uint32.go deleted file mode 100644 index 3ec75c8..0000000 --- a/array_uint32.go +++ /dev/null @@ -1,34 +0,0 @@ -package binflags - -func HasFlagArrayUint32(flags []uint32, flag uint64) bool { - if len(flags) == 0 { - return false - } - - idx, bit := flagExt(flag, FlagMaxInt32) - if len(flags) <= int(idx) { - return false - } - - return HasFlagUint32(flags[idx], bit) -} - -func SetFlagArrayUint32(flags []uint32, flag uint64, set bool) error { - if flags == nil { - return ErrorFlagsArrayNil(ErrorMsgFlagsArrayNil) - } - - if HasFlagArrayUint32(flags, flag) == set { - return nil - } - - idx, bit := flagExt(flag, FlagMaxInt32) - if len(flags) <= int(idx) { - return ErrorOutOfRange(ErrorMsgOutOfRange) - } - - var err error - flags[idx], err = SetFlagUint32(flags[idx], bit, set) - - return err -} diff --git a/array_uint32_test.go b/array_uint32_test.go deleted file mode 100644 index cebee64..0000000 --- a/array_uint32_test.go +++ /dev/null @@ -1,238 +0,0 @@ -package binflags_test - -import ( - "reflect" - "testing" - - "github.com/aohorodnyk/binflags" -) - -func TestHasFlagArrayUint32(t *testing.T) { - t.Parallel() - - for _, prov := range providerHasFlagArrayUint32() { - prov := prov - t.Run(prov.name, func(t *testing.T) { - t.Parallel() - - actual := binflags.HasFlagArrayUint32(prov.flags, prov.flag) - - if prov.expected != actual { - t.Fatalf("Unexpected result.\nExpected: %t\nActual: %t", prov.expected, actual) - } - }) - } -} - -func TestSetFlagArrayUint32(t *testing.T) { - t.Parallel() - - for _, prov := range providerSetFlagArrayUint32() { - prov := prov - t.Run(prov.name, func(t *testing.T) { - t.Parallel() - - err := binflags.SetFlagArrayUint32(prov.flags, prov.flag, prov.set) - - if !reflect.DeepEqual(prov.err, err) { - t.Fatalf("Unexpected error.\nExpected: %T(%v)\nActual: %T(%v)", prov.err, prov.err, err, err) - } - - if !reflect.DeepEqual(prov.expected, prov.flags) { - t.Fatalf("Unexpected flags.\nExpected: %+v\nActual: %+v", prov.expected, prov.flags) - } - }) - } -} - -type providerTypeHasFlagArrayUint32 struct { - name string - flags []uint32 - flag uint64 - expected bool -} - -type providerTypeSetFlagArrayUint32 struct { - name string - flags []uint32 - flag uint64 - set bool - expected []uint32 - err error -} - -func providerHasFlagArrayUint32() []providerTypeHasFlagArrayUint32 { - return []providerTypeHasFlagArrayUint32{ - { - name: "Flags nil, flag 0", - flags: nil, - flag: 0, - expected: false, - }, - { - name: "Flags [115, 12, 105], flag 36", - flags: []uint32{115, 12, 105}, - flag: 36, - expected: false, - }, - { - name: "Flags [115], flag 5", - flags: []uint32{115}, - flag: 5, - expected: true, - }, - { - name: "Flags [0, 0, 0, 0], flag 0", - flags: []uint32{0, 0, 0, 0}, - flag: 0, - expected: false, - }, - { - name: "Flags [0, 0, 0, 0], flag 8", - flags: []uint32{0, 0, 0, 0}, - flag: 8, - expected: false, - }, - { - name: "Flags [2147483713, 1, 0, 0], flag 31", - flags: []uint32{2147483713, 1, 0, 0}, - flag: 31, - expected: true, - }, - { - name: "Flags [0, 1, 0, 0], flag 32", - flags: []uint32{0, 1, 0, 0}, - flag: 32, - expected: true, - }, - { - name: "Flags [0, 0, 1, 0], flag 64", - flags: []uint32{0, 0, 1, 0}, - flag: 64, - expected: true, - }, - { - name: "Flags [0, 0, 9, 0], flag 67", - flags: []uint32{0, 0, 9, 0}, - flag: 67, - expected: true, - }, - { - name: "Flags [0, 0, 217, 0], flag 71", - flags: []uint32{0, 0, 217, 0}, - flag: 71, - expected: true, - }, - { - name: "Flags [0, 0, 217, 0], flag 70", - flags: []uint32{0, 0, 217, 0}, - flag: 70, - expected: true, - }, - { - name: "Flags [0, 0, 217, 0], flag 69", - flags: []uint32{0, 0, 217, 0}, - flag: 69, - expected: false, - }, - { - name: "Flags [0, 0, 217, 0], flag 72", - flags: []uint32{0, 0, 217, 0}, - flag: 72, - expected: false, - }, - } -} - -func providerSetFlagArrayUint32() []providerTypeSetFlagArrayUint32 { - return []providerTypeSetFlagArrayUint32{ - { - name: "Flags [0], flag 0, set", - flags: []uint32{0}, - flag: 0, - set: true, - expected: []uint32{1}, - err: nil, - }, - { - name: "Flags [1], flag 6, set", - flags: []uint32{1}, - flag: 6, - set: true, - expected: []uint32{65}, - err: nil, - }, - { - name: "Flags [65], flag 6, set", - flags: []uint32{65}, - flag: 6, - set: false, - expected: []uint32{1}, - err: nil, - }, - { - name: "Flags [65], flag 12, set", - flags: []uint32{65}, - flag: 12, - set: true, - expected: []uint32{4161}, - err: nil, - }, - { - name: "Flags [65], flag 31, set", - flags: []uint32{65}, - flag: 31, - set: true, - expected: []uint32{2147483713}, - err: nil, - }, - { - name: "Flags [65, 0], flag 32, set", - flags: []uint32{65, 0}, - flag: 32, - set: true, - expected: []uint32{65, 1}, - err: nil, - }, - { - name: "Flags [65, 0], flag 63, set", - flags: []uint32{65, 0}, - flag: 63, - set: true, - expected: []uint32{65, 2147483648}, - err: nil, - }, - { - name: "Flags [65, 0, 1235, 724, 635], flag 68, set", - flags: []uint32{65, 0, 1235, 724, 635}, - flag: 68, - set: true, - expected: []uint32{65, 0, 1235, 724, 635}, - err: nil, - }, - { - name: "Flags [65, 0, 1235, 724, 635], flag 68, unset", - flags: []uint32{65, 0, 1235, 724, 635}, - flag: 68, - set: false, - expected: []uint32{65, 0, 1219, 724, 635}, - err: nil, - }, - { - name: "Flags [0], flag 32, set", - flags: []uint32{0}, - flag: 32, - set: true, - expected: []uint32{0}, - err: binflags.ErrorOutOfRange(binflags.ErrorMsgOutOfRange), - }, - { - name: "Flags nil, flag 0, unset", - flags: nil, - flag: 0, - set: false, - expected: nil, - err: binflags.ErrorFlagsArrayNil(binflags.ErrorMsgFlagsArrayNil), - }, - } -} diff --git a/array_uint64.go b/array_uint64.go deleted file mode 100644 index 48702a0..0000000 --- a/array_uint64.go +++ /dev/null @@ -1,34 +0,0 @@ -package binflags - -func HasFlagArrayUint64(flags []uint64, flag uint64) bool { - if len(flags) == 0 { - return false - } - - idx, bit := flagExt(flag, FlagMaxInt64) - if len(flags) <= int(idx) { - return false - } - - return HasFlagUint64(flags[idx], bit) -} - -func SetFlagArrayUint64(flags []uint64, flag uint64, set bool) error { - if flags == nil { - return ErrorFlagsArrayNil(ErrorMsgFlagsArrayNil) - } - - if HasFlagArrayUint64(flags, flag) == set { - return nil - } - - idx, bit := flagExt(flag, FlagMaxInt64) - if len(flags) <= int(idx) { - return ErrorOutOfRange(ErrorMsgOutOfRange) - } - - var err error - flags[idx], err = SetFlagUint64(flags[idx], bit, set) - - return err -} diff --git a/array_uint64_test.go b/array_uint64_test.go deleted file mode 100644 index 1b9c051..0000000 --- a/array_uint64_test.go +++ /dev/null @@ -1,238 +0,0 @@ -package binflags_test - -import ( - "reflect" - "testing" - - "github.com/aohorodnyk/binflags" -) - -func TestHasFlagArrayUint64(t *testing.T) { - t.Parallel() - - for _, prov := range providerHasFlagArrayUint64() { - prov := prov - t.Run(prov.name, func(t *testing.T) { - t.Parallel() - - actual := binflags.HasFlagArrayUint64(prov.flags, prov.flag) - - if prov.expected != actual { - t.Fatalf("Unexpected result.\nExpected: %t\nActual: %t", prov.expected, actual) - } - }) - } -} - -func TestSetFlagArrayUint64(t *testing.T) { - t.Parallel() - - for _, prov := range providerSetFlagArrayUint64() { - prov := prov - t.Run(prov.name, func(t *testing.T) { - t.Parallel() - - err := binflags.SetFlagArrayUint64(prov.flags, prov.flag, prov.set) - - if !reflect.DeepEqual(prov.err, err) { - t.Fatalf("Unexpected error.\nExpected: %T(%v)\nActual: %T(%v)", prov.err, prov.err, err, err) - } - - if !reflect.DeepEqual(prov.expected, prov.flags) { - t.Fatalf("Unexpected flags.\nExpected: %+v\nActual: %+v", prov.expected, prov.flags) - } - }) - } -} - -type providerTypeHasFlagArrayUint64 struct { - name string - flags []uint64 - flag uint64 - expected bool -} - -type providerTypeSetFlagArrayUint64 struct { - name string - flags []uint64 - flag uint64 - set bool - expected []uint64 - err error -} - -func providerHasFlagArrayUint64() []providerTypeHasFlagArrayUint64 { - return []providerTypeHasFlagArrayUint64{ - { - name: "Flags nil, flag 0", - flags: nil, - flag: 0, - expected: false, - }, - { - name: "Flags [115, 12, 105], flag 36", - flags: []uint64{115, 12, 105}, - flag: 36, - expected: false, - }, - { - name: "Flags [115], flag 5", - flags: []uint64{115}, - flag: 5, - expected: true, - }, - { - name: "Flags [0, 0, 0, 0], flag 0", - flags: []uint64{0, 0, 0, 0}, - flag: 0, - expected: false, - }, - { - name: "Flags [0, 0, 0, 0], flag 8", - flags: []uint64{0, 0, 0, 0}, - flag: 8, - expected: false, - }, - { - name: "Flags [9223372036854775808, 1, 0, 0], flag 63", - flags: []uint64{9223372036854775808, 1, 0, 0}, - flag: 63, - expected: true, - }, - { - name: "Flags [0, 1, 0, 0], flag 64", - flags: []uint64{0, 1, 0, 0}, - flag: 64, - expected: true, - }, - { - name: "Flags [0, 0, 1, 0], flag 128", - flags: []uint64{0, 0, 1, 0}, - flag: 128, - expected: true, - }, - { - name: "Flags [0, 0, 9, 0], flag 131", - flags: []uint64{0, 0, 9, 0}, - flag: 131, - expected: true, - }, - { - name: "Flags [0, 0, 217, 0], flag 135", - flags: []uint64{0, 0, 217, 0}, - flag: 135, - expected: true, - }, - { - name: "Flags [0, 0, 217, 0], flag 134", - flags: []uint64{0, 0, 217, 0}, - flag: 134, - expected: true, - }, - { - name: "Flags [0, 0, 217, 0], flag 69", - flags: []uint64{0, 0, 217, 0}, - flag: 69, - expected: false, - }, - { - name: "Flags [0, 0, 217, 0], flag 72", - flags: []uint64{0, 0, 217, 0}, - flag: 72, - expected: false, - }, - } -} - -func providerSetFlagArrayUint64() []providerTypeSetFlagArrayUint64 { - return []providerTypeSetFlagArrayUint64{ - { - name: "Flags [0], flag 0, set", - flags: []uint64{0}, - flag: 0, - set: true, - expected: []uint64{1}, - err: nil, - }, - { - name: "Flags [1], flag 6, set", - flags: []uint64{1}, - flag: 6, - set: true, - expected: []uint64{65}, - err: nil, - }, - { - name: "Flags [65], flag 6, unset", - flags: []uint64{65}, - flag: 6, - set: false, - expected: []uint64{1}, - err: nil, - }, - { - name: "Flags [65], flag 12, set", - flags: []uint64{65}, - flag: 12, - set: true, - expected: []uint64{4161}, - err: nil, - }, - { - name: "Flags [65], flag 63, set", - flags: []uint64{65}, - flag: 63, - set: true, - expected: []uint64{9223372036854775873}, - err: nil, - }, - { - name: "Flags [65], flag 64, set", - flags: []uint64{65, 0}, - flag: 64, - set: true, - expected: []uint64{65, 1}, - err: nil, - }, - { - name: "Flags [65, 0], flag 217, set", - flags: []uint64{65, 0}, - flag: 127, - set: true, - expected: []uint64{65, 9223372036854775808}, - err: nil, - }, - { - name: "Flags [65, 0, 1235, 724, 635], flag 132, set", - flags: []uint64{65, 0, 1235, 724, 635}, - flag: 132, - set: true, - expected: []uint64{65, 0, 1235, 724, 635}, - err: nil, - }, - { - name: "Flags [65, 0, 1235, 724, 635], flag 132, unset", - flags: []uint64{65, 0, 1235, 724, 635}, - flag: 132, - set: false, - expected: []uint64{65, 0, 1219, 724, 635}, - err: nil, - }, - { - name: "Flags [0], flag 64, set", - flags: []uint64{0}, - flag: 64, - set: true, - expected: []uint64{0}, - err: binflags.ErrorOutOfRange(binflags.ErrorMsgOutOfRange), - }, - { - name: "Flags nil, flag 0, unset", - flags: nil, - flag: 0, - set: false, - expected: nil, - err: binflags.ErrorFlagsArrayNil(binflags.ErrorMsgFlagsArrayNil), - }, - } -} diff --git a/array_uint8.go b/array_uint8.go deleted file mode 100644 index 11fc474..0000000 --- a/array_uint8.go +++ /dev/null @@ -1,42 +0,0 @@ -package binflags - -func HasFlagArrayByte(flags []byte, flag uint64) bool { - return HasFlagArrayUint8(flags, flag) -} - -func SetFlagArrayByte(flags []uint8, flag uint64, set bool) error { - return SetFlagArrayUint8(flags, flag, set) -} - -func HasFlagArrayUint8(flags []uint8, flag uint64) bool { - if len(flags) == 0 { - return false - } - - idx, bit := flagExt(flag, FlagMaxInt8) - if len(flags) <= int(idx) { - return false - } - - return HasFlagUint8(flags[idx], bit) -} - -func SetFlagArrayUint8(flags []uint8, flag uint64, set bool) error { - if flags == nil { - return ErrorFlagsArrayNil(ErrorMsgFlagsArrayNil) - } - - if HasFlagArrayUint8(flags, flag) == set { - return nil - } - - idx, bit := flagExt(flag, FlagMaxInt8) - if len(flags) <= int(idx) { - return ErrorOutOfRange(ErrorMsgOutOfRange) - } - - var err error - flags[idx], err = SetFlagUint8(flags[idx], bit, set) - - return err -} diff --git a/array_uint8_test.go b/array_uint8_test.go deleted file mode 100644 index 4dbdf35..0000000 --- a/array_uint8_test.go +++ /dev/null @@ -1,228 +0,0 @@ -package binflags_test - -import ( - "reflect" - "testing" - - "github.com/aohorodnyk/binflags" -) - -func TestHasFlagArrayUint8(t *testing.T) { - t.Parallel() - - for _, prov := range providerHasFlagArrayUint8() { - prov := prov - t.Run(prov.name, func(t *testing.T) { - t.Parallel() - - actual := binflags.HasFlagArrayUint8(prov.flags, prov.flag) - - if prov.expected != actual { - t.Fatalf("Unexpected result.\nExpected: %t\nActual: %t", prov.expected, actual) - } - }) - } -} - -func TestHasFlagArrayByte(t *testing.T) { - t.Parallel() - - for _, prov := range providerHasFlagArrayUint8() { - prov := prov - t.Run(prov.name, func(t *testing.T) { - t.Parallel() - - actual := binflags.HasFlagArrayByte(prov.flags, prov.flag) - - if prov.expected != actual { - t.Fatalf("Unexpected result.\nExpected: %t\nActual: %t", prov.expected, actual) - } - }) - } -} - -func TestSetFlagArrayUint8(t *testing.T) { - t.Parallel() - - for _, prov := range providerSetFlagArrayUint8() { - prov := prov - t.Run(prov.name, func(t *testing.T) { - t.Parallel() - - err := binflags.SetFlagArrayUint8(prov.flags, prov.flag, prov.set) - - if !reflect.DeepEqual(prov.err, err) { - t.Fatalf("Unexpected error.\nExpected: %T(%v)\nActual: %T(%v)", prov.err, prov.err, err, err) - } - - if !reflect.DeepEqual(prov.expected, prov.flags) { - t.Fatalf("Unexpected flags.\nExpected: %+v\nActual: %+v", prov.expected, prov.flags) - } - }) - } -} - -func TestSetFlagArrayByte(t *testing.T) { - t.Parallel() - - for _, prov := range providerSetFlagArrayUint8() { - prov := prov - t.Run(prov.name, func(t *testing.T) { - t.Parallel() - - err := binflags.SetFlagArrayByte(prov.flags, prov.flag, prov.set) - - if !reflect.DeepEqual(prov.err, err) { - t.Fatalf("Unexpected error.\nExpected: %T(%v)\nActual: %T(%v)", prov.err, prov.err, err, err) - } - - if !reflect.DeepEqual(prov.expected, prov.flags) { - t.Fatalf("Unexpected flags.\nExpected: %+v\nActual: %+v", prov.expected, prov.flags) - } - }) - } -} - -type providerTypeHasFlagArrayUint8 struct { - name string - flags []uint8 - flag uint64 - expected bool -} - -type providerTypeSetFlagArrayUint8 struct { - name string - flags []uint8 - flag uint64 - set bool - expected []uint8 - err error -} - -func providerHasFlagArrayUint8() []providerTypeHasFlagArrayUint8 { - return []providerTypeHasFlagArrayUint8{ - { - name: "Flags nil, flag 0", - flags: nil, - flag: 0, - expected: false, - }, - { - name: "Flags [115, 12, 105], flag 36", - flags: []uint8{115, 12, 105}, - flag: 36, - expected: false, - }, - { - name: "Flags [115], flag 5", - flags: []uint8{115}, - flag: 5, - expected: true, - }, - { - name: "Flags [0, 0, 0, 0], flag 0", - flags: []uint8{0, 0, 0, 0}, - flag: 0, - expected: false, - }, - { - name: "Flags [0, 0, 0, 0], flag 8", - flags: []uint8{0, 0, 0, 0}, - flag: 8, - expected: false, - }, - { - name: "Flags [0, 0, 1, 0], flag 16", - flags: []uint8{0, 0, 1, 0}, - flag: 16, - expected: true, - }, - { - name: "Flags [0, 0, 9, 0], flag 19", - flags: []uint8{0, 0, 9, 0}, - flag: 19, - expected: true, - }, - { - name: "Flags [0, 0, 127, 0], flag 23", - flags: []uint8{0, 0, 217, 0}, - flag: 23, - expected: true, - }, - { - name: "Flags [0, 0, 217, 0], flag 24", - flags: []uint8{0, 0, 217, 0}, - flag: 24, - expected: false, - }, - } -} - -func providerSetFlagArrayUint8() []providerTypeSetFlagArrayUint8 { - return []providerTypeSetFlagArrayUint8{ - { - name: "Flags [0], flag 0, set", - flags: []uint8{0}, - flag: 0, - set: true, - expected: []uint8{1}, - err: nil, - }, - { - name: "Flags [1], flag 6, set", - flags: []uint8{1}, - flag: 6, - set: true, - expected: []uint8{65}, - err: nil, - }, - { - name: "Flags [65], flag 6, unset", - flags: []uint8{65}, - flag: 6, - set: false, - expected: []uint8{1}, - err: nil, - }, - { - name: "Flags [65, 1, 0, 83], flag 30, set", - flags: []uint8{65, 1, 0, 83}, - flag: 30, - set: true, - expected: []uint8{65, 1, 0, 83}, - err: nil, - }, - { - name: "Flags [65, 1, 0, 83], flag 30, unset", - flags: []uint8{65, 1, 0, 83}, - flag: 30, - set: false, - expected: []uint8{65, 1, 0, 19}, - err: nil, - }, - { - name: "Flags [0, 0], flag 7, set", - flags: []uint8{0, 0}, - flag: 7, - set: true, - expected: []uint8{128, 0}, - err: nil, - }, - { - name: "Flags [0], flag 8, set", - flags: []uint8{0}, - flag: 8, - set: true, - expected: []uint8{0}, - err: binflags.ErrorOutOfRange(binflags.ErrorMsgOutOfRange), - }, - { - name: "Flags nil, flag 0, unset", - flags: nil, - flag: 0, - set: false, - expected: nil, - err: binflags.ErrorFlagsArrayNil(binflags.ErrorMsgFlagsArrayNil), - }, - } -} diff --git a/bits.go b/bits.go new file mode 100644 index 0000000..06c9257 --- /dev/null +++ b/bits.go @@ -0,0 +1,21 @@ +package binflags + +// BitsInByte collects number of bits in byte. +const BitsInByte = 8 + +// IsSetBit returns true if bit is set in a container value. +func IsSetBit[T Integer](container T, bitNumber uint8) bool { + var val T = 1 << bitNumber + + return val != 0 && container&val == val +} + +// SetBit sets bit in a container value and returns the new value. +func SetBit[T Integer](container T, bitNumber uint8) T { + return container | (1 << bitNumber) +} + +// UnsetBit unsets bit in a container value and returns the new value. +func UnsetBit[T Integer](container T, bitNumber uint8) T { + return container &^ (1 << bitNumber) +} diff --git a/bits_example_test.go b/bits_example_test.go new file mode 100644 index 0000000..7b5d1c3 --- /dev/null +++ b/bits_example_test.go @@ -0,0 +1,160 @@ +package binflags_test + +import ( + "fmt" + "math" + + "github.com/aohorodnyk/binflags" +) + +func ExampleIsSetBit() { + flagInt8 := int8(-1) + fmt.Println(binflags.IsSetBit(flagInt8, 0)) + fmt.Println(binflags.IsSetBit(flagInt8, 7)) + fmt.Println(binflags.IsSetBit(flagInt8, 8)) + fmt.Println(binflags.IsSetBit(flagInt8, 9)) + + flagInt8 = 0 + fmt.Println(binflags.IsSetBit(flagInt8, 0)) + + flagInt8 = 1 + fmt.Println(binflags.IsSetBit(flagInt8, 0)) + + flagInt8 = 1 << 2 + fmt.Println(binflags.IsSetBit(flagInt8, 1)) + fmt.Println(binflags.IsSetBit(flagInt8, 2)) + fmt.Println(binflags.IsSetBit(flagInt8, 3)) + + flagUint32 := uint32(math.MaxUint32) + fmt.Println(binflags.IsSetBit(flagUint32, 0)) + fmt.Println(binflags.IsSetBit(flagUint32, 31)) + fmt.Println(binflags.IsSetBit(flagUint32, 32)) + + flagInt64 := int64(-1) + fmt.Println(binflags.IsSetBit(flagInt64, 0)) + fmt.Println(binflags.IsSetBit(flagInt64, 63)) + fmt.Println(binflags.IsSetBit(flagInt64, 64)) + + // Output: + // true + // true + // false + // false + // false + // true + // false + // true + // false + // true + // true + // false + // true + // true + // false +} + +func ExampleSetBit() { + int8s := binflags.SetBit(int8(0), 0) + fmt.Println(int8s) + int8s = binflags.SetBit(int8s, 1) + fmt.Println(int8s) + int8s = binflags.SetBit(int8s, 1) + fmt.Println(int8s) + int8s = binflags.SetBit(int8s, 6) + fmt.Println(int8s) + int8s = binflags.SetBit(int8s, 7) + fmt.Println(int8s) + int8s = binflags.SetBit(int8s, 8) + fmt.Println(int8s) + + int8u := binflags.SetBit(uint8(0), 0) + fmt.Println(int8u) + int8u = binflags.SetBit(int8u, 1) + fmt.Println(int8u) + int8u = binflags.SetBit(int8u, 1) + fmt.Println(int8u) + int8u = binflags.SetBit(int8u, 6) + fmt.Println(int8u) + int8u = binflags.SetBit(int8u, 7) + fmt.Println(int8u) + int8u = binflags.SetBit(int8u, 8) + fmt.Println(int8u) + + int64s := binflags.SetBit(int64(0), 0) + fmt.Println(int64s) + int64s = binflags.SetBit(int64s, 1) + fmt.Println(int64s) + int64s = binflags.SetBit(int64s, 1) + fmt.Println(int64s) + int64s = binflags.SetBit(int64s, 6) + fmt.Println(int64s) + int64s = binflags.SetBit(int64s, 60) + fmt.Println(int64s) + int64s = binflags.SetBit(int64s, 61) + fmt.Println(int64s) + int64s = binflags.SetBit(int64s, 62) + fmt.Println(int64s) + int64s = binflags.SetBit(int64s, 63) + fmt.Println(int64s) + int64s = binflags.SetBit(int64s, 64) + fmt.Println(int64s) + + // Output: + // 1 + // 3 + // 3 + // 67 + // -61 + // -61 + // 1 + // 3 + // 3 + // 67 + // 195 + // 195 + // 1 + // 3 + // 3 + // 67 + // 1152921504606847043 + // 3458764513820540995 + // 8070450532247928899 + // -1152921504606846909 + // -1152921504606846909 +} + +func ExampleUnsetBit() { + int8s := binflags.UnsetBit(int8(0), 0) + fmt.Println(int8s) + int8s = binflags.UnsetBit(int8(1), 0) + fmt.Println(int8s) + int8s = binflags.UnsetBit(int8(7), 1) + fmt.Println(int8s) + int8s = binflags.UnsetBit(int8(5), 1) + fmt.Println(int8s) + int8s = binflags.UnsetBit(int8(-1), 6) + fmt.Println(int8s) + int8s = binflags.UnsetBit(int8(-1), 7) + fmt.Println(int8s) + + int64u := binflags.UnsetBit(uint64(0), 0) + fmt.Println(int64u) + int64u = binflags.UnsetBit(uint64(267435374365236435), 53) + fmt.Println(int64u) + int64u = binflags.UnsetBit(uint64(math.MaxUint64), 62) + fmt.Println(int64u) + int64u = binflags.UnsetBit(uint64(math.MaxUint64), 60) + fmt.Println(int64u) + + // Output: + // 0 + // 0 + // 5 + // 5 + // -65 + // 127 + // 0 + // 258428175110495443 + // 13835058055282163711 + // 17293822569102704639 +} diff --git a/bits_test.go b/bits_test.go new file mode 100644 index 0000000..14c7e65 --- /dev/null +++ b/bits_test.go @@ -0,0 +1,160 @@ +package binflags_test + +import ( + "fmt" + "testing" + + "github.com/aohorodnyk/binflags" +) + +func TestSetBit_int8(t *testing.T) { + t.Parallel() + + provider := []struct { + value uint8 + bitNum uint8 + exp uint8 + }{ + {0, 0, 1}, + {1, 0, 1}, + {8, 7, 136}, + {127, 7, 255}, + {255, 7, 255}, + {8, 8, 8}, + } + + for idx, prov := range provider { + prov := prov + + t.Run(fmt.Sprintf("TestSetBit_int8_%d", idx), func(t *testing.T) { + t.Parallel() + + act := binflags.SetBit(prov.value, prov.bitNum) + if act != prov.exp { + t.Errorf("SetBit returned unexpected result, expected %d, got %d", prov.exp, act) + } + }) + } +} + +func TestSetBit_uint8(t *testing.T) { + t.Parallel() + + provider := []struct { + value int8 + bitNum uint8 + exp int8 + }{ + {0, 0, 1}, + {1, 0, 1}, + {-1, 7, -1}, + {-128, 7, -128}, + {8, 7, -120}, + {8, 8, 8}, + } + + for idx, prov := range provider { + prov := prov + + t.Run(fmt.Sprintf("TestSetBit_int8_%d", idx), func(t *testing.T) { + t.Parallel() + + act := binflags.SetBit(prov.value, prov.bitNum) + if act != prov.exp { + t.Errorf("SetBit returned unexpected result, expected %d, got %d", prov.exp, act) + } + }) + } +} + +func TestUnsetBit(t *testing.T) { + t.Parallel() + + provider := []struct { + value int8 + bitNum uint8 + exp int8 + }{ + {0, 0, 0}, + {1, 0, 0}, + {-1, 7, 127}, + {-121, 4, -121}, + {-125, 7, 3}, + } + + for idx, prov := range provider { + prov := prov + + t.Run(fmt.Sprintf("TestUnsetBit_%d", idx), func(t *testing.T) { + t.Parallel() + + act := binflags.UnsetBit(prov.value, prov.bitNum) + if act != prov.exp { + t.Errorf("Unexpected resilt, expected %d, got %d", prov.exp, act) + } + }) + } +} + +func TestIsSetBit_int8(t *testing.T) { + t.Parallel() + + provider := []struct { + value int8 + bitNum uint8 + exp bool + }{ + {0, 0, false}, + {1, 0, true}, + {-128, 7, true}, + {0b0100000, 5, true}, + {0b0100100, 2, true}, + {-1, 6, true}, + {-1, 7, true}, + {-1, 8, false}, + } + + for idx, prov := range provider { + prov := prov + + t.Run(fmt.Sprintf("TestIsSetBit_int8_%d", idx), func(t *testing.T) { + t.Parallel() + + act := binflags.IsSetBit(prov.value, prov.bitNum) + if act != prov.exp { + t.Errorf("Unexpected resilt, expected %t, got %t", prov.exp, act) + } + }) + } +} + +func TestIsSetBit_uint8(t *testing.T) { + t.Parallel() + + provider := []struct { + value uint8 + bitNum uint8 + exp bool + }{ + {0, 0, false}, + {1, 0, true}, + {0b0100000, 5, true}, + {0b0100100, 2, true}, + {255, 6, true}, + {255, 7, true}, + {255, 8, false}, + } + + for idx, prov := range provider { + prov := prov + + t.Run(fmt.Sprintf("TestIsSetBit_uint8_%d", idx), func(t *testing.T) { + t.Parallel() + + act := binflags.IsSetBit(prov.value, prov.bitNum) + if act != prov.exp { + t.Errorf("Unexpected result, expected %t, got %t", prov.exp, act) + } + }) + } +} diff --git a/consts.go b/consts.go deleted file mode 100644 index 5901f0d..0000000 --- a/consts.go +++ /dev/null @@ -1,16 +0,0 @@ -package binflags - -// Max values for bits by int size. -const ( - FlagMaxInt8 uint8 = 7 - FlagMaxInt16 uint8 = 15 - FlagMaxInt32 uint8 = 31 - FlagMaxInt64 uint8 = 63 -) - -// List of error texts used in the library. -const ( - ErrorMsgOutOfRange = "flag is out of range" - ErrorMsgFlagsArrayNil = "flags array is nil" - ErrorMsgFlagsMapNil = "flags map is nil" -) diff --git a/contraings.go b/contraings.go new file mode 100644 index 0000000..c19beaf --- /dev/null +++ b/contraings.go @@ -0,0 +1,22 @@ +package binflags + +// Signed is a constraint that permits any signed integer type. +// If future releases of Go add new predeclared signed integer types, +// this constraint will be modified to include them. +type Signed interface { + ~int | ~int8 | ~int16 | ~int32 | ~int64 +} + +// Unsigned is a constraint that permits any unsigned integer type. +// If future releases of Go add new predeclared unsigned integer types, +// this constraint will be modified to include them. +type Unsigned interface { + ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr +} + +// Integer is a constraint that permits any integer type. +// If future releases of Go add new predeclared integer types, +// this constraint will be modified to include them. +type Integer interface { + Signed | Unsigned +} diff --git a/doc.go b/doc.go new file mode 100644 index 0000000..2f372d8 --- /dev/null +++ b/doc.go @@ -0,0 +1,14 @@ +/* +This package contains a super fast and collection that stores a state in bits. +There are two implementations you can find in the package: + - Static + - Dynamic + +Both of them can help to minify the memory usage and the time spent for Set data-structure. +If you need to save a uint in set, then this implementation will be much more efficient than the usual Set. +The Set key is storing in 1 bit in an array or map. + +This data structure is useful to share the flags in JSON or store them in a database. +There's an article that shares the motivation to have this implementation: https://aohorodnyk.com/post/2021-01-03-binary-flags/ +*/ +package binflags diff --git a/dynamic.go b/dynamic.go new file mode 100644 index 0000000..7530b7e --- /dev/null +++ b/dynamic.go @@ -0,0 +1,78 @@ +package binflags + +import ( + "unsafe" +) + +// Dynamic is a dynamic size implementation of the binflags. +// This implementation uses a map under the hood. +// The map's key is a uint. +// Specify an expected size for the map to improve performance. +// To initialize the Dynamic type, use make function or usual instatination: +// +// flags := make(Dynamic[uint], 3) +// +// or +// +// flags := make(Dynamic[uint]) +// +// or +// +// flags := Dynamic[uint]{} +type Dynamic[T Integer] map[uint]T + +// IsSet returns true if the flag is set. +// If the flag is not set, then it returns false. +// This function has O(1) complexity. +func (d Dynamic[T]) IsSet(bitNumber uint) bool { + size := uint(unsafe.Sizeof(d[0])) * BitsInByte + byteIdx := bitNumber / size + + val, ok := d[byteIdx] + if !ok { + return false + } + + bitIdx := uint8(bitNumber % size) + + return IsSetBit(val, bitIdx) +} + +// Set sets the flag. +// This function is idempotent. +// This function has O(1) complexity. +// It always returns true. +func (d Dynamic[T]) Set(bitNumber uint) bool { + size := uint(unsafe.Sizeof(d[0])) * BitsInByte + byteIdx := bitNumber / size + bitIdx := uint8(bitNumber % size) + + d[byteIdx] = SetBit(d[byteIdx], bitIdx) + + return true +} + +// Unset unsets the flag. +// This function is idempotent. +// This function has O(1) complexity. +// It always returns true. +func (d Dynamic[T]) Unset(bitNumber uint) bool { + size := uint(unsafe.Sizeof(d[0])) * BitsInByte + byteIdx := bitNumber / size + + val := d[byteIdx] + if val == 0 { + return true + } + + bitIdx := uint8(bitNumber % size) + + val = UnsetBit(val, bitIdx) + if val == 0 { + delete(d, byteIdx) + } else { + d[byteIdx] = val + } + + return true +} diff --git a/dynamic_test.go b/dynamic_test.go new file mode 100644 index 0000000..27b51ab --- /dev/null +++ b/dynamic_test.go @@ -0,0 +1,120 @@ +package binflags_test + +import ( + "fmt" + + "github.com/aohorodnyk/binflags" +) + +var ( + _ binflags.Flags = &binflags.Dynamic[int]{} + _ binflags.Flags = &binflags.Dynamic[uint]{} + _ binflags.Flags = &binflags.Dynamic[uint16]{} + _ binflags.Flags = &binflags.Dynamic[int16]{} + _ binflags.Flags = &binflags.Dynamic[byte]{} +) + +func ExampleDynamic() { + flags := make(binflags.Dynamic[uint8], 3) + fmt.Println(flags) + fmt.Println(flags.Set(436235346)) + fmt.Println(flags.Set(0)) + fmt.Println(flags.Set(52)) + fmt.Println(flags.Set(3462363)) + fmt.Println(flags.Set(9874563524235)) + fmt.Println(flags) + fmt.Println(flags.IsSet(0)) + fmt.Println(flags.IsSet(52)) + fmt.Println(flags.IsSet(3462363)) + fmt.Println(flags.IsSet(9874563524235)) + fmt.Println(flags.IsSet(436235346)) + fmt.Println(flags.IsSet(1)) + fmt.Println(flags.IsSet(436235345)) + fmt.Println(flags.IsSet(436235347)) + fmt.Println(flags.Unset(0)) + fmt.Println(flags.Unset(9874563524235)) + fmt.Println(flags.Unset(2523)) + fmt.Println(flags) + + // Output: + // map[] + // true + // true + // true + // true + // true + // map[0:1 6:16 432795:8 54529418:4 1234320440529:8] + // true + // true + // true + // true + // true + // false + // false + // false + // true + // true + // true + // map[6:16 432795:8 54529418:4] +} + +func ExampleDynamic_IsSet() { + flags := binflags.Dynamic[uint8]{432795: 8, 54529418: 4, 1234320440529: 8} + fmt.Println(flags.IsSet(0)) + fmt.Println(flags.IsSet(342)) + fmt.Println(flags.IsSet(3462363)) + fmt.Println(flags.IsSet(9874563524235)) + fmt.Println(flags.IsSet(9874563524234)) + fmt.Println(flags.IsSet(9874563524236)) + + // Output: + // false + // false + // true + // true + // false + // false +} + +func ExampleDynamic_Unset() { + flags := binflags.Dynamic[uint8]{432795: 8, 54529418: 4, 1234320440529: 8} + fmt.Println(flags) + fmt.Println(flags.Unset(0)) + fmt.Println(flags.Unset(342)) + fmt.Println(flags.Unset(3462363)) + fmt.Println(flags.Unset(9874563524235)) + fmt.Println(flags.Unset(9874563524234)) + fmt.Println(flags.Unset(9874563524236)) + fmt.Println(flags) + + // Output: + // map[432795:8 54529418:4 1234320440529:8] + // true + // true + // true + // true + // true + // true + // map[54529418:4] +} + +func ExampleDynamic_Set() { + flags := binflags.Dynamic[uint8]{} + fmt.Println(flags.Set(234)) + fmt.Println(flags.Set(65234)) + fmt.Println(flags.Set(123)) + fmt.Println(flags.Set(2)) + fmt.Println(flags.Set(1)) + fmt.Println(flags.Set(16)) + + fmt.Println(flags) + + // Output: + // true + // true + // true + // true + // true + // true + // map[0:6 2:1 15:8 29:4 8154:4] +} diff --git a/errors.go b/errors.go deleted file mode 100644 index f9a2797..0000000 --- a/errors.go +++ /dev/null @@ -1,19 +0,0 @@ -package binflags - -type ErrorOutOfRange string - -func (e ErrorOutOfRange) Error() string { - return string(e) -} - -type ErrorFlagsArrayNil string - -func (e ErrorFlagsArrayNil) Error() string { - return string(e) -} - -type ErrorFlagsMapNil string - -func (e ErrorFlagsMapNil) Error() string { - return string(e) -} diff --git a/fixed.go b/fixed.go new file mode 100644 index 0000000..88d5eb9 --- /dev/null +++ b/fixed.go @@ -0,0 +1,87 @@ +package binflags + +import ( + "unsafe" +) + +// Fixed is a fixed size implementation of the binflags. +// This is a slice that is allocated to collect flags in bits. +// The max amount of flags can be stored per slice can be calculated by the formula: `slice length` * `bits in byte`. +// +// This implementation can be used whenever you want to have a predictable size and performance. +// Also it can be saved in a database as an array type. +// To initialize the Dynamic type, use make function or usual instatination: +// +// flags := make(Fixed[uint], 3) +// +// or +// +// flags := Fixed[uint]{0, 0, 0} +type Fixed[T Integer] []T + +// IsSet returns true if the flag is set. +// If the flag is not set, then it returns false. +// This function has O(1) complexity. +func (s Fixed[T]) IsSet(bitNumber uint) bool { + if len(s) == 0 { + return false + } + + size := uint(unsafe.Sizeof(s[0])) * BitsInByte + + byteIdx := int(bitNumber / size) + if byteIdx >= len(s) { + // Bit number is out of range. + return false + } + + bitIdx := uint8(bitNumber % size) + + return IsSetBit(s[byteIdx], bitIdx) +} + +// Set sets the flag. +// This function is idempotent. +// This function has O(1) complexity. +// It returns false if the flag is out of range. +func (s Fixed[T]) Set(bitNumber uint) bool { + if len(s) == 0 { + return false + } + + size := uint(unsafe.Sizeof(s[0])) * BitsInByte + + byteIdx := int(bitNumber / size) + if byteIdx >= len(s) { + // Bit number is out of range. + return false + } + + bitIdx := uint8(bitNumber % size) + s[byteIdx] = SetBit(s[byteIdx], bitIdx) + + return true +} + +// Unset unsets the flag. +// This function is idempotent. +// This function has O(1) complexity. +// It returns false if the flag is out of range. +func (s Fixed[T]) Unset(bitNumber uint) bool { + if len(s) == 0 { + return false + } + + size := uint(unsafe.Sizeof(s[0])) * BitsInByte + + byteIdx := int(bitNumber / size) + if byteIdx >= len(s) { + // Bit number is out of range. + return false + } + + bitIdx := uint8(bitNumber % size) + s[byteIdx] = UnsetBit(s[byteIdx], bitIdx) + + return true +} diff --git a/fixed_test.go b/fixed_test.go new file mode 100644 index 0000000..6b221d5 --- /dev/null +++ b/fixed_test.go @@ -0,0 +1,132 @@ +package binflags_test + +import ( + "fmt" + + "github.com/aohorodnyk/binflags" +) + +var ( + _ binflags.Flags = &binflags.Fixed[int]{} + _ binflags.Flags = &binflags.Fixed[uint]{} + _ binflags.Flags = &binflags.Fixed[uint16]{} + _ binflags.Flags = &binflags.Fixed[int16]{} + _ binflags.Flags = &binflags.Fixed[int8]{} + _ binflags.Flags = &binflags.Fixed[byte]{} +) + +func ExampleFixed() { + flags := make(binflags.Fixed[uint8], 3) + fmt.Println(flags) + fmt.Println(flags.Set(20)) + fmt.Println(flags) + fmt.Println(flags.Set(23)) + fmt.Println(flags) + fmt.Println(flags.Set(13)) + fmt.Println(flags) + fmt.Println(flags.Set(24)) + fmt.Println(flags) + fmt.Println(flags.IsSet(5)) + fmt.Println(flags.IsSet(13)) + fmt.Println(flags.Unset(24)) + fmt.Println(flags) + fmt.Println(flags.Unset(7)) + fmt.Println(flags) + fmt.Println(flags.Unset(13)) + fmt.Println(flags) + + // Output: + // [0 0 0] + // true + // [0 0 16] + // true + // [0 0 144] + // true + // [0 32 144] + // false + // [0 32 144] + // false + // true + // false + // [0 32 144] + // true + // [0 32 144] + // true + // [0 0 144] +} + +func ExampleFixed_IsSet() { + flags := binflags.Fixed[uint8]{0, 255, 123, 53, 255} + fmt.Println(flags.IsSet(0)) + fmt.Println(flags.IsSet(7)) + fmt.Println(flags.IsSet(8)) + fmt.Println(flags.IsSet(15)) + fmt.Println(flags.IsSet(39)) + fmt.Println(flags.IsSet(40)) + fmt.Println(flags.IsSet(12423)) + + // Output: + // false + // false + // true + // true + // true + // false + // false +} + +func ExampleFixed_Set() { + flags := make(binflags.Fixed[uint8], 3) + fmt.Println(flags) + fmt.Println(flags.Set(0)) + fmt.Println(flags) + fmt.Println(flags.Set(14)) + fmt.Println(flags) + fmt.Println(flags.Set(21)) + fmt.Println(flags) + fmt.Println(flags.Set(23)) + fmt.Println(flags) + fmt.Println(flags.Set(24)) + fmt.Println(flags) + + // Output: + // [0 0 0] + // true + // [1 0 0] + // true + // [1 64 0] + // true + // [1 64 32] + // true + // [1 64 160] + // false + // [1 64 160] +} + +func ExampleFixed_Unset() { + flags := binflags.Fixed[uint8]{255, 255, 255} + fmt.Println(flags) + fmt.Println(flags.Unset(0)) + fmt.Println(flags) + fmt.Println(flags.Unset(14)) + fmt.Println(flags) + fmt.Println(flags.Unset(21)) + fmt.Println(flags) + fmt.Println(flags.Unset(23)) + fmt.Println(flags) + fmt.Println(flags.Unset(24)) + fmt.Println(flags) + + // Output: + // [255 255 255] + // true + // [254 255 255] + // true + // [254 191 255] + // true + // [254 191 223] + // true + // [254 191 95] + // false + // [254 191 95] +} diff --git a/flags.go b/flags.go new file mode 100644 index 0000000..48062ac --- /dev/null +++ b/flags.go @@ -0,0 +1,16 @@ +package binflags + +// Flags is an interface to standardize the interface for all binflags implementations. +type Flags interface { + // IsSet returns true if the flag is set. + // If the flag is not set, then it returns false. + IsSet(bitNumber uint) bool + + // Set sets the flag. + // This function is idempotent. + Set(bitNumber uint) bool + + // Unset unsets the flag. + // This function is idempotent. + Unset(bitNumber uint) bool +} diff --git a/go.mod b/go.mod index 22a70e8..322a346 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ module github.com/aohorodnyk/binflags -go 1.14 +go 1.18 diff --git a/helper.go b/helper.go deleted file mode 100644 index 7a07505..0000000 --- a/helper.go +++ /dev/null @@ -1,14 +0,0 @@ -package binflags - -func flagExt(flag uint64, size uint8) (idx uint64, bit uint8) { - bytes64 := uint64(size) - if flag <= bytes64 { - return 0, uint8(flag) - } - - bytes64Div := bytes64 + 1 - key := flag / bytes64Div - val := flag % bytes64Div - - return key, uint8(val) -} diff --git a/int16.go b/int16.go deleted file mode 100644 index e36bc59..0000000 --- a/int16.go +++ /dev/null @@ -1,26 +0,0 @@ -package binflags - -func HasFlagInt16(flags int16, flag uint8) bool { - if flag > FlagMaxInt16 { - return false - } - - conv := int16(1 << flag) - - return flags&conv == conv -} - -func SetFlagInt16(flags int16, flag uint8, set bool) (int16, error) { - if flag > FlagMaxInt16 { - return flags, ErrorOutOfRange(ErrorMsgOutOfRange) - } - - if HasFlagInt16(flags, flag) == set { - return flags, nil - } - - conv := int16(1 << flag) - ret := flags ^ conv - - return ret, nil -} diff --git a/int16_test.go b/int16_test.go deleted file mode 100644 index 358cbb6..0000000 --- a/int16_test.go +++ /dev/null @@ -1,209 +0,0 @@ -package binflags_test - -import ( - "math" - "reflect" - "testing" - - "github.com/aohorodnyk/binflags" -) - -func TestHasFlagInt16(t *testing.T) { - t.Parallel() - - for _, prov := range providerHasFlagInt16() { - prov := prov - t.Run(prov.name, func(t *testing.T) { - t.Parallel() - - actual := binflags.HasFlagInt16(prov.flags, prov.flag) - - if prov.expected != actual { - t.Fatalf("Unexpected result.\nExpected: %t\nActual: %t", prov.expected, actual) - } - }) - } -} - -func TestSetFlagInt16(t *testing.T) { - t.Parallel() - - for _, prov := range providerSetFlagInt16() { - prov := prov - t.Run(prov.name, func(t *testing.T) { - t.Parallel() - - actual, err := binflags.SetFlagInt16(prov.flags, prov.flag, prov.set) - - if !reflect.DeepEqual(prov.err, err) { - t.Fatalf("Unexpected error.\nExpected: %T(%v)\nActual: %T(%v)", prov.err, prov.err, err, err) - } - - if prov.expected != actual { - t.Fatalf("Unexpected result.\nExpected: %d\nActual: %d", prov.expected, actual) - } - }) - } -} - -type providerTypeHasFlagInt16 struct { - name string - flags int16 - flag uint8 - expected bool -} - -type providerTypeSetFlagInt16 struct { - name string - flags int16 - flag uint8 - set bool - expected int16 - err error -} - -func providerHasFlagInt16() []providerTypeHasFlagInt16 { - return []providerTypeHasFlagInt16{ - { - name: "Flags 0, flag 0", - flags: 0, - flag: 0, - expected: false, - }, - { - name: "Flags 52, flag 7", - flags: 52, - flag: 7, - expected: false, - }, - { - name: "Flags 52, flag 15", - flags: 52, - flag: 15, - expected: false, - }, - { - name: "Flags -32768, flag 15", - flags: math.MinInt16, - flag: 15, - expected: true, - }, - { - name: "Flags -32768, flag 14", - flags: math.MinInt16, - flag: 14, - expected: false, - }, - { - name: "Flags -1, flag 3", - flags: -1, - flag: 3, - expected: true, - }, - { - name: "Flags -1, flag 7", - flags: -1, - flag: 7, - expected: true, - }, - { - name: "Flags -1, flag 16", - flags: -1, - flag: 16, - expected: false, - }, - } -} - -func providerSetFlagInt16() []providerTypeSetFlagInt16 { - return []providerTypeSetFlagInt16{ - { - name: "Flags 0, flag 0, unset", - flags: 0, - flag: 0, - set: false, - expected: 0, - err: nil, - }, - { - name: "Flags 0, flag 0, set", - flags: 0, - flag: 0, - set: true, - expected: 1, - err: nil, - }, - { - name: "Flags 1, flag 1, set", - flags: 1, - flag: 1, - set: true, - expected: 3, - err: nil, - }, - { - name: "Flags 53, flag 1, set", - flags: 53, - flag: 1, - set: true, - expected: 55, - err: nil, - }, - { - name: "Flags 53, flag 0, unset", - flags: 53, - flag: 0, - set: false, - expected: 52, - err: nil, - }, - { - name: "Flags -1, flag 5, set", - flags: -1, - flag: 5, - set: true, - expected: -1, - err: nil, - }, - { - name: "Flags 2567, flag 11, unset", - flags: 2567, - flag: 11, - set: false, - expected: 519, - err: nil, - }, - { - name: "Flags -1, flag 5, unset", - flags: -1, - flag: 5, - set: false, - expected: -33, - err: nil, - }, - { - name: "Flags -1, flag 15, unset", - flags: -1, - flag: 15, - set: false, - expected: 32767, - err: nil, - }, - { - name: "Flags -32768, flag 15, unset", - flags: math.MinInt16, - flag: 15, - set: false, - expected: 0, - err: nil, - }, - { - name: "Flags -32768, flag 16, unset", - flags: math.MinInt16, - flag: 16, - set: false, - expected: math.MinInt16, - err: binflags.ErrorOutOfRange(binflags.ErrorMsgOutOfRange), - }, - } -} diff --git a/int32.go b/int32.go deleted file mode 100644 index c36f0cd..0000000 --- a/int32.go +++ /dev/null @@ -1,26 +0,0 @@ -package binflags - -func HasFlagInt32(flags int32, flag uint8) bool { - if flag > FlagMaxInt32 { - return false - } - - conv := int32(1 << flag) - - return flags&conv == conv -} - -func SetFlagInt32(flags int32, flag uint8, set bool) (int32, error) { - if flag > FlagMaxInt32 { - return flags, ErrorOutOfRange(ErrorMsgOutOfRange) - } - - if HasFlagInt32(flags, flag) == set { - return flags, nil - } - - conv := int32(1 << flag) - ret := flags ^ conv - - return ret, nil -} diff --git a/int32_test.go b/int32_test.go deleted file mode 100644 index cb963c1..0000000 --- a/int32_test.go +++ /dev/null @@ -1,215 +0,0 @@ -package binflags_test - -import ( - "math" - "reflect" - "testing" - - "github.com/aohorodnyk/binflags" -) - -func TestHasFlagInt32(t *testing.T) { - t.Parallel() - - for _, prov := range providerHasFlagInt32() { - prov := prov - t.Run(prov.name, func(t *testing.T) { - t.Parallel() - - actual := binflags.HasFlagInt32(prov.flags, prov.flag) - - if prov.expected != actual { - t.Fatalf("Unexpected result.\nExpected: %t\nActual: %t", prov.expected, actual) - } - }) - } -} - -func TestSetFlagInt32(t *testing.T) { - t.Parallel() - - for _, prov := range providerSetFlagInt32() { - prov := prov - t.Run(prov.name, func(t *testing.T) { - t.Parallel() - - actual, err := binflags.SetFlagInt32(prov.flags, prov.flag, prov.set) - - if !reflect.DeepEqual(prov.err, err) { - t.Fatalf("Unexpected error.\nExpected: %T(%v)\nActual: %T(%v)", prov.err, prov.err, err, err) - } - - if prov.expected != actual { - t.Fatalf("Unexpected result.\nExpected: %d\nActual: %d", prov.expected, actual) - } - }) - } -} - -type providerTypeHasFlagInt32 struct { - name string - flags int32 - flag uint8 - expected bool -} - -type providerTypeSetFlagInt32 struct { - name string - flags int32 - flag uint8 - set bool - expected int32 - err error -} - -func providerHasFlagInt32() []providerTypeHasFlagInt32 { - return []providerTypeHasFlagInt32{ - { - name: "Flags 0, flag 0", - flags: 0, - flag: 0, - expected: false, - }, - { - name: "Flags 52, flag 15", - flags: 52, - flag: 15, - expected: false, - }, - { - name: "Flags 52, flag 4", - flags: 52, - flag: 4, - expected: true, - }, - { - name: "Flags -2147483648, flag 31", - flags: math.MinInt32, - flag: 31, - expected: true, - }, - { - name: "Flags -2147483648, flag 14", - flags: math.MinInt32, - flag: 14, - expected: false, - }, - { - name: "Flags -1, flag 3", - flags: -1, - flag: 3, - expected: true, - }, - { - name: "Flags -1, flag 7", - flags: -1, - flag: 7, - expected: true, - }, - { - name: "Flags -1, flag 31", - flags: -1, - flag: 31, - expected: true, - }, - { - name: "Flags -1, flag 32", - flags: -1, - flag: 32, - expected: false, - }, - } -} - -func providerSetFlagInt32() []providerTypeSetFlagInt32 { - return []providerTypeSetFlagInt32{ - { - name: "Flags 0, flag 0, unset", - flags: 0, - flag: 0, - set: false, - expected: 0, - err: nil, - }, - { - name: "Flags 0, flag 0, set", - flags: 0, - flag: 0, - set: true, - expected: 1, - err: nil, - }, - { - name: "Flags 1, flag 1, set", - flags: 1, - flag: 1, - set: true, - expected: 3, - err: nil, - }, - { - name: "Flags 53, flag 1, set", - flags: 53, - flag: 1, - set: true, - expected: 55, - err: nil, - }, - { - name: "Flags 53, flag 0, unset", - flags: 53, - flag: 0, - set: false, - expected: 52, - err: nil, - }, - { - name: "Flags -1, flag 5, set", - flags: -1, - flag: 5, - set: true, - expected: -1, - err: nil, - }, - { - name: "Flags 2567, flag 11, unset", - flags: 2567, - flag: 11, - set: false, - expected: 519, - err: nil, - }, - { - name: "Flags -1, flag 5, unset", - flags: -1, - flag: 5, - set: false, - expected: -33, - err: nil, - }, - { - name: "Flags -1, flag 31, unset", - flags: -1, - flag: 31, - set: false, - expected: 2147483647, - err: nil, - }, - { - name: "Flags -2147483648, flag 31, unset", - flags: math.MinInt32, - flag: 31, - set: false, - expected: 0, - err: nil, - }, - { - name: "Flags -2147483648, flag 32, set", - flags: math.MinInt32, - flag: 32, - set: true, - expected: math.MinInt32, - err: binflags.ErrorOutOfRange(binflags.ErrorMsgOutOfRange), - }, - } -} diff --git a/int64.go b/int64.go deleted file mode 100644 index 326f068..0000000 --- a/int64.go +++ /dev/null @@ -1,26 +0,0 @@ -package binflags - -func HasFlagInt64(flags int64, flag uint8) bool { - if flag > FlagMaxInt64 { - return false - } - - conv := int64(1 << flag) - - return flags&conv == conv -} - -func SetFlagInt64(flags int64, flag uint8, set bool) (int64, error) { - if flag > FlagMaxInt64 { - return flags, ErrorOutOfRange(ErrorMsgOutOfRange) - } - - if HasFlagInt64(flags, flag) == set { - return flags, nil - } - - conv := int64(1 << flag) - ret := flags ^ conv - - return ret, nil -} diff --git a/int64_test.go b/int64_test.go deleted file mode 100644 index 73c3fef..0000000 --- a/int64_test.go +++ /dev/null @@ -1,231 +0,0 @@ -package binflags_test - -import ( - "math" - "reflect" - "testing" - - "github.com/aohorodnyk/binflags" -) - -func TestHasFlagInt64(t *testing.T) { - t.Parallel() - - for _, prov := range providerHasFlagInt64() { - prov := prov - t.Run(prov.name, func(t *testing.T) { - t.Parallel() - - actual := binflags.HasFlagInt64(prov.flags, prov.flag) - - if prov.expected != actual { - t.Fatalf("Unexpected result.\nExpected: %t\nActual: %t", prov.expected, actual) - } - }) - } -} - -func TestSetFlagInt64(t *testing.T) { - t.Parallel() - - for _, prov := range providerSetFlagInt64() { - prov := prov - t.Run(prov.name, func(t *testing.T) { - t.Parallel() - - actual, err := binflags.SetFlagInt64(prov.flags, prov.flag, prov.set) - - if !reflect.DeepEqual(prov.err, err) { - t.Fatalf("Unexpected error.\nExpected: %T(%v)\nActual: %T(%v)", prov.err, prov.err, err, err) - } - - if prov.expected != actual { - t.Fatalf("Unexpected result.\nExpected: %d\nActual: %d", prov.expected, actual) - } - }) - } -} - -type providerTypeHasFlagInt64 struct { - name string - flags int64 - flag uint8 - expected bool -} - -type providerTypeSetFlagInt64 struct { - name string - flags int64 - flag uint8 - set bool - expected int64 - err error -} - -func providerHasFlagInt64() []providerTypeHasFlagInt64 { - return []providerTypeHasFlagInt64{ - { - name: "Flags 0, flag 0", - flags: 0, - flag: 0, - expected: false, - }, - { - name: "Flags 52, flag 15", - flags: 52, - flag: 15, - expected: false, - }, - { - name: "Flags 52, flag 4", - flags: 52, - flag: 4, - expected: true, - }, - { - name: "Flags -9223372036854775808, flag 31", - flags: math.MinInt64, - flag: 31, - expected: false, - }, - { - name: "Flags -9223372036854775808, flag 63", - flags: math.MinInt64, - flag: 63, - expected: true, - }, - { - name: "Flags -9223372036854775808, flag 14", - flags: math.MinInt64, - flag: 14, - expected: false, - }, - { - name: "Flags -1, flag 3", - flags: -1, - flag: 3, - expected: true, - }, - { - name: "Flags -1, flag 7", - flags: -1, - flag: 7, - expected: true, - }, - { - name: "Flags -1, flag 64", - flags: -1, - flag: 64, - expected: false, - }, - } -} - -func providerSetFlagInt64() []providerTypeSetFlagInt64 { - return []providerTypeSetFlagInt64{ - { - name: "Flags 0, flag 0, unset", - flags: 0, - flag: 0, - set: false, - expected: 0, - err: nil, - }, - { - name: "Flags 0, flag 0, set", - flags: 0, - flag: 0, - set: true, - expected: 1, - err: nil, - }, - { - name: "Flags 1, flag 1, set", - flags: 1, - flag: 1, - set: true, - expected: 3, - err: nil, - }, - { - name: "Flags 53, flag 1, set", - flags: 53, - flag: 1, - set: true, - expected: 55, - err: nil, - }, - { - name: "Flags 53, flag 0, unset", - flags: 53, - flag: 0, - set: false, - expected: 52, - err: nil, - }, - { - name: "Flags -1, flag 5, set", - flags: -1, - flag: 5, - set: true, - expected: -1, - err: nil, - }, - { - name: "Flags 2567, flag 11, unset", - flags: 2567, - flag: 11, - set: false, - expected: 519, - err: nil, - }, - { - name: "Flags -1, flag 5, unset", - flags: -1, - flag: 5, - set: false, - expected: -33, - err: nil, - }, - { - name: "Flags -1, flag 31, unset", - flags: -1, - flag: 31, - set: false, - expected: -2147483649, - err: nil, - }, - { - name: "Flags -1, flag 63, unset", - flags: -1, - flag: 63, - set: false, - expected: 9223372036854775807, - err: nil, - }, - { - name: "Flags 2147483648, flag 31, unset", - flags: 2147483648, - flag: 31, - set: false, - expected: 0, - err: nil, - }, - { - name: "Flags -9223372036854775808, flag 63, unset", - flags: math.MinInt64, - flag: 63, - set: false, - expected: 0, - err: nil, - }, - { - name: "Flags -9223372036854775808, flag 64, unset", - flags: math.MinInt64, - flag: 64, - set: true, - expected: math.MinInt64, - err: binflags.ErrorOutOfRange(binflags.ErrorMsgOutOfRange), - }, - } -} diff --git a/int8.go b/int8.go deleted file mode 100644 index 1cbffbc..0000000 --- a/int8.go +++ /dev/null @@ -1,26 +0,0 @@ -package binflags - -func HasFlagInt8(flags int8, flag uint8) bool { - if flag > FlagMaxInt8 { - return false - } - - conv := int8(1 << flag) - - return flags&conv == conv -} - -func SetFlagInt8(flags int8, flag uint8, set bool) (int8, error) { - if flag > FlagMaxInt8 { - return flags, ErrorOutOfRange(ErrorMsgOutOfRange) - } - - if HasFlagInt8(flags, flag) == set { - return flags, nil - } - - conv := int8(1 << flag) - ret := flags ^ conv - - return ret, nil -} diff --git a/int8_test.go b/int8_test.go deleted file mode 100644 index bd59728..0000000 --- a/int8_test.go +++ /dev/null @@ -1,195 +0,0 @@ -package binflags_test - -import ( - "math" - "reflect" - "testing" - - "github.com/aohorodnyk/binflags" -) - -func TestHasFlagInt8(t *testing.T) { - t.Parallel() - - for _, prov := range providerHasFlagInt8() { - prov := prov - t.Run(prov.name, func(t *testing.T) { - t.Parallel() - - actual := binflags.HasFlagInt8(prov.flags, prov.flag) - - if prov.expected != actual { - t.Fatalf("Unexpected result.\nExpected: %t\nActual: %t", prov.expected, actual) - } - }) - } -} - -func TestSetFlagInt8(t *testing.T) { - t.Parallel() - - for _, prov := range providerSetFlagInt8() { - prov := prov - t.Run(prov.name, func(t *testing.T) { - t.Parallel() - - actual, err := binflags.SetFlagInt8(prov.flags, prov.flag, prov.set) - - if !reflect.DeepEqual(prov.err, err) { - t.Fatalf("Unexpected error.\nExpected: %T(%v)\nActual: %T(%v)", prov.err, prov.err, err, err) - } - - if prov.expected != actual { - t.Fatalf("Unexpected result.\nExpected: %d\nActual: %d", prov.expected, actual) - } - }) - } -} - -type providerTypeHasFlagInt8 struct { - name string - flags int8 - flag uint8 - expected bool -} - -type providerTypeSetFlagInt8 struct { - name string - flags int8 - flag uint8 - set bool - expected int8 - err error -} - -func providerHasFlagInt8() []providerTypeHasFlagInt8 { - return []providerTypeHasFlagInt8{ - { - name: "Flags 0, flag 0", - flags: 0, - flag: 0, - expected: false, - }, - { - name: "Flags 52, flag 7", - flags: 52, - flag: 7, - expected: false, - }, - { - name: "Flags -128, flag 7", - flags: math.MinInt8, - flag: 7, - expected: true, - }, - { - name: "Flags -128, flag 3", - flags: math.MinInt8, - flag: 3, - expected: false, - }, - { - name: "Flags -1, flag 3", - flags: -1, - flag: 3, - expected: true, - }, - { - name: "Flags -1, flag 7", - flags: -1, - flag: 7, - expected: true, - }, - { - name: "Flags -1, flag 8", - flags: -1, - flag: 8, - expected: false, - }, - } -} - -func providerSetFlagInt8() []providerTypeSetFlagInt8 { - return []providerTypeSetFlagInt8{ - { - name: "Flags 0, flag 0, unset", - flags: 0, - flag: 0, - set: false, - expected: 0, - err: nil, - }, - { - name: "Flags 0, flag 0, set", - flags: 0, - flag: 0, - set: true, - expected: 1, - err: nil, - }, - { - name: "Flags 1, flag 1, set", - flags: 1, - flag: 1, - set: true, - expected: 3, - err: nil, - }, - { - name: "Flags 53, flag 1, set", - flags: 53, - flag: 1, - set: true, - expected: 55, - err: nil, - }, - { - name: "Flags 53, flag 0, unset", - flags: 53, - flag: 0, - set: false, - expected: 52, - err: nil, - }, - { - name: "Flags -1, flag 5, set", - flags: -1, - flag: 5, - set: true, - expected: -1, - err: nil, - }, - { - name: "Flags -1, flag 5, unset", - flags: -1, - flag: 5, - set: false, - expected: -33, - err: nil, - }, - { - name: "Flags -1, flag 7, unset", - flags: -1, - flag: 7, - set: false, - expected: 127, - err: nil, - }, - { - name: "Flags -128, flag 7, unset", - flags: math.MinInt8, - flag: 7, - set: false, - expected: 0, - err: nil, - }, - { - name: "Flags -128, flag 8, unset", - flags: math.MinInt8, - flag: 8, - set: false, - expected: math.MinInt8, - err: binflags.ErrorOutOfRange(binflags.ErrorMsgOutOfRange), - }, - } -} diff --git a/map_int16.go b/map_int16.go deleted file mode 100644 index 13faf88..0000000 --- a/map_int16.go +++ /dev/null @@ -1,37 +0,0 @@ -package binflags - -func HasFlagMapInt16(flags map[uint64]int16, flag uint64) bool { - if len(flags) == 0 { - return false - } - - idx, bit := flagExt(flag, FlagMaxInt16) - - return HasFlagInt16(flags[idx], bit) -} - -func SetFlagMapInt16(flags map[uint64]int16, flag uint64, set bool) error { - if flags == nil { - return ErrorFlagsMapNil(ErrorMsgFlagsMapNil) - } - - if HasFlagMapInt16(flags, flag) == set { - return nil - } - - idx, bit := flagExt(flag, FlagMaxInt16) - bits, ok := flags[idx] - - ret, err := SetFlagInt16(bits, bit, set) - if err != nil { - return err - } - - if ret != 0 { - flags[idx] = ret - } else if ok { - delete(flags, idx) - } - - return nil -} diff --git a/map_int16_test.go b/map_int16_test.go deleted file mode 100644 index cafdee6..0000000 --- a/map_int16_test.go +++ /dev/null @@ -1,246 +0,0 @@ -package binflags_test - -import ( - "math" - "reflect" - "testing" - - "github.com/aohorodnyk/binflags" -) - -func TestHasFlagMapInt16(t *testing.T) { - t.Parallel() - - for _, prov := range providerHasFlagMapInt16() { - prov := prov - t.Run(prov.name, func(t *testing.T) { - t.Parallel() - - actual := binflags.HasFlagMapInt16(prov.flags, prov.flag) - - if prov.expected != actual { - t.Fatalf("Unexpected result.\nExpected: %t\nActual: %t", prov.expected, actual) - } - }) - } -} - -func TestSetFlagMapInt16(t *testing.T) { - t.Parallel() - - for _, prov := range providerSetFlagMapInt16() { - prov := prov - t.Run(prov.name, func(t *testing.T) { - t.Parallel() - - err := binflags.SetFlagMapInt16(prov.flags, prov.flag, prov.set) - - if !reflect.DeepEqual(prov.err, err) { - t.Fatalf("Unexpected error.\nExpected: %T(%v)\nActual: %T(%v)", prov.err, prov.err, err, err) - } - - if !reflect.DeepEqual(prov.expected, prov.flags) { - t.Fatalf("Unexpected flags.\nExpected: %+v\nActual: %+v", prov.expected, prov.flags) - } - }) - } -} - -type providerTypeHasFlagMapInt16 struct { - name string - flags map[uint64]int16 - flag uint64 - expected bool -} - -type providerTypeSetFlagMapInt16 struct { - name string - flags map[uint64]int16 - flag uint64 - set bool - expected map[uint64]int16 - err error -} - -func providerHasFlagMapInt16() []providerTypeHasFlagMapInt16 { - return []providerTypeHasFlagMapInt16{ - { - name: "Flags {}, flag 0", - flags: map[uint64]int16{}, - flag: 0, - expected: false, - }, - { - name: "Flags {}, flag 12425", - flags: map[uint64]int16{}, - flag: 12425, - expected: false, - }, - { - name: "Flags nil, flag 0", - flags: nil, - flag: 0, - expected: false, - }, - { - name: "Flags nil, flag 12425", - flags: nil, - flag: 12425, - expected: false, - }, - { - name: "Flags {}, flag 2356241245436", - flags: map[uint64]int16{}, - flag: 2356241245436, - expected: false, - }, - { - name: "Flags {0: 1}, flag 0", - flags: map[uint64]int16{0: 1}, - flag: 0, - expected: true, - }, - { - name: "Flags {0: -32768}, flag 15", - flags: map[uint64]int16{0: math.MinInt16}, - flag: 15, - expected: true, - }, - { - name: "Flags {1: -32768}, flag 31", - flags: map[uint64]int16{1: math.MinInt16}, - flag: 31, - expected: true, - }, - { - name: "Flags {6235: 32}, flag 99765", - flags: map[uint64]int16{6235: 32}, - flag: 99765, - expected: true, - }, - { - name: "Flags {6235: 32}, flag 99764", - flags: map[uint64]int16{6235: 32}, - flag: 99764, - expected: false, - }, - { - name: "Flags {6235: 32}, flag 99766", - flags: map[uint64]int16{6235: 32}, - flag: 99766, - expected: false, - }, - } -} - -func providerSetFlagMapInt16() []providerTypeSetFlagMapInt16 { - return []providerTypeSetFlagMapInt16{ - { - name: "Flags {}, flag 0, set", - flags: map[uint64]int16{}, - flag: 0, - set: true, - expected: map[uint64]int16{0: 1}, - }, - { - name: "Flags {}, flag 7, set", - flags: map[uint64]int16{}, - flag: 7, - set: true, - expected: map[uint64]int16{0: 128}, - }, - { - name: "Flags {}, flag 15, set", - flags: map[uint64]int16{}, - flag: 15, - set: true, - expected: map[uint64]int16{0: -32768}, - }, - { - name: "Flags {0: 92}, flag 7, set", - flags: map[uint64]int16{0: 92}, - flag: 7, - set: true, - expected: map[uint64]int16{0: 220}, - }, - { - name: "Flags {0: 220}, flag 7, unset", - flags: map[uint64]int16{0: 220}, - flag: 7, - set: false, - expected: map[uint64]int16{0: 92}, - }, - { - name: "Flags {0: 220}, flag 7, set", - flags: map[uint64]int16{0: 220}, - flag: 7, - set: true, - expected: map[uint64]int16{0: 220}, - }, - { - name: "Flags {0: 220}, flag 1, unset", - flags: map[uint64]int16{0: 220}, - flag: 1, - set: false, - expected: map[uint64]int16{0: 220}, - }, - { - name: "Flags {0: 220}, flag 14, set", - flags: map[uint64]int16{0: 220}, - flag: 14, - set: true, - expected: map[uint64]int16{0: 16604}, - }, - { - name: "Flags {0: 16604, 6: 23562}, flag 95, set", - flags: map[uint64]int16{0: 16604, 6: 23562}, - flag: 95, - set: true, - expected: map[uint64]int16{0: 16604, 5: -32768, 6: 23562}, - }, - { - name: "Flags {0: 16604, 5: -32768, 6: 23562}, flag 92, set", - flags: map[uint64]int16{0: 16604, 5: -32768, 6: 23562}, - flag: 92, - set: true, - expected: map[uint64]int16{0: 16604, 5: -28672, 6: 23562}, - }, - { - name: "Flags {0: 16604, 5: -32768, 6: 23562}, flag 95, unset", - flags: map[uint64]int16{0: 16604, 5: -32768, 6: 23562}, - flag: 95, - set: false, - expected: map[uint64]int16{0: 16604, 6: 23562}, - }, - { - name: "Flags {0: 16604, 5: -32768, 6: 23562}, flag 97, unset", - flags: map[uint64]int16{0: 16604, 5: -32768, 6: 23562}, - flag: 97, - set: false, - expected: map[uint64]int16{0: 16604, 5: -32768, 6: 23560}, - }, - { - name: "Flags {0: 16604, 5: -32768, 6: 23562}, flag 97, set", - flags: map[uint64]int16{0: 16604, 5: -32768, 6: 23562}, - flag: 25365262353, - set: true, - expected: map[uint64]int16{0: 16604, 5: -32768, 6: 23562, 1585328897: 2}, - }, - { - name: "Flags nil, flag 0, unset", - flags: nil, - flag: 0, - set: false, - expected: nil, - err: binflags.ErrorFlagsMapNil(binflags.ErrorMsgFlagsMapNil), - }, - { - name: "Flags nil, flag 0, set", - flags: nil, - flag: 0, - set: true, - expected: nil, - err: binflags.ErrorFlagsMapNil(binflags.ErrorMsgFlagsMapNil), - }, - } -} diff --git a/map_int32.go b/map_int32.go deleted file mode 100644 index c461a12..0000000 --- a/map_int32.go +++ /dev/null @@ -1,37 +0,0 @@ -package binflags - -func HasFlagMapInt32(flags map[uint64]int32, flag uint64) bool { - if len(flags) == 0 { - return false - } - - idx, bit := flagExt(flag, FlagMaxInt32) - - return HasFlagInt32(flags[idx], bit) -} - -func SetFlagMapInt32(flags map[uint64]int32, flag uint64, set bool) error { - if flags == nil { - return ErrorFlagsMapNil(ErrorMsgFlagsMapNil) - } - - if HasFlagMapInt32(flags, flag) == set { - return nil - } - - idx, bit := flagExt(flag, FlagMaxInt32) - bits, ok := flags[idx] - - ret, err := SetFlagInt32(bits, bit, set) - if err != nil { - return err - } - - if ret != 0 { - flags[idx] = ret - } else if ok { - delete(flags, idx) - } - - return nil -} diff --git a/map_int32_test.go b/map_int32_test.go deleted file mode 100644 index 8667cad..0000000 --- a/map_int32_test.go +++ /dev/null @@ -1,238 +0,0 @@ -package binflags_test - -import ( - "reflect" - "testing" - - "github.com/aohorodnyk/binflags" -) - -func TestHasFlagMapInt32(t *testing.T) { - t.Parallel() - - for _, prov := range providerHasFlagMapInt32() { - prov := prov - t.Run(prov.name, func(t *testing.T) { - t.Parallel() - - actual := binflags.HasFlagMapInt32(prov.flags, prov.flag) - - if prov.expected != actual { - t.Fatalf("Unexpected result.\nExpected: %t\nActual: %t", prov.expected, actual) - } - }) - } -} - -func TestSetFlagMapInt32(t *testing.T) { - t.Parallel() - - for _, prov := range providerSetFlagMapInt32() { - prov := prov - t.Run(prov.name, func(t *testing.T) { - t.Parallel() - - err := binflags.SetFlagMapInt32(prov.flags, prov.flag, prov.set) - - if !reflect.DeepEqual(prov.err, err) { - t.Fatalf("Unexpected error.\nExpected: %T(%v)\nActual: %T(%v)", prov.err, prov.err, err, err) - } - - if !reflect.DeepEqual(prov.expected, prov.flags) { - t.Fatalf("Unexpected flags.\nExpected: %+v\nActual: %+v", prov.expected, prov.flags) - } - }) - } -} - -type providerTypeHasFlagMapInt32 struct { - name string - flags map[uint64]int32 - flag uint64 - expected bool -} - -type providerTypeSetFlagMapInt32 struct { - name string - flags map[uint64]int32 - flag uint64 - set bool - expected map[uint64]int32 - err error -} - -func providerHasFlagMapInt32() []providerTypeHasFlagMapInt32 { - return []providerTypeHasFlagMapInt32{ - { - name: "Flags {}, flag 0", - flags: map[uint64]int32{}, - flag: 0, - expected: false, - }, - { - name: "Flags {}, flag 12425", - flags: map[uint64]int32{}, - flag: 12425, - expected: false, - }, - { - name: "Flags nil, flag 0", - flags: nil, - flag: 0, - expected: false, - }, - { - name: "Flags nil, flag 12425", - flags: nil, - flag: 12425, - expected: false, - }, - { - name: "Flags nil, flag 2356241245436", - flags: map[uint64]int32{}, - flag: 2356241245436, - expected: false, - }, - { - name: "Flags {0: 1}, flag 0", - flags: map[uint64]int32{0: 1}, - flag: 0, - expected: true, - }, - { - name: "Flags {0: 32768}, flag 15", - flags: map[uint64]int32{0: 32768}, - flag: 15, - expected: true, - }, - { - name: "Flags {0: -2147483648}, flag 31", - flags: map[uint64]int32{0: -2147483648}, - flag: 31, - expected: true, - }, - { - name: "Flags {0: -2147483648, 1: 1}, flag 32", - flags: map[uint64]int32{0: -2147483648, 1: 1}, - flag: 32, - expected: true, - }, - { - name: "Flags {0: -2147483648, 1: 1449509}, flag 52", - flags: map[uint64]int32{0: -2147483648, 1: 1449509}, - flag: 52, - expected: true, - }, - { - name: "Flags {0: -2147483648, 1323: 1449509}, flag 42356", - flags: map[uint64]int32{0: -2147483648, 1323: 1449509}, - flag: 42356, - expected: true, - }, - } -} - -func providerSetFlagMapInt32() []providerTypeSetFlagMapInt32 { - return []providerTypeSetFlagMapInt32{ - { - name: "Flags {}, flag 0, set", - flags: map[uint64]int32{}, - flag: 0, - set: true, - expected: map[uint64]int32{0: 1}, - }, - { - name: "Flags {}, flag 7, set", - flags: map[uint64]int32{}, - flag: 7, - set: true, - expected: map[uint64]int32{0: 128}, - }, - { - name: "Flags {}, flag 15, set", - flags: map[uint64]int32{}, - flag: 15, - set: true, - expected: map[uint64]int32{0: 32768}, - }, - { - name: "Flags {0: 92}, flag 7, set", - flags: map[uint64]int32{0: 92}, - flag: 7, - set: true, - expected: map[uint64]int32{0: 220}, - }, - { - name: "Flags {0: 220}, flag 7, unset", - flags: map[uint64]int32{0: 220}, - flag: 7, - set: false, - expected: map[uint64]int32{0: 92}, - }, - { - name: "Flags {0: 220}, flag 7, set", - flags: map[uint64]int32{0: 220}, - flag: 7, - set: true, - expected: map[uint64]int32{0: 220}, - }, - { - name: "Flags {0: 220}, flag 1, unset", - flags: map[uint64]int32{0: 220}, - flag: 1, - set: false, - expected: map[uint64]int32{0: 220}, - }, - { - name: "Flags {0: 220}, flag 14, set", - flags: map[uint64]int32{0: 220}, - flag: 14, - set: true, - expected: map[uint64]int32{0: 16604}, - }, - { - name: "Flags {0: 16604}, flag 31, set", - flags: map[uint64]int32{0: 16604}, - flag: 31, - set: true, - expected: map[uint64]int32{0: -2147467044}, - }, - { - name: "Flags {0: -2147467044}, flag 14, set", - flags: map[uint64]int32{0: -2147467044}, - flag: 14, - set: false, - expected: map[uint64]int32{0: -2147483428}, - }, - { - name: "Flags {0: -2147467044}, flag 5234, set", - flags: map[uint64]int32{0: -2147467044}, - flag: 5234, - set: true, - expected: map[uint64]int32{0: -2147467044, 163: 262144}, - }, - { - name: "Flags {0: 436324, 1: 1, 2: 25623541}, flag 32, unset", - flags: map[uint64]int32{0: 436324, 1: 1, 2: 25623541}, - flag: 32, - set: false, - expected: map[uint64]int32{0: 436324, 2: 25623541}, - }, - { - name: "Flags nil, flag 0, unset", - flags: nil, - flag: 0, - set: false, - expected: nil, - err: binflags.ErrorFlagsMapNil(binflags.ErrorMsgFlagsMapNil), - }, - { - name: "Flags nil, flag 0, set", - flags: nil, - flag: 0, - set: true, - expected: nil, - err: binflags.ErrorFlagsMapNil(binflags.ErrorMsgFlagsMapNil), - }, - } -} diff --git a/map_int64.go b/map_int64.go deleted file mode 100644 index f1e5d2c..0000000 --- a/map_int64.go +++ /dev/null @@ -1,37 +0,0 @@ -package binflags - -func HasFlagMapInt64(flags map[uint64]int64, flag uint64) bool { - if len(flags) == 0 { - return false - } - - idx, bit := flagExt(flag, FlagMaxInt64) - - return HasFlagInt64(flags[idx], bit) -} - -func SetFlagMapInt64(flags map[uint64]int64, flag uint64, set bool) error { - if flags == nil { - return ErrorFlagsMapNil(ErrorMsgFlagsMapNil) - } - - if HasFlagMapInt64(flags, flag) == set { - return nil - } - - idx, bit := flagExt(flag, FlagMaxInt64) - bits, ok := flags[idx] - - ret, err := SetFlagInt64(bits, bit, set) - if err != nil { - return err - } - - if ret != 0 { - flags[idx] = ret - } else if ok { - delete(flags, idx) - } - - return nil -} diff --git a/map_int64_test.go b/map_int64_test.go deleted file mode 100644 index af35c18..0000000 --- a/map_int64_test.go +++ /dev/null @@ -1,295 +0,0 @@ -package binflags_test - -import ( - "reflect" - "testing" - - "github.com/aohorodnyk/binflags" -) - -func TestHasFlagMapInt64(t *testing.T) { - t.Parallel() - - for _, prov := range providerHasFlagMapInt64() { - prov := prov - t.Run(prov.name, func(t *testing.T) { - t.Parallel() - - actual := binflags.HasFlagMapInt64(prov.flags, prov.flag) - - if prov.expected != actual { - t.Fatalf("Unexpected result.\nExpected: %t\nActual: %t", prov.expected, actual) - } - }) - } -} - -func TestSetFlagMapInt64(t *testing.T) { - t.Parallel() - - for _, prov := range providerSetFlagMapInt64() { - prov := prov - t.Run(prov.name, func(t *testing.T) { - t.Parallel() - - err := binflags.SetFlagMapInt64(prov.flags, prov.flag, prov.set) - - if !reflect.DeepEqual(prov.err, err) { - t.Fatalf("Unexpected error.\nExpected: %T(%v)\nActual: %T(%v)", prov.err, prov.err, err, err) - } - - if !reflect.DeepEqual(prov.expected, prov.flags) { - t.Fatalf("Unexpected flags.\nExpected: %+v\nActual: %+v", prov.expected, prov.flags) - } - }) - } -} - -type providerTypeHasFlagMapInt64 struct { - name string - flags map[uint64]int64 - flag uint64 - expected bool -} - -type providerTypeSetFlagMapInt64 struct { - name string - flags map[uint64]int64 - flag uint64 - set bool - expected map[uint64]int64 - err error -} - -func providerHasFlagMapInt64() []providerTypeHasFlagMapInt64 { - return []providerTypeHasFlagMapInt64{ - { - name: "Flags {}, flag 0", - flags: map[uint64]int64{}, - flag: 0, - expected: false, - }, - { - name: "Flags {}, flag 12425", - flags: map[uint64]int64{}, - flag: 12425, - expected: false, - }, - { - name: "Flags nil, flag 0", - flags: nil, - flag: 0, - expected: false, - }, - { - name: "Flags nil, flag 12425", - flags: nil, - flag: 12425, - expected: false, - }, - { - name: "Flags {}, flag 2356241245436", - flags: map[uint64]int64{}, - flag: 2356241245436, - expected: false, - }, - { - name: "Flags {0: 1}, flag 0", - flags: map[uint64]int64{0: 1}, - flag: 0, - expected: true, - }, - { - name: "Flags {0: 32768}, flag 15", - flags: map[uint64]int64{0: 32768}, - flag: 15, - expected: true, - }, - { - name: "Flags {0: 2147483648}, flag 31", - flags: map[uint64]int64{0: 2147483648}, - flag: 31, - expected: true, - }, - { - name: "Flags {0: 4503601774854144}, flag 52", - flags: map[uint64]int64{0: 4503601774854144}, - flag: 52, - expected: true, - }, - { - name: "Flags {0: 4503599627370496}, flag 52", - flags: map[uint64]int64{0: 4503599627370496}, - flag: 52, - expected: true, - }, - { - name: "Flags {0: -9218868473709551616}, flag 63", - flags: map[uint64]int64{0: -9218868473709551616}, - flag: 63, - expected: true, - }, - { - name: "Flags {0: -9218868473709551616, 36644: 262144}, flag 2345234", - flags: map[uint64]int64{0: -9218868473709551616, 36644: 262144}, - flag: 2345234, - expected: true, - }, - } -} - -func providerSetFlagMapInt64() []providerTypeSetFlagMapInt64 { - return []providerTypeSetFlagMapInt64{ - { - name: "Flags {}, flag 0, set", - flags: map[uint64]int64{}, - flag: 0, - set: true, - expected: map[uint64]int64{0: 1}, - }, - { - name: "Flags {}, flag 7, set", - flags: map[uint64]int64{}, - flag: 7, - set: true, - expected: map[uint64]int64{0: 128}, - }, - { - name: "Flags {}, flag 15, set", - flags: map[uint64]int64{}, - flag: 15, - set: true, - expected: map[uint64]int64{ - 0: 32768, - }, - }, - { - name: "Flags {0: 92}, flag 7, set", - flags: map[uint64]int64{0: 92}, - flag: 7, - set: true, - expected: map[uint64]int64{0: 220}, - }, - { - name: "Flags {0: 220}, flag 7, unset", - flags: map[uint64]int64{0: 220}, - flag: 7, - set: false, - expected: map[uint64]int64{0: 92}, - }, - { - name: "Flags {0: 220}, flag 7, set", - flags: map[uint64]int64{0: 220}, - flag: 7, - set: true, - expected: map[uint64]int64{0: 220}, - }, - { - name: "Flags {0: 220}, flag 1, unset", - flags: map[uint64]int64{0: 220}, - flag: 1, - set: false, - expected: map[uint64]int64{0: 220}, - }, - { - name: "Flags {0: 220}, flag 14, set", - flags: map[uint64]int64{0: 220}, - flag: 14, - set: true, - expected: map[uint64]int64{0: 16604}, - }, - { - name: "Flags {0: 16604}, flag 31, set", - flags: map[uint64]int64{0: 16604}, - flag: 31, - set: true, - expected: map[uint64]int64{0: 2147500252}, - }, - { - name: "Flags {0: 2147500252}, flag 14, unset", - flags: map[uint64]int64{0: 2147500252}, - flag: 14, - set: false, - expected: map[uint64]int64{0: 2147483868}, - }, - { - name: "Flags {0: 2147500252}, flag 60, set", - flags: map[uint64]int64{0: 2147500252}, - flag: 60, - set: true, - expected: map[uint64]int64{0: 1152921506754347228}, - }, - { - name: "Flags {0: 2147500252}, flag 63, set", - flags: map[uint64]int64{0: 2147500252}, - flag: 63, - set: true, - expected: map[uint64]int64{0: -9223372034707275556}, - }, - { - name: "Flags {0: -9223372034707275556}, flag 63, unset", - flags: map[uint64]int64{0: -9223372034707275556}, - flag: 63, - set: false, - expected: map[uint64]int64{0: 2147500252}, - }, - { - name: "Flags {0: -9223372034707275556}, flag 1245, set", - flags: map[uint64]int64{0: -9223372034707275556}, - flag: 1245, - set: true, - expected: map[uint64]int64{0: -9223372034707275556, 19: 536870912}, - }, - { - name: "Flags {0: -9223372034707275556, 19: 536870912}, flag 1242, set", - flags: map[uint64]int64{0: -9223372034707275556, 19: 536870912}, - flag: 1242, - set: true, - expected: map[uint64]int64{0: -9223372034707275556, 19: 603979776}, - }, - { - name: "Flags {0: -9223372034707275556, 19: 603979776}, flag 1242, unset", - flags: map[uint64]int64{0: -9223372034707275556, 19: 603979776}, - flag: 1242, - set: false, - expected: map[uint64]int64{0: -9223372034707275556, 19: 536870912}, - }, - { - name: "Flags {0: -9223372034707275556, 19: 603979776}, flag 1242, set", - flags: map[uint64]int64{0: -9223372034707275556, 19: 603979776}, - flag: 1242, - set: true, - expected: map[uint64]int64{0: -9223372034707275556, 19: 603979776}, - }, - { - name: "Flags {0: -9223372034707275556, 19: 536870912}, flag 1242, unset", - flags: map[uint64]int64{0: -9223372034707275556, 19: 536870912}, - flag: 1242, - set: false, - expected: map[uint64]int64{0: -9223372034707275556, 19: 536870912}, - }, - { - name: "Flags {0: 436324, 1: 1, 2: 25623541}, flag 64, unset", - flags: map[uint64]int64{0: 436324, 1: 1, 2: 25623541}, - flag: 64, - set: false, - expected: map[uint64]int64{0: 436324, 2: 25623541}, - }, - { - name: "Flags nil, flag 0, unset", - flags: nil, - flag: 0, - set: false, - expected: nil, - err: binflags.ErrorFlagsMapNil(binflags.ErrorMsgFlagsMapNil), - }, - { - name: "Flags nil, flag 0, set", - flags: nil, - flag: 0, - set: true, - expected: nil, - err: binflags.ErrorFlagsMapNil(binflags.ErrorMsgFlagsMapNil), - }, - } -} diff --git a/map_int8.go b/map_int8.go deleted file mode 100644 index 29446c2..0000000 --- a/map_int8.go +++ /dev/null @@ -1,37 +0,0 @@ -package binflags - -func HasFlagMapInt8(flags map[uint64]int8, flag uint64) bool { - if len(flags) == 0 { - return false - } - - idx, bit := flagExt(flag, FlagMaxInt8) - - return HasFlagInt8(flags[idx], bit) -} - -func SetFlagMapInt8(flags map[uint64]int8, flag uint64, set bool) error { - if flags == nil { - return ErrorFlagsMapNil(ErrorMsgFlagsMapNil) - } - - if HasFlagMapInt8(flags, flag) == set { - return nil - } - - idx, bit := flagExt(flag, FlagMaxInt8) - bits, ok := flags[idx] - - ret, err := SetFlagInt8(bits, bit, set) - if err != nil { - return err - } - - if ret != 0 { - flags[idx] = ret - } else if ok { - delete(flags, idx) - } - - return nil -} diff --git a/map_int8_test.go b/map_int8_test.go deleted file mode 100644 index f829655..0000000 --- a/map_int8_test.go +++ /dev/null @@ -1,249 +0,0 @@ -package binflags_test - -import ( - "reflect" - "testing" - - "github.com/aohorodnyk/binflags" -) - -func TestHasFlagMapInt8(t *testing.T) { - t.Parallel() - - for _, prov := range providerHasFlagMapInt8() { - prov := prov - t.Run(prov.name, func(t *testing.T) { - t.Parallel() - - actual := binflags.HasFlagMapInt8(prov.flags, prov.flag) - - if prov.expected != actual { - t.Fatalf("Unexpected result.\nExpected: %t\nActual: %t", prov.expected, actual) - } - }) - } -} - -func TestSetFlagMapInt8(t *testing.T) { - t.Parallel() - - for _, prov := range providerSetFlagMapInt8() { - prov := prov - t.Run(prov.name, func(t *testing.T) { - t.Parallel() - - err := binflags.SetFlagMapInt8(prov.flags, prov.flag, prov.set) - - if !reflect.DeepEqual(prov.err, err) { - t.Fatalf("Unexpected error.\nExpected: %T(%v)\nActual: %T(%v)", prov.err, prov.err, err, err) - } - - if !reflect.DeepEqual(prov.expected, prov.flags) { - t.Fatalf("Unexpected flags.\nExpected: %+v\nActual: %+v", prov.expected, prov.flags) - } - }) - } -} - -type providerTypeHasFlagMapInt8 struct { - name string - flags map[uint64]int8 - flag uint64 - expected bool -} - -type providerTypeSetFlagMapInt8 struct { - name string - flags map[uint64]int8 - flag uint64 - set bool - expected map[uint64]int8 - err error -} - -func providerHasFlagMapInt8() []providerTypeHasFlagMapInt8 { - return []providerTypeHasFlagMapInt8{ - { - name: "Flags {}, flag 0", - flags: map[uint64]int8{}, - flag: 0, - expected: false, - }, - { - name: "Flags {}, flag 12425", - flags: map[uint64]int8{}, - flag: 12425, - expected: false, - }, - { - name: "Flags nil, flag 0", - flags: nil, - flag: 0, - expected: false, - }, - { - name: "Flags nil, flag 12425", - flags: nil, - flag: 12425, - expected: false, - }, - { - name: "Flags {}, flag 2356241245436", - flags: map[uint64]int8{}, - flag: 2356241245436, - expected: false, - }, - { - name: "Flags {0: 1}, flag 0", - flags: map[uint64]int8{0: 1}, - flag: 0, - expected: true, - }, - { - name: "Flags {234125: -128}, flag 1873007", - flags: map[uint64]int8{234125: -128}, - flag: 1873007, - expected: true, - }, - { - name: "Flags {234125: -128}, flag 1873006", - flags: map[uint64]int8{234125: -128}, - flag: 1873006, - expected: false, - }, - { - name: "Flags {234125: -128}, flag 1873008", - flags: map[uint64]int8{234125: -128}, - flag: 1873008, - expected: false, - }, - { - name: "Flags {0: -128, 234125: -128}, flag 7", - flags: map[uint64]int8{0: -128, 234125: -128}, - flag: 7, - expected: true, - }, - { - name: "Flags {0: -1, 5: -1, 63: -1, 234125: -128}, flag 7", - flags: map[uint64]int8{0: -1, 5: -1, 63: -1, 234125: -128}, - flag: 7, - expected: true, - }, - { - name: "Flags {0: -1, 5: 64, 63: -128, 234125: -128}, flag 7", - flags: map[uint64]int8{0: -1, 5: 64, 63: -128, 234125: -128}, - flag: 7, - expected: true, - }, - { - name: "Flags {0: -1, 5: 64, 63: -128, 234125: -128}, flag 46", - flags: map[uint64]int8{0: -1, 5: 64, 63: -128, 234125: -128}, - flag: 46, - expected: true, - }, - { - name: "Flags {0: -1, 5: 64, 63: -128, 234125: -128}, flag 511", - flags: map[uint64]int8{0: -1, 5: 64, 63: -128, 234125: -128}, - flag: 511, - expected: true, - }, - } -} - -func providerSetFlagMapInt8() []providerTypeSetFlagMapInt8 { - return []providerTypeSetFlagMapInt8{ - { - name: "Flags {}, flag 0, set", - flags: map[uint64]int8{}, - flag: 0, - set: true, - expected: map[uint64]int8{0: 1}, - }, - { - name: "Flags {0: -128}, flag 7, unset", - flags: map[uint64]int8{0: -128}, - flag: 7, - set: false, - expected: map[uint64]int8{}, - }, - { - name: "Flags {0: -128}, flag 7, set", - flags: map[uint64]int8{0: -128}, - flag: 7, - set: true, - expected: map[uint64]int8{0: -128}, - }, - { - name: "Flags {}, flag 7, set", - flags: map[uint64]int8{}, - flag: 7, - set: true, - expected: map[uint64]int8{0: -128}, - }, - { - name: "Flags {}, flag 2346, set", - flags: map[uint64]int8{}, - flag: 2346, - set: true, - expected: map[uint64]int8{293: 4}, - }, - { - name: "Flags {0: 92}, flag 7, set", - flags: map[uint64]int8{0: 92}, - flag: 7, - set: true, - expected: map[uint64]int8{0: -36}, - }, - { - name: "Flags {0: -36}, flag 7, unset", - flags: map[uint64]int8{0: -36}, - flag: 7, - set: false, - expected: map[uint64]int8{0: 92}, - }, - { - name: "Flags {0: -36, 1: 56, 6: 32, 6235: 123}, flag 48, set", - flags: map[uint64]int8{0: -36, 1: 56, 6: 32, 6235: 123}, - flag: 48, - set: true, - expected: map[uint64]int8{0: -36, 1: 56, 6: 33, 6235: 123}, - }, - { - name: "Flags {0: -36, 1: 56, 6: 32, 6235: 123}, flag 53, unset", - flags: map[uint64]int8{0: -36, 1: 56, 6: 32, 6235: 123}, - flag: 53, - set: false, - expected: map[uint64]int8{0: -36, 1: 56, 6235: 123}, - }, - { - name: "Flags {0: -36, 1: 56, 6235: 123}, flag 53, set", - flags: map[uint64]int8{0: -36, 1: 56, 6235: 123}, - flag: 53, - set: true, - expected: map[uint64]int8{0: -36, 1: 56, 6: 32, 6235: 123}, - }, - { - name: "Flags {0: -36, 1: 56, 6235: 123}, flag 53, unset", - flags: map[uint64]int8{0: -36, 1: 56, 6235: 123}, - flag: 53, - set: false, - expected: map[uint64]int8{0: -36, 1: 56, 6235: 123}, - }, - { - name: "Flags nil, flag 0, unset", - flags: nil, - flag: 0, - set: false, - expected: nil, - err: binflags.ErrorFlagsMapNil(binflags.ErrorMsgFlagsMapNil), - }, - { - name: "Flags nil, flag 0, set", - flags: nil, - flag: 0, - set: true, - expected: nil, - err: binflags.ErrorFlagsMapNil(binflags.ErrorMsgFlagsMapNil), - }, - } -} diff --git a/map_uint16.go b/map_uint16.go deleted file mode 100644 index 39869fa..0000000 --- a/map_uint16.go +++ /dev/null @@ -1,37 +0,0 @@ -package binflags - -func HasFlagMapUint16(flags map[uint64]uint16, flag uint64) bool { - if len(flags) == 0 { - return false - } - - idx, bit := flagExt(flag, FlagMaxInt16) - - return HasFlagUint16(flags[idx], bit) -} - -func SetFlagMapUint16(flags map[uint64]uint16, flag uint64, set bool) error { - if flags == nil { - return ErrorFlagsMapNil(ErrorMsgFlagsMapNil) - } - - if HasFlagMapUint16(flags, flag) == set { - return nil - } - - idx, bit := flagExt(flag, FlagMaxInt16) - bits, ok := flags[idx] - - ret, err := SetFlagUint16(bits, bit, set) - if err != nil { - return err - } - - if ret != 0 { - flags[idx] = ret - } else if ok { - delete(flags, idx) - } - - return nil -} diff --git a/map_uint16_test.go b/map_uint16_test.go deleted file mode 100644 index a2b9bea..0000000 --- a/map_uint16_test.go +++ /dev/null @@ -1,245 +0,0 @@ -package binflags_test - -import ( - "reflect" - "testing" - - "github.com/aohorodnyk/binflags" -) - -func TestHasFlagMapUint16(t *testing.T) { - t.Parallel() - - for _, prov := range providerHasFlagMapUint16() { - prov := prov - t.Run(prov.name, func(t *testing.T) { - t.Parallel() - - actual := binflags.HasFlagMapUint16(prov.flags, prov.flag) - - if prov.expected != actual { - t.Fatalf("Unexpected result.\nExpected: %t\nActual: %t", prov.expected, actual) - } - }) - } -} - -func TestSetFlagMapUint16(t *testing.T) { - t.Parallel() - - for _, prov := range providerSetFlagMapUint16() { - prov := prov - t.Run(prov.name, func(t *testing.T) { - t.Parallel() - - err := binflags.SetFlagMapUint16(prov.flags, prov.flag, prov.set) - - if !reflect.DeepEqual(prov.err, err) { - t.Fatalf("Unexpected error.\nExpected: %T(%v)\nActual: %T(%v)", prov.err, prov.err, err, err) - } - - if !reflect.DeepEqual(prov.expected, prov.flags) { - t.Fatalf("Unexpected flags.\nExpected: %+v\nActual: %+v", prov.expected, prov.flags) - } - }) - } -} - -type providerTypeHasFlagMapUint16 struct { - name string - flags map[uint64]uint16 - flag uint64 - expected bool -} - -type providerTypeSetFlagMapUint16 struct { - name string - flags map[uint64]uint16 - flag uint64 - set bool - expected map[uint64]uint16 - err error -} - -func providerHasFlagMapUint16() []providerTypeHasFlagMapUint16 { - return []providerTypeHasFlagMapUint16{ - { - name: "Flags {}, flag 0", - flags: map[uint64]uint16{}, - flag: 0, - expected: false, - }, - { - name: "Flags {}, flag 12425", - flags: map[uint64]uint16{}, - flag: 12425, - expected: false, - }, - { - name: "Flags nil, flag 0", - flags: nil, - flag: 0, - expected: false, - }, - { - name: "Flags nil, flag 12425", - flags: nil, - flag: 12425, - expected: false, - }, - { - name: "Flags {}, flag 2356241245436", - flags: map[uint64]uint16{}, - flag: 2356241245436, - expected: false, - }, - { - name: "Flags {0: 1}, flag 0", - flags: map[uint64]uint16{0: 1}, - flag: 0, - expected: true, - }, - { - name: "Flags {0: 32768}, flag 0", - flags: map[uint64]uint16{0: 32768}, - flag: 15, - expected: true, - }, - { - name: "Flags {0: 32768}, flag 31", - flags: map[uint64]uint16{1: 32768}, - flag: 31, - expected: true, - }, - { - name: "Flags {6235: 32}, flag 99765", - flags: map[uint64]uint16{6235: 32}, - flag: 99765, - expected: true, - }, - { - name: "Flags {6235: 32}, flag 99764", - flags: map[uint64]uint16{6235: 32}, - flag: 99764, - expected: false, - }, - { - name: "Flags {6235: 32}, flag 99766", - flags: map[uint64]uint16{6235: 32}, - flag: 99766, - expected: false, - }, - } -} - -func providerSetFlagMapUint16() []providerTypeSetFlagMapUint16 { - return []providerTypeSetFlagMapUint16{ - { - name: "Flags {}, flag 0, set", - flags: map[uint64]uint16{}, - flag: 0, - set: true, - expected: map[uint64]uint16{0: 1}, - }, - { - name: "Flags {}, flag 7, set", - flags: map[uint64]uint16{}, - flag: 7, - set: true, - expected: map[uint64]uint16{0: 128}, - }, - { - name: "Flags {}, flag 15, set", - flags: map[uint64]uint16{}, - flag: 15, - set: true, - expected: map[uint64]uint16{0: 32768}, - }, - { - name: "Flags {0: 92}, flag 7, set", - flags: map[uint64]uint16{0: 92}, - flag: 7, - set: true, - expected: map[uint64]uint16{0: 220}, - }, - { - name: "Flags {0: 220}, flag 7, unset", - flags: map[uint64]uint16{0: 220}, - flag: 7, - set: false, - expected: map[uint64]uint16{0: 92}, - }, - { - name: "Flags {0: 220}, flag 7, set", - flags: map[uint64]uint16{0: 220}, - flag: 7, - set: true, - expected: map[uint64]uint16{0: 220}, - }, - { - name: "Flags {0: 220}, flag 1, unset", - flags: map[uint64]uint16{0: 220}, - flag: 1, - set: false, - expected: map[uint64]uint16{0: 220}, - }, - { - name: "Flags {0: 220}, flag 14, set", - flags: map[uint64]uint16{0: 220}, - flag: 14, - set: true, - expected: map[uint64]uint16{0: 16604}, - }, - { - name: "Flags {0: 16604, 6: 23562}, flag 95, set", - flags: map[uint64]uint16{0: 16604, 6: 23562}, - flag: 95, - set: true, - expected: map[uint64]uint16{0: 16604, 5: 32768, 6: 23562}, - }, - { - name: "Flags {0: 16604, 5: 32768, 6: 23562}, flag 94, set", - flags: map[uint64]uint16{0: 16604, 5: 32768, 6: 23562}, - flag: 94, - set: true, - expected: map[uint64]uint16{0: 16604, 5: 49152, 6: 23562}, - }, - { - name: "Flags {0: 16604, 5: 32768, 6: 23562}, flag 95, unset", - flags: map[uint64]uint16{0: 16604, 5: 32768, 6: 23562}, - flag: 95, - set: false, - expected: map[uint64]uint16{0: 16604, 6: 23562}, - }, - { - name: "Flags {0: 16604, 5: 32768, 6: 23562}, flag 97, unset", - flags: map[uint64]uint16{0: 16604, 5: 32768, 6: 23562}, - flag: 97, - set: false, - expected: map[uint64]uint16{0: 16604, 5: 32768, 6: 23560}, - }, - { - name: "Flags {0: 16604, 5: 32768, 6: 23562}, flag 25365262353, set", - flags: map[uint64]uint16{0: 16604, 5: 32768, 6: 23562}, - flag: 25365262353, - set: true, - expected: map[uint64]uint16{0: 16604, 5: 32768, 6: 23562, 1585328897: 2}, - }, - { - name: "Flags nil, flag 0, unset", - flags: nil, - flag: 0, - set: false, - expected: nil, - err: binflags.ErrorFlagsMapNil(binflags.ErrorMsgFlagsMapNil), - }, - { - name: "Flags nil, flag 0, set", - flags: nil, - flag: 0, - set: true, - expected: nil, - err: binflags.ErrorFlagsMapNil(binflags.ErrorMsgFlagsMapNil), - }, - } -} diff --git a/map_uint32.go b/map_uint32.go deleted file mode 100644 index c256636..0000000 --- a/map_uint32.go +++ /dev/null @@ -1,37 +0,0 @@ -package binflags - -func HasFlagMapUint32(flags map[uint64]uint32, flag uint64) bool { - if len(flags) == 0 { - return false - } - - idx, bit := flagExt(flag, FlagMaxInt32) - - return HasFlagUint32(flags[idx], bit) -} - -func SetFlagMapUint32(flags map[uint64]uint32, flag uint64, set bool) error { - if flags == nil { - return ErrorFlagsMapNil(ErrorMsgFlagsMapNil) - } - - if HasFlagMapUint32(flags, flag) == set { - return nil - } - - idx, bit := flagExt(flag, FlagMaxInt32) - bits, ok := flags[idx] - - ret, err := SetFlagUint32(bits, bit, set) - if err != nil { - return err - } - - if ret != 0 { - flags[idx] = ret - } else if ok { - delete(flags, idx) - } - - return nil -} diff --git a/map_uint32_test.go b/map_uint32_test.go deleted file mode 100644 index ecaf962..0000000 --- a/map_uint32_test.go +++ /dev/null @@ -1,238 +0,0 @@ -package binflags_test - -import ( - "reflect" - "testing" - - "github.com/aohorodnyk/binflags" -) - -func TestHasFlagMapUint32(t *testing.T) { - t.Parallel() - - for _, prov := range providerHasFlagMapUint32() { - prov := prov - t.Run(prov.name, func(t *testing.T) { - t.Parallel() - - actual := binflags.HasFlagMapUint32(prov.flags, prov.flag) - - if prov.expected != actual { - t.Fatalf("Unexpected result.\nExpected: %t\nActual: %t", prov.expected, actual) - } - }) - } -} - -func TestSetFlagMapUint32(t *testing.T) { - t.Parallel() - - for _, prov := range providerSetFlagMapUint32() { - prov := prov - t.Run(prov.name, func(t *testing.T) { - t.Parallel() - - err := binflags.SetFlagMapUint32(prov.flags, prov.flag, prov.set) - - if !reflect.DeepEqual(prov.err, err) { - t.Fatalf("Unexpected error.\nExpected: %T(%v)\nActual: %T(%v)", prov.err, prov.err, err, err) - } - - if !reflect.DeepEqual(prov.expected, prov.flags) { - t.Fatalf("Unexpected flags.\nExpected: %+v\nActual: %+v", prov.expected, prov.flags) - } - }) - } -} - -type providerTypeHasFlagMapUint32 struct { - name string - flags map[uint64]uint32 - flag uint64 - expected bool -} - -type providerTypeSetFlagMapUint32 struct { - name string - flags map[uint64]uint32 - flag uint64 - set bool - expected map[uint64]uint32 - err error -} - -func providerHasFlagMapUint32() []providerTypeHasFlagMapUint32 { - return []providerTypeHasFlagMapUint32{ - { - name: "Flags {}, flag 0", - flags: map[uint64]uint32{}, - flag: 0, - expected: false, - }, - { - name: "Flags {}, flag 12425", - flags: map[uint64]uint32{}, - flag: 12425, - expected: false, - }, - { - name: "Flags nil, flag 0", - flags: nil, - flag: 0, - expected: false, - }, - { - name: "Flags nil, flag 12425", - flags: nil, - flag: 12425, - expected: false, - }, - { - name: "Flags {}, flag 2356241245436", - flags: map[uint64]uint32{}, - flag: 2356241245436, - expected: false, - }, - { - name: "Flags {0: 1}, flag 0", - flags: map[uint64]uint32{0: 1}, - flag: 0, - expected: true, - }, - { - name: "Flags {0: 32768}, flag 15", - flags: map[uint64]uint32{0: 32768}, - flag: 15, - expected: true, - }, - { - name: "Flags {0: 2147483648}, flag 31", - flags: map[uint64]uint32{0: 2147483648}, - flag: 31, - expected: true, - }, - { - name: "Flags {0: 2147483648, 1: 1}, flag 32", - flags: map[uint64]uint32{0: 2147483648, 1: 1}, - flag: 32, - expected: true, - }, - { - name: "Flags {0: 2147483648, 1: 1449509}, flag 52", - flags: map[uint64]uint32{0: 2147483648, 1: 1449509}, - flag: 52, - expected: true, - }, - { - name: "Flags {0: 2147483648, 1323: 1449509}, flag 42356", - flags: map[uint64]uint32{0: 2147483648, 1323: 1449509}, - flag: 42356, - expected: true, - }, - } -} - -func providerSetFlagMapUint32() []providerTypeSetFlagMapUint32 { - return []providerTypeSetFlagMapUint32{ - { - name: "Flags {}, flag 0, set", - flags: map[uint64]uint32{}, - flag: 0, - set: true, - expected: map[uint64]uint32{0: 1}, - }, - { - name: "Flags {}, flag 7, set", - flags: map[uint64]uint32{}, - flag: 7, - set: true, - expected: map[uint64]uint32{0: 128}, - }, - { - name: "Flags {}, flag 15, set", - flags: map[uint64]uint32{}, - flag: 15, - set: true, - expected: map[uint64]uint32{0: 32768}, - }, - { - name: "Flags {}, flag 7, set", - flags: map[uint64]uint32{0: 92}, - flag: 7, - set: true, - expected: map[uint64]uint32{0: 220}, - }, - { - name: "Flags {0: 220}, flag 7, unset", - flags: map[uint64]uint32{0: 220}, - flag: 7, - set: false, - expected: map[uint64]uint32{0: 92}, - }, - { - name: "Flags {0: 220}, flag 7, set", - flags: map[uint64]uint32{0: 220}, - flag: 7, - set: true, - expected: map[uint64]uint32{0: 220}, - }, - { - name: "Flags {0: 220}, flag 1, unset", - flags: map[uint64]uint32{0: 220}, - flag: 1, - set: false, - expected: map[uint64]uint32{0: 220}, - }, - { - name: "Flags {0: 220}, flag 14, set", - flags: map[uint64]uint32{0: 220}, - flag: 14, - set: true, - expected: map[uint64]uint32{0: 16604}, - }, - { - name: "Flags {0: 16604}, flag 31, set", - flags: map[uint64]uint32{0: 16604}, - flag: 31, - set: true, - expected: map[uint64]uint32{0: 2147500252}, - }, - { - name: "Flags {0: 2147500252}, flag 14, unset", - flags: map[uint64]uint32{0: 2147500252}, - flag: 14, - set: false, - expected: map[uint64]uint32{0: 2147483868}, - }, - { - name: "Flags {0: 2147500252}, flag 5234, set", - flags: map[uint64]uint32{0: 2147500252}, - flag: 5234, - set: true, - expected: map[uint64]uint32{0: 2147500252, 163: 262144}, - }, - { - name: "Flags {0: 436324, 1: 1, 2: 25623541}, flag 32, unset", - flags: map[uint64]uint32{0: 436324, 1: 1, 2: 25623541}, - flag: 32, - set: false, - expected: map[uint64]uint32{0: 436324, 2: 25623541}, - }, - { - name: "Flags nil, flag 0, unset", - flags: nil, - flag: 0, - set: false, - expected: nil, - err: binflags.ErrorFlagsMapNil(binflags.ErrorMsgFlagsMapNil), - }, - { - name: "Flags nil, flag 0, set", - flags: nil, - flag: 0, - set: true, - expected: nil, - err: binflags.ErrorFlagsMapNil(binflags.ErrorMsgFlagsMapNil), - }, - } -} diff --git a/map_uint64.go b/map_uint64.go deleted file mode 100644 index 6f76e6b..0000000 --- a/map_uint64.go +++ /dev/null @@ -1,37 +0,0 @@ -package binflags - -func HasFlagMapUint64(flags map[uint64]uint64, flag uint64) bool { - if len(flags) == 0 { - return false - } - - idx, bit := flagExt(flag, FlagMaxInt64) - - return HasFlagUint64(flags[idx], bit) -} - -func SetFlagMapUint64(flags map[uint64]uint64, flag uint64, set bool) error { - if flags == nil { - return ErrorFlagsMapNil(ErrorMsgFlagsMapNil) - } - - if HasFlagMapUint64(flags, flag) == set { - return nil - } - - idx, bit := flagExt(flag, FlagMaxInt64) - bits, ok := flags[idx] - - ret, err := SetFlagUint64(bits, bit, set) - if err != nil { - return err - } - - if ret != 0 { - flags[idx] = ret - } else if ok { - delete(flags, idx) - } - - return nil -} diff --git a/map_uint64_test.go b/map_uint64_test.go deleted file mode 100644 index 45003f8..0000000 --- a/map_uint64_test.go +++ /dev/null @@ -1,293 +0,0 @@ -package binflags_test - -import ( - "reflect" - "testing" - - "github.com/aohorodnyk/binflags" -) - -func TestHasFlagMapUint64(t *testing.T) { - t.Parallel() - - for _, prov := range providerHasFlagMapUint64() { - prov := prov - t.Run(prov.name, func(t *testing.T) { - t.Parallel() - - actual := binflags.HasFlagMapUint64(prov.flags, prov.flag) - - if prov.expected != actual { - t.Fatalf("Unexpected result.\nExpected: %t\nActual: %t", prov.expected, actual) - } - }) - } -} - -func TestSetFlagMapUint64(t *testing.T) { - t.Parallel() - - for _, prov := range providerSetFlagMapUint64() { - prov := prov - t.Run(prov.name, func(t *testing.T) { - t.Parallel() - - err := binflags.SetFlagMapUint64(prov.flags, prov.flag, prov.set) - - if !reflect.DeepEqual(prov.err, err) { - t.Fatalf("Unexpected error.\nExpected: %T(%v)\nActual: %T(%v)", prov.err, prov.err, err, err) - } - - if !reflect.DeepEqual(prov.expected, prov.flags) { - t.Fatalf("Unexpected flags.\nExpected: %+v\nActual: %+v", prov.expected, prov.flags) - } - }) - } -} - -type providerTypeHasFlagMapUint64 struct { - name string - flags map[uint64]uint64 - flag uint64 - expected bool -} - -type providerTypeSetFlagMapUint64 struct { - name string - flags map[uint64]uint64 - flag uint64 - set bool - expected map[uint64]uint64 - err error -} - -func providerHasFlagMapUint64() []providerTypeHasFlagMapUint64 { - return []providerTypeHasFlagMapUint64{ - { - name: "Flags {}, flag 0", - flags: map[uint64]uint64{}, - flag: 0, - expected: false, - }, - { - name: "Flags {}, flag 12425", - flags: map[uint64]uint64{}, - flag: 12425, - expected: false, - }, - { - name: "Flags nil, flag 0", - flags: nil, - flag: 0, - expected: false, - }, - { - name: "Flags nil, flag 12425", - flags: nil, - flag: 12425, - expected: false, - }, - { - name: "Flags {}, flag 2356241245436", - flags: map[uint64]uint64{}, - flag: 2356241245436, - expected: false, - }, - { - name: "Flags {0: 1}, flag 0", - flags: map[uint64]uint64{0: 1}, - flag: 0, - expected: true, - }, - { - name: "Flags {0: 32768}, flag 15", - flags: map[uint64]uint64{0: 32768}, - flag: 15, - expected: true, - }, - { - name: "Flags {0: 2147483648}, flag 31", - flags: map[uint64]uint64{0: 2147483648}, - flag: 31, - expected: true, - }, - { - name: "Flags {0: 4503601774854144}, flag 52", - flags: map[uint64]uint64{0: 4503601774854144}, - flag: 52, - expected: true, - }, - { - name: "Flags {0: 4503599627370496}, flag 52", - flags: map[uint64]uint64{0: 4503599627370496}, - flag: 52, - expected: true, - }, - { - name: "Flags {0: 9227875600000000000}, flag 63", - flags: map[uint64]uint64{0: 9227875600000000000}, - flag: 63, - expected: true, - }, - { - name: "Flags {0: 9227875600000000000, 36644: 262144}, flag 2345234", - flags: map[uint64]uint64{0: 9227875600000000000, 36644: 262144}, - flag: 2345234, - expected: true, - }, - } -} - -func providerSetFlagMapUint64() []providerTypeSetFlagMapUint64 { - return []providerTypeSetFlagMapUint64{ - { - name: "Flags {}, flag 0, set", - flags: map[uint64]uint64{}, - flag: 0, - set: true, - expected: map[uint64]uint64{0: 1}, - }, - { - name: "Flags {}, flag 7, set", - flags: map[uint64]uint64{}, - flag: 7, - set: true, - expected: map[uint64]uint64{0: 128}, - }, - { - name: "Flags {}, flag 15, set", - flags: map[uint64]uint64{}, - flag: 15, - set: true, - expected: map[uint64]uint64{0: 32768}, - }, - { - name: "Flags {0: 92}, flag 7, set", - flags: map[uint64]uint64{0: 92}, - flag: 7, - set: true, - expected: map[uint64]uint64{0: 220}, - }, - { - name: "Flags {0: 220}, flag 7, unset", - flags: map[uint64]uint64{0: 220}, - flag: 7, - set: false, - expected: map[uint64]uint64{0: 92}, - }, - { - name: "Flags {0: 220}, flag 7, set", - flags: map[uint64]uint64{0: 220}, - flag: 7, - set: true, - expected: map[uint64]uint64{0: 220}, - }, - { - name: "Flags {0: 220}, flag 1, unset", - flags: map[uint64]uint64{0: 220}, - flag: 1, - set: false, - expected: map[uint64]uint64{0: 220}, - }, - { - name: "Flags {0: 220}, flag 14, set", - flags: map[uint64]uint64{0: 220}, - flag: 14, - set: true, - expected: map[uint64]uint64{0: 16604}, - }, - { - name: "Flags {0: 16604}, flag 31, set", - flags: map[uint64]uint64{0: 16604}, - flag: 31, - set: true, - expected: map[uint64]uint64{0: 2147500252}, - }, - { - name: "Flags {0: 2147500252}, flag 14, unset", - flags: map[uint64]uint64{0: 2147500252}, - flag: 14, - set: false, - expected: map[uint64]uint64{0: 2147483868}, - }, - { - name: "Flags {0: 2147500252}, flag 60, set", - flags: map[uint64]uint64{0: 2147500252}, - flag: 60, - set: true, - expected: map[uint64]uint64{0: 1152921506754347228}, - }, - { - name: "Flags {0: 2147500252}, flag 63, set", - flags: map[uint64]uint64{0: 2147500252}, - flag: 63, - set: true, - expected: map[uint64]uint64{0: 9223372039002276060}, - }, - { - name: "Flags {0: 9223372039002276060}, flag 63, unset", - flags: map[uint64]uint64{0: 9223372039002276060}, - flag: 63, - set: false, - expected: map[uint64]uint64{0: 2147500252}, - }, - { - name: "Flags {0: 9223372039002276060}, flag 1245, set", - flags: map[uint64]uint64{0: 9223372039002276060}, - flag: 1245, - set: true, - expected: map[uint64]uint64{0: 9223372039002276060, 19: 536870912}, - }, - { - name: "Flags {0: 9223372039002276060}, flag 1242, set", - flags: map[uint64]uint64{0: 9223372039002276060, 19: 536870912}, - flag: 1242, - set: true, - expected: map[uint64]uint64{0: 9223372039002276060, 19: 603979776}, - }, - { - name: "Flags {0: 9223372039002276060, 19: 603979776}, flag 1242, unset", - flags: map[uint64]uint64{0: 9223372039002276060, 19: 603979776}, - flag: 1242, - set: false, - expected: map[uint64]uint64{0: 9223372039002276060, 19: 536870912}, - }, - { - name: "Flags {0: 9223372039002276060, 19: 603979776}, flag 1242, set", - flags: map[uint64]uint64{0: 9223372039002276060, 19: 603979776}, - flag: 1242, - set: true, - expected: map[uint64]uint64{0: 9223372039002276060, 19: 603979776}, - }, - { - name: "Flags {0: 9223372039002276060, 19: 536870912}, flag 1242, unset", - flags: map[uint64]uint64{0: 9223372039002276060, 19: 536870912}, - flag: 1242, - set: false, - expected: map[uint64]uint64{0: 9223372039002276060, 19: 536870912}, - }, - { - name: "Flags {0: 436324, 1: 1, 2: 25623541}, flag 64, unset", - flags: map[uint64]uint64{0: 436324, 1: 1, 2: 25623541}, - flag: 64, - set: false, - expected: map[uint64]uint64{0: 436324, 2: 25623541}, - }, - { - name: "Flags nil, flag 0, unset", - flags: nil, - flag: 0, - set: false, - expected: nil, - err: binflags.ErrorFlagsMapNil(binflags.ErrorMsgFlagsMapNil), - }, - { - name: "Flags nil, flag 0, set", - flags: nil, - flag: 0, - set: true, - expected: nil, - err: binflags.ErrorFlagsMapNil(binflags.ErrorMsgFlagsMapNil), - }, - } -} diff --git a/map_uint8.go b/map_uint8.go deleted file mode 100644 index cf8f293..0000000 --- a/map_uint8.go +++ /dev/null @@ -1,45 +0,0 @@ -package binflags - -func HasFlagMapByte(flags map[uint64]uint8, flag uint64) bool { - return HasFlagMapUint8(flags, flag) -} - -func SetFlagMapByte(flags map[uint64]uint8, flag uint64, val bool) error { - return SetFlagMapUint8(flags, flag, val) -} - -func HasFlagMapUint8(flags map[uint64]uint8, flag uint64) bool { - if len(flags) == 0 { - return false - } - - idx, bit := flagExt(flag, FlagMaxInt8) - - return HasFlagUint8(flags[idx], bit) -} - -func SetFlagMapUint8(flags map[uint64]uint8, flag uint64, set bool) error { - if flags == nil { - return ErrorFlagsMapNil(ErrorMsgFlagsMapNil) - } - - if HasFlagMapUint8(flags, flag) == set { - return nil - } - - idx, bit := flagExt(flag, FlagMaxInt8) - bits, ok := flags[idx] - - ret, err := SetFlagUint8(bits, bit, set) - if err != nil { - return err - } - - if ret != 0 { - flags[idx] = ret - } else if ok { - delete(flags, idx) - } - - return nil -} diff --git a/map_uint8_test.go b/map_uint8_test.go deleted file mode 100644 index 226035b..0000000 --- a/map_uint8_test.go +++ /dev/null @@ -1,273 +0,0 @@ -package binflags_test - -import ( - "reflect" - "testing" - - "github.com/aohorodnyk/binflags" -) - -func TestHasFlagMapUint8(t *testing.T) { - t.Parallel() - - for _, prov := range providerHasFlagMapUint8() { - prov := prov - t.Run(prov.name, func(t *testing.T) { - t.Parallel() - - actual := binflags.HasFlagMapUint8(prov.flags, prov.flag) - - if prov.expected != actual { - t.Fatalf("Unexpected result.\nExpected: %t\nActual: %t", prov.expected, actual) - } - }) - } -} - -func TestHasFlagMapByte(t *testing.T) { - t.Parallel() - - for _, prov := range providerHasFlagMapUint8() { - prov := prov - t.Run(prov.name, func(t *testing.T) { - t.Parallel() - - actual := binflags.HasFlagMapByte(prov.flags, prov.flag) - - if prov.expected != actual { - t.Fatalf("Unexpected result.\nExpected: %t\nActual: %t", prov.expected, actual) - } - }) - } -} - -func TestSetFlagMapUint8(t *testing.T) { - t.Parallel() - - for _, prov := range providerSetFlagMapUint8() { - prov := prov - t.Run(prov.name, func(t *testing.T) { - t.Parallel() - - err := binflags.SetFlagMapUint8(prov.flags, prov.flag, prov.set) - - if !reflect.DeepEqual(prov.err, err) { - t.Fatalf("Unexpected error.\nExpected: %T(%v)\nActual: %T(%v)", prov.err, prov.err, err, err) - } - - if !reflect.DeepEqual(prov.expected, prov.flags) { - t.Fatalf("Unexpected flags.\nExpected: %+v\nActual: %+v", prov.expected, prov.flags) - } - }) - } -} - -func TestSetFlagMapByte(t *testing.T) { - t.Parallel() - - for _, prov := range providerSetFlagMapUint8() { - prov := prov - t.Run(prov.name, func(t *testing.T) { - t.Parallel() - - err := binflags.SetFlagMapByte(prov.flags, prov.flag, prov.set) - - if !reflect.DeepEqual(prov.err, err) { - t.Fatalf("Unexpected error.\nExpected: %T(%v)\nActual: %T(%v)", prov.err, prov.err, err, err) - } - - if !reflect.DeepEqual(prov.expected, prov.flags) { - t.Fatalf("Unexpected flags.\nExpected: %+v\nActual: %+v", prov.expected, prov.flags) - } - }) - } -} - -type providerTypeHasFlagMapUint8 struct { - name string - flags map[uint64]uint8 - flag uint64 - expected bool -} - -type providerTypeSetFlagMapUint8 struct { - name string - flags map[uint64]uint8 - flag uint64 - set bool - expected map[uint64]uint8 - err error -} - -func providerHasFlagMapUint8() []providerTypeHasFlagMapUint8 { - return []providerTypeHasFlagMapUint8{ - { - name: "Flags {}, flag 0", - flags: map[uint64]uint8{}, - flag: 0, - expected: false, - }, - { - name: "Flags {}, flag 12425", - flags: map[uint64]uint8{}, - flag: 12425, - expected: false, - }, - { - name: "Flags nil, flag 0", - flags: nil, - flag: 0, - expected: false, - }, - { - name: "Flags nil, flag 12425", - flags: nil, - flag: 12425, - expected: false, - }, - { - name: "Flags {}, flag 2356241245436", - flags: map[uint64]uint8{}, - flag: 2356241245436, - expected: false, - }, - { - name: "Flags {0: 1}, flag 0", - flags: map[uint64]uint8{0: 1}, - flag: 0, - expected: true, - }, - { - name: "Flags {234125: 128}, flag 1873007", - flags: map[uint64]uint8{234125: 128}, - flag: 1873007, - expected: true, - }, - { - name: "Flags {234125: 128}, flag 1873006", - flags: map[uint64]uint8{234125: 128}, - flag: 1873006, - expected: false, - }, - { - name: "Flags {234125: 128}, flag 1873008", - flags: map[uint64]uint8{234125: 128}, - flag: 1873008, - expected: false, - }, - { - name: "Flags {0: 128, 234125: 128}, flag 7", - flags: map[uint64]uint8{0: 128, 234125: 128}, - flag: 7, - expected: true, - }, - { - name: "Flags {0: 255, 5: 255, 63: 255, 234125: 128}, flag 7", - flags: map[uint64]uint8{0: 255, 5: 255, 63: 255, 234125: 128}, - flag: 7, - expected: true, - }, - { - name: "Flags {0: 255, 5: 64, 63: 128, 234125: 128}, flag 7", - flags: map[uint64]uint8{0: 255, 5: 64, 63: 128, 234125: 128}, - flag: 7, - expected: true, - }, - { - name: "Flags {0: 255, 5: 64, 63: 128, 234125: 128}, flag 46", - flags: map[uint64]uint8{0: 255, 5: 64, 63: 128, 234125: 128}, - flag: 46, - expected: true, - }, - { - name: "Flags {0: 255, 5: 64, 63: 128, 234125: 128}, flag 511", - flags: map[uint64]uint8{0: 255, 5: 64, 63: 128, 234125: 128}, - flag: 511, - expected: true, - }, - } -} - -func providerSetFlagMapUint8() []providerTypeSetFlagMapUint8 { - return []providerTypeSetFlagMapUint8{ - { - name: "Flags {}, flag 0, set", - flags: map[uint64]uint8{}, - flag: 0, - set: true, - expected: map[uint64]uint8{0: 1}, - }, - { - name: "Flags {}, flag 7, set", - flags: map[uint64]uint8{}, - flag: 7, - set: true, - expected: map[uint64]uint8{0: 128}, - }, - { - name: "Flags {}, flag 2346, set", - flags: map[uint64]uint8{}, - flag: 2346, - set: true, - expected: map[uint64]uint8{293: 4}, - }, - { - name: "Flags {0: 92}, flag 7, set", - flags: map[uint64]uint8{0: 92}, - flag: 7, - set: true, - expected: map[uint64]uint8{0: 220}, - }, - { - name: "Flags {0: 220}, flag 7, unset", - flags: map[uint64]uint8{0: 220}, - flag: 7, - set: false, - expected: map[uint64]uint8{0: 92}, - }, - { - name: "Flags {0: 220, 1: 56, 6: 32, 6235: 123}, flag 48, set", - flags: map[uint64]uint8{0: 220, 1: 56, 6: 32, 6235: 123}, - flag: 48, - set: true, - expected: map[uint64]uint8{0: 220, 1: 56, 6: 33, 6235: 123}, - }, - { - name: "Flags {0: 220, 1: 56, 6: 32, 6235: 123}, flag 53, unset", - flags: map[uint64]uint8{0: 220, 1: 56, 6: 32, 6235: 123}, - flag: 53, - set: false, - expected: map[uint64]uint8{0: 220, 1: 56, 6235: 123}, - }, - { - name: "Flags {0: 220, 1: 56, 6235: 123}, flag 53, set", - flags: map[uint64]uint8{0: 220, 1: 56, 6235: 123}, - flag: 53, - set: true, - expected: map[uint64]uint8{0: 220, 1: 56, 6: 32, 6235: 123}, - }, - { - name: "Flags {0: 220, 1: 56, 6235: 123}, flag 53, unset", - flags: map[uint64]uint8{0: 220, 1: 56, 6235: 123}, - flag: 53, - set: false, - expected: map[uint64]uint8{0: 220, 1: 56, 6235: 123}, - }, - { - name: "Flags nil, flag 0, unset", - flags: nil, - flag: 0, - set: false, - expected: nil, - err: binflags.ErrorFlagsMapNil(binflags.ErrorMsgFlagsMapNil), - }, - { - name: "Flags nil, flag 0, set", - flags: nil, - flag: 0, - set: true, - expected: nil, - err: binflags.ErrorFlagsMapNil(binflags.ErrorMsgFlagsMapNil), - }, - } -} diff --git a/sync.go b/sync.go new file mode 100644 index 0000000..5464c12 --- /dev/null +++ b/sync.go @@ -0,0 +1,58 @@ +package binflags + +import ( + "sync" +) + +// NewSync returns new instance of binflags.Sync. +// It is safe to use it concurrently. +// As a parameter it takes a pointer to binflags.Flags implementation. +// It implements binflags.Flags interface. +func NewSync(flags Flags) *Sync { + return &Sync{ + flags: flags, + mu: sync.RWMutex{}, + } +} + +// Sync is a wrapper around binflags.Flags implementation. +// It is safe to use it concurrently. +type Sync struct { + mu sync.RWMutex + flags Flags +} + +// IsSet returns true if bitNumber is set. +// It is safe to use it concurrently. +// If the flag is not set, then it returns false. +// This function has O(1) complexity. +func (s *Sync) IsSet(bitNumber uint) bool { + s.mu.RLock() + defer s.mu.RUnlock() + + return s.flags.IsSet(bitNumber) +} + +// Set sets bitNumber. +// It is safe to use it concurrently. +// This function is idempotent. +// This function has O(1) complexity. +// It always returns true. +func (s *Sync) Set(bitNumber uint) bool { + s.mu.Lock() + defer s.mu.Unlock() + + return s.flags.Set(bitNumber) +} + +// Unset unsets bitNumber. +// It is safe to use it concurrently. +// This function is idempotent. +// This function has O(1) complexity. +// It always returns true. +func (s *Sync) Unset(bitNumber uint) bool { + s.mu.Lock() + defer s.mu.Unlock() + + return s.flags.Unset(bitNumber) +} diff --git a/sync_test.go b/sync_test.go new file mode 100644 index 0000000..c5628ad --- /dev/null +++ b/sync_test.go @@ -0,0 +1,141 @@ +package binflags_test + +import ( + "fmt" + + "github.com/aohorodnyk/binflags" +) + +var ( + _ binflags.Flags = binflags.NewSync(&binflags.Dynamic[int]{}) + _ binflags.Flags = binflags.NewSync(&binflags.Dynamic[uint]{}) + _ binflags.Flags = binflags.NewSync(&binflags.Dynamic[uint16]{}) + _ binflags.Flags = binflags.NewSync(&binflags.Dynamic[int16]{}) + _ binflags.Flags = binflags.NewSync(&binflags.Dynamic[byte]{}) +) + +func ExampleSync() { + flags := binflags.NewSync(&binflags.Dynamic[uint8]{}) + fmt.Println(flags.Set(436235346)) + fmt.Println(flags.Set(0)) + fmt.Println(flags.Set(52)) + fmt.Println(flags.Set(3462363)) + fmt.Println(flags.Set(9874563524235)) + fmt.Println(flags.IsSet(0)) + fmt.Println(flags.IsSet(52)) + fmt.Println(flags.IsSet(3462363)) + fmt.Println(flags.IsSet(9874563524235)) + fmt.Println(flags.IsSet(436235346)) + fmt.Println(flags.IsSet(1)) + fmt.Println(flags.IsSet(436235345)) + fmt.Println(flags.IsSet(436235347)) + fmt.Println(flags.Unset(0)) + fmt.Println(flags.Unset(9874563524235)) + fmt.Println(flags.Unset(2523)) + + // Output: + // true + // true + // true + // true + // true + // true + // true + // true + // true + // true + // false + // false + // false + // true + // true + // true +} + +func ExampleSync_IsSet() { + flags := binflags.NewSync(&binflags.Dynamic[uint8]{432795: 8, 54529418: 4, 1234320440529: 8}) + fmt.Println(flags.IsSet(0)) + fmt.Println(flags.IsSet(342)) + fmt.Println(flags.IsSet(3462363)) + fmt.Println(flags.IsSet(9874563524235)) + fmt.Println(flags.IsSet(9874563524234)) + fmt.Println(flags.IsSet(9874563524236)) + + // Output: + // false + // false + // true + // true + // false + // false +} + +func ExampleSync_Unset() { + flags := binflags.NewSync(binflags.Dynamic[uint8]{432795: 8, 54529418: 4, 1234320440529: 8}) + fmt.Println(flags.Unset(0)) + fmt.Println(flags.Unset(342)) + fmt.Println(flags.Unset(3462363)) + fmt.Println(flags.Unset(9874563524235)) + fmt.Println(flags.Unset(9874563524234)) + fmt.Println(flags.Unset(9874563524236)) + + fmt.Println(flags.IsSet(0)) + fmt.Println(flags.IsSet(342)) + fmt.Println(flags.IsSet(3462363)) + fmt.Println(flags.IsSet(9874563524235)) + fmt.Println(flags.IsSet(9874563524234)) + fmt.Println(flags.IsSet(9874563524236)) + fmt.Println(flags.IsSet(436235346)) + + // Output: + // true + // true + // true + // true + // true + // true + // false + // false + // false + // false + // false + // false + // true +} + +func ExampleSync_Set() { + flags := binflags.NewSync(binflags.Dynamic[uint8]{}) + fmt.Println(flags.Set(234)) + fmt.Println(flags.Set(65234)) + fmt.Println(flags.Set(123)) + fmt.Println(flags.Set(2)) + fmt.Println(flags.Set(1)) + fmt.Println(flags.Set(16)) + + fmt.Println(flags.IsSet(233)) + fmt.Println(flags.IsSet(234)) + fmt.Println(flags.IsSet(235)) + fmt.Println(flags.IsSet(122)) + fmt.Println(flags.IsSet(123)) + fmt.Println(flags.IsSet(124)) + fmt.Println(flags.IsSet(15)) + fmt.Println(flags.IsSet(16)) + fmt.Println(flags.IsSet(17)) + + // Output: + // true + // true + // true + // true + // true + // true + // false + // true + // false + // false + // true + // false + // false + // true + // false +} diff --git a/uint16.go b/uint16.go deleted file mode 100644 index b8251bb..0000000 --- a/uint16.go +++ /dev/null @@ -1,26 +0,0 @@ -package binflags - -func HasFlagUint16(flags uint16, flag uint8) bool { - if flag > FlagMaxInt16 { - return false - } - - conv := uint16(1 << flag) - - return flags&conv == conv -} - -func SetFlagUint16(flags uint16, flag uint8, set bool) (uint16, error) { - if flag > FlagMaxInt16 { - return flags, ErrorOutOfRange(ErrorMsgOutOfRange) - } - - if HasFlagUint16(flags, flag) == set { - return flags, nil - } - - conv := uint16(1 << flag) - ret := flags ^ conv - - return ret, nil -} diff --git a/uint16_test.go b/uint16_test.go deleted file mode 100644 index 27d094f..0000000 --- a/uint16_test.go +++ /dev/null @@ -1,317 +0,0 @@ -package binflags_test - -import ( - "math" - "reflect" - "testing" - - "github.com/aohorodnyk/binflags" -) - -func TestHasFlagUint16(t *testing.T) { - t.Parallel() - - for _, prov := range providerHasFlagUint16() { - prov := prov - t.Run(prov.name, func(t *testing.T) { - t.Parallel() - - actual := binflags.HasFlagUint16(prov.flags, prov.flag) - - if prov.expected != actual { - t.Fatalf("Unexpected result.\nExpected: %t\nActual: %t", prov.expected, actual) - } - }) - } -} - -func TestSetFlagUint16(t *testing.T) { - t.Parallel() - - for _, prov := range providerSetFlagUint16() { - prov := prov - t.Run(prov.name, func(t *testing.T) { - t.Parallel() - - actual, err := binflags.SetFlagUint16(prov.flags, prov.flag, prov.set) - - if !reflect.DeepEqual(prov.err, err) { - t.Fatalf("Unexpected error.\nExpected: %T(%v)\nActual: %T(%v)", prov.err, prov.err, err, err) - } - - if prov.expected != actual { - t.Fatalf("Unexpected result.\nExpected: %d\nActual: %d", prov.expected, actual) - } - }) - } -} - -type providerTypeHasFlagUint16 struct { - name string - flags uint16 - flag uint8 - expected bool -} - -type providerTypeSetFlagUint16 struct { - name string - flags uint16 - flag uint8 - set bool - expected uint16 - err error -} - -func providerHasFlagUint16() []providerTypeHasFlagUint16 { - return []providerTypeHasFlagUint16{ - { - name: "Flags 0, flag 0", - flags: 0, - flag: 0, - expected: false, - }, - { - name: "Flags 0, flag 1", - flags: 0, - flag: 1, - expected: false, - }, - { - name: "Flags 0, flag 3", - flags: 0, - flag: 3, - expected: false, - }, - { - name: "Flags 0, flag 5", - flags: 0, - flag: 5, - expected: false, - }, - { - name: "Flags 0, flag 7", - flags: 0, - flag: 7, - expected: false, - }, - { - name: "Flags 2, flag 0", - flags: 2, - flag: 0, - expected: false, - }, - { - name: "Flags 1, flag 0", - flags: 1, - flag: 0, - expected: true, - }, - { - name: "Flags 3, flag 0", - flags: 3, - flag: 0, - expected: true, - }, - { - name: "Flags 26, flag 1", - flags: 26, - flag: 1, - expected: true, - }, - { - name: "Flags 26, flag 3", - flags: 26, - flag: 3, - expected: true, - }, - { - name: "Flags 26, flag 4", - flags: 26, - flag: 4, - expected: true, - }, - { - name: "Flags 128, flag 7", - flags: 128, - flag: 7, - expected: true, - }, - { - name: "Flags 64, flag 6", - flags: 64, - flag: 6, - expected: true, - }, - { - name: "Flags 30000, flag 4", - flags: 30000, - flag: 4, - expected: true, - }, - { - name: "Flags 30000, flag 14", - flags: 30000, - flag: 14, - expected: true, - }, - { - name: "Flags 30000, flag 3", - flags: 30000, - flag: 3, - expected: false, - }, - { - name: "Flags 65535, flag 0", - flags: math.MaxUint16, - flag: 0, - expected: true, - }, - { - name: "Flags 65535, flag 1", - flags: math.MaxUint16, - flag: 1, - expected: true, - }, - { - name: "Flags 65535, flag 3", - flags: math.MaxUint16, - flag: 3, - expected: true, - }, - { - name: "Flags 65535, flag 5", - flags: math.MaxUint16, - flag: 5, - expected: true, - }, - { - name: "Flags 65535, flag 7", - flags: math.MaxUint16, - flag: 7, - expected: true, - }, - { - name: "Flags 65535, flag 16", - flags: math.MaxUint16, - flag: 16, - expected: false, - }, - } -} - -func providerSetFlagUint16() []providerTypeSetFlagUint16 { - return []providerTypeSetFlagUint16{ - { - name: "Flags 0, flag 0, set", - flags: 0, - flag: 0, - set: true, - expected: 1, - err: nil, - }, - { - name: "Flags 0, flag 1, set", - flags: 0, - flag: 1, - set: true, - expected: 2, - err: nil, - }, - { - name: "Flags 0, flag 2, set", - flags: 0, - flag: 2, - set: true, - expected: 4, - err: nil, - }, - { - name: "Flags 1, flag 2, set", - flags: 1, - flag: 2, - set: true, - expected: 5, - err: nil, - }, - { - name: "Flags 53, flag 1, set", - flags: 53, - flag: 1, - set: true, - expected: 55, - err: nil, - }, - { - name: "Flags 53, flag 0, set", - flags: 53, - flag: 0, - set: true, - expected: 53, - err: nil, - }, - { - name: "Flags 65535, flag 2, set", - flags: math.MaxUint16, - flag: 2, - set: true, - expected: math.MaxUint16, - err: nil, - }, - { - name: "Flags 65535, flag 2, unset", - flags: math.MaxUint16, - flag: 2, - set: false, - expected: math.MaxUint16 - 4, - err: nil, - }, - { - name: "Flags 5, flag 7, set", - flags: 5, - flag: 7, - set: true, - expected: 133, - err: nil, - }, - { - name: "Flags 128, flag 7, unset", - flags: 128, - flag: 7, - set: false, - expected: 0, - err: nil, - }, - { - name: "Flags 235, flag 5, unset", - flags: 235, - flag: 5, - set: false, - expected: 203, - err: nil, - }, - { - name: "Flags 62768, flag 12, unset", - flags: 62768, - flag: 12, - set: false, - expected: 58672, - err: nil, - }, - { - name: "Flags 32768, flag 15, unset", - flags: 32768, - flag: 15, - set: false, - expected: 0, - err: nil, - }, - { - name: "Flags 5, flag 16, set", - flags: 5, - flag: 16, - set: true, - expected: 5, - err: binflags.ErrorOutOfRange(binflags.ErrorMsgOutOfRange), - }, - } -} diff --git a/uint32.go b/uint32.go deleted file mode 100644 index 88396bd..0000000 --- a/uint32.go +++ /dev/null @@ -1,26 +0,0 @@ -package binflags - -func HasFlagUint32(flags uint32, flag uint8) bool { - if flag > FlagMaxInt32 { - return false - } - - conv := uint32(1 << flag) - - return flags&conv == conv -} - -func SetFlagUint32(flags uint32, flag uint8, set bool) (uint32, error) { - if flag > FlagMaxInt32 { - return flags, ErrorOutOfRange(ErrorMsgOutOfRange) - } - - if HasFlagUint32(flags, flag) == set { - return flags, nil - } - - conv := uint32(1 << flag) - ret := flags ^ conv - - return ret, nil -} diff --git a/uint32_test.go b/uint32_test.go deleted file mode 100644 index 0e8ceab..0000000 --- a/uint32_test.go +++ /dev/null @@ -1,322 +0,0 @@ -package binflags_test - -import ( - "math" - "reflect" - "testing" - - "github.com/aohorodnyk/binflags" -) - -func TestHasFlagUint32(t *testing.T) { - t.Parallel() - - for _, prov := range providerHasFlagUint32() { - prov := prov - t.Run(prov.name, func(t *testing.T) { - t.Parallel() - - actual := binflags.HasFlagUint32(prov.flags, prov.flag) - - if prov.expected != actual { - t.Fatalf("Unexpected result.\nExpected: %t\nActual: %t", prov.expected, actual) - } - }) - } -} - -func TestSetFlagUint32(t *testing.T) { - t.Parallel() - - for _, prov := range providerSetFlagUint32() { - prov := prov - t.Run(prov.name, func(t *testing.T) { - t.Parallel() - - actual, err := binflags.SetFlagUint32(prov.flags, prov.flag, prov.set) - if !reflect.DeepEqual(prov.err, err) { - t.Fatalf("Unexpected error.\nExpected: %T(%v)\nActual: %T(%v)", prov.err, prov.err, err, err) - } - - if prov.expected != actual { - t.Fatalf("Unexpected result.\nExpected: %d\nActual: %d", prov.expected, actual) - } - }) - } -} - -type providerTypeHasFlagUint32 struct { - name string - flags uint32 - flag uint8 - expected bool -} - -type providerTypeSetFlagUint32 struct { - name string - flags uint32 - flag uint8 - set bool - expected uint32 - err error -} - -func providerHasFlagUint32() []providerTypeHasFlagUint32 { - return []providerTypeHasFlagUint32{ - { - name: "Flags 0, flag 0", - flags: 0, - flag: 0, - expected: false, - }, - { - name: "Flags 0, flag 1", - flags: 0, - flag: 1, - expected: false, - }, - { - name: "Flags 0, flag 3", - flags: 0, - flag: 3, - expected: false, - }, - { - name: "Flags 0, flag 5", - flags: 0, - flag: 5, - expected: false, - }, - { - name: "Flags 0, flag 7", - flags: 0, - flag: 7, - expected: false, - }, - { - name: "Flags 2, flag 0", - flags: 2, - flag: 0, - expected: false, - }, - { - name: "Flags 1, flag 0", - flags: 1, - flag: 0, - expected: true, - }, - { - name: "Flags 3, flag 0", - flags: 3, - flag: 0, - expected: true, - }, - { - name: "Flags 26, flag 1", - flags: 26, - flag: 1, - expected: true, - }, - { - name: "Flags 26, flag 3", - flags: 26, - flag: 3, - expected: true, - }, - { - name: "Flags 26, flag 4", - flags: 26, - flag: 4, - expected: true, - }, - { - name: "Flags 128, flag 7", - flags: 128, - flag: 7, - expected: true, - }, - { - name: "Flags 64, flag 6", - flags: 64, - flag: 6, - expected: true, - }, - { - name: "Flags 30000, flag 4", - flags: 30000, - flag: 4, - expected: true, - }, - { - name: "Flags 30000, flag 14", - flags: 30000, - flag: 14, - expected: true, - }, - { - name: "Flags 30000, flag 3", - flags: 30000, - flag: 3, - expected: false, - }, - { - name: "Flags 4294967295, flag 0", - flags: math.MaxUint32, - flag: 0, - expected: true, - }, - { - name: "Flags 4294967295, flag 1", - flags: math.MaxUint32, - flag: 1, - expected: true, - }, - { - name: "Flags 4294967295, flag 3", - flags: math.MaxUint32, - flag: 3, - expected: true, - }, - { - name: "Flags 4294967295, flag 5", - flags: math.MaxUint32, - flag: 5, - expected: true, - }, - { - name: "Flags 4294967295, flag 7", - flags: math.MaxUint32, - flag: 7, - expected: true, - }, - { - name: "Flags 4294967295, flag 31", - flags: math.MaxUint32, - flag: 31, - expected: true, - }, - { - name: "Flags 4294967295, flag 32", - flags: math.MaxUint32, - flag: 32, - expected: false, - }, - } -} - -func providerSetFlagUint32() []providerTypeSetFlagUint32 { - return []providerTypeSetFlagUint32{ - { - flags: 0, - flag: 0, - set: true, - expected: 1, - err: nil, - }, - { - flags: 0, - flag: 1, - set: true, - expected: 2, - err: nil, - }, - { - flags: 0, - flag: 2, - set: true, - expected: 4, - err: nil, - }, - { - flags: 1, - flag: 2, - set: true, - expected: 5, - err: nil, - }, - { - flags: 53, - flag: 1, - set: true, - expected: 55, - err: nil, - }, - { - flags: 53, - flag: 0, - set: true, - expected: 53, - err: nil, - }, - { - flags: math.MaxUint32, - flag: 2, - set: true, - expected: math.MaxUint32, - err: nil, - }, - { - flags: math.MaxUint32, - flag: 2, - set: false, - expected: math.MaxUint32 - 4, - err: nil, - }, - { - flags: 235, - flag: 5, - set: false, - expected: 203, - err: nil, - }, - { - flags: 62768, - flag: 12, - set: false, - expected: 58672, - err: nil, - }, - { - flags: 5, - flag: 7, - set: true, - expected: 133, - err: nil, - }, - { - flags: 128, - flag: 7, - set: false, - expected: 0, - err: nil, - }, - { - flags: 32768, - flag: 15, - set: false, - expected: 0, - err: nil, - }, - { - flags: 2147483648, - flag: 31, - set: false, - expected: 0, - err: nil, - }, - { - flags: 3467463648, - flag: 26, - set: false, - expected: 3400354784, - err: nil, - }, - { - flags: 5, - flag: 32, - set: true, - expected: 5, - err: binflags.ErrorOutOfRange(binflags.ErrorMsgOutOfRange), - }, - } -} diff --git a/uint64.go b/uint64.go deleted file mode 100644 index 90f0738..0000000 --- a/uint64.go +++ /dev/null @@ -1,26 +0,0 @@ -package binflags - -func HasFlagUint64(flags uint64, flag uint8) bool { - if flag > FlagMaxInt64 { - return false - } - - conv := uint64(1 << flag) - - return flags&conv == conv -} - -func SetFlagUint64(flags uint64, flag uint8, set bool) (uint64, error) { - if flag > FlagMaxInt64 { - return flags, ErrorOutOfRange(ErrorMsgOutOfRange) - } - - if HasFlagUint64(flags, flag) == set { - return flags, nil - } - - conv := uint64(1 << flag) - ret := flags ^ conv - - return ret, nil -} diff --git a/uint64_test.go b/uint64_test.go deleted file mode 100644 index adb4fe4..0000000 --- a/uint64_test.go +++ /dev/null @@ -1,387 +0,0 @@ -package binflags_test - -import ( - "math" - "reflect" - "testing" - - "github.com/aohorodnyk/binflags" -) - -func TestHasFlagUint64(t *testing.T) { - t.Parallel() - - for _, prov := range providerHasFlagUint64() { - prov := prov - t.Run(prov.name, func(t *testing.T) { - t.Parallel() - - actual := binflags.HasFlagUint64(prov.flags, prov.flag) - - if prov.expected != actual { - t.Fatalf("Unexpected result.\nExpected: %t\nActual: %t", prov.expected, actual) - } - }) - } -} - -func TestSetFlagUint64(t *testing.T) { - t.Parallel() - - for _, prov := range providerSetFlagUint64() { - prov := prov - t.Run(prov.name, func(t *testing.T) { - t.Parallel() - - actual, err := binflags.SetFlagUint64(prov.flags, prov.flag, prov.set) - - if !reflect.DeepEqual(prov.err, err) { - t.Fatalf("Unexpected error.\nExpected: %T(%v)\nActual: %T(%v)", prov.err, prov.err, err, err) - } - - if prov.expected != actual { - t.Fatalf("Unexpected result.\nExpected: %d\nActual: %d", prov.expected, actual) - } - }) - } -} - -type providerTypeHasFlagUint64 struct { - name string - flags uint64 - flag uint8 - expected bool -} - -type providerTypeSetFlagUint64 struct { - name string - flags uint64 - flag uint8 - set bool - expected uint64 - err error -} - -func providerHasFlagUint64() []providerTypeHasFlagUint64 { - return []providerTypeHasFlagUint64{ - { - name: "Flags 0, flag 0", - flags: 0, - flag: 0, - expected: false, - }, - { - name: "Flags 0, flag 1", - flags: 0, - flag: 1, - expected: false, - }, - { - name: "Flags 0, flag 3", - flags: 0, - flag: 3, - expected: false, - }, - { - name: "Flags 0, flag 5", - flags: 0, - flag: 5, - expected: false, - }, - { - name: "Flags 0, flag 7", - flags: 0, - flag: 7, - expected: false, - }, - { - name: "Flags 2, flag 0", - flags: 2, - flag: 0, - expected: false, - }, - { - name: "Flags 1, flag 0", - flags: 1, - flag: 0, - expected: true, - }, - { - name: "Flags 3, flag 0", - flags: 3, - flag: 0, - expected: true, - }, - { - name: "Flags 26, flag 1", - flags: 26, - flag: 1, - expected: true, - }, - { - name: "Flags 26, flag 3", - flags: 26, - flag: 3, - expected: true, - }, - { - name: "Flags 26, flag 4", - flags: 26, - flag: 4, - expected: true, - }, - { - name: "Flags 128, flag 7", - flags: 128, - flag: 7, - expected: true, - }, - { - name: "Flags 64, flag 6", - flags: 64, - flag: 6, - expected: true, - }, - { - name: "Flags 30000, flag 4", - flags: 30000, - flag: 4, - expected: true, - }, - { - name: "Flags 30000, flag 14", - flags: 30000, - flag: 14, - expected: true, - }, - { - name: "Flags 30000, flag 3", - flags: 30000, - flag: 3, - expected: false, - }, - { - name: "Flags 822372036854775808, flag 59", - flags: 822372036854775808, - flag: 59, - expected: true, - }, - { - name: "Flags 18446744073709551615, flag 0", - flags: math.MaxUint64, - flag: 0, - expected: true, - }, - { - name: "Flags 18446744073709551615, flag 1", - flags: math.MaxUint64, - flag: 1, - expected: true, - }, - { - name: "Flags 18446744073709551615, flag 3", - flags: math.MaxUint64, - flag: 3, - expected: true, - }, - { - name: "Flags 18446744073709551615, flag 5", - flags: math.MaxUint64, - flag: 5, - expected: true, - }, - { - name: "Flags 18446744073709551615, flag 7", - flags: math.MaxUint64, - flag: 7, - expected: true, - }, - { - name: "Flags 18446744073709551615, flag 15", - flags: math.MaxUint64, - flag: 15, - expected: true, - }, - { - name: "Flags 18446744073709551615, flag 31", - flags: math.MaxUint64, - flag: 31, - expected: true, - }, - { - name: "Flags 18446744073709551615, flag 53", - flags: math.MaxUint64, - flag: 53, - expected: true, - }, - { - name: "Flags 18446744073709551615, flag 63", - flags: math.MaxUint64, - flag: 63, - expected: true, - }, - { - name: "Flags 18446744073709551615, flag 64", - flags: math.MaxUint64, - flag: 64, - expected: false, - }, - } -} - -func providerSetFlagUint64() []providerTypeSetFlagUint64 { - return []providerTypeSetFlagUint64{ - { - name: "Flags 0, flag 0, set", - flags: 0, - flag: 0, - set: true, - expected: 1, - err: nil, - }, - { - name: "Flags 0, flag 1, set", - flags: 0, - flag: 1, - set: true, - expected: 2, - err: nil, - }, - { - name: "Flags 0, flag 2, set", - flags: 0, - flag: 2, - set: true, - expected: 4, - err: nil, - }, - { - name: "Flags 1, flag 2, set", - flags: 1, - flag: 2, - set: true, - expected: 5, - err: nil, - }, - { - name: "Flags 53, flag 1, set", - flags: 53, - flag: 1, - set: true, - expected: 55, - err: nil, - }, - { - name: "Flags 1, flag 0, set", - flags: 53, - flag: 0, - set: true, - expected: 53, - err: nil, - }, - { - name: "Flags 18446744073709551615, flag 2, set", - flags: math.MaxUint64, - flag: 2, - set: true, - expected: math.MaxUint64, - err: nil, - }, - { - name: "Flags 18446744073709551615, flag 2, unset", - flags: math.MaxUint64, - flag: 2, - set: false, - expected: math.MaxUint64 - 4, - err: nil, - }, - { - name: "Flags 5, flag 7, set", - flags: 5, - flag: 7, - set: true, - expected: 133, - err: nil, - }, - { - name: "Flags 128, flag 7, unset", - flags: 128, - flag: 7, - set: false, - expected: 0, - err: nil, - }, - { - name: "Flags 32768, flag 15, unset", - flags: 32768, - flag: 15, - set: false, - expected: 0, - err: nil, - }, - { - name: "Flags 235, flag 5, unset", - flags: 235, - flag: 5, - set: false, - expected: 203, - err: nil, - }, - { - name: "Flags 62768, flag 12, unset", - flags: 62768, - flag: 12, - set: false, - expected: 58672, - err: nil, - }, - { - name: "Flags 3467463648, flag 26, unset", - flags: 3467463648, - flag: 26, - set: false, - expected: 3400354784, - err: nil, - }, - { - name: "Flags 2147483648, flag 31, unset", - flags: 2147483648, - flag: 31, - set: false, - expected: 0, - err: nil, - }, - { - name: "Flags 2147483648, flag 31, unset", - flags: 2147483648, - flag: 31, - set: false, - expected: 0, - err: nil, - }, - { - name: "Flags 822372036854775808, flag 59, unset", - flags: 822372036854775808, - flag: 59, - set: false, - expected: 245911284551352320, - err: nil, - }, - { - name: "Flags 9223372036854775808, flag 63, unset", - flags: 9223372036854775808, - flag: 63, - set: false, - expected: 0, - err: nil, - }, - { - name: "Flags 235234, flag 64, unset", - flags: 235234, - flag: 64, - set: true, - expected: 235234, - err: binflags.ErrorOutOfRange(binflags.ErrorMsgOutOfRange), - }, - } -} diff --git a/uint8.go b/uint8.go deleted file mode 100644 index 29859d5..0000000 --- a/uint8.go +++ /dev/null @@ -1,34 +0,0 @@ -package binflags - -func HasFlagByte(flags byte, flag uint8) bool { - return HasFlagUint8(flags, flag) -} - -func SetFlagByte(flags, flag uint8, set bool) (uint8, error) { - return SetFlagUint8(flags, flag, set) -} - -func HasFlagUint8(flags, flag uint8) bool { - if flag > FlagMaxInt8 { - return false - } - - conv := uint8(1 << flag) - - return flags&conv == conv -} - -func SetFlagUint8(flags, flag uint8, set bool) (uint8, error) { - if flag > FlagMaxInt8 { - return flags, ErrorOutOfRange(ErrorMsgOutOfRange) - } - - if HasFlagUint8(flags, flag) == set { - return flags, nil - } - - conv := uint8(1 << flag) - ret := flags ^ conv - - return ret, nil -} diff --git a/uint8_test.go b/uint8_test.go deleted file mode 100644 index 7e6415c..0000000 --- a/uint8_test.go +++ /dev/null @@ -1,321 +0,0 @@ -package binflags_test - -import ( - "math" - "reflect" - "testing" - - "github.com/aohorodnyk/binflags" -) - -func TestHasFlagUint8(t *testing.T) { - t.Parallel() - - for _, prov := range providerHasFlagUint8() { - prov := prov - t.Run(prov.name, func(t *testing.T) { - t.Parallel() - - actual := binflags.HasFlagUint8(prov.flags, prov.flag) - - if prov.expected != actual { - t.Fatalf("Unexpected result.\nExpected: %t\nActual: %t", prov.expected, actual) - } - }) - } -} - -func TestSetFlagUint8(t *testing.T) { - t.Parallel() - - for _, prov := range providerSetFlagUint8() { - prov := prov - t.Run(prov.name, func(t *testing.T) { - t.Parallel() - - actual, err := binflags.SetFlagUint8(prov.flags, prov.flag, prov.set) - - if !reflect.DeepEqual(prov.err, err) { - t.Fatalf("Unexpected error.\nExpected: %T(%v)\nActual: %T(%v)", prov.err, prov.err, err, err) - } - - if prov.expected != actual { - t.Fatalf("Unexpected result.\nExpected: %d\nActual: %d", prov.expected, actual) - } - }) - } -} - -func TestHasFlagByte(t *testing.T) { - t.Parallel() - - for _, prov := range providerHasFlagUint8() { - prov := prov - t.Run(prov.name, func(t *testing.T) { - t.Parallel() - - actual := binflags.HasFlagByte(prov.flags, prov.flag) - - if prov.expected != actual { - t.Fatalf("Unexpected result.\nExpected: %t\nActual: %t", prov.expected, actual) - } - }) - } -} - -func TestSetFlagByte(t *testing.T) { - t.Parallel() - - for _, prov := range providerSetFlagUint8() { - prov := prov - t.Run(prov.name, func(t *testing.T) { - t.Parallel() - - actual, err := binflags.SetFlagByte(prov.flags, prov.flag, prov.set) - - if !reflect.DeepEqual(prov.err, err) { - t.Fatalf("Unexpected error.\nExpected: %T(%v)\nActual: %T(%v)", prov.err, prov.err, err, err) - } - - if prov.expected != actual { - t.Fatalf("Unexpected flags.\nExpected: %d\nActual: %d", prov.expected, prov.flags) - } - }) - } -} - -type providerTypeHasFlagUint8 struct { - name string - flags uint8 - flag uint8 - expected bool -} - -type providerTypeSetFlagUint8 struct { - name string - flags uint8 - flag uint8 - set bool - expected uint8 - err error -} - -func providerHasFlagUint8() []providerTypeHasFlagUint8 { - return []providerTypeHasFlagUint8{ - { - name: "Flags 0, flag 0", - flags: 0, - flag: 0, - expected: false, - }, - { - name: "Flags 0, flag 1", - flags: 0, - flag: 1, - expected: false, - }, - { - name: "Flags 0, flag 3", - flags: 0, - flag: 3, - expected: false, - }, - { - name: "Flags 0, flag 5", - flags: 0, - flag: 5, - expected: false, - }, - { - name: "Flags 0, flag 7", - flags: 0, - flag: 7, - expected: false, - }, - { - name: "Flags 2, flag 0", - flags: 2, - flag: 0, - expected: false, - }, - { - name: "Flags 1, flag 0", - flags: 1, - flag: 0, - expected: true, - }, - { - name: "Flags 3, flag 0", - flags: 3, - flag: 0, - expected: true, - }, - { - name: "Flags 26, flag 1", - flags: 26, - flag: 1, - expected: true, - }, - { - name: "Flags 26, flag 3", - flags: 26, - flag: 3, - expected: true, - }, - { - name: "Flags 26, flag 4", - flags: 26, - flag: 4, - expected: true, - }, - { - name: "Flags 128, flag 7", - flags: 128, - flag: 7, - expected: true, - }, - { - name: "Flags 82, flag 6", - flags: 82, - flag: 6, - expected: true, - }, - { - name: "Flags 255, flag 0", - flags: math.MaxUint8, - flag: 0, - expected: true, - }, - { - name: "Flags 255, flag 1", - flags: math.MaxUint8, - flag: 1, - expected: true, - }, - { - name: "Flags 255, flag 3", - flags: math.MaxUint8, - flag: 3, - expected: true, - }, - { - name: "Flags 255, flag 5", - flags: math.MaxUint8, - flag: 5, - expected: true, - }, - { - name: "Flags 255, flag 7", - flags: math.MaxUint8, - flag: 7, - expected: true, - }, - { - name: "Flags 255, flag 8", - flags: math.MaxUint8, - flag: 8, - expected: false, - }, - } -} - -func providerSetFlagUint8() []providerTypeSetFlagUint8 { - return []providerTypeSetFlagUint8{ - { - name: "Flags 0, flag 0, set", - flags: 0, - flag: 0, - set: true, - expected: 1, - err: nil, - }, - { - name: "Flags 0, flag 1, set", - flags: 0, - flag: 1, - set: true, - expected: 2, - err: nil, - }, - { - name: "Flags 0, flag 2, set", - flags: 0, - flag: 2, - set: true, - expected: 4, - err: nil, - }, - { - name: "Flags 1, flag 2, set", - flags: 1, - flag: 2, - set: true, - expected: 5, - err: nil, - }, - { - name: "Flags 53, flag 1, set", - flags: 53, - flag: 1, - set: true, - expected: 55, - err: nil, - }, - { - name: "Flags 53, flag 0, set", - flags: 53, - flag: 0, - set: true, - expected: 53, - err: nil, - }, - { - name: "Flags 255, flag 2, set", - flags: math.MaxUint8, - flag: 2, - set: true, - expected: math.MaxUint8, - err: nil, - }, - { - name: "Flags 255, flag 2, unset", - flags: math.MaxUint8, - flag: 2, - set: false, - expected: math.MaxUint8 - 4, - err: nil, - }, - { - name: "Flags 5, flag 7, set", - flags: 5, - flag: 7, - set: true, - expected: 133, - err: nil, - }, - { - name: "Flags 128, flag 7, unset", - flags: 128, - flag: 7, - set: false, - expected: 0, - err: nil, - }, - { - name: "Flags 235, flag 5, unset", - flags: 235, - flag: 5, - set: false, - expected: 203, - err: nil, - }, - { - name: "Flags 5, flag 8, set", - flags: 5, - flag: 8, - set: true, - expected: 5, - err: binflags.ErrorOutOfRange(binflags.ErrorMsgOutOfRange), - }, - } -}