summaryrefslogtreecommitdiffstats
path: root/tests/internal/utils/slice.go
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 12:36:04 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 12:36:04 +0000
commitb09c6d56832eb1718c07d74abf3bc6ae3fe4e030 (patch)
treed2caec2610d4ea887803ec9e9c3cd77136c448ba /tests/internal/utils/slice.go
parentInitial commit. (diff)
downloadicingadb-b09c6d56832eb1718c07d74abf3bc6ae3fe4e030.tar.xz
icingadb-b09c6d56832eb1718c07d74abf3bc6ae3fe4e030.zip
Adding upstream version 1.1.0.upstream/1.1.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tests/internal/utils/slice.go')
-rw-r--r--tests/internal/utils/slice.go37
1 files changed, 37 insertions, 0 deletions
diff --git a/tests/internal/utils/slice.go b/tests/internal/utils/slice.go
new file mode 100644
index 0000000..85821f5
--- /dev/null
+++ b/tests/internal/utils/slice.go
@@ -0,0 +1,37 @@
+package utils
+
+import (
+ "fmt"
+ "reflect"
+)
+
+// AnySliceToInterfaceSlice takes a slice of type []T for any T and returns a slice of type []interface{} containing
+// the same elements, somewhat like casting []T to []interface{}.
+func AnySliceToInterfaceSlice(in interface{}) []interface{} {
+ v := reflect.ValueOf(in)
+ if v.Kind() != reflect.Slice {
+ panic(fmt.Errorf("AnySliceToInterfaceSlice() called on %T instead of a slice type", in))
+ }
+
+ out := make([]interface{}, v.Len())
+ for i := 0; i < v.Len(); i++ {
+ out[i] = v.Index(i).Interface()
+ }
+ return out
+}
+
+func SliceSubsets(in ...string) [][]string {
+ result := make([][]string, 0, 1<<len(in))
+
+ for bitset := 0; bitset < (1 << len(in)); bitset++ {
+ var subset []string
+ for i := 0; i < len(in); i++ {
+ if bitset&(1<<i) != 0 {
+ subset = append(subset, in[i])
+ }
+ }
+ result = append(result, subset)
+ }
+
+ return result
+}