github.com/apache/beam/sdks/v2@v2.48.2/go/cmd/specialize/main_test.go (about) 1 // Licensed to the Apache Software Foundation (ASF) under one or more 2 // contributor license agreements. See the NOTICE file distributed with 3 // this work for additional information regarding copyright ownership. 4 // The ASF licenses this file to You under the Apache License, Version 2.0 5 // (the "License"); you may not use this file except in compliance with 6 // the License. You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 16 package main 17 18 import ( 19 "testing" 20 21 "github.com/google/go-cmp/cmp" 22 ) 23 24 func TestGenericTypingRepresentation(t *testing.T) { 25 tests := []struct { 26 in int 27 out int 28 includeType bool 29 representation string 30 }{ 31 {0, 0, true, ""}, 32 {0, 0, false, ""}, 33 {0, 1, true, "[R0 any]"}, 34 {0, 1, false, "[R0]"}, 35 {0, 3, true, "[R0, R1, R2 any]"}, 36 {0, 3, false, "[R0, R1, R2]"}, 37 {1, 0, true, "[I0 any]"}, 38 {1, 0, false, "[I0]"}, 39 {3, 0, true, "[I0, I1, I2 any]"}, 40 {3, 0, false, "[I0, I1, I2]"}, 41 {1, 1, true, "[I0, R0 any]"}, 42 {1, 1, false, "[I0, R0]"}, 43 {4, 1, true, "[I0, I1, I2, I3, R0 any]"}, 44 {4, 1, false, "[I0, I1, I2, I3, R0]"}, 45 {4, 2, true, "[I0, I1, I2, I3, R0, R1 any]"}, 46 {4, 2, false, "[I0, I1, I2, I3, R0, R1]"}, 47 {1, 2, true, "[I0, R0, R1 any]"}, 48 {1, 2, false, "[I0, R0, R1]"}, 49 } 50 51 for _, test := range tests { 52 if got, want := genericTypingRepresentation(test.in, test.out, test.includeType), test.representation; got != want { 53 t.Errorf("genericTypingRepresentation(%v, %v, %v) = %v, want: %v", test.in, test.out, test.includeType, got, want) 54 } 55 } 56 } 57 58 func TestPossibleBundleLifecycleParameterCombos(t *testing.T) { 59 tests := []struct { 60 numIn int 61 processElementIn int 62 representation [][]string 63 }{ 64 {0, 0, [][]string{[]string{}}}, 65 {0, 1, [][]string{[]string{}}}, 66 {1, 0, [][]string{[]string{"context.Context"}, []string{"typex.PaneInfo"}, []string{"[]typex.Window"}, []string{"typex.EventTime"}, []string{"typex.BundleFinalization"}}}, 67 {1, 1, [][]string{[]string{"I0"}, []string{"context.Context"}, []string{"typex.PaneInfo"}, []string{"[]typex.Window"}, []string{"typex.EventTime"}, []string{"typex.BundleFinalization"}}}, 68 {2, 1, [][]string{[]string{"context.Context", "I0"}, []string{"context.Context", "typex.PaneInfo"}, []string{"context.Context", "[]typex.Window"}, []string{"context.Context", "typex.EventTime"}, []string{"context.Context", "typex.BundleFinalization"}, 69 []string{"typex.PaneInfo", "I0"}, []string{"typex.PaneInfo", "[]typex.Window"}, []string{"typex.PaneInfo", "typex.EventTime"}, []string{"typex.PaneInfo", "typex.BundleFinalization"}, 70 []string{"[]typex.Window", "I0"}, []string{"[]typex.Window", "typex.EventTime"}, []string{"[]typex.Window", "typex.BundleFinalization"}, 71 []string{"typex.EventTime", "I0"}, []string{"typex.EventTime", "typex.BundleFinalization"}, 72 []string{"typex.BundleFinalization", "I0"}}}, 73 {2, 2, [][]string{[]string{"context.Context", "I1"}, []string{"context.Context", "typex.PaneInfo"}, []string{"context.Context", "[]typex.Window"}, []string{"context.Context", "typex.EventTime"}, []string{"context.Context", "typex.BundleFinalization"}, 74 []string{"typex.PaneInfo", "I1"}, []string{"typex.PaneInfo", "[]typex.Window"}, []string{"typex.PaneInfo", "typex.EventTime"}, []string{"typex.PaneInfo", "typex.BundleFinalization"}, 75 []string{"[]typex.Window", "I1"}, []string{"[]typex.Window", "typex.EventTime"}, []string{"[]typex.Window", "typex.BundleFinalization"}, 76 []string{"typex.EventTime", "I1"}, []string{"typex.EventTime", "typex.BundleFinalization"}, 77 []string{"typex.BundleFinalization", "I1"}, 78 []string{"I0", "I1"}}}, 79 {2, 3, [][]string{[]string{"context.Context", "I2"}, []string{"context.Context", "typex.PaneInfo"}, []string{"context.Context", "[]typex.Window"}, []string{"context.Context", "typex.EventTime"}, []string{"context.Context", "typex.BundleFinalization"}, 80 []string{"typex.PaneInfo", "I2"}, []string{"typex.PaneInfo", "[]typex.Window"}, []string{"typex.PaneInfo", "typex.EventTime"}, []string{"typex.PaneInfo", "typex.BundleFinalization"}, 81 []string{"[]typex.Window", "I2"}, []string{"[]typex.Window", "typex.EventTime"}, []string{"[]typex.Window", "typex.BundleFinalization"}, 82 []string{"typex.EventTime", "I2"}, []string{"typex.EventTime", "typex.BundleFinalization"}, 83 []string{"typex.BundleFinalization", "I2"}, 84 []string{"I1", "I2"}}}, 85 } 86 87 for _, test := range tests { 88 got, want := possibleBundleLifecycleParameterCombos(test.numIn, test.processElementIn), test.representation 89 if len(got) != len(want) { 90 t.Errorf("possibleBundleLifecycleParameterCombos(%v, %v) returned list of length %v, want: %v. Full list: %v", test.numIn, test.processElementIn, len(got), len(want), got) 91 } else { 92 for _, l1 := range got { 93 found := false 94 for _, l2 := range want { 95 // cmp.Equal doesn't seem to handle the empty list case correctly 96 if (len(l1) == 0 && len(l2) == 0) || cmp.Equal(l1, l2) { 97 found = true 98 break 99 } 100 } 101 if !found { 102 t.Errorf("possibleBundleLifecycleParameterCombos(%v, %v) does not contain list %v", test.numIn, test.processElementIn, l1) 103 } 104 } 105 } 106 } 107 }