github.com/hashicorp/terraform-plugin-sdk@v1.17.2/internal/lang/funcs/number_test.go (about) 1 package funcs 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/zclconf/go-cty/cty" 8 ) 9 10 func TestCeil(t *testing.T) { 11 tests := []struct { 12 Num cty.Value 13 Want cty.Value 14 Err bool 15 }{ 16 { 17 cty.NumberFloatVal(-1.8), 18 cty.NumberFloatVal(-1), 19 false, 20 }, 21 { 22 cty.NumberFloatVal(1.2), 23 cty.NumberFloatVal(2), 24 false, 25 }, 26 } 27 28 for _, test := range tests { 29 t.Run(fmt.Sprintf("ceil(%#v)", test.Num), func(t *testing.T) { 30 got, err := Ceil(test.Num) 31 32 if test.Err { 33 if err == nil { 34 t.Fatal("succeeded; want error") 35 } 36 return 37 } else if err != nil { 38 t.Fatalf("unexpected error: %s", err) 39 } 40 41 if !got.RawEquals(test.Want) { 42 t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, test.Want) 43 } 44 }) 45 } 46 } 47 48 func TestFloor(t *testing.T) { 49 tests := []struct { 50 Num cty.Value 51 Want cty.Value 52 Err bool 53 }{ 54 { 55 cty.NumberFloatVal(-1.8), 56 cty.NumberFloatVal(-2), 57 false, 58 }, 59 { 60 cty.NumberFloatVal(1.2), 61 cty.NumberFloatVal(1), 62 false, 63 }, 64 } 65 66 for _, test := range tests { 67 t.Run(fmt.Sprintf("floor(%#v)", test.Num), func(t *testing.T) { 68 got, err := Floor(test.Num) 69 70 if test.Err { 71 if err == nil { 72 t.Fatal("succeeded; want error") 73 } 74 return 75 } else if err != nil { 76 t.Fatalf("unexpected error: %s", err) 77 } 78 79 if !got.RawEquals(test.Want) { 80 t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, test.Want) 81 } 82 }) 83 } 84 } 85 86 func TestLog(t *testing.T) { 87 tests := []struct { 88 Num cty.Value 89 Base cty.Value 90 Want cty.Value 91 Err bool 92 }{ 93 { 94 cty.NumberFloatVal(1), 95 cty.NumberFloatVal(10), 96 cty.NumberFloatVal(0), 97 false, 98 }, 99 { 100 cty.NumberFloatVal(10), 101 cty.NumberFloatVal(10), 102 cty.NumberFloatVal(1), 103 false, 104 }, 105 106 { 107 cty.NumberFloatVal(0), 108 cty.NumberFloatVal(10), 109 cty.NegativeInfinity, 110 false, 111 }, 112 { 113 cty.NumberFloatVal(10), 114 cty.NumberFloatVal(0), 115 cty.NumberFloatVal(-0), 116 false, 117 }, 118 } 119 120 for _, test := range tests { 121 t.Run(fmt.Sprintf("log(%#v, %#v)", test.Num, test.Base), func(t *testing.T) { 122 got, err := Log(test.Num, test.Base) 123 124 if test.Err { 125 if err == nil { 126 t.Fatal("succeeded; want error") 127 } 128 return 129 } else if err != nil { 130 t.Fatalf("unexpected error: %s", err) 131 } 132 133 if !got.RawEquals(test.Want) { 134 t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, test.Want) 135 } 136 }) 137 } 138 } 139 140 func TestPow(t *testing.T) { 141 tests := []struct { 142 Num cty.Value 143 Power cty.Value 144 Want cty.Value 145 Err bool 146 }{ 147 { 148 cty.NumberFloatVal(1), 149 cty.NumberFloatVal(0), 150 cty.NumberFloatVal(1), 151 false, 152 }, 153 { 154 cty.NumberFloatVal(1), 155 cty.NumberFloatVal(1), 156 cty.NumberFloatVal(1), 157 false, 158 }, 159 160 { 161 cty.NumberFloatVal(2), 162 cty.NumberFloatVal(0), 163 cty.NumberFloatVal(1), 164 false, 165 }, 166 { 167 cty.NumberFloatVal(2), 168 cty.NumberFloatVal(1), 169 cty.NumberFloatVal(2), 170 false, 171 }, 172 { 173 cty.NumberFloatVal(3), 174 cty.NumberFloatVal(2), 175 cty.NumberFloatVal(9), 176 false, 177 }, 178 { 179 cty.NumberFloatVal(-3), 180 cty.NumberFloatVal(2), 181 cty.NumberFloatVal(9), 182 false, 183 }, 184 { 185 cty.NumberFloatVal(2), 186 cty.NumberFloatVal(-2), 187 cty.NumberFloatVal(0.25), 188 false, 189 }, 190 { 191 cty.NumberFloatVal(0), 192 cty.NumberFloatVal(2), 193 cty.NumberFloatVal(0), 194 false, 195 }, 196 } 197 198 for _, test := range tests { 199 t.Run(fmt.Sprintf("pow(%#v, %#v)", test.Num, test.Power), func(t *testing.T) { 200 got, err := Pow(test.Num, test.Power) 201 202 if test.Err { 203 if err == nil { 204 t.Fatal("succeeded; want error") 205 } 206 return 207 } else if err != nil { 208 t.Fatalf("unexpected error: %s", err) 209 } 210 211 if !got.RawEquals(test.Want) { 212 t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, test.Want) 213 } 214 }) 215 } 216 } 217 218 func TestSignum(t *testing.T) { 219 tests := []struct { 220 Num cty.Value 221 Want cty.Value 222 Err bool 223 }{ 224 { 225 cty.NumberFloatVal(0), 226 cty.NumberFloatVal(0), 227 false, 228 }, 229 { 230 cty.NumberFloatVal(12), 231 cty.NumberFloatVal(1), 232 false, 233 }, 234 { 235 cty.NumberFloatVal(-29), 236 cty.NumberFloatVal(-1), 237 false, 238 }, 239 } 240 241 for _, test := range tests { 242 t.Run(fmt.Sprintf("signum(%#v)", test.Num), func(t *testing.T) { 243 got, err := Signum(test.Num) 244 245 if test.Err { 246 if err == nil { 247 t.Fatal("succeeded; want error") 248 } 249 return 250 } else if err != nil { 251 t.Fatalf("unexpected error: %s", err) 252 } 253 254 if !got.RawEquals(test.Want) { 255 t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, test.Want) 256 } 257 }) 258 } 259 } 260 261 func TestParseInt(t *testing.T) { 262 tests := []struct { 263 Num cty.Value 264 Base cty.Value 265 Want cty.Value 266 Err bool 267 }{ 268 { 269 cty.StringVal("128"), 270 cty.NumberIntVal(10), 271 cty.NumberIntVal(128), 272 false, 273 }, 274 { 275 cty.StringVal("-128"), 276 cty.NumberIntVal(10), 277 cty.NumberIntVal(-128), 278 false, 279 }, 280 { 281 cty.StringVal("00128"), 282 cty.NumberIntVal(10), 283 cty.NumberIntVal(128), 284 false, 285 }, 286 { 287 cty.StringVal("-00128"), 288 cty.NumberIntVal(10), 289 cty.NumberIntVal(-128), 290 false, 291 }, 292 { 293 cty.StringVal("FF00"), 294 cty.NumberIntVal(16), 295 cty.NumberIntVal(65280), 296 false, 297 }, 298 { 299 cty.StringVal("ff00"), 300 cty.NumberIntVal(16), 301 cty.NumberIntVal(65280), 302 false, 303 }, 304 { 305 cty.StringVal("-FF00"), 306 cty.NumberIntVal(16), 307 cty.NumberIntVal(-65280), 308 false, 309 }, 310 { 311 cty.StringVal("00FF00"), 312 cty.NumberIntVal(16), 313 cty.NumberIntVal(65280), 314 false, 315 }, 316 { 317 cty.StringVal("-00FF00"), 318 cty.NumberIntVal(16), 319 cty.NumberIntVal(-65280), 320 false, 321 }, 322 { 323 cty.StringVal("1011111011101111"), 324 cty.NumberIntVal(2), 325 cty.NumberIntVal(48879), 326 false, 327 }, 328 { 329 cty.StringVal("aA"), 330 cty.NumberIntVal(62), 331 cty.NumberIntVal(656), 332 false, 333 }, 334 { 335 cty.StringVal("Aa"), 336 cty.NumberIntVal(62), 337 cty.NumberIntVal(2242), 338 false, 339 }, 340 { 341 cty.StringVal("999999999999999999999999999999999999999999999999999999999999"), 342 cty.NumberIntVal(10), 343 cty.MustParseNumberVal("999999999999999999999999999999999999999999999999999999999999"), 344 false, 345 }, 346 { 347 cty.StringVal("FF"), 348 cty.NumberIntVal(10), 349 cty.UnknownVal(cty.Number), 350 true, 351 }, 352 { 353 cty.StringVal("00FF"), 354 cty.NumberIntVal(10), 355 cty.UnknownVal(cty.Number), 356 true, 357 }, 358 { 359 cty.StringVal("-00FF"), 360 cty.NumberIntVal(10), 361 cty.UnknownVal(cty.Number), 362 true, 363 }, 364 { 365 cty.NumberIntVal(2), 366 cty.NumberIntVal(10), 367 cty.UnknownVal(cty.Number), 368 true, 369 }, 370 { 371 cty.StringVal("1"), 372 cty.NumberIntVal(63), 373 cty.UnknownVal(cty.Number), 374 true, 375 }, 376 { 377 cty.StringVal("1"), 378 cty.NumberIntVal(-1), 379 cty.UnknownVal(cty.Number), 380 true, 381 }, 382 { 383 cty.StringVal("1"), 384 cty.NumberIntVal(1), 385 cty.UnknownVal(cty.Number), 386 true, 387 }, 388 { 389 cty.StringVal("1"), 390 cty.NumberIntVal(0), 391 cty.UnknownVal(cty.Number), 392 true, 393 }, 394 { 395 cty.StringVal("1.2"), 396 cty.NumberIntVal(10), 397 cty.UnknownVal(cty.Number), 398 true, 399 }, 400 } 401 402 for _, test := range tests { 403 t.Run(fmt.Sprintf("parseint(%#v, %#v)", test.Num, test.Base), func(t *testing.T) { 404 got, err := ParseInt(test.Num, test.Base) 405 406 if test.Err { 407 if err == nil { 408 t.Fatal("succeeded; want error") 409 } 410 return 411 } else if err != nil { 412 t.Fatalf("unexpected error: %s", err) 413 } 414 415 if !got.RawEquals(test.Want) { 416 t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, test.Want) 417 } 418 }) 419 } 420 }