示例#1
0
func init() {
	// i1
	var err error
	i1Typ, err = types.NewInt(1)
	if err != nil {
		log.Fatalln(err)
	}
	// i3
	i3Typ, err = types.NewInt(3)
	if err != nil {
		log.Fatalln(err)
	}
	// i5
	i5Typ, err = types.NewInt(5)
	if err != nil {
		log.Fatalln(err)
	}
	// i8
	i8Typ, err = types.NewInt(8)
	if err != nil {
		log.Fatalln(err)
	}
	// i32
	i32Typ, err = types.NewInt(32)
	if err != nil {
		log.Fatalln(err)
	}
	// i64
	i64Typ, err = types.NewInt(64)
	if err != nil {
		log.Fatalln(err)
	}
	// float
	f32Typ, err = types.NewFloat(types.Float32)
	if err != nil {
		log.Fatalln(err)
	}
	// double
	f64Typ, err = types.NewFloat(types.Float64)
	if err != nil {
		log.Fatalln(err)
	}
	// fp128
	f128Typ, err = types.NewFloat(types.Float128)
	if err != nil {
		log.Fatalln(err)
	}
	// ppc_fp128
	f128_ppcTyp, err = types.NewFloat(types.Float128_PPC)
	if err != nil {
		log.Fatalln(err)
	}
	// <2 x i32>
	i32x2VecTyp, err = types.NewVector(i32Typ, 2)
	if err != nil {
		log.Fatalln(err)
	}
	// <3 x i32>
	i32x3VecTyp, err = types.NewVector(i32Typ, 3)
	if err != nil {
		log.Fatalln(err)
	}
	// <2 x float>
	f32x2VecTyp, err = types.NewVector(f32Typ, 2)
	if err != nil {
		log.Fatalln(err)
	}
	// <3 x float>
	f32x3VecTyp, err = types.NewVector(f32Typ, 3)
	if err != nil {
		log.Fatalln(err)
	}
	// [2 x i32]
	i32x2ArrTyp, err = types.NewArray(i32Typ, 2)
	if err != nil {
		log.Fatalln(err)
	}
	// {i32, i8}
	i32i8StructTyp, err = types.NewStruct([]types.Type{i32Typ, i8Typ}, false)
	if err != nil {
		log.Fatalln(err)
	}
	// [2 x {i32, i8}]
	i32i8x2ArrTyp, err = types.NewArray(i32i8StructTyp, 2)
	if err != nil {
		log.Fatalln(err)
	}

	// i1 1
	i1One, err = constant.NewInt(i1Typ, "1")
	if err != nil {
		log.Fatalln(err)
	}
	// i8 3
	i8Three, err = constant.NewInt(i8Typ, "3")
	if err != nil {
		log.Fatalln(err)
	}
	// i8 4
	i8Four, err = constant.NewInt(i8Typ, "4")
	if err != nil {
		log.Fatalln(err)
	}
	// i32 -13
	i32MinusThirteen, err = constant.NewInt(i32Typ, "-13")
	if err != nil {
		log.Fatalln(err)
	}
	// i32 -4
	i32MinusFour, err = constant.NewInt(i32Typ, "-4")
	if err != nil {
		log.Fatalln(err)
	}
	// i32 -3
	i32MinusThree, err = constant.NewInt(i32Typ, "-3")
	if err != nil {
		log.Fatalln(err)
	}
	// i32 1
	i32One, err = constant.NewInt(i32Typ, "1")
	if err != nil {
		log.Fatalln(err)
	}
	// i32 2
	i32Two, err = constant.NewInt(i32Typ, "2")
	if err != nil {
		log.Fatalln(err)
	}
	// i32 3
	i32Three, err = constant.NewInt(i32Typ, "3")
	if err != nil {
		log.Fatalln(err)
	}
	// i32 4
	i32Four, err = constant.NewInt(i32Typ, "4")
	if err != nil {
		log.Fatalln(err)
	}
	// i32 15
	i32Fifteen, err = constant.NewInt(i32Typ, "15")
	if err != nil {
		log.Fatalln(err)
	}
	// i32 42
	i32FortyTwo, err = constant.NewInt(i32Typ, "42")
	if err != nil {
		log.Fatalln(err)
	}
	// float -3.0
	f32MinusThree, err = constant.NewFloat(f32Typ, "-3.0")
	if err != nil {
		log.Fatalln(err)
	}
	// float -4.0
	f32MinusFour, err = constant.NewFloat(f32Typ, "-4.0")
	if err != nil {
		log.Fatalln(err)
	}
	// float 1.0
	f32One, err = constant.NewFloat(f32Typ, "1.0")
	if err != nil {
		log.Fatalln(err)
	}
	// float 2.0
	f32Two, err = constant.NewFloat(f32Typ, "2.0")
	if err != nil {
		log.Fatalln(err)
	}
	// float 3.0
	f32Three, err = constant.NewFloat(f32Typ, "3.0")
	if err != nil {
		log.Fatalln(err)
	}
	// float 4.0
	f32Four, err = constant.NewFloat(f32Typ, "4.0")
	if err != nil {
		log.Fatalln(err)
	}
	// double 4.0
	f64Four, err = constant.NewFloat(f64Typ, "4.0")
	if err != nil {
		log.Fatalln(err)
	}
	// TODO: Uncomment when fp128 and ppc_fp128 are supported.
	/*
		// fp128 3.0
		f128Three, err = constant.NewFloat(f128Typ, "3.0")
		if err != nil {
			log.Fatalln(err)
		}
		// ppc_fp128 4.0
		f128_ppcFour, err = constant.NewFloat(f128_ppcTyp, "4.0")
		if err != nil {
			log.Fatalln(err)
		}
	*/
	// <3 x i32> <i32 1, i32 2, i32 3>
	i32x3OneTwoThree, err = constant.NewVector(i32x3VecTyp, []constant.Constant{i32One, i32Two, i32Three})
	if err != nil {
		log.Fatalln(err)
	}
	// <2 x i32> <i32 3, i32 42>
	i32x2VecThreeFortyTwo, err = constant.NewVector(i32x2VecTyp, []constant.Constant{i32Three, i32FortyTwo})
	if err != nil {
		log.Fatalln(err)
	}
	// <2 x i32> <i32 -3, i32 15>
	i32x2VecMinusThreeFifteen, err = constant.NewVector(i32x2VecTyp, []constant.Constant{i32MinusThree, i32Fifteen})
	if err != nil {
		log.Fatalln(err)
	}
	// <2 x float> <float 3.0, float 4.0>
	f32x2VecThreeFour, err = constant.NewVector(f32x2VecTyp, []constant.Constant{f32Three, f32Four})
	if err != nil {
		log.Fatalln(err)
	}
	// <2 x float> <float -3.0, float 4.0>
	f32x2VecMinusThreeFour, err = constant.NewVector(f32x2VecTyp, []constant.Constant{f32MinusThree, f32Four})
	if err != nil {
		log.Fatalln(err)
	}
	// <3 x float> <float 3.0, float 2.0, float 1.0>
	f32x3VecThreeFourFifteen, err = constant.NewVector(f32x3VecTyp, []constant.Constant{f32Three, f32Two, f32One})
	if err != nil {
		log.Fatalln(err)
	}
	// {i32, i8} {i32 4, i8 3}
	i32i8FourThree, err = constant.NewStruct(i32i8StructTyp, []constant.Constant{i32Four, i8Three})
	if err != nil {
		log.Fatalln(err)
	}
	// {i32, i8} {i32 3, i8 4}
	i32i8ThreeFour, err = constant.NewStruct(i32i8StructTyp, []constant.Constant{i32Three, i8Four})
	if err != nil {
		log.Fatalln(err)
	}
}
示例#2
0
func TestFloatString(t *testing.T) {
	golden := []struct {
		input string
		typ   types.Type
		want  string
		err   string
	}{
		// i=0
		{
			input: "3.14", typ: f32Typ,
			want: "", err: `invalid floating point constant "3.14" for type "float"; precision loss ("3.14")`,
		},
		// i=1
		{
			input: "2.0", typ: f32Typ,
			want: "float 2.0",
		},
		// TODO: Enable test case after investigating why big.Float.Float32 reports the value as big.Exact when it is really not.
		// // i=2
		// {
		// 	input: "2.0001220703125", typ: f32Typ,
		// 	want: "", err: `invalid floating point constant "2.0001220703125" for type "float"; precision loss (2.000122)`,
		// },
		// i=3
		{
			input: "2.0001220703125", typ: f64Typ,
			want: "double 2.0001220703125",
		},
		// i=3
		{
			input: "-25000000000.0", typ: f64Typ,
			want: "double -2.5e10",
		},
		// i=4
		{
			input: "3e14", typ: f64Typ,
			want: "double 3.0e14",
		},
		// i=5
		{
			input: "12", typ: i32Typ,
			want: "", err: `invalid type "i32" for floating point constant`,
		},
		// i=6
		{
			input: "foo", typ: f32Typ,
			want: "", err: `unable to parse floating point constant "foo"`,
		},
		//{want: "3.14159265358979323846264338327950288419716939937510", input: "3.14159265358979323846264338327950288419716939937510"},
	}

	for i, g := range golden {
		v, err := constant.NewFloat(g.typ, g.input)
		if !sameError(err, g.err) {
			t.Errorf("i=%d: error mismatch; expected %v, got %v", i, g.err, err)
			continue
		} else if err != nil {
			// Expected error match, check next test case.
			continue
		}
		got := fmt.Sprintf("%s %s", v.Type(), v)
		if got != g.want {
			t.Errorf("i=%d: string mismatch; expected %v, got %v", i, g.want, got)
		}
	}
}