github.com/llir/llvm@v0.3.6/ir/inst_conversion_test.go (about) 1 package ir 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/llir/llvm/ir/constant" 8 "github.com/llir/llvm/ir/types" 9 "github.com/pkg/errors" 10 ) 11 12 func TestTypeCheckTrunc(t *testing.T) { 13 cases := []struct { 14 fromTyp, toTyp types.Type 15 panicMessage string // "OK" if not panic'ing. 16 }{ 17 {types.I64, types.I1, 18 "OK"}, 19 {types.NewVector(2, types.I32), types.NewVector(2, types.I1), 20 "OK"}, 21 22 {types.I32, types.I64, 23 "invalid trunc operands: from.BitSize < to.BitSize (i32 is smaller than i64)"}, 24 {types.NewVector(2, types.I32), types.I1, 25 "trunc operands are not compatible: from=<2 x i32>; to=i1"}, 26 {types.NewVector(1, types.I32), types.NewVector(2, types.I1), 27 "trunc vector operand length mismatch: from=<1 x i32>; to=<2 x i1>"}, 28 {types.NewVector(2, types.I32), types.NewVector(2, types.I64), 29 "invalid trunc operands: from.BitSize < to.BitSize (<2 x i32> is smaller than <2 x i64>)"}, 30 } 31 32 errOK := errors.New("OK") 33 34 for _, c := range cases { 35 testName := fmt.Sprintf("%v to %v", c.fromTyp, c.toTyp) 36 t.Run(testName, func(t *testing.T) { 37 var panicErr error 38 zeroVal := constant.NewZeroInitializer(c.fromTyp) 39 func() { 40 defer func() { panicErr = recover().(error) }() 41 trunc := NewTrunc(zeroVal, c.toTyp) 42 _ = trunc.String() 43 panic(errOK) 44 }() 45 got := panicErr.Error() 46 if got != c.panicMessage { 47 t.Errorf("expected %q, got %q", c.panicMessage, got) 48 } 49 }) 50 } 51 }