Skip to content

Commit

Permalink
Encapsulate external test variables into its functions
Browse files Browse the repository at this point in the history
This makes the tests more compact and readable.
  • Loading branch information
mrekucci committed Jul 19, 2015
1 parent e0183c8 commit 9ba3012
Show file tree
Hide file tree
Showing 12 changed files with 344 additions and 396 deletions.
90 changes: 42 additions & 48 deletions closestint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,25 @@ import (
"testing"
)

var intWeightTests = []struct {
in uint64
want int
}{
{0, 0},
{1, 1},
{2, 1},
{3, 2},
{4, 1},
{5, 2},
{6, 2},
{7, 3},
{8, 1},
{9, 2},
{math.MaxUint64, 64},
}

func TestIntWeight(t *testing.T) {
for _, tt := range intWeightTests {
got := IntWeight(tt.in)
if got != tt.want {
t.Errorf("IntWeight(%d) = %d; want %d", tt.in, got, tt.want)
for _, test := range []struct {
in uint64
want int
}{
{0, 0},
{1, 1},
{2, 1},
{3, 2},
{4, 1},
{5, 2},
{6, 2},
{7, 3},
{8, 1},
{9, 2},
{math.MaxUint64, 64},
} {
if got := IntWeight(test.in); got != test.want {
t.Errorf("IntWeight(%d) = %d; want %d", test.in, got, test.want)
}
}
}
Expand All @@ -41,34 +38,31 @@ func BenchmarkIntWeight(b *testing.B) {
}
}

var closestIntTests = []struct {
in uint64
want uint64
ok bool
}{
{0x0000000000000000, 0x0, false},
{0xffffffffffffffff, 0x0, false},
{0x01, 0x02, true},
{0x03, 0x05, true},
{0x04, 0x02, true},
{0x05, 0x06, true},
{0x06, 0x05, true},
{0x07, 0x0b, true},
{0x08, 0x04, true},
{0x09, 0x0a, true},
{0x0a, 0x09, true},
{0x0b, 0x0d, true},
{0x0c, 0x0a, true},
{0x0d, 0x0e, true},
{0x0e, 0x0d, true},
{0x0f, 0x17, true},
}

func TestClosestInt(t *testing.T) {
for _, tt := range closestIntTests {
got, ok := ClosestInt(tt.in)
if got != tt.want || ok != tt.ok {
t.Errorf("ClosestInt(%#x) = %#x, %t; want %#x, %t", tt.in, got, ok, tt.want, tt.ok)
for _, test := range []struct {
in uint64
want uint64
ok bool
}{
{0x0000000000000000, 0x0, false},
{0xffffffffffffffff, 0x0, false},
{0x01, 0x02, true},
{0x03, 0x05, true},
{0x04, 0x02, true},
{0x05, 0x06, true},
{0x06, 0x05, true},
{0x07, 0x0b, true},
{0x08, 0x04, true},
{0x09, 0x0a, true},
{0x0a, 0x09, true},
{0x0b, 0x0d, true},
{0x0c, 0x0a, true},
{0x0d, 0x0e, true},
{0x0e, 0x0d, true},
{0x0f, 0x17, true},
} {
if got, ok := ClosestInt(test.in); got != test.want || ok != test.ok {
t.Errorf("ClosestInt(%#x) = %#x, %t; want %#x, %t", test.in, got, ok, test.want, test.ok)
}
}
}
Expand Down
34 changes: 16 additions & 18 deletions dutchflag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,23 @@ import (
"testing"
)

var rearrangeTests = []struct {
an [5]int
i int
want [5]int
}{
{[...]int{5, 4, 3, 2, 1}, 0, [...]int{4, 3, 2, 1, 5}},
{[...]int{5, 4, 3, 2, 1}, 1, [...]int{1, 3, 2, 4, 5}},
{[...]int{5, 4, 3, 2, 1}, 2, [...]int{1, 2, 3, 4, 5}},
{[...]int{5, 4, 3, 2, 1}, 3, [...]int{1, 2, 3, 4, 5}},
{[...]int{5, 4, 3, 2, 1}, 4, [...]int{1, 3, 2, 4, 5}},
{[...]int{4, 3, 3, 5, 5}, 0, [...]int{3, 3, 4, 5, 5}},
}

func TestRearrange(t *testing.T) {
for _, tt := range rearrangeTests {
got := tt.an
Rearrange(got[:], tt.i)
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("rearrange(%d, %d) got %d; want %d", tt.an, tt.i, got, tt.want)
for _, test := range []struct {
an [5]int
i int
want [5]int
}{
{[...]int{5, 4, 3, 2, 1}, 0, [...]int{4, 3, 2, 1, 5}},
{[...]int{5, 4, 3, 2, 1}, 1, [...]int{1, 3, 2, 4, 5}},
{[...]int{5, 4, 3, 2, 1}, 2, [...]int{1, 2, 3, 4, 5}},
{[...]int{5, 4, 3, 2, 1}, 3, [...]int{1, 2, 3, 4, 5}},
{[...]int{5, 4, 3, 2, 1}, 4, [...]int{1, 3, 2, 4, 5}},
{[...]int{4, 3, 3, 5, 5}, 0, [...]int{3, 3, 4, 5, 5}},
} {
got := test.an
Rearrange(got[:], test.i)
if !reflect.DeepEqual(got, test.want) {
t.Errorf("Rearrange(%d, %d) got %d; want %d", test.an, test.i, got, test.want)
}
}
}
Expand Down
96 changes: 45 additions & 51 deletions intstrconv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,37 @@ package epi

import "testing"

var stringToIntTests = []struct {
in string
want int64
err error
}{
{"", 0, errSyntax},
{"0", 0, nil},
{"-0", 0, nil},
{"1", 1, nil},
{"-1", -1, nil},
{"+1", 1, nil},
{"0123456789", 123456789, nil},
{"-0123456789", -123456789, nil},
{"123456789", 123456789, nil},
{"-123456789", -123456789, nil},
{"9223372036854775807", 1<<63 - 1, nil},
{"-9223372036854775807", -(1<<63 - 1), nil},
{"9223372036854775808", 0, errRange},
{"-9223372036854775808", -1 << 63, nil},
{"9223372036854775809", 0, errRange},
{"-9223372036854775809", 0, errRange},
{"9223372036854775810", 0, errRange},
{"-9223372036854775810", 0, errRange},
{"a", 0, errSyntax},
{"-a", 0, errSyntax},
{"123a", 0, errSyntax},
{"-123a", 0, errSyntax},
}

func TestStringToInt(t *testing.T) {
for _, tt := range stringToIntTests {
got, err := StringToInt(tt.in)
if got != tt.want || err != tt.err {
t.Errorf("StringToInt(%q) = %d, %v; want %d, %v", tt.in, got, err, tt.want, tt.err)
for _, test := range []struct {
in string
want int64
err error
}{
{"", 0, errSyntax},
{"0", 0, nil},
{"-0", 0, nil},
{"1", 1, nil},
{"-1", -1, nil},
{"+1", 1, nil},
{"0123456789", 123456789, nil},
{"-0123456789", -123456789, nil},
{"123456789", 123456789, nil},
{"-123456789", -123456789, nil},
{"9223372036854775807", 1<<63 - 1, nil},
{"-9223372036854775807", -(1<<63 - 1), nil},
{"9223372036854775808", 0, errRange},
{"-9223372036854775808", -1 << 63, nil},
{"9223372036854775809", 0, errRange},
{"-9223372036854775809", 0, errRange},
{"9223372036854775810", 0, errRange},
{"-9223372036854775810", 0, errRange},
{"a", 0, errSyntax},
{"-a", 0, errSyntax},
{"123a", 0, errSyntax},
{"-123a", 0, errSyntax},
} {
if got, err := StringToInt(test.in); got != test.want || err != test.err {
t.Errorf("StringToInt(%q) = %d, %v; want %d, %v", test.in, got, err, test.want, test.err)
}
}
}
Expand All @@ -50,25 +47,22 @@ func BenchmarkStringToInt(b *testing.B) {
}
}

var intToStringTests = []struct {
in int64
want string
}{
{0, "0"},
{1, "1"},
{-1, "-1"},
{123456789, "123456789"},
{-123456789, "-123456789"},
{1<<63 - 1, "9223372036854775807"},
{-(1<<63 - 1), "-9223372036854775807"},
{-1 << 63, "-9223372036854775808"},
}

func TestIntToString(t *testing.T) {
for _, tt := range intToStringTests {
got := IntToString(tt.in)
if got != tt.want {
t.Errorf("IntToString(%d) = %q; want %q", tt.in, got, tt.want)
for _, test := range []struct {
in int64
want string
}{
{0, "0"},
{1, "1"},
{-1, "-1"},
{123456789, "123456789"},
{-123456789, "-123456789"},
{1<<63 - 1, "9223372036854775807"},
{-(1<<63 - 1), "-9223372036854775807"},
{-1 << 63, "-9223372036854775808"},
} {
if got := IntToString(test.in); got != test.want {
t.Errorf("IntToString(%d) = %q; want %q", test.in, got, test.want)
}
}
}
Expand Down
57 changes: 27 additions & 30 deletions maxdiff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,34 @@ import (
"testing"
)

var minBatteryCapTests = []struct {
in []int
want int
ok bool
}{
{[]int{}, 0, true},
{[]int{0}, 0, true},
{[]int{1}, 0, true},
{[]int{-1}, 0, true},
{[]int{0, 0}, 0, true},
{[]int{0, 1}, 1, true},
{[]int{1, 0}, 0, true},
{[]int{3, 3, 10}, 7, true},
{[]int{-3, 3, 10}, 13, true},
{[]int{3, -3, 10}, 13, true},
{[]int{3, 3, -10}, 0, true},
{[]int{1, maxInt}, maxInt - 1, true},
{[]int{-1, maxInt}, 0, false},
{[]int{1, -maxInt}, 0, true},
{[]int{-1, -maxInt}, 0, true},
{[]int{1, minInt}, 0, false},
{[]int{-1, minInt}, 0, true},
{[]int{minInt, maxInt}, 0, false},
{[]int{maxInt, minInt}, 0, false},
}

func TestMinBatteryCap(t *testing.T) {
for _, tt := range minBatteryCapTests {
got, ok := MinBatteryCap(tt.in)
if ok != tt.ok || got != tt.want {
t.Errorf("MinBatteryCap(%d) = %d, %t; want %d, %t", tt.in, got, ok, tt.want, tt.ok)
for _, test := range []struct {
in []int
want int
ok bool
}{
{[]int{}, 0, true},
{[]int{0}, 0, true},
{[]int{1}, 0, true},
{[]int{-1}, 0, true},
{[]int{0, 0}, 0, true},
{[]int{0, 1}, 1, true},
{[]int{1, 0}, 0, true},
{[]int{3, 3, 10}, 7, true},
{[]int{-3, 3, 10}, 13, true},
{[]int{3, -3, 10}, 13, true},
{[]int{3, 3, -10}, 0, true},
{[]int{1, maxInt}, maxInt - 1, true},
{[]int{-1, maxInt}, 0, false},
{[]int{1, -maxInt}, 0, true},
{[]int{-1, -maxInt}, 0, true},
{[]int{1, minInt}, 0, false},
{[]int{-1, minInt}, 0, true},
{[]int{minInt, maxInt}, 0, false},
{[]int{maxInt, minInt}, 0, false},
} {
if got, ok := MinBatteryCap(test.in); ok != test.ok || got != test.want {
t.Errorf("MinBatteryCap(%d) = %d, %t; want %d, %t", test.in, got, ok, test.want, test.ok)
}
}
}
Expand Down
35 changes: 16 additions & 19 deletions nextperm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,23 @@ import (
"testing"
)

var nextTests = []struct {
in []int
want []int
}{
{[]int{1}, []int{}},
{[]int{1, 2}, []int{2, 1}},
{[]int{2, 1}, []int{}},
{[]int{1, 2, 3}, []int{1, 3, 2}},
{[]int{1, 3, 2}, []int{2, 1, 3}},
{[]int{2, 1, 3}, []int{2, 3, 1}},
{[]int{2, 3, 1}, []int{3, 1, 2}},
{[]int{3, 1, 2}, []int{3, 2, 1}},
{[]int{3, 2, 1}, []int{}},
}

func TestNext(t *testing.T) {
for _, tt := range nextTests {
got := NextPerm(tt.in)
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("NextPerm(%d) = %d; want %d", tt.in, got, tt.want)
for _, test := range []struct {
in []int
want []int
}{
{[]int{1}, []int{}},
{[]int{1, 2}, []int{2, 1}},
{[]int{2, 1}, []int{}},
{[]int{1, 2, 3}, []int{1, 3, 2}},
{[]int{1, 3, 2}, []int{2, 1, 3}},
{[]int{2, 1, 3}, []int{2, 3, 1}},
{[]int{2, 3, 1}, []int{3, 1, 2}},
{[]int{3, 1, 2}, []int{3, 2, 1}},
{[]int{3, 2, 1}, []int{}},
} {
if got := NextPerm(test.in); !reflect.DeepEqual(got, test.want) {
t.Errorf("NextPerm(%d) = %d; want %d", test.in, got, test.want)
}
}
}
Expand Down
Loading

0 comments on commit 9ba3012

Please sign in to comment.