func TestMatrix_Col(t *testing.T) {
	c := test.Checker(t)
	m := Matrix{1, 2, 3, 4, 5, 6, 7, 8, 9}
	c.Expect(test.EQ, Col{1, 4, 7}, m.Col(0))
	c.Expect(test.EQ, Col{2, 5, 8}, m.Col(1))
	c.Expect(test.EQ, Col{3, 6, 9}, m.Col(2))
}
func TestVector_Neg(t *testing.T) {
	c := test.Checker(t)

	c.Expect(test.EQ, Vector{-1, 0}, X.Neg())
	c.Expect(test.EQ, Vector{0, -1}, Y.Neg())
	c.Expect(test.EQ, Vector{-1, -1}, X.Add(Y).Neg())
}
func TestMatrix_Sub(t *testing.T) {
	c := test.Checker(t)
	m1 := Matrix{10, 20, 30, 40, 50, 60, 70, 80, 90}
	m2 := Matrix{1, 2, 3, 4, 5, 6, 7, 8, 9}
	c.Expect(test.EQ, Matrix{9, 18, 27, 36, 45, 54, 63, 72, 81}, m1.Sub(m2))
	c.Expect(test.EQ, Matrix{-9, -18, -27, -36, -45, -54, -63, -72, -81}, m2.Sub(m1))
}
func TestVec2_Variables(t *testing.T) {
	c := test.Checker(t, test.Summary("spec testing exported variables"))

	c.Expect(test.EQ, Vector{0, 0}, Origin, "origin")
	c.Expect(test.EQ, Vector{1, 0}, X, "x-axis")
	c.Expect(test.EQ, Vector{0, 1}, Y, "y-axis")
}
func TestNormal_Mag2(t *testing.T) {
	c := test.Checker(t)

	c.Expect(test.FloatEQ, 0.0, Normal{0, 0}.Mag2())
	c.Expect(test.FloatEQ, 1.0, Normal{0, 1}.Mag2())
	c.Expect(test.FloatEQ, 25.0, Normal{3, 4}.Mag2())
}
func TestVector_Mag2(t *testing.T) {
	c := test.Checker(t)

	c.Expect(test.FloatEQ, 0.0, Origin.Mag2())
	c.Expect(test.FloatEQ, 1.0, Y.Mag2())
	c.Expect(test.FloatEQ, 25.0, Vector{3, 4}.Mag2())
}
func TestMatrix_Row(t *testing.T) {
	c := test.Checker(t)
	m := Matrix{1, 2, 3, 4, 5, 6, 7, 8, 9}
	c.Expect(test.EQ, Row{1, 2, 3}, m.Row(0))
	c.Expect(test.EQ, Row{4, 5, 6}, m.Row(1))
	c.Expect(test.EQ, Row{7, 8, 9}, m.Row(2))
}
func TestMat2_Construct(t *testing.T) {
	tests := []struct {
		summary string
		args    interface{}
		exp     Matrix
	}{
		{
			summary: "an slice of float32",
			args:    []float32{0, 1, 2, 3, 4, 5, 6, 7, 8},
			exp:     Matrix{0, 1, 2, 3, 4, 5, 6, 7, 8},
		},

		{
			summary: "an [16]float32",
			args:    [9]float32{0, 1},
			exp:     Matrix{0, 1},
		},

		{
			summary: "a Matrix",
			args:    Matrix{7, 12, 15, -1, -1, -1, 0, 8, 7},
			exp:     Matrix{7, 12, 15, -1, -1, -1, 0, 8, 7},
		},

		{
			summary: "a *Matrix",
			args:    &Matrix{10, 9, 8, 7, 6, 5, 4, 3, 2},
			exp:     Matrix{10, 9, 8, 7, 6, 5, 4, 3, 2},
		},

		{
			summary: "a []Row",
			args:    []Row{{1, 2, 0}, {0, 1, 2}, {2, 0, 1}},
			exp:     Matrix{1, 2, 0, 0, 1, 2, 2, 0, 1},
		},

		{
			summary: "a [3]Row",
			args:    [3]Row{{1, 2, 0}, {0, 1, 2}, {2, 0, 1}},
			exp:     Matrix{1, 2, 0, 0, 1, 2, 2, 0, 1},
		},

		{
			summary: "a []Col",
			args:    []Col{{1, 2, 0}, {0, 1, 2}, {2, 0, 1}},
			exp:     Matrix{1, 0, 2, 2, 1, 0, 0, 2, 1},
		},

		{
			summary: "a [3]Col",
			args:    [3]Col{{1, 2, 0}, {0, 1, 2}, {2, 0, 1}},
			exp:     Matrix{1, 0, 2, 2, 1, 0, 0, 2, 1},
		},
	}
	for i, tt := range tests {
		c := test.Checker(t, test.Summary("with test %v: %v", i, tt.summary))
		c.Expect(test.EQ, tt.exp, Construct(tt.args))
	}
}
func TestVector_Sub(t *testing.T) {
	c := test.Checker(t)

	c.Expect(test.EQ, Vector{-2, -1}, Origin.Sub(Vector{2, 1}))
	c.Expect(test.EQ, Vector{0, 0}, X.Sub(X))
	c.Expect(test.EQ, Vector{1, -1}, X.Sub(Y))
	c.Expect(test.EQ, Vector{-1, 1}, Y.Sub(X))
}
func TestVector_Add(t *testing.T) {
	c := test.Checker(t)

	c.Expect(test.EQ, Vector{2, 1}, Origin.Add(Vector{2, 1}))
	c.Expect(test.EQ, Vector{2, 0}, X.Add(X))
	c.Expect(test.EQ, Vector{1, 1}, X.Add(Y))
	c.Expect(test.EQ, Vector{1, 1}, Y.Add(X))
}
func TestMatrix_Add(t *testing.T) {
	c := test.Checker(t)
	m1 := Matrix{10, 20, 30, 40, 50, 60, 70, 80, 90}
	m2 := Matrix{1, 2, 3, 4, 5, 6, 7, 8, 9}
	r := Matrix{11, 22, 33, 44, 55, 66, 77, 88, 99}
	c.Expect(test.EQ, r, m1.Add(m2))
	c.Expect(test.EQ, r, m2.Add(m1))
}
func TestVector_Norm(t *testing.T) {
	c := test.Checker(t)

	c.Expect(test.EQ, Y, Y.Norm())
	c.Expect(test.EQ, X, X.Norm())
	c.Expect(test.EQ, X, Vector{5, 0}.Norm())
	c.Expect(test.EQ, Vector{.70710677, .70710677}, Vector{1, 1}.Norm())
}
func TestNormal_Norm(t *testing.T) {
	c := test.Checker(t)

	c.Expect(test.EQ, Normal{0, 1}, Normal{0, 1}.Norm())
	c.Expect(test.EQ, Normal{1, 0}, Normal{1, 0}.Norm())
	c.Expect(test.EQ, Normal{1, 0}, Normal{5, 0}.Norm())
	c.Expect(test.EQ, Normal{.70710677, .70710677}, Normal{1, 1}.Norm())
}
Exemple #14
0
func TestGoc_Serialization(t *testing.T) {
	c := test.Checker(t)
	// tt1 := TT1{
	// 	// TT2: TT2{
	// 	// 	S: "jeff",
	// 	// },
	// 	I: 13,
	// }
	// tt1.SetS("jeff")
	// fmt.Println("name:", tt1.S())
	tt2 := &TT2{}
	tt2.SetS("jeff")
	fmt.Println("S:", tt2.S())
	b, err := json.MarshalIndent(tt2, "    ", "    ")
	if err != nil {
		panic(err)
	}
	fmt.Println(string(b))

	// tf1 := testCmp1{
	// 	T: &testCmp1{
	// 		B: true,
	// 		S: "string",
	// 	},
	// }
	// tf1.Name_ = "name"
	// tf1.SetName("john")
	// b, err := json.MarshalIndent(tf1, "    ", "    ")
	// if err != nil {
	// 	panic(err)
	// }
	// fmt.Println(string(b))

	// err = json.Unmarshal(b, &tf2)
	// 	var tf2 = testCmp1
	// 	holder, err := support.ReadData([]byte(`
	// {
	//     "Type": "types.TestCmp1",
	//     "B": true,
	//     "T" : {
	//         "Type": "types.TestCmp1",
	//         "S": "string"
	//     }
	// }
	//         `))
	// 	if err != nil {
	// 		panic(err)
	// 	}
	// 	serialization.SerializeInPlace(&tf2, holder)
	// 	if err != nil {
	// 		panic(err)
	// 	}
	// 	// fmt.Println("data:", *tf2.(*testCmp1), *tf2.(*testCmp1).T.(*testCmp1))

	c.Assert(test.True, false, "not finished")
}
Exemple #15
0
func TestMat2_ExportedVariables(t *testing.T) {
	c := test.Checker(t)

	i := Matrix{}
	i[0] = 1
	i[4] = 1
	i[8] = 1
	c.Expect(test.EQ, i, Identity)
	c.Expect(test.EQ, Matrix{}, Zero)
}
func TestVector_Mul(t *testing.T) {
	c := test.Checker(t)

	c.Expect(test.EQ, Origin, Origin.Mul(5))
	c.Expect(test.EQ, X, X.Mul(1))
	c.Expect(test.EQ, Vector{2, 0}, X.Mul(2))
	c.Expect(test.EQ, Vector{-5, 0}, X.Mul(-5))
	c.Expect(test.EQ, Origin, Y.Mul(0))
	c.Expect(test.EQ, Vector{0, 1}, Y.Mul(1))
}
func TestVector_Div(t *testing.T) {
	c := test.Checker(t)

	c.Expect(test.EQ, Origin, Origin.Div(5))
	c.Expect(test.PanicEQ, "div by zero", func() { Origin.Div(0) })
	c.Expect(test.EQ, Vector{.5, 0}, X.Div(2))
	c.Expect(test.EQ, Vector{-1, 0}, X.Div(-1))
	c.Expect(test.EQ, Vector{0, .2}, Y.Div(5))
	c.Expect(test.EQ, Y, Y.Div(1))
}
Exemple #18
0
func TestMat2_MakeRow(t *testing.T) {
	c := test.Checker(t)

	c.Expect(test.EQ, Row{1, 2, 0}, MakeRow(vec2.Vector{1, 2}))
	c.Expect(test.EQ, Row{4, 7, 0}, MakeRow(&vec2.Vector{4, 7}))
	c.Expect(test.EQ, Row{3, 2, 1}, MakeRow(pnt2.Point{3, 2}))
	c.Expect(test.EQ, Row{1, 1, 1}, MakeRow(&pnt2.Point{1, 1}))
	c.Expect(test.EQ, Row{1, 2, 3}, MakeRow(Col{1, 2, 3}))
	type unknown int
	c.Expect(test.PanicEQ, "invalid type passed into MakeRow: mat2.unknown", func() { MakeRow(unknown(5)) })
}
func TestVector_Unmarshal(t *testing.T) {
	b := []byte(`{"Type": "vec2.Vector", "X": 25, "Y": 12}`)
	holder, err := support.ReadData(b)
	if err != nil {
		panic(err)
	}
	v := Vector{}
	serialization.SerializeInPlace(&v, holder)

	c := test.Checker(t)
	c.Expect(test.EQ, Vector{25, 12}, v)
}
func TestMatrix_Mul(t *testing.T) {
	c := test.Checker(t)
	m2 := Matrix{
		0, 2, 4,
		6, 8, 7,
		5, 3, 1,
	}
	m1 := Matrix{
		1, 2, 3,
		4, 5, 6,
		7, 8, 9,
	}
	c.Expect(test.EQ, Matrix{27, 27, 21, 60, 66, 57, 93, 105, 93}, m1.Mul(m2))
	c.Expect(test.EQ, Matrix{36, 42, 48, 87, 108, 129, 24, 33, 42}, m2.Mul(m1))
}
func TestMatrix_Inverse(t *testing.T) {
	c := test.Checker(t)

	r, err := Identity.Inverse()
	c.Expect(test.Ok, err)
	c.Expect(test.EQ, Identity, r)
	r, err = Matrix{7, 2, 1, 0, 3, -1, -3, 4, -2}.Inverse()
	c.Expect(test.Ok, err)
	c.Expect(test.EQ, Matrix{-2, 8, -5, 3, -11, 7, 9, -34, 21}, r)
	r, err = Zero.Inverse()
	c.Expect(test.Err, err)
	c.Expect(test.EQ, Zero, r)
	r, err = Matrix{0, 0, 0, 1, 1, 1, 2, 3, 4}.Inverse()
	c.Expect(test.Err, err)
	c.Expect(test.EQ, Zero, r)
}
Exemple #22
0
func TestMat2_ConstructPanicsOnInvalidTypes(t *testing.T) {
	tests := []struct {
		f   func()
		msg string
	}{
		{
			msg: "invalid type passed to Construct: mat2.invalid",
			f:   func() { type invalid int; Construct(invalid(5)) },
		},

		{
			msg: "Construct(a []float32) must have len(a)==9",
			f:   func() { Construct([]float32{}) },
		},

		{
			msg: "Construct(a []float32) must have len(a)==9",
			f:   func() { Construct([]float32{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}) },
		},

		{
			msg: "Construct(a []Row) must have len(a)==3",
			f:   func() { Construct([]Row{}) },
		},

		{
			msg: "Construct(a []Row) must have len(a)==3",
			f:   func() { Construct([]Row{{}, {}, {}, {}}) },
		},

		{
			msg: "Construct(a []Col) must have len(a)==3",
			f:   func() { Construct([]Col{}) },
		},

		{
			msg: "Construct(a []Col) must have len(a)==3",
			f:   func() { Construct([]Col{{}, {}, {}, {}}) },
		},
	}
	for i, tt := range tests {
		c := test.Checker(t, test.Summary("with test %v", i))
		c.Expect(test.PanicEQ, tt.msg, tt.f)
	}
}
func TestPoint_Add(t *testing.T) {
	c := test.Checker(t)
	c.Expect(test.EQ, Point{2, 3}, Point{1, 2}.Add(vec2.Vector{1, 1}))
}
func TestPnt2_convert(t *testing.T) {
	c := test.Checker(t)
	c.Expect(test.EQ, Point{2, 2}, convert(vec2.Vector{2, 2}))
	c.Expect(test.EQ, Point{1, 21}, convert(&vec2.Vector{1, 21}))
	c.Expect(test.PanicEQ, "unknown", func() { convert("unknown") })
}
func TestMatrix_String(t *testing.T) {
	c := test.Checker(t)

	m := Matrix{1, 2, 3, 4, 5, 6, 7, 8, 9}
	c.Expect(test.EQ, "[[1 2 3] [4 5 6] [7 8 9]]", m.String())
}
func TestSerialization(t *testing.T) {
	tests := []struct {
		summary string
		testFn  func(c *test.Chkr, obj, data interface{})
		data    []byte
		objPtr  interface{}
	}{
		{
			summary: "serializing uses golang's encoding/json serializer",
			objPtr:  &testType{},
			data:    []byte(`{"I": 5, "F": 3.14, "S": "string", "B": true}`),
			testFn: func(c *test.Chkr, obj, data interface{}) {
				SerializeInPlace(obj, data)

				typ := obj.(*testType)
				c.Expect(test.EQ, 5, typ.I)
				c.Expect(test.FloatEQ, 3.14, typ.F)
				c.Expect(test.EQ, "string", typ.S)
				c.Expect(test.EQ, true, typ.B)
			},
		},

		{
			summary: "tag support isn't in yet",
			objPtr:  &testType{},
			data:    []byte(`{"b": true}`),
			testFn: func(c *test.Chkr, obj, data interface{}) {
				c.Expect(test.Panic, func() { SerializeInPlace(obj, data) })
			},
		},

		{
			summary: "compound objects can be serialized in ",
			objPtr:  &testType{},
			data:    []byte(` {"T": { "I": 1} }`),
			// data:    []byte(` {"T": {"Type": "serialization.testType", "B": true } }`),
			testFn: func(c *test.Chkr, obj, data interface{}) {

				tf1 := testType{
					T: &testType{
						B: true,
					},
				}
				b, err := json.MarshalIndent(tf1, "    ", "    ")
				if err != nil {
					panic(err)
				}
				fmt.Println(string(b))
				tf2 := testType{}
				err = json.Unmarshal(b, &tf2)
				if err != nil {
					panic(err)
				}
				fmt.Println(tf2, *tf2.T)
				// SerializeInPlace(obj, data)

				// typ := obj.(*testType)
				c.Assert(test.True, false, "not finished")
				// c.Expect(test.EQ, true, typ.T.B)
			},
		},
	}
	for i, tt := range tests {
		c := test.Checker(t, test.Summary("with test %v: %v", i, tt.summary))

		holder, err := support.ReadData(tt.data)
		if err != nil {
			panic(err)
		}
		tt.testFn(c, tt.objPtr, holder)
	}
}
func TestVector_DefaultState(t *testing.T) {
	c := test.Checker(t)
	c.Expect(test.EQ, Vector{}, Origin)
}
func TestPoint_Sub(t *testing.T) {
	c := test.Checker(t)
	c.Expect(test.EQ, vec2.Vector{0, -1}, Point{1, 2}.Sub(Point{1, 1}))
}
func TestPoint_DefaultState(t *testing.T) {
	c := test.Checker(t)
	c.Expect(test.EQ, Point{}, Origin)
}
func TestVector_Dot(t *testing.T) {
	c := test.Checker(t)

	c.Expect(test.FloatEQ, 1.0, Y.Dot(Y))
	c.Expect(test.FloatEQ, 0.0, Y.Dot(X))
}