github.com/llir/llvm@v0.3.6/ir/inst_aggregate_test.go (about) 1 package ir 2 3 import ( 4 "testing" 5 6 "github.com/llir/llvm/ir/constant" 7 "github.com/llir/llvm/ir/types" 8 "github.com/llir/llvm/ir/value" 9 ) 10 11 func TestTypeCheckInstExtractValue(t *testing.T) { 12 structType := types.NewStruct(types.I32, types.I64) 13 14 // Should succeed. 15 var v value.Value = constant.NewUndef(structType) 16 _ = v.String() 17 v = NewInsertValue(v, constant.NewInt(types.I32, 1), 0) 18 _ = v.String() 19 v = NewInsertValue(v, constant.NewInt(types.I64, 1), 1) 20 _ = v.String() 21 22 var panicErr error 23 func() { 24 defer func() { panicErr = recover().(error) }() 25 // Should panic because index 1 is I64, not I32. 26 v = NewInsertValue(v, constant.NewInt(types.I32, 1), 1) 27 t.Fatal("unreachable") 28 }() 29 expected := "insertvalue elem type mismatch, expected i64, got i32" 30 got := panicErr.Error() 31 if got != expected { 32 t.Errorf("expected %q, got %q", expected, got) 33 } 34 35 func() { 36 defer func() { panicErr = recover().(error) }() 37 // Should panic because index 0 is I32, not I64. 38 v = NewInsertValue(v, constant.NewInt(types.I64, 1), 0) 39 t.Fatal("unreachable") 40 }() 41 expected = "insertvalue elem type mismatch, expected i32, got i64" 42 got = panicErr.Error() 43 if got != expected { 44 t.Errorf("expected %q, got %q", expected, got) 45 } 46 }