123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346 |
- 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)
- }
- }
|