考试测试代码
package main import ( "fmt" "strconv" "strings" "testing" ) type testStruct1 = struct { testId int inputStr inputStruct want string } type inputStruct struct { input1 string input2 []int } func InArray(needle interface{}, hyStack interface{}) bool { switch key := needle.(type) { case string: for _, item := range hyStack.([]string) { if key == item { return true } } case int: for _, item := range hyStack.([]int) { if key == item { return true } } case int64: for _, item := range hyStack.([]int64) { if key == item { return true } } default: return false } return false } /** * 将输入快速转化成go数组,并输出到屏幕,复制后即可使用 * [[1,2,3],[4,5,6]] -> [][]int{{1,2,3},{4,5,6}} * * @param input 输入 * @param dimension 数组维度,eg:二级数组->2 */ func handToArray(allowType string, input string, dimension int) string { // 支持的类型 var allowedTypes = []string{ "string", "int", "long", "double", "float", "char", "boolean", "short", "byte", } if !InArray(allowType, allowedTypes) { fmt.Println("can not this type to array") return "" } var sb strings.Builder var leftOut = strings.Replace(input, "[", "{", -1) var rightOut = strings.Replace(leftOut, "]", "}", -1) for i := 0; i < dimension; i++ { sb.WriteString("[]") } sb.WriteString(allowType) sb.WriteString(rightOut) fmt.Println(sb.String()) return sb.String() } func TestHandToArray(t *testing.T) { fmt.Println(handToArray("int", "[7, 4]", 1)) } func TestSomeFunction(t *testing.T) { tests1 := []testStruct1{ { testId: 1, inputStr: inputStruct{ "5", []int{7, 4}, }, want: "3", }, { testId: 2, inputStr: inputStruct{ "5", []int{7, 4}, }, want: "4", }, } for _, tt := range tests1 { inputInt, _ := strconv.Atoi(tt.inputStr.input1) got := getMinLoad(inputInt, tt.inputStr.input2) //gotstr := "" //for _, val := range got { // gotstr += strconv.Itoa(val) //} want, _ := strconv.Atoi(tt.want) fmt.Println(got, want) if got != want { t.Errorf("testId:(%v), getMinLoad(%v) = %v, want %v", tt.testId, tt.inputStr, got, tt.want) } else { t.Logf("testId:(%v) passed", tt.testId) } } }
本文来自博客园,作者:易先讯,转载请注明原文链接:https://www.cnblogs.com/gongxianjin/p/17236512.html