| 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 TestDoubleAdd(t *testing.T) { |
| 35 |
if !Double(4).Add(Double(-3.5)).Equal(Double(0.5)).(Bool) { |
| 36 |
t.Error("Adding two doubles did not match expected value.") |
| 37 |
} |
| 38 |
if !IsError(Double(-1).Add(String("-1"))) { |
| 39 |
t.Error("Adding non-double to double was not an error.") |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
func TestDoubleCompare(t *testing.T) { |
| 44 |
tests := []struct { |
| 45 |
a ref.Val |
| 46 |
b ref.Val |
| 47 |
out ref.Val |
| 48 |
}{ |
| 49 |
{ |
| 50 |
a: Double(42), |
| 51 |
b: Double(42), |
| 52 |
out: IntZero, |
| 53 |
}, |
| 54 |
{ |
| 55 |
a: Double(42), |
| 56 |
b: Uint(42), |
| 57 |
out: IntZero, |
| 58 |
}, |
| 59 |
{ |
| 60 |
a: Double(42), |
| 61 |
b: Int(42), |
| 62 |
out: IntZero, |
| 63 |
}, |
| 64 |
{ |
| 65 |
a: Double(-1300), |
| 66 |
b: Double(204), |
| 67 |
out: IntNegOne, |
| 68 |
}, |
| 69 |
{ |
| 70 |
a: Double(-1300), |
| 71 |
b: Uint(204), |
| 72 |
out: IntNegOne, |
| 73 |
}, |
| 74 |
{ |
| 75 |
a: Double(203.9), |
| 76 |
b: Int(204), |
| 77 |
out: IntNegOne, |
| 78 |
}, |
| 79 |
{ |
| 80 |
a: Double(1300), |
| 81 |
b: Uint(math.MaxInt64) + 1, |
| 82 |
out: IntNegOne, |
| 83 |
}, |
| 84 |
{ |
| 85 |
a: Double(204), |
| 86 |
b: Uint(205), |
| 87 |
out: IntNegOne, |
| 88 |
}, |
| 89 |
{ |
| 90 |
a: Double(204), |
| 91 |
b: Double(math.MaxInt64) + 1025.0, |
| 92 |
out: IntNegOne, |
| 93 |
}, |
| 94 |
{ |
| 95 |
a: Double(204), |
| 96 |
b: Double(math.NaN()), |
| 97 |
out: NewErr("NaN values cannot be ordered"), |
| 98 |
}, |
| 99 |
{ |
| 100 |
a: Double(math.NaN()), |
| 101 |
b: Double(204), |
| 102 |
out: NewErr("NaN values cannot be ordered"), |
| 103 |
}, |
| 104 |
{ |
| 105 |
a: Double(204), |
| 106 |
b: Double(-1300), |
| 107 |
out: IntOne, |
| 108 |
}, |
| 109 |
{ |
| 110 |
a: Double(204), |
| 111 |
b: Uint(10), |
| 112 |
out: IntOne, |
| 113 |
}, |
| 114 |
{ |
| 115 |
a: Double(204.1), |
| 116 |
b: Int(204), |
| 117 |
out: IntOne, |
| 118 |
}, |
| 119 |
{ |
| 120 |
a: Double(1), |
| 121 |
b: String("1"), |
| 122 |
out: NoSuchOverloadErr(), |
| 123 |
}, |
| 124 |
} |
| 125 |
for _, tc := range tests { |
| 126 |
comparer := tc.a.(traits.Comparer) |
| 127 |
got := comparer.Compare(tc.b) |
| 128 |
if !reflect.DeepEqual(got, tc.out) { |
| 129 |
t.Errorf("%v.Compare(%v) got %v, wanted %v", tc.a, tc.b, got, tc.out) |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
func TestDoubleConvertToNative_Any(t *testing.T) { |
| 135 |
val, err := Double(math.MaxFloat64).ConvertToNative(anyValueType) |
| 136 |
if err != nil { |
| 137 |
t.Error(err) |
| 138 |
} |
| 139 |
want, err := anypb.New(wrapperspb.Double(1.7976931348623157e+308)) |
| 140 |
if err != nil { |
| 141 |
t.Error(err) |
| 142 |
} |
| 143 |
if !proto.Equal(val.(proto.Message), want) { |
| 144 |
t.Errorf("Got '%v', wanted %v", val, want) |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
func TestDoubleConvertToNative_Error(t *testing.T) { |
| 149 |
val, err := Double(-10000).ConvertToNative(reflect.TypeOf("")) |
| 150 |
if err == nil { |
| 151 |
t.Errorf("Got '%v', expected error", val) |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
func TestDoubleConvertToNative_Float32(t *testing.T) { |
| 156 |
val, err := Double(3.1415).ConvertToNative(reflect.TypeOf(float32(0))) |
| 157 |
if err != nil { |
| 158 |
t.Error(err) |
| 159 |
} else if val.(float32) != 3.1415 { |
| 160 |
t.Errorf("Got '%v', wanted 3.1415", val) |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
func TestDoubleConvertToNative_Float64(t *testing.T) { |
| 165 |
val, err := Double(30000000.1).ConvertToNative(reflect.TypeOf(float64(0))) |
| 166 |
if err != nil { |
| 167 |
t.Error(err) |
| 168 |
} else if val.(float64) != 30000000.1 { |
| 169 |
t.Errorf("Got '%v', wanted 330000000.1", val) |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
func TestDoubleConvertToNative_Json(t *testing.T) { |
| 174 |
val, err := Double(-1.4).ConvertToNative(JSONValueType) |
| 175 |
pbVal := structpb.NewNumberValue(-1.4) |
| 176 |
if err != nil { |
| 177 |
t.Error(err) |
| 178 |
} else if !proto.Equal(val.(proto.Message), pbVal) { |
| 179 |
t.Errorf("Got '%v', expected -1.4", val) |
| 180 |
} |
| 181 |
|
| 182 |
val, err = Double(math.NaN()).ConvertToNative(JSONValueType) |
| 183 |
if err != nil { |
| 184 |
t.Error(err) |
| 185 |
} else { |
| 186 |
v := val.(*structpb.Value) |
| 187 |
if !math.IsNaN(v.GetNumberValue()) { |
| 188 |
t.Errorf("Got '%v', expected NaN", val) |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
val, err = Double(math.Inf(-1)).ConvertToNative(JSONValueType) |
| 193 |
pbVal = structpb.NewNumberValue(math.Inf(-1)) |
| 194 |
if err != nil { |
| 195 |
t.Error(err) |
| 196 |
} else if !proto.Equal(val.(proto.Message), pbVal) { |
| 197 |
t.Errorf("Got '%v', expected -Infinity", val) |
| 198 |
} |
| 199 |
val, err = Double(math.Inf(0)).ConvertToNative(JSONValueType) |
| 200 |
pbVal = structpb.NewNumberValue(math.Inf(0)) |
| 201 |
if err != nil { |
| 202 |
t.Error(err) |
| 203 |
} else if !proto.Equal(val.(proto.Message), pbVal) { |
| 204 |
t.Errorf("Got '%v', expected Infinity", val) |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
func TestDoubleConvertToNative_Ptr_Float32(t *testing.T) { |
| 209 |
ptrType := float32(0) |
| 210 |
val, err := Double(3.1415).ConvertToNative(reflect.TypeOf(&ptrType)) |
| 211 |
if err != nil { |
| 212 |
t.Error(err) |
| 213 |
} else if *val.(*float32) != 3.1415 { |
| 214 |
t.Errorf("Got '%v', wanted 3.1415", val) |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
func TestDoubleConvertToNative_Ptr_Float64(t *testing.T) { |
| 219 |
ptrType := float64(0) |
| 220 |
val, err := Double(30000000.1).ConvertToNative(reflect.TypeOf(&ptrType)) |
| 221 |
if err != nil { |
| 222 |
t.Error(err) |
| 223 |
} else if *val.(*float64) != 30000000.1 { |
| 224 |
t.Errorf("Got '%v', wanted 330000000.1", val) |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
func TestDoubleConvertToNative_Wrapper(t *testing.T) { |
| 229 |
val, err := Double(3.1415).ConvertToNative(floatWrapperType) |
| 230 |
if err != nil { |
| 231 |
t.Error(err) |
| 232 |
} |
| 233 |
want := wrapperspb.Float(3.1415) |
| 234 |
if !proto.Equal(val.(proto.Message), want) { |
| 235 |
t.Errorf("Got '%v', wanted %v", val, want) |
| 236 |
} |
| 237 |
|
| 238 |
val, err = Double(math.MaxFloat64).ConvertToNative(doubleWrapperType) |
| 239 |
if err != nil { |
| 240 |
t.Error(err) |
| 241 |
} |
| 242 |
want2 := wrapperspb.Double(1.7976931348623157e+308) |
| 243 |
if !proto.Equal(val.(proto.Message), want2) { |
| 244 |
t.Errorf("Got '%v', wanted %v", val, want2) |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
func TestDoubleConvertToType(t *testing.T) { |
| 249 |
tests := []struct { |
| 250 |
name string |
| 251 |
in float64 |
| 252 |
toType ref.Type |
| 253 |
out any |
| 254 |
}{ |
| 255 |
{ |
| 256 |
name: "DoubleToDouble", |
| 257 |
in: float64(-4.2), |
| 258 |
toType: DoubleType, |
| 259 |
out: float64(-4.2), |
| 260 |
}, |
| 261 |
{ |
| 262 |
name: "DoubleToType", |
| 263 |
in: float64(-4.2), |
| 264 |
toType: TypeType, |
| 265 |
out: DoubleType.TypeName(), |
| 266 |
}, |
| 267 |
{ |
| 268 |
name: "DoubleToInt", |
| 269 |
in: float64(4.2), |
| 270 |
toType: IntType, |
| 271 |
out: int64(4), |
| 272 |
}, |
| 273 |
{ |
| 274 |
name: "DoubleToIntNaN", |
| 275 |
in: math.NaN(), |
| 276 |
toType: IntType, |
| 277 |
out: errIntOverflow, |
| 278 |
}, |
| 279 |
{ |
| 280 |
name: "DoubleToIntPosInf", |
| 281 |
in: math.Inf(1), |
| 282 |
toType: IntType, |
| 283 |
out: errIntOverflow, |
| 284 |
}, |
| 285 |
{ |
| 286 |
name: "DoubleToIntPosOverflow", |
| 287 |
in: float64(math.MaxInt64), |
| 288 |
toType: IntType, |
| 289 |
out: errIntOverflow, |
| 290 |
}, |
| 291 |
{ |
| 292 |
name: "DoubleToIntNegOverflow", |
| 293 |
in: float64(math.MinInt64), |
| 294 |
toType: IntType, |
| 295 |
out: errIntOverflow, |
| 296 |
}, |
| 297 |
{ |
| 298 |
name: "DoubleToUint", |
| 299 |
in: float64(4.7), |
| 300 |
toType: UintType, |
| 301 |
out: uint64(4), |
| 302 |
}, |
| 303 |
{ |
| 304 |
name: "DoubleToUintNaN", |
| 305 |
in: math.NaN(), |
| 306 |
toType: UintType, |
| 307 |
out: errUintOverflow, |
| 308 |
}, |
| 309 |
{ |
| 310 |
name: "DoubleToUintPosInf", |
| 311 |
in: math.Inf(1), |
| 312 |
toType: UintType, |
| 313 |
out: errUintOverflow, |
| 314 |
}, |
| 315 |
{ |
| 316 |
name: "DoubleToUintPosOverflow", |
| 317 |
in: float64(math.MaxUint64), |
| 318 |
toType: UintType, |
| 319 |
out: errUintOverflow, |
| 320 |
}, |
| 321 |
{ |
| 322 |
name: "DoubleToUintNegOverflow", |
| 323 |
in: float64(-0.1), |
| 324 |
toType: UintType, |
| 325 |
out: errUintOverflow, |
| 326 |
}, |
| 327 |
{ |
| 328 |
name: "DoubleToString", |
| 329 |
in: float64(4.5), |
| 330 |
toType: StringType, |
| 331 |
out: "4.5", |
| 332 |
}, |
| 333 |
{ |
| 334 |
name: "DoubleToUnsupportedType", |
| 335 |
in: float64(4), |
| 336 |
toType: MapType, |
| 337 |
out: errors.New("type conversion error"), |
| 338 |
}, |
| 339 |
} |
| 340 |
for _, tst := range tests { |
| 341 |
got := Double(tst.in).ConvertToType(tst.toType).Value() |
| 342 |
var eq bool |
| 343 |
switch gotVal := got.(type) { |
| 344 |
case error: |
| 345 |
eq = strings.Contains(gotVal.Error(), tst.out.(error).Error()) |
| 346 |
default: |
| 347 |
eq = reflect.DeepEqual(gotVal, tst.out) |
| 348 |
} |
| 349 |
if !eq { |
| 350 |
t.Errorf("Double(%v).ConvertToType(%v) failed, got: %v, wanted: %v", |
| 351 |
tst.in, tst.toType, got, tst.out) |
| 352 |
} |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
func TestDoubleDivide(t *testing.T) { |
| 357 |
if !Double(3).Divide(Double(1.5)).Equal(Double(2)).(Bool) { |
| 358 |
t.Error("Dividing two doubles did not match expectations.") |
| 359 |
} |
| 360 |
var z float64 // Avoid 0.0 since const div by zero is an error. |
| 361 |
if !Double(1.1).Divide(Double(0)).Equal(Double(1.1 / z)).(Bool) { |
| 362 |
t.Error("Division by zero did not match infinity.") |
| 363 |
} |
| 364 |
if !IsError(Double(1.1).Divide(IntNegOne)) { |
| 365 |
t.Error("Division permitted without express type-conversion.") |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
func TestDoubleEqual(t *testing.T) { |
| 370 |
tests := []struct { |
| 371 |
a ref.Val |
| 372 |
b ref.Val |
| 373 |
out ref.Val |
| 374 |
}{ |
| 375 |
{ |
| 376 |
a: Double(-10), |
| 377 |
b: Double(-10), |
| 378 |
out: True, |
| 379 |
}, |
| 380 |
{ |
| 381 |
a: Double(-10), |
| 382 |
b: Double(10), |
| 383 |
out: False, |
| 384 |
}, |
| 385 |
{ |
| 386 |
a: Double(10), |
| 387 |
b: Uint(10), |
| 388 |
out: True, |
| 389 |
}, |
| 390 |
{ |
| 391 |
a: Double(9), |
| 392 |
b: Uint(10), |
| 393 |
out: False, |
| 394 |
}, |
| 395 |
{ |
| 396 |
a: Double(10), |
| 397 |
b: Int(10), |
| 398 |
out: True, |
| 399 |
}, |
| 400 |
{ |
| 401 |
a: Double(10), |
| 402 |
b: Int(-15), |
| 403 |
out: False, |
| 404 |
}, |
| 405 |
{ |
| 406 |
a: Double(math.NaN()), |
| 407 |
b: Int(10), |
| 408 |
out: False, |
| 409 |
}, |
| 410 |
} |
| 411 |
for _, tc := range tests { |
| 412 |
got := tc.a.Equal(tc.b) |
| 413 |
if !reflect.DeepEqual(got, tc.out) { |
| 414 |
t.Errorf("%v.Equal(%v) got %v, wanted %v", tc.a, tc.b, got, tc.out) |
| 415 |
} |
| 416 |
} |
| 417 |
} |
| 418 |
|
| 419 |
func TestDoubleIsZeroValue(t *testing.T) { |
| 420 |
if Double(math.Inf(1)).IsZeroValue() { |
| 421 |
t.Error("Double(+infinity).IsZeroValue() returned true, wanted false.") |
| 422 |
} |
| 423 |
if Double(math.Inf(-1)).IsZeroValue() { |
| 424 |
t.Error("Double(-infinity).IsZeroValue() returned true, wanted false.") |
| 425 |
} |
| 426 |
if Double(math.NaN()).IsZeroValue() { |
| 427 |
t.Error("Double(NaN).IsZeroValue() returned true, wanted false.") |
| 428 |
} |
| 429 |
if !Double(0.0).IsZeroValue() { |
| 430 |
t.Error("Double(0.0).IsZeroValue() returned false, wanted true") |
| 431 |
} |
| 432 |
} |
| 433 |
|
| 434 |
func TestDoubleMultiply(t *testing.T) { |
| 435 |
if !Double(1.1).Multiply(Double(-1.2)).Equal(Double(-1.32)).(Bool) { |
| 436 |
t.Error("Multiplying two doubles did not match expectations.") |
| 437 |
} |
| 438 |
if !IsError(Double(1.1).Multiply(IntNegOne)) { |
| 439 |
t.Error("Multiplication permitted without express type-conversion.") |
| 440 |
} |
| 441 |
} |
| 442 |
|
| 443 |
func TestDoubleNegate(t *testing.T) { |
| 444 |
if !Double(1.1).Negate().Equal(Double(-1.1)).(Bool) { |
| 445 |
t.Error("Negating double value did not succeed") |
| 446 |
} |
| 447 |
} |
| 448 |
|
| 449 |
func TestDoubleSubtract(t *testing.T) { |
| 450 |
if !Double(4).Subtract(Double(-3.5)).Equal(Double(7.5)).(Bool) { |
| 451 |
t.Error("Subtracting two doubles did not match expected value.") |
| 452 |
} |
| 453 |
if !IsError(Double(1.1).Subtract(IntNegOne)) { |
| 454 |
t.Error("Subtraction permitted without express type-conversion.") |
| 455 |
} |
| 456 |
} |