package quadtree import ( "reflect" "testing" ) // Test the NewCoord function func TestNewCoord(t *testing.T) { // create a struct storing the arguments that should be passed to the function type args struct { x float64 y float64 } // create a struct storing the test cases tests := []struct { name string args args want Coord }{ // define the tests {"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}}, } // run the tests 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) { // create a struct storing the arguments that should be passed to the function type args struct { inCenter Coord inHalfDimension float64 } // create a struct storing the test cases tests := []struct { name string args args want BoundingBox }{ // define the tests {"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}}, } // run the tests 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) { // create a struct storing the arguments that should be passed to the function 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 tests {"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) { // create a struct storing the arguments that should be passed to the function type args struct { boundary BoundingBox depth int } // create a struct storing the test cases tests := []struct { name string args args want *Quadtree }{ // define the tests { 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, }, }, } // run the tests 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) { // create a struct storing the test cases tests := []struct { name string quadtree *Quadtree }{ // define the tests { 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, }, }, } // run the tests for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { tt.quadtree.subdivide() }) } } func TestQuadtree_Insert(t *testing.T) { // create a struct storing the arguments that should be passed to the function type args struct { point Coord } // create a struct storing the test cases tests := []struct { name string quadtree *Quadtree args args }{ // define the tests { 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, }, }, }, } // run the tests for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { tt.quadtree.Insert(tt.args.point) }) } } func TestQuadtree_Print(t *testing.T) { // create a struct storing the test cases tests := []struct { name string quadtree *Quadtree }{ // define the tests { 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, }, }, } // run the tests for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { tt.quadtree.Print() }) } }