forked from shekhargulati/52-technologies-in-2016
-
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.
29-go-unit-testing first draft finished
- Loading branch information
1 parent
69477ee
commit da8b105
Showing
8 changed files
with
476 additions
and
1 deletion.
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,2 @@ | ||
github.com/ | ||
pkg/ |
Large diffs are not rendered by default.
Oops, something went wrong.
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,25 @@ | ||
package problems | ||
|
||
import ( | ||
"math" | ||
"sort" | ||
) | ||
|
||
type Pair struct { | ||
First, Second int | ||
} | ||
|
||
func ClosestPair(numbers []int) Pair { | ||
sort.Ints(numbers) | ||
var pair Pair | ||
var diff int = math.MaxInt32 | ||
for index := 0; index < len(numbers)-1; index++ { | ||
cur := numbers[index] | ||
next := numbers[index+1] | ||
if next-cur < diff { | ||
diff = next - cur | ||
pair = Pair{cur, next} | ||
} | ||
} | ||
return pair | ||
} |
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,32 @@ | ||
package problems_test | ||
|
||
import ( | ||
"fmt" | ||
"github.com/stretchr/testify/assert" | ||
. "problems" | ||
"testing" | ||
) | ||
|
||
func TestFindClosestPair(t *testing.T) { | ||
numbers := []int{2, 10, 5, 6, 15} | ||
pair := ClosestPair(numbers) | ||
assert.Equal(t, 5, pair.First, fmt.Sprintf("first should be 5 but was %d", pair.First)) | ||
assert.Equal(t, 6, pair.Second, fmt.Sprintf("second should be 6 but was %d", pair.Second)) | ||
} | ||
|
||
var closestPairTests = []struct { | ||
in []int | ||
out Pair | ||
}{ | ||
{[]int{2, 10, 5, 6, 15}, Pair{5, 6}}, | ||
{[]int{2, 4, 5}, Pair{4, 5}}, | ||
{[]int{100, 5, 7, 99, 11}, Pair{99, 100}}, | ||
} | ||
|
||
func TestClosestPair(t *testing.T) { | ||
for _, tt := range closestPairTests { | ||
pair := ClosestPair(tt.in) | ||
assert.Equal(t, tt.out.First, pair.First) | ||
assert.Equal(t, tt.out.Second, pair.Second) | ||
} | ||
} |
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,26 @@ | ||
package problems_test | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
. "problems" | ||
"testing" | ||
) | ||
|
||
func TestShouldBeTrueWhenThreeNumbersAreEqual(t *testing.T) { | ||
isThreeNumbersEqual := EqualOrNotEqual(1, 1, 1) | ||
if !isThreeNumbersEqual { | ||
t.Error("Expected true, got ", isThreeNumbersEqual) | ||
} | ||
} | ||
|
||
func TestShouldBeFalseWhenThreeNumbersAreNotEqual(t *testing.T) { | ||
isThreeNumbersEqual := EqualOrNotEqual(1, 2, 3) | ||
if isThreeNumbersEqual { | ||
t.Error("Expected false, got", isThreeNumbersEqual) | ||
} | ||
} | ||
|
||
func TestShouldAssertThatFunctionReturnsFalseWhenThreeNumbersAreNotEqual(t *testing.T) { | ||
isThreeNumbersEqual := EqualOrNotEqual(1, 2, 3) | ||
assert.False(t, isThreeNumbersEqual, "Expected false got true") | ||
} |
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 problems | ||
|
||
func EqualOrNotEqual(first, second, third int) bool { | ||
if first == second && second == third { | ||
return true | ||
} else { | ||
return false | ||
} | ||
} |
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
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 |
---|---|---|
|
@@ -75,6 +75,8 @@ Below is the list of technologies covered in this series: | |
|
||
29. **[Week 29: July 15, 2016](./29-golang-github-slacknotification)** [Go Language - GitHub System Status API & Slack Notifications](./29-golang-github-slacknotification/README.md). In this blog, we are going to investigate a few more features of the language and combine that into a real life application where we can monitor the status of the GitHub System via its Status API and report its current status directly into a Slack Team page. | ||
|
||
30. **[Week 29: July 17, 2016](./29-go-unit-testing)** [Learn GoLang For Great Good Part 2: Unit Testing in Go](./29-go-unit-testing/README.md). This week we will take our Go knowledge to the next level by learning how to perform unit testing in Go. Unit testing has become an essential skill set for every programmer. | ||
|
||
----------- | ||
You can follow me on twitter at [https://twitter.com/shekhargulati](https://twitter.com/shekhargulati) or email me at <[email protected]>. Also, you can read my blogs at [http://shekhargulati.com/](http://shekhargulati.com/) | ||
|
||
|