k8s.io/test-infra/triage@v0.0.0-20240520184403-27c6b4c223d8/utils/utils_test.go (about) 1 /* 2 Copyright 2020 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package utils 18 19 import ( 20 "fmt" 21 "testing" 22 ) 23 24 func TestMin(t *testing.T) { 25 testCases := []struct { 26 name string 27 arguments []int 28 want int 29 }{ 30 {"One value", []int{1}, 1}, 31 {"Two values", []int{1, 2}, 1}, 32 {"Three values", []int{1, 2, 3}, 1}, 33 {"Three values reordered", []int{2, 1, 3}, 1}, 34 {"Negative values", []int{-1, -2}, -2}, 35 } 36 37 for _, tc := range testCases { 38 t.Run(tc.name, func(t *testing.T) { 39 got := Min(tc.arguments...) 40 41 if got != tc.want { 42 // Format the function arguments nicely 43 formattedArgs := fmt.Sprintf("%d", tc.arguments[0]) 44 for _, arg := range tc.arguments[1:] { 45 formattedArgs = fmt.Sprintf("%s, %d", formattedArgs, arg) 46 } 47 48 t.Errorf("Min(%s) = %d, wanted %d", formattedArgs, got, tc.want) 49 } 50 }) 51 } 52 } 53 54 func TestMax(t *testing.T) { 55 testCases := []struct { 56 name string 57 arguments []int 58 want int 59 }{ 60 {"One value", []int{1}, 1}, 61 {"Two values", []int{1, 2}, 2}, 62 {"Three values", []int{1, 2, 3}, 3}, 63 {"Three values reordered", []int{3, 1, 2}, 3}, 64 {"Negative values", []int{-1, -2}, -1}, 65 } 66 67 for _, tc := range testCases { 68 t.Run(tc.name, func(t *testing.T) { 69 got := Max(tc.arguments...) 70 71 if got != tc.want { 72 // Format the function arguments nicely 73 formattedArgs := fmt.Sprintf("%d", tc.arguments[0]) 74 for _, arg := range tc.arguments[1:] { 75 formattedArgs = fmt.Sprintf("%s, %d", formattedArgs, arg) 76 } 77 78 t.Errorf("Max(%s) = %d, wanted %d", formattedArgs, got, tc.want) 79 } 80 }) 81 } 82 } 83 84 func TestAbs(t *testing.T) { 85 testCases := []struct { 86 name string 87 argument int 88 want int 89 }{ 90 {"Negative", -1, 1}, 91 {"Positive", 1, 1}, 92 {"Zero", 0, 0}, 93 } 94 95 for _, tc := range testCases { 96 t.Run(tc.name, func(t *testing.T) { 97 got := Abs(tc.argument) 98 99 if got != tc.want { 100 t.Errorf("Abs(%d) = %d; wanted %d", tc.argument, got, tc.want) 101 } 102 }) 103 } 104 } 105 106 func TestBtoI(t *testing.T) { 107 testCases := []struct { 108 name string 109 argument bool 110 want int 111 }{ 112 {"True", true, 1}, 113 {"False", false, 0}, 114 } 115 116 for _, tc := range testCases { 117 t.Run(tc.name, func(t *testing.T) { 118 got := BtoI(tc.argument) 119 120 if got != tc.want { 121 t.Errorf("BtoI(%t) = %d, wanted %d", tc.argument, got, tc.want) 122 } 123 }) 124 } 125 } 126 127 func TestByteSliceInsert(t *testing.T) { 128 t.Run("Normal slice", func(t *testing.T) { 129 s := []byte{'1', '3', '4'} 130 ByteSliceInsert(&s, '2', 1) 131 132 for i, element := range s { 133 // We want a slice of ['1' '2' '3' '4'] 134 if element != byte(48+i+1) { // U+0030 (48 decimal) is '0', add 1 to make it equal the current element 135 t.Errorf("s[%d] = %q; wanted %d", i, element, i+1) 136 } 137 } 138 }) 139 140 t.Run("Empty slice", func(t *testing.T) { 141 s := make([]byte, 0) 142 ByteSliceInsert(&s, '1', 0) 143 144 if s[0] != '1' { 145 t.Errorf("s[0] = %q; wanted %q", s[0], '1') 146 } 147 }) 148 } 149 150 func TestRemoveDuplicateLines(t *testing.T) { 151 testCases := []struct { 152 name string 153 argument string 154 want string 155 }{ 156 {"No duplicates", "this\nis\nmultiline\nstring", "this\nis\nmultiline\nstring"}, 157 {"Duplicates", "this\nis\nis\nstring", "this\nis\nstring"}, 158 {"\\n at beginning", "\nthis\nis\nmultiline\nstring", "\nthis\nis\nmultiline\nstring"}, 159 {"\\n at end", "this\nis\nmultiline\nstring\n", "this\nis\nmultiline\nstring\n"}, 160 {"No \\n", "this is multiline string", "this is multiline string"}, 161 {"Only one \\n", "\n", ""}, 162 {"Only two \\n", "\n\n", ""}, 163 {"Two \\n with space", "\n \n", "\n \n"}, 164 {"Empty string", "", ""}, 165 } 166 167 for _, tc := range testCases { 168 t.Run(tc.name, func(t *testing.T) { 169 got := RemoveDuplicateLines(tc.argument) 170 171 if got != tc.want { 172 t.Errorf("RemoveDuplicateLines(%#v) = %#v, wanted %#v", tc.argument, got, tc.want) 173 } 174 }) 175 } 176 }