| 1 | // Copyright 2018 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 types |
| 16 | |
| 17 | import ( |
| 18 | "errors" |
| 19 | "math" |
| 20 | "reflect" |
| 21 | "strings" |
| 22 | "testing" |
| 23 | |
| 24 | "google.golang.org/protobuf/proto" |
| 25 | |
| 26 | "github.com/google/cel-go/common/types/ref" |
| 27 | "github.com/google/cel-go/common/types/traits" |
| 28 | |
| 29 | anypb "google.golang.org/protobuf/types/known/anypb" |
| 30 | structpb "google.golang.org/protobuf/types/known/structpb" |
| 31 | wrapperspb "google.golang.org/protobuf/types/known/wrapperspb" |
| 32 | ) |
| 33 | |
| 34 | func TestUintAdd(t *testing.T) { |
| 35 | if !Uint(4).Add(Uint(3)).Equal(Uint(7)).(Bool) { |
| 36 | t.Error("Adding two uints did not match expected value.") |
| 37 | } |
| 38 | if !IsError(Uint(1).Add(String("-1"))) { |
| 39 | t.Error("Adding non-uint to uint was not an error.") |
| 40 | } |
| 41 | if lhs, rhs := uint64(math.MaxUint64), 1; !IsError(Uint(lhs).Add(Uint(rhs))) { |
| 42 | t.Errorf("Expected adding %d and %d to result in overflow.", lhs, rhs) |
| 43 | } |
| 44 | if lhs, rhs := uint64(math.MaxUint64-1), 1; !Uint(lhs).Add(Uint(rhs)).Equal(Uint(math.MaxUint64)).(Bool) { |
| 45 | t.Errorf("Expected adding %d and %d to yield %d", lhs, rhs, uint64(math.MaxUint64)) |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | func TestUintCompare(t *testing.T) { |
| 50 | tests := []struct { |
| 51 | a ref.Val |
| 52 | b ref.Val |
| 53 | out ref.Val |
| 54 | }{ |
| 55 | { |
| 56 | a: Uint(42), |
| 57 | b: Uint(42), |
| 58 | out: IntZero, |
| 59 | }, |
| 60 | { |
| 61 | a: Uint(42), |
| 62 | b: Int(42), |
| 63 | out: IntZero, |
| 64 | }, |
| 65 | { |
| 66 | a: Uint(42), |
| 67 | b: Double(42), |
| 68 | out: IntZero, |
| 69 | }, |
| 70 | { |
| 71 | a: Uint(13), |
| 72 | b: Int(204), |
| 73 | out: IntNegOne, |
| 74 | }, |
| 75 | { |
| 76 | a: Uint(13), |
| 77 | b: Uint(204), |
| 78 | out: IntNegOne, |
| 79 | }, |
| 80 | { |
| 81 | a: Uint(204), |
| 82 | b: Double(204.1), |
| 83 | out: IntNegOne, |
| 84 | }, |
| 85 | { |
| 86 | a: Uint(204), |
| 87 | b: Int(205), |
| 88 | out: IntNegOne, |
| 89 | }, |
| 90 | { |
| 91 | a: Uint(204), |
| 92 | b: Double(math.MaxUint64) + 2049.0, |
| 93 | out: IntNegOne, |
| 94 | }, |
| 95 | { |
| 96 | a: Uint(204), |
| 97 | b: Double(math.NaN()), |
| 98 | out: NewErr("NaN values cannot be ordered"), |
| 99 | }, |
| 100 | { |
| 101 | a: Uint(1300), |
| 102 | b: Int(-1), |
| 103 | out: IntOne, |
| 104 | }, |
| 105 | { |
| 106 | a: Uint(204), |
| 107 | b: Uint(13), |
| 108 | out: IntOne, |
| 109 | }, |
| 110 | { |
| 111 | a: Uint(204), |
| 112 | b: Double(203.9), |
| 113 | out: IntOne, |
| 114 | }, |
| 115 | { |
| 116 | a: Uint(204), |
| 117 | b: Double(-1.0), |
| 118 | out: IntOne, |
| 119 | }, |
| 120 | { |
| 121 | a: Uint(1), |
| 122 | b: String("1"), |
| 123 | out: NoSuchOverloadErr(), |
| 124 | }, |
| 125 | } |
| 126 | for _, tc := range tests { |
| 127 | comparer := tc.a.(traits.Comparer) |
| 128 | got := comparer.Compare(tc.b) |
| 129 | if !reflect.DeepEqual(got, tc.out) { |
| 130 | t.Errorf("%v.Compare(%v) got %v, wanted %v", tc.a, tc.b, got, tc.out) |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | func TestUintConvertToNative_Any(t *testing.T) { |
| 136 | val, err := Uint(math.MaxUint64).ConvertToNative(anyValueType) |
| 137 | if err != nil { |
| 138 | t.Error(err) |
| 139 | } |
| 140 | want, err := anypb.New(wrapperspb.UInt64(math.MaxUint64)) |
| 141 | if err != nil { |
| 142 | t.Error(err) |
| 143 | } |
| 144 | if !proto.Equal(val.(proto.Message), want) { |
| 145 | t.Errorf("Got %v, wanted %v", val, want) |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | func TestUintConvertToNative_Error(t *testing.T) { |
| 150 | val, err := Uint(10000).ConvertToNative(reflect.TypeOf(int(0))) |
| 151 | if err == nil { |
| 152 | t.Errorf("Got '%v', expected error", val) |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | func TestUintConvertToNative_Json(t *testing.T) { |
| 157 | // Value can be represented accurately as a JSON number. |
| 158 | val, err := Uint(maxIntJSON).ConvertToNative(JSONValueType) |
| 159 | if err != nil { |
| 160 | t.Error(err) |
| 161 | } else if !proto.Equal(val.(proto.Message), |
| 162 | structpb.NewNumberValue(9007199254740991.0)) { |
| 163 | t.Errorf("Got '%v', expected a json number for a 32-bit uint", val) |
| 164 | } |
| 165 | |
| 166 | // Value converts to a JSON decimal string |
| 167 | val, err = Uint(maxIntJSON + 1).ConvertToNative(JSONValueType) |
| 168 | if err != nil { |
| 169 | t.Error(err) |
| 170 | } else if !proto.Equal(val.(proto.Message), structpb.NewStringValue("9007199254740992")) { |
| 171 | t.Errorf("Got '%v', expected a json string for a 64-bit uint", val) |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | func TestUintConvertToNative_Uint8(t *testing.T) { |
| 176 | val, err := Uint(128).ConvertToNative(reflect.TypeOf(uint8(0))) |
| 177 | if err != nil { |
| 178 | t.Fatalf("Uint.ConvertToNative(uint8) failed: %v", err) |
| 179 | } |
| 180 | if val.(uint8) != 128 { |
| 181 | t.Errorf("Got '%v', expected 128", val) |
| 182 | } |
| 183 | val, err = Uint(math.MaxUint8 + 1).ConvertToNative(reflect.TypeOf(uint8(0))) |
| 184 | if err == nil { |
| 185 | t.Errorf("(MaxUint+1).ConvertToNative(uint8) did not error, got: %v", val) |
| 186 | } else if !strings.Contains(err.Error(), "unsigned integer overflow") { |
| 187 | t.Errorf("ConvertToNative(uint8) returned unexpected error: %v, wanted unsigned integer overflow", err) |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | func TestUintConvertToNative_Uint16(t *testing.T) { |
| 192 | val, err := Uint(20050).ConvertToNative(reflect.TypeOf(uint16(0))) |
| 193 | if err != nil { |
| 194 | t.Fatalf("Uint.ConvertToNative(uint16) failed: %v", err) |
| 195 | } |
| 196 | if val.(uint16) != 20050 { |
| 197 | t.Errorf("Got '%v', expected 20050", val) |
| 198 | } |
| 199 | val, err = Uint(math.MaxUint16 + 1).ConvertToNative(reflect.TypeOf(uint16(0))) |
| 200 | if err == nil { |
| 201 | t.Errorf("(MaxUint+1).ConvertToNative(uint16) did not error, got: %v", val) |
| 202 | } else if !strings.Contains(err.Error(), "unsigned integer overflow") { |
| 203 | t.Errorf("ConvertToNative(uint16) returned unexpected error: %v, wanted unsigned integer overflow", err) |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | func TestUintConvertToNative_Uint32(t *testing.T) { |
| 208 | val, err := Uint(20050).ConvertToNative(reflect.TypeOf(uint32(0))) |
| 209 | if err != nil { |
| 210 | t.Fatalf("Uint.ConvertToNative(uint32) failed: %v", err) |
| 211 | } |
| 212 | if val.(uint32) != 20050 { |
| 213 | t.Errorf("Got '%v', expected 20050", val) |
| 214 | } |
| 215 | val, err = Uint(math.MaxUint32 + 1).ConvertToNative(reflect.TypeOf(uint32(0))) |
| 216 | if err == nil { |
| 217 | t.Errorf("(MaxUint+1).ConvertToNative(uint32) did not error, got: %v", val) |
| 218 | } else if !strings.Contains(err.Error(), "unsigned integer overflow") { |
| 219 | t.Errorf("ConvertToNative(uint32) returned unexpected error: %v, wanted unsigned integer overflow", err) |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | func TestUintConvertToNative_Ptr_Uint32(t *testing.T) { |
| 224 | ptrType := uint32(0) |
| 225 | val, err := Uint(10000).ConvertToNative(reflect.TypeOf(&ptrType)) |
| 226 | if err != nil { |
| 227 | t.Error(err) |
| 228 | } else if *val.(*uint32) != uint32(10000) { |
| 229 | t.Errorf("Error converting uint to *uint32. Got '%v', expected 10000.", val) |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | func TestUintConvertToNative_Ptr_Uint64(t *testing.T) { |
| 234 | ptrType := uint64(0) |
| 235 | val, err := Uint(18446744073709551612).ConvertToNative(reflect.TypeOf(&ptrType)) |
| 236 | if err != nil { |
| 237 | t.Error(err) |
| 238 | } else if *val.(*uint64) != uint64(18446744073709551612) { |
| 239 | t.Errorf("Error converting uint to *uint64. Got '%v', expected 18446744073709551612.", val) |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | func TestUintConvertToNative_Wrapper(t *testing.T) { |
| 244 | val, err := Uint(math.MaxUint32).ConvertToNative(uint32WrapperType) |
| 245 | if err != nil { |
| 246 | t.Error(err) |
| 247 | } |
| 248 | want := wrapperspb.UInt32(math.MaxUint32) |
| 249 | if !proto.Equal(val.(proto.Message), want) { |
| 250 | t.Errorf("Got %v, wanted %v", val, want) |
| 251 | } |
| 252 | |
| 253 | val, err = Uint(math.MaxUint64).ConvertToNative(uint64WrapperType) |
| 254 | if err != nil { |
| 255 | t.Error(err) |
| 256 | } |
| 257 | want2 := wrapperspb.UInt64(math.MaxUint64) |
| 258 | if !proto.Equal(val.(proto.Message), want2) { |
| 259 | t.Errorf("Got %v, wanted %v", val, want2) |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | func TestUintConvertToType(t *testing.T) { |
| 264 | tests := []struct { |
| 265 | name string |
| 266 | in uint64 |
| 267 | toType ref.Type |
| 268 | out any |
| 269 | }{ |
| 270 | { |
| 271 | name: "UintToUint", |
| 272 | in: uint64(4), |
| 273 | toType: UintType, |
| 274 | out: uint64(4), |
| 275 | }, |
| 276 | { |
| 277 | name: "UintToType", |
| 278 | in: uint64(4), |
| 279 | toType: TypeType, |
| 280 | out: UintType.TypeName(), |
| 281 | }, |
| 282 | { |
| 283 | name: "UintToInt", |
| 284 | in: uint64(4), |
| 285 | toType: IntType, |
| 286 | out: int64(4), |
| 287 | }, |
| 288 | { |
| 289 | name: "UintToIntOverflow", |
| 290 | in: uint64(math.MaxInt64) + 1, |
| 291 | toType: IntType, |
| 292 | out: errIntOverflow, |
| 293 | }, |
| 294 | { |
| 295 | name: "UintToDouble", |
| 296 | in: uint64(4), |
| 297 | toType: DoubleType, |
| 298 | out: float64(4), |
| 299 | }, |
| 300 | { |
| 301 | name: "UintToString", |
| 302 | in: uint64(4), |
| 303 | toType: StringType, |
| 304 | out: "4", |
| 305 | }, |
| 306 | { |
| 307 | name: "UintToUnsupportedType", |
| 308 | in: uint64(4), |
| 309 | toType: MapType, |
| 310 | out: errors.New("type conversion error"), |
| 311 | }, |
| 312 | } |
| 313 | for _, tst := range tests { |
| 314 | got := Uint(tst.in).ConvertToType(tst.toType).Value() |
| 315 | var eq bool |
| 316 | switch gotVal := got.(type) { |
| 317 | case error: |
| 318 | eq = strings.Contains(gotVal.Error(), tst.out.(error).Error()) |
| 319 | default: |
| 320 | eq = reflect.DeepEqual(gotVal, tst.out) |
| 321 | } |
| 322 | if !eq { |
| 323 | t.Errorf("Uint(%v).ConvertToType(%v) failed, got: %v, wanted: %v", |
| 324 | tst.in, tst.toType, got, tst.out) |
| 325 | } |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | func TestUintDivide(t *testing.T) { |
| 330 | if !Uint(3).Divide(Uint(2)).Equal(Uint(1)).(Bool) { |
| 331 | t.Error("Dividing two uints did not match expectations.") |
| 332 | } |
| 333 | if !IsError(uintZero.Divide(uintZero)) { |
| 334 | t.Error("Divide by zero did not cause error.") |
| 335 | } |
| 336 | if !IsError(Uint(1).Divide(Double(-1))) { |
| 337 | t.Error("Division permitted without express type-conversion.") |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | func TestUintEqual(t *testing.T) { |
| 342 | tests := []struct { |
| 343 | a ref.Val |
| 344 | b ref.Val |
| 345 | out ref.Val |
| 346 | }{ |
| 347 | { |
| 348 | a: Uint(10), |
| 349 | b: Uint(10), |
| 350 | out: True, |
| 351 | }, |
| 352 | { |
| 353 | a: Uint(10), |
| 354 | b: Int(-10), |
| 355 | out: False, |
| 356 | }, |
| 357 | { |
| 358 | a: Uint(10), |
| 359 | b: Int(10), |
| 360 | out: True, |
| 361 | }, |
| 362 | { |
| 363 | a: Uint(9), |
| 364 | b: Int(10), |
| 365 | out: False, |
| 366 | }, |
| 367 | { |
| 368 | a: Uint(10), |
| 369 | b: Double(10), |
| 370 | out: True, |
| 371 | }, |
| 372 | { |
| 373 | a: Uint(10), |
| 374 | b: Double(-10.5), |
| 375 | out: False, |
| 376 | }, |
| 377 | { |
| 378 | a: Uint(10), |
| 379 | b: Double(math.NaN()), |
| 380 | out: False, |
| 381 | }, |
| 382 | } |
| 383 | for _, tc := range tests { |
| 384 | got := tc.a.Equal(tc.b) |
| 385 | if !reflect.DeepEqual(got, tc.out) { |
| 386 | t.Errorf("%v.Equal(%v) got %v, wanted %v", tc.a, tc.b, got, tc.out) |
| 387 | } |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | func TestUintIsZeroValue(t *testing.T) { |
| 392 | if Uint(1).IsZeroValue() { |
| 393 | t.Error("Uint(1).IsZeroValue() returned true, wanted false.") |
| 394 | } |
| 395 | if !Uint(0).IsZeroValue() { |
| 396 | t.Error("Uint(0).IsZeroValue() returned false, wanted true") |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | func TestUintModulo(t *testing.T) { |
| 401 | if !Uint(21).Modulo(Uint(2)).Equal(Uint(1)).(Bool) { |
| 402 | t.Error("Unexpected result from modulus operator.") |
| 403 | } |
| 404 | if !IsError(Uint(21).Modulo(uintZero)) { |
| 405 | t.Error("Modulus by zero did not cause error.") |
| 406 | } |
| 407 | if !IsError(Uint(21).Modulo(IntOne)) { |
| 408 | t.Error("Modulus permitted between different types without type conversion.") |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | func TestUintMultiply(t *testing.T) { |
| 413 | if !Uint(2).Multiply(Uint(2)).Equal(Uint(4)).(Bool) { |
| 414 | t.Error("Multiplying two values did not match expectations.") |
| 415 | } |
| 416 | if !IsError(Uint(1).Multiply(Double(-4.0))) { |
| 417 | t.Error("Multiplication permitted without express type-conversion.") |
| 418 | } |
| 419 | if lhs, rhs := uint64(math.MaxUint64/2), 3; !IsError(Uint(lhs).Multiply(Uint(rhs))) { |
| 420 | t.Errorf("Expected multiplying %d and %d to result in overflow.", lhs, rhs) |
| 421 | } |
| 422 | if lhs, rhs := uint64(math.MaxUint64/2), 2; !Uint(lhs).Multiply(Uint(rhs)).Equal(Uint(uint64(math.MaxUint64 - 1))).(Bool) { |
| 423 | t.Errorf("Expected multiplying %d and %d to yield %d", lhs, rhs, uint64(math.MaxUint64-1)) |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | func TestUintSubtract(t *testing.T) { |
| 428 | if !Uint(4).Subtract(Uint(3)).Equal(Uint(1)).(Bool) { |
| 429 | t.Error("Subtracting two uints did not match expected value.") |
| 430 | } |
| 431 | if !IsError(Uint(1).Subtract(Int(1))) { |
| 432 | t.Error("Subtraction permitted without express type-conversion.") |
| 433 | } |
| 434 | if lhs, rhs := uint64(math.MaxUint64-1), uint64(math.MaxUint64); !IsError(Uint(lhs).Subtract(Uint(rhs))) { |
| 435 | t.Errorf("Expected subtracting %d and %d to result in overflow.", lhs, rhs) |
| 436 | } |
| 437 | if lhs, rhs := uint64(math.MaxUint64), uint64(math.MaxUint64); !Uint(lhs).Subtract(Uint(rhs)).Equal(Uint(0)).(Bool) { |
| 438 | t.Errorf("Expected subtracting %d and %d to yield %d", lhs, rhs, 0) |
| 439 | } |
| 440 | } |