forked from RedHatOfficial/GoCourse
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The standard 'testing' framework: example RedHatOfficial#7
- Loading branch information
Showing
2 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |