package point2d import ( "testing" ) func TestPoint2Set(t *testing.T) { p := &Point{ 1, 3, } if p.X != 1 || p.Y != 3 { t.Fail() t.Logf("Point expected to be '1 3' got '%d %d'", p.X, p.Y) } if p.String() != "1 3" { if !t.Failed() { t.Fail() } t.Logf("Point.String() expected to be '1 3' got '%s'", p.String()) } p.Set(2) if p.X != 2 || p.Y != 2 { if !t.Failed() { t.Fail() } t.Logf("Point expected to be '2 2' got '%s'", p) } p.SetTo(&Point{3, 4}) if p.X != 3 || p.Y != 4 { if !t.Failed() { t.Fail() } t.Logf("Point expected to be '3 4' got '%s'", p) } } func TestPoint2Move(t *testing.T) { p := &Point{1, 1} if p.X != 1 || p.Y != 1 { t.Fail() t.Logf("Point expected to be '1 1' got '%s'", p) } p.Move(1, -1) if p.X != 2 || p.Y != 0 { if !t.Failed() { t.Fail() } t.Logf("Point expected to be '2 0' got '%s'", p) } p.Set(1, 1) p.MoveBy(&Point{1, -1}) if p.X != 2 || p.Y != 0 { if !t.Failed() { t.Fail() } t.Logf("Point expected to be '2 0' got '%s'", p) } p.Move(-2, 2).Move(1, -1) if p.X != 1 || p.Y != 1 { if !t.Failed() { t.Fail() } t.Logf("Point expected to be '1 1' got '%s'", p) } p.Set(1, 1) p.Move(3) if !p.Equal(4) { if !t.Failed() { t.Fail() } t.Logf("Point expected to be '4 4' got '%s'", p) } if p.Equal() { if !t.Failed() { t.Fail() } t.Logf("Point.Equal called with no values, should always return false") } } func TestPoint2Zero(t *testing.T) { p := &Point{1, 1} if p.X != 1 || p.Y != 1 { t.Fail() t.Logf("Point expected to be '1 1' got '%s'", p) } if p.IsZero() { if !t.Failed() { t.Fail() } t.Logf("Point not zero, Point is '%s'", p) } p.Zero() if p.X != 0 || p.Y != 0 { if !t.Failed() { t.Fail() } t.Logf("Point expected to be '0 0' got '%s'", p) } if !p.IsZero() { if !t.Failed() { t.Fail() } t.Logf("Point is zero, Point is '%s'", p) } } func TestPoint2Negate(t *testing.T) { p := &Point{1, 1} if !p.Equal(1) { t.Fail() t.Logf("Point expected to be '1 1' got '%s'", p) } p.Negate() if p.X != -1 || p.Y != -1 { if !t.Failed() { t.Fail() } t.Logf("Point expected to be '-1 -1' got '%s'", p) } p.Negate() if !p.EqualTo(&Point{1, 1}) { if !t.Failed() { t.Fail() } t.Logf("Point expected to be '1 1' got '%s'", p) } p.Set(-3, -4) if !p.Equal(-3, -4) { if !t.Failed() { t.Fail() } t.Logf("Point expected ot be '-3 -4' got '%s'", p) } p.Abs() if !p.Equal(3, 4) { if !t.Failed() { t.Fail() } t.Logf("Point expected to be '3 4' got '%s'", p) } } func TestPoint2Copy(t *testing.T) { p := &Point{3, 4} p2 := p.Copy() p.Set(2) if p2.EqualTo(p) { t.Fail() t.Logf("p2 expected to not equal '2 2' got '%s' (Point = '%s')", p2, p) } } func TestPoint2Axis(t *testing.T) { p := &Point{3, 4} if p.IsX(2) { t.Fail() t.Logf("Point not on X axis 2, Point is '%s'", p) } if !p.IsY(4) { if !t.Failed() { t.Fail() } t.Logf("Point is on Y axis 4, Point is '%s'", p) } } func TestPoint2LessGreater(t *testing.T) { p := &Point{3, 4} if !p.Equal(3, 4) { t.Fail() t.Logf("Point expected to be '3 4' got '%s'", p) } if p.Greater(5) { if !t.Failed() { t.Fail() } t.Logf("Point should be less than 5, Point is '%s'", p) } if !p.Greater(3, 3) { if !t.Failed() { t.Fail() } t.Logf("Point should be greater than 3 3, Point is '%s'", p) } if p.Greater() { if !t.Failed() { t.Fail() } t.Logf("Point.Greater() should always return false") } if !p.GreaterThan(&Point{2, 2}) { if !t.Failed() { t.Fail() } t.Logf("Point should be greater than '2 2', Point is '%s'", p) } if p.Less(1) { if !t.Failed() { t.Fail() } t.Logf("Point should be greater than 1, Point is '%s'", p) } if !p.Less(10, 10) { if !t.Failed() { t.Fail() } t.Logf("Point should be less than 10 10, Point is '%s'", p) } if p.Less() { if !t.Failed() { t.Fail() } t.Logf("Point.Less() should always return false") } if !p.LessThan(&Point{15, 15}) { if !t.Failed() { t.Fail() } t.Logf("Point should be less than '15 15', Point is '%s'", p) } } func TestPoint2Distance(t *testing.T) { p := &Point{3, 4} if !p.Equal(3, 4) { t.Fail() t.Logf("Point expected to be '3 4' got '%s'", p) } if p.Distance(0, 0) != 4 { if !t.Failed() { t.Fail() } t.Logf("Point distance to 0 0 should be 4, got %d", p.Distance(0, 0)) } if p.DistanceTo(&Point{0, 0}) != 4 { if !t.Failed() { t.Fail() } t.Logf("Point distance to '0 0' should be 4, got %d", p.DistanceTo(&Point{0, 0})) } } func TestPoint2Within(t *testing.T) { p := &Point{5, 5} if !p.Equal(5) { t.Fail() t.Logf("Points not equal assigned values p='%s'", p) } if !p.Within(&Point{5, 5}, &Point{10, 10}) { if !t.Failed() { t.Fail() } t.Logf("p == topLeft, but returned false") } p.Set(10, 10) if !p.Within(&Point{5, 5}, &Point{10, 10}) { if !t.Failed() { t.Fail() } t.Logf("p == botRight, but returned false") } p.Set(5, 5) var ( tmp *Point = &Point{} y int x int ) for y = 0; y <= 11; y++ { for x = 0; x <= 11; x++ { tmp.Set(x, y) if tmp.X == 0 || tmp.Y == 0 { if !tmp.Within(&Point{0, 0}, &Point{11, 11}) { if !t.Failed() { t.Fail() } t.Logf("'%s' not in '0 0' '11 11'?", tmp) } } if !tmp.Within(&Point{}, &Point{11, 11}) { if !t.Failed() { t.Fail() } t.Logf("'%s' not in '0 0' '11 11'?", tmp) } } } if p.Within(&Point{20, 25}, &Point{15, 12}) { if !t.Failed() { t.Fail() } t.Logf("'%s' in '15 12' '20 25'?", p) } if p.Within(&Point{10, 10}, &Point{10, 10}) { if !t.Failed() { t.Fail() } t.Logf("'%s' in '10 10' '10 10'?", p) } } func TestPointNew(t *testing.T) { var p *Point = NewPoint() if p == nil { t.Fail() t.Logf("Expected a point at '0 0', but got nil") } if !p.IsZero() { if !t.Failed() { t.Fail() } t.Logf("Expected a point at '0 0', but got '%s'", p) } } func TestPointAs(t *testing.T) { var p *Point = AsPoint(3, 2) if p == nil { t.Fail() t.Logf("Expected a point, got nil") } if !p.Equal(3, 2) { if !t.Failed() { t.Fail() } t.Logf("Expected a point at '3 2', got '%s'", p) } p = AsPoint(5) if p == nil { if !t.Failed() { t.Fail() } t.Logf("Expected a point, got nil") } if !p.Equal(5, 5) { if !t.Failed() { t.Fail() } t.Logf("Expected a point at '5 5', got '%s'", p) } }