| 1 | // Copyright 2022 Google LLC |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package ext |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "strings" |
| 20 | "testing" |
| 21 | |
| 22 | "github.com/google/cel-go/cel" |
| 23 | "github.com/google/cel-go/common/types" |
| 24 | ) |
| 25 | |
| 26 | func TestMath(t *testing.T) { |
| 27 | mathTests := []struct { |
| 28 | expr string |
| 29 | in any |
| 30 | }{ |
| 31 | // Tests for math.least |
| 32 | {expr: "math.least(-0.5) == -0.5"}, |
| 33 | {expr: "math.least(-1) == -1"}, |
| 34 | {expr: "math.least(1u) == 1u"}, |
| 35 | {expr: "math.least(42.0, -0.5) == -0.5"}, |
| 36 | {expr: "math.least(-1, 0) == -1"}, |
| 37 | {expr: "math.least(-1, -1) == -1"}, |
| 38 | {expr: "math.least(1u, 42u) == 1u"}, |
| 39 | {expr: "math.least(42.0, -0.5, -0.25) == -0.5"}, |
| 40 | {expr: "math.least(-1, 0, 1) == -1"}, |
| 41 | {expr: "math.least(-1, -1, -1) == -1"}, |
| 42 | {expr: "math.least(1u, 42u, 0u) == 0u"}, |
| 43 | // math.least two arg overloads across type. |
| 44 | {expr: "math.least(1, 1.0) == 1"}, |
| 45 | {expr: "math.least(1, -2.0) == -2.0"}, |
| 46 | {expr: "math.least(2, 1u) == 1u"}, |
| 47 | {expr: "math.least(1.5, 2) == 1.5"}, |
| 48 | {expr: "math.least(1.5, -2) == -2"}, |
| 49 | {expr: "math.least(2.5, 1u) == 1u"}, |
| 50 | {expr: "math.least(1u, 2) == 1u"}, |
| 51 | {expr: "math.least(1u, -2) == -2"}, |
| 52 | {expr: "math.least(2u, 2.5) == 2u"}, |
| 53 | // math.least with dynamic values across type. |
| 54 | {expr: "math.least(1u, dyn(42)) == 1"}, |
| 55 | {expr: "math.least(1u, dyn(42), dyn(0.0)) == 0u"}, |
| 56 | // math.least with a list literal. |
| 57 | {expr: "math.least([1u, 42u, 0u]) == 0u"}, |
| 58 | // math.least with expression arguments. |
| 59 | { |
| 60 | expr: "math.least(a, b) == a", |
| 61 | in: map[string]any{ |
| 62 | "a": 1, |
| 63 | "b": 2, |
| 64 | }, |
| 65 | }, |
| 66 | { |
| 67 | expr: "math.least(numbers) == dyn(a)", |
| 68 | in: map[string]any{ |
| 69 | "a": -21, |
| 70 | "numbers": []float64{-21.0, -10.5, 1.0}, |
| 71 | }, |
| 72 | }, |
| 73 | |
| 74 | // Tests for math.greatest |
| 75 | {expr: "math.greatest(-0.5) == -0.5"}, |
| 76 | {expr: "math.greatest(-1) == -1"}, |
| 77 | {expr: "math.greatest(1u) == 1u"}, |
| 78 | {expr: "math.greatest(42.0, -0.5) == 42.0"}, |
| 79 | {expr: "math.greatest(-1, 0) == 0"}, |
| 80 | {expr: "math.greatest(-1, -1) == -1"}, |
| 81 | {expr: "math.greatest(1u, 42u) == 42u"}, |
| 82 | {expr: "math.greatest(42.0, -0.5, -0.25) == 42.0"}, |
| 83 | {expr: "math.greatest(-1, 0, 1) == 1"}, |
| 84 | {expr: "math.greatest(-1, -1, -1) == -1"}, |
| 85 | {expr: "math.greatest(1u, 42u, 0u) == 42u"}, |
| 86 | // math.greatest two arg overloads across type. |
| 87 | {expr: "math.greatest(1, 1.0) == 1"}, |
| 88 | {expr: "math.greatest(1, -2.0) == 1"}, |
| 89 | {expr: "math.greatest(2, 1u) == 2"}, |
| 90 | {expr: "math.greatest(1.5, 2) == 2"}, |
| 91 | {expr: "math.greatest(1.5, -2) == 1.5"}, |
| 92 | {expr: "math.greatest(2.5, 1u) == 2.5"}, |
| 93 | {expr: "math.greatest(1u, 2) == 2"}, |
| 94 | {expr: "math.greatest(1u, -2) == 1u"}, |
| 95 | {expr: "math.greatest(2u, 2.5) == 2.5"}, |
| 96 | // math.greatest with dynamic values across type. |
| 97 | {expr: "math.greatest(1u, dyn(42)) == 42.0"}, |
| 98 | {expr: "math.greatest(1u, dyn(0.0), 0u) == 1"}, |
| 99 | // math.greatest with a list literal |
| 100 | {expr: "math.greatest([1u, dyn(0.0), 0u]) == 1"}, |
| 101 | // math.greatest with expression arguments. |
| 102 | { |
| 103 | expr: "math.greatest(a, b) == b", |
| 104 | in: map[string]any{ |
| 105 | "a": 1, |
| 106 | "b": 2, |
| 107 | }, |
| 108 | }, |
| 109 | { |
| 110 | expr: "math.greatest(numbers) == dyn(a)", |
| 111 | in: map[string]any{ |
| 112 | "a": 1, |
| 113 | "numbers": []float64{-21.0, -10.5, 1.0}, |
| 114 | }, |
| 115 | }, |
| 116 | |
| 117 | // Tests for math bitwise operators |
| 118 | // Signed bitwise ops |
| 119 | {expr: "math.bitAnd(1, 2) == 0"}, |
| 120 | {expr: "math.bitAnd(1, -1) == 1"}, |
| 121 | {expr: "math.bitAnd(1, 3) == 1"}, |
| 122 | {expr: "math.bitOr(1, 2) == 3"}, |
| 123 | {expr: "math.bitXor(1, 3) == 2"}, |
| 124 | {expr: "math.bitXor(3, 5) == 6"}, |
| 125 | {expr: "math.bitNot(1) == -2"}, |
| 126 | {expr: "math.bitNot(0) == -1"}, |
| 127 | {expr: "math.bitNot(-1) == 0"}, |
| 128 | {expr: "math.bitShiftLeft(1, 2) == 4"}, |
| 129 | {expr: "math.bitShiftLeft(1, 200) == 0"}, |
| 130 | {expr: "math.bitShiftLeft(-1, 200) == 0"}, |
| 131 | {expr: "math.bitShiftRight(1024, 2) == 256"}, |
| 132 | {expr: "math.bitShiftRight(1024, 64) == 0"}, |
| 133 | {expr: "math.bitShiftRight(-1024, 3) == 2305843009213693824"}, |
| 134 | {expr: "math.bitShiftRight(-1024, 64) == 0"}, |
| 135 | // Unsigned bitwise ops |
| 136 | {expr: "math.bitAnd(1u, 2u) == 0u"}, |
| 137 | {expr: "math.bitAnd(1u, 3u) == 1u"}, |
| 138 | {expr: "math.bitOr(1u, 2u) == 3u"}, |
| 139 | {expr: "math.bitXor(1u, 3u) == 2u"}, |
| 140 | {expr: "math.bitXor(3u, 5u) == 6u"}, |
| 141 | {expr: "math.bitNot(1u) == 18446744073709551614u"}, |
| 142 | {expr: "math.bitNot(0u) == 18446744073709551615u"}, |
| 143 | {expr: "math.bitShiftLeft(1u, 2) == 4u"}, |
| 144 | {expr: "math.bitShiftLeft(1u, 200) == 0u"}, |
| 145 | {expr: "math.bitShiftRight(1024u, 2) == 256u"}, |
| 146 | {expr: "math.bitShiftRight(1024u, 64) == 0u"}, |
| 147 | |
| 148 | // Tests for floating point helpers |
| 149 | {expr: "math.isNaN(0.0/0.0)"}, |
| 150 | {expr: "!math.isNaN(1.0/0.0)"}, |
| 151 | {expr: "math.isFinite(1.0/1.5)"}, |
| 152 | {expr: "!math.isFinite(1.0/0.0)"}, |
| 153 | {expr: "math.isInf(1.0/0.0)"}, |
| 154 | |
| 155 | // Tests for rounding functions |
| 156 | {expr: "math.ceil(1.2) == 2.0"}, |
| 157 | {expr: "math.ceil(-1.2) == -1.0"}, |
| 158 | {expr: "math.floor(1.2) == 1.0"}, |
| 159 | {expr: "math.floor(-1.2) == -2.0"}, |
| 160 | {expr: "math.round(1.2) == 1.0"}, |
| 161 | {expr: "math.round(1.5) == 2.0"}, |
| 162 | {expr: "math.round(-1.5) == -2.0"}, |
| 163 | {expr: "math.isNaN(math.round(0.0/0.0))"}, |
| 164 | {expr: "math.round(-1.2) == -1.0"}, |
| 165 | {expr: "math.trunc(-1.3) == -1.0"}, |
| 166 | {expr: "math.trunc(1.3) == 1.0"}, |
| 167 | |
| 168 | // Tests for signedness related functions |
| 169 | {expr: "math.sign(-42) == -1"}, |
| 170 | {expr: "math.sign(0) == 0"}, |
| 171 | {expr: "math.sign(42) == 1"}, |
| 172 | {expr: "math.sign(0u) == 0u"}, |
| 173 | {expr: "math.sign(42u) == 1u"}, |
| 174 | {expr: "math.sign(-0.3) == -1.0"}, |
| 175 | {expr: "math.sign(0.0) == 0.0"}, |
| 176 | {expr: "math.isNaN(math.sign(0.0/0.0))"}, |
| 177 | {expr: "math.sign(1.0/0.0) == 1.0"}, |
| 178 | {expr: "math.sign(-1.0/0.0) == -1.0"}, |
| 179 | {expr: "math.sign(0.3) == 1.0"}, |
| 180 | {expr: "math.abs(-1) == 1"}, |
| 181 | {expr: "math.abs(1) == 1"}, |
| 182 | {expr: "math.abs(-234.5) == 234.5"}, |
| 183 | {expr: "math.abs(234.5) == 234.5"}, |
| 184 | |
| 185 | // Tests for Square root function |
| 186 | {expr: "math.sqrt(49.0) == 7.0"}, |
| 187 | {expr: "math.sqrt(0) == 0.0"}, |
| 188 | {expr: "math.sqrt(1) == 1.0"}, |
| 189 | {expr: "math.sqrt(25u) == 5.0"}, |
| 190 | {expr: "math.sqrt(82) == 9.055385138137417"}, |
| 191 | {expr: "math.sqrt(985.25) == 31.388692231439016"}, |
| 192 | {expr: "math.isNaN(math.sqrt(-15.34))"}, |
| 193 | } |
| 194 | |
| 195 | env := testMathEnv(t, |
| 196 | cel.Variable("a", cel.IntType), |
| 197 | cel.Variable("b", cel.IntType), |
| 198 | cel.Variable("numbers", cel.ListType(cel.DoubleType)), |
| 199 | ) |
| 200 | for i, tst := range mathTests { |
| 201 | tc := tst |
| 202 | t.Run(fmt.Sprintf("%d", i), func(t *testing.T) { |
| 203 | var asts []*cel.Ast |
| 204 | pAst, iss := env.Parse(tc.expr) |
| 205 | if iss.Err() != nil { |
| 206 | t.Fatalf("env.Parse(%v) failed: %v", tc.expr, iss.Err()) |
| 207 | } |
| 208 | asts = append(asts, pAst) |
| 209 | cAst, iss := env.Check(pAst) |
| 210 | if iss.Err() != nil { |
| 211 | t.Fatalf("env.Check(%v) failed: %v", tc.expr, iss.Err()) |
| 212 | } |
| 213 | asts = append(asts, cAst) |
| 214 | |
| 215 | for _, ast := range asts { |
| 216 | prg, err := env.Program(ast) |
| 217 | if err != nil { |
| 218 | t.Fatalf("env.Program() failed: %v", err) |
| 219 | } |
| 220 | in := tc.in |
| 221 | if in == nil { |
| 222 | in = cel.NoVars() |
| 223 | } |
| 224 | out, _, err := prg.Eval(in) |
| 225 | if err != nil { |
| 226 | t.Fatalf("prg.Eval() failed: %v", err) |
| 227 | } |
| 228 | if out.Value() != true { |
| 229 | t.Errorf("prg.Eval() got %v, wanted true for expr: %s", out.Value(), tc.expr) |
| 230 | } |
| 231 | } |
| 232 | }) |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | func TestMathStaticErrors(t *testing.T) { |
| 237 | mathTests := []struct { |
| 238 | expr string |
| 239 | err string |
| 240 | }{ |
| 241 | // Tests for math.least |
| 242 | { |
| 243 | expr: "math.least()", |
| 244 | err: "math.least() requires at least one argument", |
| 245 | }, |
| 246 | { |
| 247 | expr: "math.least('hello')", |
| 248 | err: "math.least() invalid single argument value", |
| 249 | }, |
| 250 | { |
| 251 | expr: "math.least({})", |
| 252 | err: "math.least() invalid single argument value", |
| 253 | }, |
| 254 | { |
| 255 | expr: "math.least(1, true)", |
| 256 | err: "math.least() simple literal arguments must be numeric", |
| 257 | }, |
| 258 | { |
| 259 | expr: "math.least(1, 2, true)", |
| 260 | err: "math.least() simple literal arguments must be numeric", |
| 261 | }, |
| 262 | |
| 263 | // Tests for math.greatest |
| 264 | { |
| 265 | expr: "math.greatest()", |
| 266 | err: "math.greatest() requires at least one argument", |
| 267 | }, |
| 268 | { |
| 269 | expr: "math.greatest(true)", |
| 270 | err: "math.greatest() invalid single argument value", |
| 271 | }, |
| 272 | { |
| 273 | expr: "math.greatest([])", |
| 274 | err: "math.greatest() invalid single argument value", |
| 275 | }, |
| 276 | { |
| 277 | expr: "math.greatest([1, true])", |
| 278 | err: "math.greatest() invalid single argument value", |
| 279 | }, |
| 280 | { |
| 281 | expr: "math.greatest(1, true)", |
| 282 | err: "math.greatest() simple literal arguments must be numeric", |
| 283 | }, |
| 284 | { |
| 285 | expr: "math.greatest(1, 2, true)", |
| 286 | err: "math.greatest() simple literal arguments must be numeric", |
| 287 | }, |
| 288 | } |
| 289 | |
| 290 | env := testMathEnv(t) |
| 291 | for i, tst := range mathTests { |
| 292 | tc := tst |
| 293 | t.Run(fmt.Sprintf("%d", i), func(t *testing.T) { |
| 294 | _, iss := env.Compile(tc.expr) |
| 295 | if iss.Err() == nil || !strings.Contains(iss.Err().Error(), tc.err) { |
| 296 | t.Errorf("env.Compile(%q) got %v, wanted error %v", tc.expr, iss.Err(), tc.err) |
| 297 | } |
| 298 | }) |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | func TestMathRuntimeErrors(t *testing.T) { |
| 303 | mathTests := []struct { |
| 304 | expr string |
| 305 | err string |
| 306 | in any |
| 307 | }{ |
| 308 | // Tests for math.least |
| 309 | { |
| 310 | expr: "math.least(a, b)", |
| 311 | err: "no such overload: math.@min", |
| 312 | in: map[string]any{ |
| 313 | "a": []int{}, |
| 314 | "b": 1, |
| 315 | }, |
| 316 | }, |
| 317 | { |
| 318 | expr: "math.least(b, a)", |
| 319 | err: "no such overload: math.@min", |
| 320 | in: map[string]any{ |
| 321 | "a": []int{}, |
| 322 | "b": 1, |
| 323 | }, |
| 324 | }, |
| 325 | { |
| 326 | expr: "math.least(a)", |
| 327 | err: "math.@min(list) argument must not be empty", |
| 328 | in: map[string]any{ |
| 329 | "a": []int{}, |
| 330 | }, |
| 331 | }, |
| 332 | { |
| 333 | expr: "math.least(a)", |
| 334 | err: "no such overload: math.@min", |
| 335 | in: map[string]any{ |
| 336 | "a": []any{"hello"}, |
| 337 | }, |
| 338 | }, |
| 339 | { |
| 340 | expr: "math.least(a)", |
| 341 | err: "no such overload: math.@min", |
| 342 | in: map[string]any{ |
| 343 | "a": []any{[]int{}, []int{}}, |
| 344 | }, |
| 345 | }, |
| 346 | { |
| 347 | expr: "math.least(a)", |
| 348 | err: "no such overload: math.@min", |
| 349 | in: map[string]any{ |
| 350 | "a": []any{1, true, 2}, |
| 351 | }, |
| 352 | }, |
| 353 | { |
| 354 | expr: "math.least(dyn('string'))", |
| 355 | err: "no such overload: math.@min", |
| 356 | }, |
| 357 | |
| 358 | // Tests for math.greatest |
| 359 | { |
| 360 | expr: "math.greatest(a, b)", |
| 361 | err: "no such overload: math.@max", |
| 362 | in: map[string]any{ |
| 363 | "a": []int{}, |
| 364 | "b": 1, |
| 365 | }, |
| 366 | }, |
| 367 | { |
| 368 | expr: "math.greatest(b, a)", |
| 369 | err: "no such overload: math.@max", |
| 370 | in: map[string]any{ |
| 371 | "a": []int{}, |
| 372 | "b": 1, |
| 373 | }, |
| 374 | }, |
| 375 | { |
| 376 | expr: "math.greatest(a)", |
| 377 | err: "math.@max(list) argument must not be empty", |
| 378 | in: map[string]any{ |
| 379 | "a": []int{}, |
| 380 | }, |
| 381 | }, |
| 382 | { |
| 383 | expr: "math.greatest(a)", |
| 384 | err: "no such overload: math.@max", |
| 385 | in: map[string]any{ |
| 386 | "a": []any{true}, |
| 387 | }, |
| 388 | }, |
| 389 | { |
| 390 | expr: "math.greatest(a)", |
| 391 | err: "no such overload: math.@max", |
| 392 | in: map[string]any{ |
| 393 | "a": []any{1, true, 2}, |
| 394 | }, |
| 395 | }, |
| 396 | { |
| 397 | expr: "math.greatest(dyn('string'))", |
| 398 | err: "no such overload: math.@max", |
| 399 | }, |
| 400 | { |
| 401 | expr: "math.bitShiftLeft(1, -2) == 4", |
| 402 | err: "math.bitShiftLeft() negative offset", |
| 403 | }, |
| 404 | { |
| 405 | expr: "math.bitShiftLeft(1u, -2) == 0u", |
| 406 | err: "math.bitShiftLeft() negative offset", |
| 407 | }, |
| 408 | { |
| 409 | expr: "math.bitShiftRight(-1024, -3) == -128", |
| 410 | err: "math.bitShiftRight() negative offset", |
| 411 | }, |
| 412 | { |
| 413 | expr: "math.bitShiftRight(1024u, -4) == 1u", |
| 414 | err: "math.bitShiftRight() negative offset", |
| 415 | }, |
| 416 | { |
| 417 | expr: "math.abs(-9223372036854775808)", |
| 418 | err: "overflow", |
| 419 | }, |
| 420 | { |
| 421 | expr: "math.bitOr(dyn(1.2), 1)", |
| 422 | err: "no such overload: math.bitOr(double, int)", |
| 423 | }, |
| 424 | { |
| 425 | expr: "math.bitAnd(2u, dyn(''))", |
| 426 | err: "no such overload: math.bitAnd(uint, string)", |
| 427 | }, |
| 428 | { |
| 429 | expr: "math.bitXor(dyn(1), dyn(1u))", |
| 430 | err: "no such overload: math.bitXor(int, uint)", |
| 431 | }, |
| 432 | { |
| 433 | expr: "math.bitXor(dyn([]), dyn([1]))", |
| 434 | err: "no such overload: math.bitXor(list, list)", |
| 435 | }, |
| 436 | { |
| 437 | expr: "math.bitNot(dyn([1]))", |
| 438 | err: "no such overload: math.bitNot(list)", |
| 439 | }, |
| 440 | { |
| 441 | expr: "math.bitShiftLeft(dyn([1]), 1)", |
| 442 | err: "no such overload: math.bitShiftLeft(list, int)", |
| 443 | }, |
| 444 | { |
| 445 | expr: "math.bitShiftRight(dyn({}), 1)", |
| 446 | err: "no such overload: math.bitShiftRight(map, int)", |
| 447 | }, |
| 448 | { |
| 449 | expr: "math.isInf(dyn(1u))", |
| 450 | err: "no such overload: math.isInf(uint)", |
| 451 | }, |
| 452 | { |
| 453 | expr: "math.isFinite(dyn(1u))", |
| 454 | err: "no such overload: math.isFinite(uint)", |
| 455 | }, |
| 456 | { |
| 457 | expr: "math.isNaN(dyn(1u))", |
| 458 | err: "no such overload: math.isNaN(uint)", |
| 459 | }, |
| 460 | { |
| 461 | expr: "math.sign(dyn(''))", |
| 462 | err: "no such overload: math.sign(string)", |
| 463 | }, |
| 464 | { |
| 465 | expr: "math.abs(dyn(''))", |
| 466 | err: "no such overload: math.abs(string)", |
| 467 | }, |
| 468 | { |
| 469 | expr: "math.ceil(dyn(''))", |
| 470 | err: "no such overload: math.ceil(string)", |
| 471 | }, |
| 472 | { |
| 473 | expr: "math.floor(dyn(''))", |
| 474 | err: "no such overload: math.floor(string)", |
| 475 | }, |
| 476 | { |
| 477 | expr: "math.round(dyn(1))", |
| 478 | err: "no such overload: math.round(int)", |
| 479 | }, |
| 480 | { |
| 481 | expr: "math.trunc(dyn(1u))", |
| 482 | err: "no such overload: math.trunc(uint)", |
| 483 | }, |
| 484 | { |
| 485 | expr: "math.sqrt(dyn(''))", |
| 486 | err: "no such overload: math.sqrt(string)", |
| 487 | }, |
| 488 | } |
| 489 | |
| 490 | env := testMathEnv(t, |
| 491 | cel.Variable("a", cel.DynType), |
| 492 | cel.Variable("b", cel.IntType), |
| 493 | cel.Variable("numbers", cel.ListType(cel.DoubleType)), |
| 494 | ) |
| 495 | for i, tst := range mathTests { |
| 496 | tc := tst |
| 497 | t.Run(fmt.Sprintf("%d", i), func(t *testing.T) { |
| 498 | ast, iss := env.Compile(tc.expr) |
| 499 | if iss.Err() != nil { |
| 500 | t.Fatalf("env.Compile(%q) failed with error %v", tc.expr, iss.Err()) |
| 501 | } |
| 502 | prg, err := env.Program(ast) |
| 503 | if err != nil { |
| 504 | t.Fatalf("env.Program(ast) failed: %v", err) |
| 505 | } |
| 506 | in := tc.in |
| 507 | if in == nil { |
| 508 | in = cel.NoVars() |
| 509 | } |
| 510 | _, _, err = prg.Eval(in) |
| 511 | if err == nil || !strings.Contains(err.Error(), tc.err) { |
| 512 | t.Errorf("prg.Eval() got %v, wanted %v", err, tc.err) |
| 513 | } |
| 514 | }) |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | func TestMathNonMatch(t *testing.T) { |
| 519 | var mathTests = []struct { |
| 520 | expr string |
| 521 | }{ |
| 522 | // Even though 'least' is the macro, the call is left unexpanded since the operand is not 'math'. |
| 523 | { |
| 524 | expr: `100.least(42) == 42`, |
| 525 | }, |
| 526 | // Even though 'greatest' is the macro, the call is left unexpanded since the operand is not 'math'. |
| 527 | { |
| 528 | expr: `100.greatest(42) == 100`, |
| 529 | }, |
| 530 | } |
| 531 | env := testMathEnv(t, |
| 532 | cel.Function("greatest", |
| 533 | cel.MemberOverload("int_greatest_int", []*cel.Type{cel.IntType, cel.IntType}, cel.IntType, |
| 534 | cel.BinaryBinding(maxPair))), |
| 535 | cel.Function("least", |
| 536 | cel.MemberOverload("int_least_int", []*cel.Type{cel.IntType, cel.IntType}, cel.IntType, |
| 537 | cel.BinaryBinding(minPair))), |
| 538 | ) |
| 539 | for i, tst := range mathTests { |
| 540 | tc := tst |
| 541 | t.Run(fmt.Sprintf("[%d]", i), func(t *testing.T) { |
| 542 | var asts []*cel.Ast |
| 543 | pAst, iss := env.Parse(tc.expr) |
| 544 | if iss.Err() != nil { |
| 545 | t.Fatalf("env.Parse(%v) failed: %v", tc.expr, iss.Err()) |
| 546 | } |
| 547 | asts = append(asts, pAst) |
| 548 | cAst, iss := env.Check(pAst) |
| 549 | if iss.Err() != nil { |
| 550 | t.Fatalf("env.Check(%v) failed: %v", tc.expr, iss.Err()) |
| 551 | } |
| 552 | asts = append(asts, cAst) |
| 553 | |
| 554 | for _, ast := range asts { |
| 555 | prg, err := env.Program(ast) |
| 556 | if err != nil { |
| 557 | t.Fatalf("env.Program() failed: %v", err) |
| 558 | } |
| 559 | out, _, err := prg.Eval(cel.NoVars()) |
| 560 | if err != nil { |
| 561 | t.Fatalf("prg.Eval() failed: %v", err) |
| 562 | } |
| 563 | if out.Value() != true { |
| 564 | t.Errorf("prg.Eval() got %v, wanted true for expr: %s", out.Value(), tc.expr) |
| 565 | } |
| 566 | } |
| 567 | }) |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | func TestMathWithExtension(t *testing.T) { |
| 572 | env := testMathEnv(t) |
| 573 | _, err := env.Extend(Math()) |
| 574 | if err != nil { |
| 575 | t.Fatalf("env.Extend(Math()) failed: %v", err) |
| 576 | } |
| 577 | _, iss := env.Compile("math.least(0, 1, 2) == 0") |
| 578 | if iss.Err() != nil { |
| 579 | t.Errorf("env.Compile() failed: %v", iss.Err()) |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | func TestMathVersions(t *testing.T) { |
| 584 | versionCases := []struct { |
| 585 | version uint32 |
| 586 | supportedFunctions map[string]string |
| 587 | }{ |
| 588 | { |
| 589 | version: 0, |
| 590 | supportedFunctions: map[string]string{ |
| 591 | "greatest": `math.greatest(1, 2) == 2`, |
| 592 | "least": `math.least(2.1, -1.0) == -1.0`, |
| 593 | }, |
| 594 | }, |
| 595 | { |
| 596 | version: 1, |
| 597 | supportedFunctions: map[string]string{ |
| 598 | "ceil": `math.ceil(1.5) == 2.0`, |
| 599 | "floor": `math.floor(1.2) == 1.0`, |
| 600 | "round": `math.round(1.5) == 2.0`, |
| 601 | "trunc": `math.trunc(1.222) == 1.0`, |
| 602 | "isInf": `!math.isInf(0.0)`, |
| 603 | "isNaN": `math.isNaN(0.0/0.0)`, |
| 604 | "isFinite": `math.isFinite(0.0)`, |
| 605 | "abs": `math.abs(1.2) == 1.2`, |
| 606 | "sign": `math.sign(-1) == -1`, |
| 607 | "bitAnd": `math.bitAnd(1, 2) == 0`, |
| 608 | "bitOr": `math.bitOr(1, 2) == 3`, |
| 609 | "bitXor": `math.bitXor(1, 3) == 2`, |
| 610 | "bitNot": `math.bitNot(-1) == 0`, |
| 611 | "bitShiftLeft": `math.bitShiftLeft(4, 2) == 16`, |
| 612 | "bitShiftRight": `math.bitShiftRight(4, 2) == 1`, |
| 613 | }, |
| 614 | }, |
| 615 | { |
| 616 | version: 2, |
| 617 | supportedFunctions: map[string]string{ |
| 618 | "sqrt": `math.sqrt(25) == 5.0`, |
| 619 | }, |
| 620 | }, |
| 621 | } |
| 622 | for _, lib := range versionCases { |
| 623 | env, err := cel.NewEnv(Math(MathVersion(lib.version))) |
| 624 | if err != nil { |
| 625 | t.Fatalf("cel.NewEnv(Math(MathVersion(%d))) failed: %v", lib.version, err) |
| 626 | } |
| 627 | t.Run(fmt.Sprintf("version=%d", lib.version), func(t *testing.T) { |
| 628 | for _, tc := range versionCases { |
| 629 | for name, expr := range tc.supportedFunctions { |
| 630 | supported := lib.version >= tc.version |
| 631 | t.Run(fmt.Sprintf("%s-supported=%t", name, supported), func(t *testing.T) { |
| 632 | ast, iss := env.Compile(expr) |
| 633 | if supported { |
| 634 | if iss.Err() != nil { |
| 635 | t.Errorf("unexpected error: %v", iss.Err()) |
| 636 | } |
| 637 | } else { |
| 638 | if iss.Err() == nil || !strings.Contains(iss.Err().Error(), "undeclared reference") { |
| 639 | t.Errorf("got error %v, wanted error %s for expr: %s, version: %d", iss.Err(), "undeclared reference", expr, tc.version) |
| 640 | } |
| 641 | return |
| 642 | } |
| 643 | prg, err := env.Program(ast) |
| 644 | if err != nil { |
| 645 | t.Fatalf("env.Program() failed: %v", err) |
| 646 | } |
| 647 | out, _, err := prg.Eval(cel.NoVars()) |
| 648 | if err != nil { |
| 649 | t.Fatalf("prg.Eval() failed: %v", err) |
| 650 | } |
| 651 | if out != types.True { |
| 652 | t.Errorf("prg.Eval() got %v, wanted true", out) |
| 653 | } |
| 654 | }) |
| 655 | } |
| 656 | } |
| 657 | }) |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | func testMathEnv(t *testing.T, opts ...cel.EnvOption) *cel.Env { |
| 662 | t.Helper() |
| 663 | baseOpts := []cel.EnvOption{Math(), cel.EnableMacroCallTracking()} |
| 664 | env, err := cel.NewEnv(append(baseOpts, opts...)...) |
| 665 | if err != nil { |
| 666 | t.Fatalf("cel.NewEnv(Math()) failed: %v", err) |
| 667 | } |
| 668 | return env |
| 669 | } |