go/algorithm/cartesian: implement cartesian product
Change-Id: I9553266ce64a104f5b8bab2e83d9d7234994cd4b
Reviewed-on: https://review.monogon.dev/c/monogon/+/1863
Reviewed-by: Tim Windelschmidt <tim@monogon.tech>
Tested-by: Jenkins CI
diff --git a/go/algorithm/cartesian/cartesian_test.go b/go/algorithm/cartesian/cartesian_test.go
new file mode 100644
index 0000000..5911ecc
--- /dev/null
+++ b/go/algorithm/cartesian/cartesian_test.go
@@ -0,0 +1,48 @@
+package cartesian
+
+import (
+ "fmt"
+ "testing"
+
+ "github.com/google/go-cmp/cmp"
+)
+
+func TestProduct(t *testing.T) {
+ for i, te := range []struct {
+ data [][]string
+ want [][]string
+ }{
+ {
+ data: [][]string{
+ {"a", "b"},
+ {"c", "d"},
+ },
+ want: [][]string{
+ {"a", "c"},
+ {"a", "d"},
+ {"b", "c"},
+ {"b", "d"},
+ },
+ },
+ {
+ data: [][]string{
+ {"a", "b"},
+ },
+ want: [][]string{
+ {"a"},
+ {"b"},
+ },
+ },
+ {
+ data: [][]string{},
+ want: nil,
+ },
+ } {
+ t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
+ got := Product(te.data...)
+ if diff := cmp.Diff(te.want, got); diff != "" {
+ t.Fatalf("Diff:\n%s", diff)
+ }
+ })
+ }
+}