github.com/iaas-resource-provision/iaas-rpc@v1.0.7-0.20211021023331-ed21f798c408/internal/plans/objchange/lcs_test.go (about) 1 package objchange 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/zclconf/go-cty/cty" 8 ) 9 10 func TestLongestCommonSubsequence(t *testing.T) { 11 tests := []struct { 12 xs []cty.Value 13 ys []cty.Value 14 want []cty.Value 15 }{ 16 { 17 []cty.Value{}, 18 []cty.Value{}, 19 []cty.Value{}, 20 }, 21 { 22 []cty.Value{cty.NumberIntVal(1), cty.NumberIntVal(2)}, 23 []cty.Value{cty.NumberIntVal(1), cty.NumberIntVal(2)}, 24 []cty.Value{cty.NumberIntVal(1), cty.NumberIntVal(2)}, 25 }, 26 { 27 []cty.Value{cty.NumberIntVal(1), cty.NumberIntVal(2)}, 28 []cty.Value{cty.NumberIntVal(3), cty.NumberIntVal(4)}, 29 []cty.Value{}, 30 }, 31 { 32 []cty.Value{cty.NumberIntVal(2)}, 33 []cty.Value{cty.NumberIntVal(1), cty.NumberIntVal(2)}, 34 []cty.Value{cty.NumberIntVal(2)}, 35 }, 36 { 37 []cty.Value{cty.NumberIntVal(1)}, 38 []cty.Value{cty.NumberIntVal(1), cty.NumberIntVal(2)}, 39 []cty.Value{cty.NumberIntVal(1)}, 40 }, 41 { 42 []cty.Value{cty.NumberIntVal(2), cty.NumberIntVal(1)}, 43 []cty.Value{cty.NumberIntVal(1), cty.NumberIntVal(2)}, 44 []cty.Value{cty.NumberIntVal(1)}, // arbitrarily selected 1; 2 would also be valid 45 }, 46 { 47 []cty.Value{cty.NumberIntVal(1), cty.NumberIntVal(2), cty.NumberIntVal(3), cty.NumberIntVal(4)}, 48 []cty.Value{cty.NumberIntVal(2), cty.NumberIntVal(4), cty.NumberIntVal(5)}, 49 []cty.Value{cty.NumberIntVal(2), cty.NumberIntVal(4)}, 50 }, 51 { 52 []cty.Value{cty.NumberIntVal(1), cty.NumberIntVal(2), cty.NumberIntVal(3), cty.NumberIntVal(4)}, 53 []cty.Value{cty.NumberIntVal(4), cty.NumberIntVal(2), cty.NumberIntVal(5)}, 54 []cty.Value{cty.NumberIntVal(4)}, // 2 would also be valid 55 }, 56 { 57 []cty.Value{cty.NumberIntVal(1), cty.NumberIntVal(2), cty.NumberIntVal(3), cty.NumberIntVal(5)}, 58 []cty.Value{cty.NumberIntVal(2), cty.NumberIntVal(4), cty.NumberIntVal(5)}, 59 []cty.Value{cty.NumberIntVal(2), cty.NumberIntVal(5)}, 60 }, 61 62 // unknowns never compare as equal 63 { 64 []cty.Value{cty.NumberIntVal(1), cty.UnknownVal(cty.Number), cty.NumberIntVal(3)}, 65 []cty.Value{cty.NumberIntVal(1), cty.UnknownVal(cty.Number), cty.NumberIntVal(3)}, 66 []cty.Value{cty.NumberIntVal(1), cty.NumberIntVal(3)}, 67 }, 68 { 69 []cty.Value{cty.UnknownVal(cty.Number)}, 70 []cty.Value{cty.UnknownVal(cty.Number)}, 71 []cty.Value{}, 72 }, 73 74 // marked values 75 { 76 []cty.Value{cty.NumberIntVal(1).Mark("foo"), cty.NumberIntVal(2).Mark("foo"), cty.NumberIntVal(3)}, 77 []cty.Value{cty.NumberIntVal(1).Mark("foo"), cty.NumberIntVal(2).Mark("foo")}, 78 []cty.Value{cty.NumberIntVal(1).Mark("foo"), cty.NumberIntVal(2).Mark("foo")}, 79 }, 80 { 81 []cty.Value{cty.NumberIntVal(1), cty.NumberIntVal(2).Mark("foo"), cty.NumberIntVal(3)}, 82 []cty.Value{cty.NumberIntVal(2), cty.NumberIntVal(3)}, 83 []cty.Value{cty.NumberIntVal(3)}, 84 }, 85 { 86 []cty.Value{cty.NumberIntVal(1), cty.NumberIntVal(2).Mark("foo")}, 87 []cty.Value{cty.NumberIntVal(2)}, 88 []cty.Value{}, 89 }, 90 { 91 []cty.Value{ 92 cty.MapVal(map[string]cty.Value{"a": cty.StringVal("x").Mark("sensitive")}), 93 cty.MapVal(map[string]cty.Value{"b": cty.StringVal("y")}), 94 }, 95 []cty.Value{ 96 cty.MapVal(map[string]cty.Value{"a": cty.StringVal("x").Mark("sensitive")}), 97 cty.MapVal(map[string]cty.Value{"b": cty.StringVal("y")}), 98 cty.MapVal(map[string]cty.Value{"c": cty.StringVal("z")}), 99 }, 100 []cty.Value{ 101 cty.MapVal(map[string]cty.Value{"a": cty.StringVal("x").Mark("sensitive")}), 102 cty.MapVal(map[string]cty.Value{"b": cty.StringVal("y")}), 103 }, 104 }, 105 } 106 107 for _, test := range tests { 108 t.Run(fmt.Sprintf("%#v,%#v", test.xs, test.ys), func(t *testing.T) { 109 got := LongestCommonSubsequence(test.xs, test.ys) 110 111 wrong := func() { 112 t.Fatalf( 113 "wrong result\nX: %#v\nY: %#v\ngot: %#v\nwant: %#v", 114 test.xs, test.ys, got, test.want, 115 ) 116 } 117 118 if len(got) != len(test.want) { 119 wrong() 120 } 121 122 for i := range got { 123 if got[i] == cty.NilVal { 124 wrong() 125 } 126 if !got[i].RawEquals(test.want[i]) { 127 wrong() 128 } 129 } 130 }) 131 } 132 }