Skip to content

Commit 12e57f6

Browse files
author
Szymon Szeliga
committedApr 24, 2016
Add a Vector struct with Add and Sub methods along with tests
1 parent f4330ad commit 12e57f6

File tree

4 files changed

+45
-2
lines changed

4 files changed

+45
-2
lines changed
 

‎.travis.yml

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
language: go
2+
3+
go:
4+
- 1.6.1

‎main.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package main
22

3-
import "fmt"
3+
import (
4+
"fmt"
5+
6+
"github.com/szeliga/goray/math"
7+
)
48

59
func main() {
6-
fmt.Println("Hello, World!")
10+
fmt.Println(math.Vector{X: 1, Y: 1, Z: 1})
711
}

‎math/vector.go

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package math
2+
3+
// Vector - struct holding X Y Z values of a 3D vector
4+
type Vector struct {
5+
X int
6+
Y int
7+
Z int
8+
}
9+
10+
// Add - adds two vectors together
11+
func (a Vector) Add(b Vector) Vector {
12+
return Vector{a.X + b.X, a.Y + b.Y, a.Z + b.Z}
13+
}
14+
15+
// Sub - subtracts b Vector from a Vector
16+
func (a Vector) Sub(b Vector) Vector {
17+
return Vector{a.X - b.X, a.Y - b.Y, a.Z - b.Z}
18+
}

‎math/vector_test.go

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package math
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestAdd(t *testing.T) {
10+
result := Vector{1, 1, 1}.Add(Vector{2, 2, 2})
11+
assert.Equal(t, Vector{3, 3, 3}, result, "should add correctly")
12+
}
13+
14+
func TestSub(t *testing.T) {
15+
result := Vector{3, 3, 3}.Sub(Vector{1, 1, 1})
16+
assert.Equal(t, Vector{2, 2, 2}, result, "should subtract correcly")
17+
}

0 commit comments

Comments
 (0)