input/gvctrl/gvctrl_test.go: added gvctrl_test.go file and wrote test for closestValIdx

This commit is contained in:
Saxon 2019-10-12 16:58:15 +10:30
parent 366226bb29
commit caa46939b4
1 changed files with 78 additions and 0 deletions

View File

@ -0,0 +1,78 @@
/*
DESCRIPTION
gvctrl_test.go provides tests of functionality in the gvctrl package.
AUTHORS
Saxon A. Nelson-Milton <saxon@ausocean.org>
LICENSE
Copyright (C) 2019 the Australian Ocean Lab (AusOcean)
It is free software: you can redistribute it and/or modify them
under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
It is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
in gpl.txt. If not, see http://www.gnu.org/licenses.
*/
package gvctrl
import "testing"
func TestClosestValIdx(t *testing.T) {
tests := []struct {
l []int
v int
want int
}{
{
l: []int{2, 5, 8, 11, 14},
v: 6,
want: 1,
},
{
l: []int{2, 5, 8, 11, 14},
v: 12,
want: 3,
},
{
l: []int{2, 5, 8, 11, 14},
v: 13,
want: 4,
},
{
l: []int{2, 5, 8, 11, 14},
v: 0,
want: 0,
},
{
l: []int{2, 5, 8, 11, 14},
v: 17,
want: 4,
},
{
l: []int{2, 5, 8, 11, 15},
v: 13,
want: 3,
},
{
l: []int{},
v: 17,
want: 0,
},
}
for i, test := range tests {
got := closestValIdx(test.v, test.l)
if got != test.want {
t.Errorf("did not get expected result for test: %d\nGot: %v\nWant: %v", i, got, test.want)
}
}
}