From caa46939b48b119e04714fc9e4b309ad940fd6f9 Mon Sep 17 00:00:00 2001 From: Saxon Date: Sat, 12 Oct 2019 16:58:15 +1030 Subject: [PATCH] input/gvctrl/gvctrl_test.go: added gvctrl_test.go file and wrote test for closestValIdx --- input/gvctrl/gvctrl_test.go | 78 +++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 input/gvctrl/gvctrl_test.go diff --git a/input/gvctrl/gvctrl_test.go b/input/gvctrl/gvctrl_test.go new file mode 100644 index 00000000..15569cf3 --- /dev/null +++ b/input/gvctrl/gvctrl_test.go @@ -0,0 +1,78 @@ +/* +DESCRIPTION + gvctrl_test.go provides tests of functionality in the gvctrl package. + +AUTHORS + Saxon A. Nelson-Milton + +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) + } + } +}