Skip to content

Commit

Permalink
The standard 'testing' framework: example RedHatOfficial#7
Browse files Browse the repository at this point in the history
  • Loading branch information
tisnik committed Nov 4, 2019
1 parent 7f42990 commit 113dd43
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
9 changes: 9 additions & 0 deletions testing/tests07/add.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

func add(x int32, y int32) int32 {
return x + y
}

func main() {
println(add(1, 2))
}
73 changes: 73 additions & 0 deletions testing/tests07/add_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package main

import (
"fmt"
"math"
"testing"
)

type AddTest struct {
x int32
y int32
expected int32
}

func checkAdd(t *testing.T, testInputs []AddTest) {
for _, i := range testInputs {
result := add(i.x, i.y)
if result != i.expected {
msg := fmt.Sprintf("%d + %d should be %d, got %d instead",
i.x, i.y, i.expected, result)
t.Error(msg)
}
}
}

func TestAddBasicValues(t *testing.T) {
var addTestInput = []AddTest{
{0, 0, 0},
{1, 0, 1},
{2, 0, 2},
{2, 1, 3},
}
checkAdd(t, addTestInput)
}

func TestAddNegativeValues(t *testing.T) {
var addTestInput = []AddTest{
{0, 0, 0},
{1, 0, 1},
{2, 0, 2},
{2, 1, 3},
{2, -2, 0},
}
checkAdd(t, addTestInput)
}

func TestAddMinValues(t *testing.T) {
var addTestInput = []AddTest{
{math.MinInt32, 0, math.MinInt32},
{math.MinInt32, 1, math.MinInt32 + 1},
}
checkAdd(t, addTestInput)
}

func TestAddMaxValues(t *testing.T) {
var addTestInput = []AddTest{
{math.MaxInt32, 0, math.MaxInt32},
{math.MaxInt32, 1, math.MinInt32},
{math.MaxInt32, math.MinInt32, -1},
}
checkAdd(t, addTestInput)
}

func TestAddMinMaxValues(t *testing.T) {
var addTestInput = []AddTest{
{math.MinInt32, 0, math.MinInt32},
{math.MinInt32, 1, math.MinInt32 + 1},
{math.MaxInt32, 0, math.MaxInt32},
{math.MaxInt32, 1, math.MinInt32},
{math.MaxInt32, math.MinInt32, -1},
}
checkAdd(t, addTestInput)
}

0 comments on commit 113dd43

Please sign in to comment.