Skip to content

Instantly share code, notes, and snippets.

@feliperyan
Created July 17, 2019 14:03
Show Gist options
  • Save feliperyan/24b7485e0ce1260467357cde228c849a to your computer and use it in GitHub Desktop.
Save feliperyan/24b7485e0ce1260467357cde228c849a to your computer and use it in GitHub Desktop.
Testing the drone_simulation
package main
import (
"testing"
"fmt"
)
func getDrone() *Drone {
d := &Drone{}
d.CurrentPosition = GPSCoord{3, 3}
d.Destinations = make([]GPSCoord, 1)
d.Destinations[0] = GPSCoord{9, 5}
d.NextDestination = 0
d.Speed = 5
d.Name = "drone-1"
return d
}
func getTravellingBackwardsDrone() *Drone {
d := &Drone{}
d.CurrentPosition = GPSCoord{9, 5}
d.Destinations = make([]GPSCoord, 1)
d.Destinations[0] = GPSCoord{3, 3}
d.NextDestination = 0
d.Speed = 5
d.Name = "drone-1"
return d
}
func getFarTravellingDrone() *Drone {
d := &Drone{}
d.CurrentPosition = GPSCoord{3, 3}
d.Destinations = make([]GPSCoord, 1)
d.Destinations[0] = GPSCoord{220, 190}
d.NextDestination = 0
d.Speed = 5
d.Name = "drone-2"
return d
}
// TestPrint is the first test
func TestPrint(t *testing.T) {
d := getDrone()
d.Name = "d-1"
if d.Name != "d-1" {
t.Error("Got wrong name", d.Name)
}
}
//TestLocationOnAxis whether my Trigonometry is accurate
func TestLocationOnAxis(t *testing.T) {
d := getDrone()
g := d.getPositionTowardsDestination()
if g.Lat != 4.743416490252569 || g.Lon != 1.5811388300841895 {
t.Error("Values different from expectation: ", g)
}
}
func TestUpdatePosition(t *testing.T) {
d := getDrone()
d.UpdatePositionTowardsDestination()
if d.CurrentPosition.Lat != 7.743416490252569 || d.CurrentPosition.Lon != 4.5811388300841895 {
t.Error("Values different from expectation: ", d.CurrentPosition)
}
}
func TestArriveAtDestination(t *testing.T) {
d := getDrone()
d.UpdatePositionTowardsDestination()
d.UpdatePositionTowardsDestination()
if d.CurrentPosition.Lat != 9 || d.CurrentPosition.Lon != 5 {
t.Error("Values different from expectation: ", d.CurrentPosition)
}
}
func TestBackwardsArriveAtDestination(t *testing.T) {
d := getTravellingBackwardsDrone()
d.UpdatePositionTowardsDestination()
d.UpdatePositionTowardsDestination()
if d.CurrentPosition.Lat != 3 || d.CurrentPosition.Lon != 3 {
t.Error("Values different from expectation: ", d.CurrentPosition)
}
}
func TestArriveTravelFar(t *testing.T) {
d := getFarTravellingDrone()
for i := 0; i < 58; i++ {
d.UpdatePositionTowardsDestination()
fmt.Println("Current Pos: ", d.CurrentPosition)
}
if d.CurrentPosition.Lat != 220 || d.CurrentPosition.Lon != 190 {
t.Error("Values different from expectation: ", d.CurrentPosition)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment