Skip to content

Instantly share code, notes, and snippets.

@Coollision
Coollision / main_test.go
Created July 12, 2020 22:39
GOLANG Introduction into Testing: simple helper function
func isTrue(t *testing.T, b bool) {
t.Helper()
if !b {
t.Fatal()
}
}
@Coollision
Coollision / main_test.go
Created July 12, 2020 22:39
GOLANG Introduction into Testing: simple table test
func TestLastIndex(t *testing.T) {
tableTests := []struct {
name string
list []int
x int
want int
}{
{name: "singleElement", list: []int{1}, x: 1, want: 0},
{name: "2timesTheSame", list: []int{1, 1}, x: 1, want: 1},
{name: "notFound", list: []int{1, 1, 1, 2, 2, 1}, x: 3, want: -1},
@Coollision
Coollision / main_test.go
Created July 12, 2020 22:37
GOLANG Introduction into Testing: basic main_test file
package main
import "testing"
func TestLastIndex(t *testing.T) {
list := []int{1, 2, 1, 1}
if got := lastIndex(list, 2); got != 1 {
t.Errorf("LastIndex(%v, %v) = %v, want %v", list, 2, got, 1)
}
}
@Coollision
Coollision / main.go
Created July 12, 2020 22:36
GOLANG Introduction into Testing: basic main file
package main
import "fmt"
func main() {
list := []int{1, 2, 3, 3, 8, 4, 5}
lastIndexOfFive := LastIndex(list, 5)
fmt.Printf("the last index of %d in the list %v is: %d",
5, list, lastIndexOfFive)