/* 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 ( "errors" "net/url" "reflect" "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) } } } func TestConvRate(t *testing.T) { tests := []struct { l []int v int want string }{ { l: []int{512, 1024, 2048, 3072}, v: 1400, want: "1024000", }, { l: []int{512, 1024, 2048, 3072}, v: 1900, want: "2048000", }, { l: []int{512, 1024, 2048, 3072}, v: 4000, want: "3072000", }, } for i, test := range tests { got := convRate(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) } } } func TestHeight(t *testing.T) { tests := []struct { h int want settings err error }{ { h: 256, want: settings{res: "4480256"}, }, { h: 360, want: settings{res: "6400360"}, }, { h: 720, want: settings{res: "12800720"}, }, { h: 500, want: settings{}, err: errors.New(""), }, } for i, test := range tests { s := settings{} got, err := Height(test.h)(s) if test.err == nil && err != nil || test.err != nil && err == nil { t.Errorf("did not get expected error: %v", test.err) } if got != test.want { t.Errorf("did not get expected result for test: %d\nGot: %v\nWant: %v", i, got, test.want) } } } func TestVBRBitrate(t *testing.T) { tests := []struct { r int in settings want settings }{ { r: 300, in: settings{vbr: "1"}, want: settings{ vbr: "1", vbrBitrate: "250000", }, }, { r: 400, in: settings{vbr: "1"}, want: settings{ vbr: "1", vbrBitrate: "500000", }, }, } for i, test := range tests { got, _ := VBRBitrate(test.r)(test.in) if got != test.want { t.Errorf("did not get expected result for test: %d\nGot: %v\nWant: %v", i, got, test.want) } } } func TestCBRBitrate(t *testing.T) { tests := []struct { r int in settings want settings }{ { r: 600, in: settings{ vbr: "0", res: res256, }, want: settings{ vbr: "0", res: res256, cbrBitrate: "512000", }, }, { r: 100, in: settings{ vbr: "0", res: res256, }, want: settings{ vbr: "0", res: res256, cbrBitrate: "128000", }, }, { r: 2048, in: settings{ vbr: "0", res: res360, }, want: settings{ vbr: "0", res: res360, cbrBitrate: "2048000", }, }, { r: 500, in: settings{ vbr: "0", res: res720, }, want: settings{ vbr: "0", res: res720, cbrBitrate: "1024000", }, }, } for i, test := range tests { got, _ := CBRBitrate(test.r)(test.in) if got != test.want { t.Errorf("did not get expected result for test: %d\nGot: %v\nWant: %v", i, got, test.want) } } } func TestRefresh(t *testing.T) { tests := []struct { r float64 want settings err error }{ { r: 1.6, want: settings{refresh: "1500"}, }, { r: 2.4, want: settings{refresh: "2500"}, }, { r: 0, want: settings{}, err: errors.New(""), }, { r: 6, want: settings{}, err: errors.New(""), }, } for i, test := range tests { s := settings{} got, err := Refresh(test.r)(s) if test.err == nil && err != nil || test.err != nil && err == nil { t.Errorf("did not get expected error: %v", test.err) } if got != test.want { t.Errorf("did not get expected result for test: %d\nGot: %v\nWant: %v", i, got, test.want) } } } func TestPopulateForm(t *testing.T) { tests := []struct { in settings want string }{ { in: newSettings(), want: "dwConnType=5&mpeg_type=10&dwflicker_hz=0&szResolution=6400360&dwFrameRate=25000&vbr_enable=0&max_bit_rate=512000&custom_rate_control_type=0&custom_bitrate=0&custom_qp_init=25&custom_qp_min=10&custom_qp_max=40&gop_N=2000&dwEncProfile=1&dwEncLevel=31&dwEntropy=0&u8PreAlarmBuf=1&u32PostAlarmBuf2Disk=1&u8SplitInterval=5&bEnableIO=1&bEbIoIn=1&bEbIoIn1=1&bOSDFontSize=0&bEnableOSDCameraName=1&bCamNamePos=2&bEnableOSDDate=1&bDatePos=2&bEnableOSDTime=1&bTimePos=2&szOsdCamName=Camera&u16PostAlarmBuf=1&dwCameraId=1&LangCode=undefined&Recflag=0&submit=Apply", }, { in: settings{ codec: CodecH265, res: defaultRes, frameRate: defaultFrameRate, vbr: defaultVBR, quality: defaultQuality, vbrBitrate: defaultVBRBitrate, cbrBitrate: defaultCBRBitrate, refresh: defaultRefresh, }, want: "dwConnType=5&mpeg_type=28&dwflicker_hz=0&szResolution=6400360&dwFrameRate=25000&vbr_enable=0&max_bit_rate=512000&custom_rate_control_type=0&custom_bitrate=0&custom_qp_init=25&custom_qp_min=10&custom_qp_max=40&gop_N=2000&dwEncProfile=1&dwEncLevel=31&dwEntropy=0&u8PreAlarmBuf=1&u32PostAlarmBuf2Disk=1&u8SplitInterval=5&bEnableIO=1&bEbIoIn=1&bEbIoIn1=1&bOSDFontSize=0&bEnableOSDCameraName=1&bCamNamePos=2&bEnableOSDDate=1&bDatePos=2&bEnableOSDTime=1&bTimePos=2&szOsdCamName=Camera&u16PostAlarmBuf=1&dwCameraId=1&LangCode=undefined&Recflag=0&submit=Apply", }, { in: settings{ codec: CodecMJPEG, res: defaultRes, frameRate: defaultFrameRate, vbr: defaultVBR, quality: defaultQuality, vbrBitrate: defaultVBRBitrate, cbrBitrate: defaultCBRBitrate, refresh: defaultRefresh, }, want: "dwConnType=5&mpeg_type=4&dwflicker_hz=0&szResolution=6400360&dwFrameRate=25000&vbr_enable=1&dwVbrQuality=2&vbrmaxbitrate=500000&custom_qp_init=25&gop_N=1500&u8PreAlarmBuf=1&u32PostAlarmBuf2Disk=1&u8SplitInterval=5&bEnableIO=1&bEbIoIn=1&bEbIoIn1=1&bOSDFontSize=0&bEnableOSDCameraName=1&bCamNamePos=2&bEnableOSDDate=1&bDatePos=2&bEnableOSDTime=1&bTimePos=2&szOsdCamName=Camera&u16PostAlarmBuf=1&dwCameraId=1&LangCode=undefined&Recflag=0&submit=Apply", }, { in: settings{ codec: CodecH264, res: defaultRes, frameRate: defaultFrameRate, vbr: "1", quality: defaultQuality, vbrBitrate: defaultVBRBitrate, cbrBitrate: defaultCBRBitrate, refresh: defaultRefresh, }, want: "dwConnType=5&mpeg_type=10&dwflicker_hz=0&szResolution=6400360&dwFrameRate=25000&vbr_enable=1&dwVbrQuality=2&vbrmaxbitrate=250000&custom_rate_control_type=0&custom_bitrate=0&custom_qp_init=25&custom_qp_min=10&custom_qp_max=40&gop_N=2000&dwEncProfile=1&dwEncLevel=31&dwEntropy=0&u8PreAlarmBuf=1&u32PostAlarmBuf2Disk=1&u8SplitInterval=5&bEnableIO=1&bEbIoIn=1&bEbIoIn1=1&bOSDFontSize=0&bEnableOSDCameraName=1&bCamNamePos=2&bEnableOSDDate=1&bDatePos=2&bEnableOSDTime=1&bTimePos=2&szOsdCamName=Camera&u16PostAlarmBuf=1&dwCameraId=1&LangCode=undefined&Recflag=0&submit=Apply", }, } for i, test := range tests { got := populateForm(test.in) want, err := url.ParseQuery(test.want) if err != nil { t.Fatalf("should not have got error: %v parsing want string for test: %d", err, i) } if !reflect.DeepEqual(got, want) { t.Errorf("did not get expected result for test: %d\nGot: %v\nWant: %v", i, got, want) } } }