From 64011a9b5b109a867c10cba5e7da160c69b6e55f Mon Sep 17 00:00:00 2001 From: hanemile Date: Fri, 16 Nov 2018 13:57:51 +0100 Subject: Wrote some tests :D --- quadtree_test.go | 341 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 341 insertions(+) create mode 100644 quadtree_test.go (limited to 'quadtree_test.go') diff --git a/quadtree_test.go b/quadtree_test.go new file mode 100644 index 0000000..e736515 --- /dev/null +++ b/quadtree_test.go @@ -0,0 +1,341 @@ +package quadtree + +import ( + "reflect" + "testing" +) + +func TestNewCoord(t *testing.T) { + type args struct { + x float64 + y float64 + } + tests := []struct { + name string + args args + want Coord + }{ + {"NewCoord both positive", args{1.0, 1.0}, Coord{1, 1}}, + {"NewCoord both negative", args{-1.0, -1.0}, Coord{-1, -1}}, + {"NewCoord both zero", args{0, 0}, Coord{0, 0}}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := NewCoord(tt.args.x, tt.args.y); !reflect.DeepEqual(got, tt.want) { + t.Errorf("NewCoord() = %v, want %v", got, tt.want) + } + }) + } +} + +func BenchmarkNewCoord(b *testing.B) { + for i := 0; i < b.N; i++ { + NewCoord(0, 1) + } +} + +func TestNewBoundingBox(t *testing.T) { + type args struct { + inCenter Coord + inHalfDimension float64 + } + tests := []struct { + name string + args args + want BoundingBox + }{ + {"bounding positive", args{Coord{0, 0}, 5.0}, BoundingBox{Coord{0, 0}, 5.0}}, + {"bounding negative", args{Coord{0, 0}, -5.0}, BoundingBox{Coord{0, 0}, -5.0}}, + {"positive", args{Coord{3, 3}, 5.0}, BoundingBox{Coord{3, 3}, 5.0}}, + {"negative", args{Coord{-3, -3}, 5.0}, BoundingBox{Coord{-3, -3}, 5.0}}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := NewBoundingBox(tt.args.inCenter, tt.args.inHalfDimension); !reflect.DeepEqual(got, tt.want) { + t.Errorf("NewBoundingBox() = %v, want %v", got, tt.want) + } + }) + } +} + +func BenchmarkNewBoundingBox(b *testing.B) { + for i := 0; i < b.N; i++ { + NewBoundingBox(Coord{1, 2}, 4) + } +} + +func TestCoord_InsideOf(t *testing.T) { + type args struct { + quadtreeCell *Quadtree + } + + // define a quadtreeCell that should be used + quadtreeCell := Quadtree{ + nodeCapacity: 0, + boundary: BoundingBox{ + center: Coord{ + X: 0, + Y: 0, + }, + halfDimension: 3, + }, + pointsSlice: nil, + northWest: nil, + northEast: nil, + southWest: nil, + southEast: nil, + depth: 0, + } + + // Define a struct storing the tests + tests := []struct { + name string + point *Coord + args args + want bool + }{ + // Define the testcases + {"point inside of the quadtree", &Coord{0, 0}, args{&quadtreeCell}, true}, + {"point outside of the quadtree", &Coord{4, 4}, args{&quadtreeCell}, false}, + } + + // Run the tests + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := tt.point.InsideOf(tt.args.quadtreeCell); got != tt.want { + t.Errorf("Coord.InsideOf() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestNewQuadtree(t *testing.T) { + type args struct { + boundary BoundingBox + depth int + } + tests := []struct { + name string + args args + want *Quadtree + }{ + { + name: "", + args: args{ + boundary: BoundingBox{ + center: Coord{ + X: 0, + Y: 0, + }, + halfDimension: 0, + }, + depth: 0, + }, + want: &Quadtree{ + nodeCapacity: 0, + boundary: BoundingBox{ + center: Coord{ + X: 0, + Y: 0, + }, + halfDimension: 0, + }, + pointsSlice: nil, + northWest: nil, + northEast: nil, + southWest: nil, + southEast: nil, + depth: 0, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := NewQuadtree(tt.args.boundary, tt.args.depth); !reflect.DeepEqual(got, tt.want) { + t.Errorf("NewQuadtree() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestQuadtree_subdivide(t *testing.T) { + tests := []struct { + name string + quadtree *Quadtree + }{ + { + name: "Subdivide", + quadtree: &Quadtree{ + nodeCapacity: 0, + boundary: BoundingBox{ + center: Coord{ + X: 0, + Y: 0, + }, + halfDimension: 0, + }, + pointsSlice: nil, + northWest: &Quadtree{}, + northEast: &Quadtree{}, + southWest: &Quadtree{}, + southEast: &Quadtree{}, + depth: 0, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + tt.quadtree.subdivide() + }) + } +} + +func TestQuadtree_Insert(t *testing.T) { + type args struct { + point Coord + } + tests := []struct { + name string + quadtree *Quadtree + args args + }{ + { + name: "", + quadtree: &Quadtree{ + nodeCapacity: 0, + boundary: BoundingBox{ + center: Coord{ + X: 0, + Y: 0, + }, + halfDimension: 0, + }, + pointsSlice: nil, + northWest: &Quadtree{}, + northEast: &Quadtree{}, + southWest: &Quadtree{}, + southEast: &Quadtree{}, + depth: 0, + }, + args: args{ + point: Coord{ + X: 1, + Y: 1, + }, + }, + }, + { + name: "", + quadtree: &Quadtree{ + nodeCapacity: 0, + boundary: BoundingBox{ + center: Coord{ + X: 0, + Y: 0, + }, + halfDimension: 0, + }, + pointsSlice: nil, + northWest: &Quadtree{}, + northEast: &Quadtree{}, + southWest: &Quadtree{}, + southEast: &Quadtree{}, + depth: 0, + }, + args: args{ + point: Coord{ + X: 1, + Y: -1, + }, + }, + }, + { + name: "", + quadtree: &Quadtree{ + nodeCapacity: 0, + boundary: BoundingBox{ + center: Coord{ + X: 0, + Y: 0, + }, + halfDimension: 0, + }, + pointsSlice: nil, + northWest: &Quadtree{}, + northEast: &Quadtree{}, + southWest: &Quadtree{}, + southEast: &Quadtree{}, + depth: 0, + }, + args: args{ + point: Coord{ + X: -1, + Y: -1, + }, + }, + }, + { + name: "", + quadtree: &Quadtree{ + nodeCapacity: 0, + boundary: BoundingBox{ + center: Coord{ + X: 0, + Y: 0, + }, + halfDimension: 0, + }, + pointsSlice: nil, + northWest: &Quadtree{}, + northEast: &Quadtree{}, + southWest: &Quadtree{}, + southEast: &Quadtree{}, + depth: 0, + }, + args: args{ + point: Coord{ + X: -1, + Y: 1, + }, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + tt.quadtree.Insert(tt.args.point) + + }) + } +} + +func TestQuadtree_Print(t *testing.T) { + tests := []struct { + name string + quadtree *Quadtree + }{ + { + name: "", + quadtree: &Quadtree{ + nodeCapacity: 0, + boundary: BoundingBox{ + center: Coord{ + X: 0, + Y: 0, + }, + halfDimension: 0, + }, + pointsSlice: nil, + northWest: &Quadtree{}, + northEast: &Quadtree{}, + southWest: &Quadtree{}, + southEast: &Quadtree{}, + depth: 0, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + tt.quadtree.Print() + }) + } +} -- cgit 1.4.1