Debian Codegraph @

common/types/int_test.go

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
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 "time"
24
25 "google.golang.org/protobuf/proto"
26
27 "github.com/google/cel-go/common/types/ref"
28 "github.com/google/cel-go/common/types/traits"
29
30 anypb "google.golang.org/protobuf/types/known/anypb"
31 structpb "google.golang.org/protobuf/types/known/structpb"
32 wrapperspb "google.golang.org/protobuf/types/known/wrapperspb"
33 )
34
35 func TestIntAdd(t *testing.T) {
36 if !Int(4).Add(Int(-3)).Equal(Int(1)).(Bool) {
37 t.Error("Adding two ints did not match expected value.")
38 }
39 if !IsError(Int(-1).Add(String("-1"))) {
40 t.Error("Adding non-int to int was not an error.")
41 }
42 if lhs, rhs := math.MaxInt64, 1; !IsError(Int(lhs).Add(Int(rhs))) {
43 t.Errorf("Expected adding %d and %d to result in overflow.", lhs, rhs)
44 }
45 if lhs, rhs := math.MinInt64, -1; !IsError(Int(lhs).Add(Int(rhs))) {
46 t.Errorf("Expected adding %d and %d to result in overflow.", lhs, rhs)
47 }
48 if lhs, rhs := math.MaxInt64-1, 1; !Int(lhs).Add(Int(rhs)).Equal(Int(math.MaxInt64)).(Bool) {
49 t.Errorf("Expected adding %d and %d to yield %d", lhs, rhs, math.MaxInt64)
50 }
51 if lhs, rhs := math.MinInt64+1, -1; !Int(lhs).Add(Int(rhs)).Equal(Int(math.MinInt64)).(Bool) {
52 t.Errorf("Expected adding %d and %d to yield %d", lhs, rhs, math.MaxInt64)
53 }
54 }
55
56 func TestIntCompare(t *testing.T) {
57 tests := []struct {
58 a ref.Val
59 b ref.Val
60 out ref.Val
61 }{
62 {
63 a: Int(42),
64 b: Int(42),
65 out: IntZero,
66 },
67 {
68 a: Int(42),
69 b: Uint(42),
70 out: IntZero,
71 },
72 {
73 a: Int(42),
74 b: Double(42),
75 out: IntZero,
76 },
77 {
78 a: Int(-1300),
79 b: Int(204),
80 out: IntNegOne,
81 },
82 {
83 a: Int(-1300),
84 b: Uint(204),
85 out: IntNegOne,
86 },
87 {
88 a: Int(204),
89 b: Double(204.1),
90 out: IntNegOne,
91 },
92 {
93 a: Int(1300),
94 b: Uint(math.MaxInt64) + 1,
95 out: IntNegOne,
96 },
97 {
98 a: Int(204),
99 b: Uint(205),
100 out: IntNegOne,
101 },
102 {
103 a: Int(204),
104 b: Double(math.MaxInt64) + 1025.0,
105 out: IntNegOne,
106 },
107 {
108 a: Int(204),
109 b: Double(math.NaN()),
110 out: NewErr("NaN values cannot be ordered"),
111 },
112 {
113 a: Int(204),
114 b: Int(-1300),
115 out: IntOne,
116 },
117 {
118 a: Int(204),
119 b: Uint(10),
120 out: IntOne,
121 },
122 {
123 a: Int(204),
124 b: Double(203.9),
125 out: IntOne,
126 },
127 {
128 a: Int(204),
129 b: Double(math.MinInt64) - 1025.0,
130 out: IntOne,
131 },
132 {
133 a: Int(1),
134 b: String("1"),
135 out: NoSuchOverloadErr(),
136 },
137 }
138 for _, tc := range tests {
139 comparer := tc.a.(traits.Comparer)
140 got := comparer.Compare(tc.b)
141 if !reflect.DeepEqual(got, tc.out) {
142 t.Errorf("%v.Compare(%v) got %v, wanted %v", tc.a, tc.b, got, tc.out)
143 }
144 }
145 }
146
147 func TestIntConvertToNative_Any(t *testing.T) {
148 val, err := Int(math.MaxInt64).ConvertToNative(anyValueType)
149 if err != nil {
150 t.Error(err)
151 }
152 want, err := anypb.New(wrapperspb.Int64(math.MaxInt64))
153 if err != nil {
154 t.Error(err)
155 }
156 if !proto.Equal(val.(proto.Message), want) {
157 t.Errorf("Got '%v', wanted %v", val, want)
158 }
159 }
160
161 func TestIntConvertToNative_Error(t *testing.T) {
162 val, err := Int(1).ConvertToNative(JSONStructType)
163 if err == nil {
164 t.Errorf("Got '%v', expected error", val)
165 }
166 }
167
168 func TestIntConvertToNative_Int8(t *testing.T) {
169 val, err := Int(127).ConvertToNative(reflect.TypeOf(int8(0)))
170 if err != nil {
171 t.Fatalf("Int.ConvertToNative(int8) failed: %v", err)
172 }
173 if val.(int8) != 127 {
174 t.Errorf("Got '%v', expected 20050", val)
175 }
176 val, err = Int(math.MaxInt8 + 1).ConvertToNative(reflect.TypeOf(int8(0)))
177 if err == nil {
178 t.Errorf("(MaxInt+1).ConvertToNative(int8) did not error, got: %v", val)
179 } else if !strings.Contains(err.Error(), "integer overflow") {
180 t.Errorf("ConvertToNative(int8) returned unexpected error: %v, wanted integer overflow", err)
181 }
182 }
183 func TestIntConvertToNative_Int16(t *testing.T) {
184 val, err := Int(20050).ConvertToNative(reflect.TypeOf(int16(0)))
185 if err != nil {
186 t.Fatalf("Int.ConvertToNative(int16) failed: %v", err)
187 }
188 if val.(int16) != 20050 {
189 t.Errorf("Got '%v', expected 20050", val)
190 }
191 val, err = Int(math.MaxInt16 + 1).ConvertToNative(reflect.TypeOf(int16(0)))
192 if err == nil {
193 t.Errorf("(MaxInt+1).ConvertToNative(int16) did not error, got: %v", val)
194 } else if !strings.Contains(err.Error(), "integer overflow") {
195 t.Errorf("ConvertToNative(int32) returned unexpected error: %v, wanted integer overflow", err)
196 }
197 }
198 func TestIntConvertToNative_Int32(t *testing.T) {
199 val, err := Int(20050).ConvertToNative(reflect.TypeOf(int32(0)))
200 if err != nil {
201 t.Fatalf("Int.ConvertToNative(int32) failed: %v", err)
202 }
203 if val.(int32) != 20050 {
204 t.Errorf("Got '%v', expected 20050", val)
205 }
206 val, err = Int(math.MaxInt32 + 1).ConvertToNative(reflect.TypeOf(int32(0)))
207 if err == nil {
208 t.Errorf("(MaxInt+1).ConvertToNative(int32) did not error, got: %v", val)
209 } else if !strings.Contains(err.Error(), "integer overflow") {
210 t.Errorf("ConvertToNative(int32) returned unexpected error: %v, wanted integer overflow", err)
211 }
212 }
213
214 func TestIntConvertToNative_Int64(t *testing.T) {
215 // Value greater than max int32.
216 val, err := Int(4147483648).ConvertToNative(reflect.TypeOf(int64(0)))
217 if err != nil {
218 t.Error(err)
219 } else if val.(int64) != 4147483648 {
220 t.Errorf("Got '%v', expected 4147483648", val)
221 }
222 }
223
224 func TestIntConvertToNative_Json(t *testing.T) {
225 // Value can be represented accurately as a JSON number.
226 val, err := Int(maxIntJSON).ConvertToNative(JSONValueType)
227 if err != nil {
228 t.Error(err)
229 } else if !proto.Equal(val.(proto.Message),
230 structpb.NewNumberValue(9007199254740991.0)) {
231 t.Errorf("Got '%v', expected a json number for a 32-bit int", val)
232 }
233
234 // Value converts to a JSON decimal string.
235 val, err = Int(maxIntJSON + 1).ConvertToNative(JSONValueType)
236 if err != nil {
237 t.Error(err)
238 } else if !proto.Equal(val.(proto.Message), structpb.NewStringValue("9007199254740992")) {
239 t.Errorf("Got '%v', expected a json string for a 64-bit int", val)
240 }
241 }
242
243 func TestIntConvertToNative_Ptr_Int32(t *testing.T) {
244 ptrType := int32(0)
245 val, err := Int(20050).ConvertToNative(reflect.TypeOf(&ptrType))
246 if err != nil {
247 t.Error(err)
248 } else if *val.(*int32) != 20050 {
249 t.Errorf("Got '%v', expected 20050", val)
250 }
251 }
252
253 func TestIntConvertToNative_Ptr_Int64(t *testing.T) {
254 // Value greater than max int32.
255 ptrType := int64(0)
256 val, err := Int(math.MaxInt32 + 1).ConvertToNative(reflect.TypeOf(&ptrType))
257 if err != nil {
258 t.Error(err)
259 } else if *val.(*int64) != math.MaxInt32+1 {
260 t.Errorf("Got '%v', expected MaxInt32 + 1", val)
261 }
262 }
263
264 func TestIntConvertToNative_Wrapper(t *testing.T) {
265 val, err := Int(math.MinInt32).ConvertToNative(int32WrapperType)
266 if err != nil {
267 t.Error(err)
268 }
269 want := wrapperspb.Int32(math.MinInt32)
270 if err != nil {
271 t.Error(err)
272 }
273 if !proto.Equal(val.(proto.Message), want) {
274 t.Errorf("Got '%v', wanted %v", val, want)
275 }
276
277 val, err = Int(math.MinInt64).ConvertToNative(int64WrapperType)
278 if err != nil {
279 t.Error(err)
280 }
281 want2 := wrapperspb.Int64(math.MinInt64)
282 if err != nil {
283 t.Error(err)
284 }
285 if !proto.Equal(val.(proto.Message), want2) {
286 t.Errorf("Got '%v', wanted %v", val, want2)
287 }
288 }
289
290 func TestIntConvertToType(t *testing.T) {
291 tests := []struct {
292 name string
293 in int64
294 toType ref.Type
295 out any
296 }{
297 {
298 name: "IntToType",
299 in: int64(4),
300 toType: TypeType,
301 out: IntType.TypeName(),
302 },
303 {
304 name: "IntToInt",
305 in: int64(4),
306 toType: IntType,
307 out: int64(4),
308 },
309 {
310 name: "IntToUint",
311 in: int64(4),
312 toType: UintType,
313 out: uint64(4),
314 },
315 {
316 name: "IntToUintOverflow",
317 in: -1,
318 toType: UintType,
319 out: errUintOverflow,
320 },
321 {
322 name: "IntToDouble",
323 in: int64(4),
324 toType: DoubleType,
325 out: float64(4),
326 },
327 {
328 name: "IntToString",
329 in: int64(-4),
330 toType: StringType,
331 out: "-4",
332 },
333 {
334 name: "IntToTimestamp",
335 in: int64(946684800),
336 toType: TimestampType,
337 out: time.Unix(946684800, 0).UTC(),
338 },
339 {
340 name: "IntToTimestampPosOverflow",
341 in: maxUnixTime + 1,
342 toType: TimestampType,
343 out: errTimestampOverflow,
344 },
345 {
346 name: "IntToTimestampMinOverflow",
347 in: minUnixTime - 1,
348 toType: TimestampType,
349 out: errTimestampOverflow,
350 },
351 {
352 name: "IntToUnsupportedType",
353 in: int64(4),
354 toType: DurationType,
355 out: errors.New("type conversion error"),
356 },
357 }
358 for _, tst := range tests {
359 got := Int(tst.in).ConvertToType(tst.toType).Value()
360 var eq bool
361 switch gotVal := got.(type) {
362 case time.Time:
363 eq = gotVal.Equal(tst.out.(time.Time))
364 case error:
365 eq = strings.Contains(gotVal.Error(), tst.out.(error).Error())
366 default:
367 eq = reflect.DeepEqual(gotVal, tst.out)
368 }
369 if !eq {
370 t.Errorf("Int(%v).ConvertToType(%v) failed, got: %v, wanted: %v",
371 tst.in, tst.toType, got, tst.out)
372 }
373 }
374 }
375
376 func TestIntDivide(t *testing.T) {
377 if !Int(3).Divide(Int(2)).Equal(Int(1)).(Bool) {
378 t.Error("Dividing two ints did not match expectations.")
379 }
380 if !IsError(IntZero.Divide(IntZero)) {
381 t.Error("Divide by zero did not cause error.")
382 }
383 if !IsError(Int(1).Divide(Double(-1))) {
384 t.Error("Division permitted without express type-conversion.")
385 }
386 if lhs, rhs := math.MinInt64, -1; !IsError(Int(lhs).Divide(Int(rhs))) {
387 t.Errorf("Expected dividing %d and %d result in overflow.", lhs, rhs)
388 }
389 }
390
391 func TestIntEqual(t *testing.T) {
392 tests := []struct {
393 a ref.Val
394 b ref.Val
395 out ref.Val
396 }{
397 {
398 a: Int(-10),
399 b: Int(-10),
400 out: True,
401 },
402 {
403 a: Int(-10),
404 b: Int(10),
405 out: False,
406 },
407 {
408 a: Int(10),
409 b: Uint(10),
410 out: True,
411 },
412 {
413 a: Int(9),
414 b: Uint(10),
415 out: False,
416 },
417 {
418 a: Int(10),
419 b: Double(10),
420 out: True,
421 },
422 {
423 a: Int(10),
424 b: Double(-10.5),
425 out: False,
426 },
427 {
428 a: Int(10),
429 b: Double(math.NaN()),
430 out: False,
431 },
432 {
433 a: Int(1),
434 b: String("1"),
435 out: False,
436 },
437 }
438 for _, tc := range tests {
439 got := tc.a.Equal(tc.b)
440 if !reflect.DeepEqual(got, tc.out) {
441 t.Errorf("%v.Equal(%v) got %v, wanted %v", tc.a, tc.b, got, tc.out)
442 }
443 }
444 }
445
446 func TestIntIsZeroValue(t *testing.T) {
447 if Int(1).IsZeroValue() {
448 t.Error("Int(1).IsZeroValue() returned true, wanted false.")
449 }
450 if !Int(0).IsZeroValue() {
451 t.Error("Int(0).IsZeroValue() returned false, wanted true")
452 }
453 }
454
455 func TestIntModulo(t *testing.T) {
456 if !Int(21).Modulo(Int(2)).Equal(Int(1)).(Bool) {
457 t.Error("Unexpected result from modulus operator.")
458 }
459 if !IsError(Int(21).Modulo(IntZero)) {
460 t.Error("Modulus by zero did not cause error.")
461 }
462 if !IsError(Int(21).Modulo(uintZero)) {
463 t.Error("Modulus permitted between different types without type conversion.")
464 }
465 if lhs, rhs := math.MinInt64, -1; !IsError(Int(lhs).Modulo(Int(rhs))) {
466 t.Errorf("Expected modulo %d and %d result in overflow.", lhs, rhs)
467 }
468 }
469
470 func TestIntMultiply(t *testing.T) {
471 if !Int(2).Multiply(Int(-2)).Equal(Int(-4)).(Bool) {
472 t.Error("Multiplying two values did not match expectations.")
473 }
474 if !IsError(Int(1).Multiply(Double(-4.0))) {
475 t.Error("Multiplication permitted without express type-conversion.")
476 }
477 if lhs, rhs := math.MaxInt64/2, 3; !IsError(Int(lhs).Multiply(Int(rhs))) {
478 t.Errorf("Expected multiplying %d and %d to result in overflow.", lhs, rhs)
479 }
480 if lhs, rhs := math.MinInt64/2, 3; !IsError(Int(lhs).Multiply(Int(rhs))) {
481 t.Errorf("Expected multiplying %d and %d to result in overflow.", lhs, rhs)
482 }
483 if lhs, rhs := math.MaxInt64/2, 2; !Int(lhs).Multiply(Int(rhs)).Equal(Int(math.MaxInt64 - 1)).(Bool) {
484 t.Errorf("Expected multiplying %d and %d to yield %d", lhs, rhs, math.MaxInt64-1)
485 }
486 if lhs, rhs := math.MinInt64/2, 2; !Int(lhs).Multiply(Int(rhs)).Equal(Int(math.MinInt64)).(Bool) {
487 t.Errorf("Expected multiplying %d and %d to yield %d", lhs, rhs, math.MinInt64)
488 }
489 if lhs, rhs := math.MaxInt64/2, -2; !Int(lhs).Multiply(Int(rhs)).Equal(Int(math.MinInt64 + 2)).(Bool) {
490 t.Errorf("Expected multiplying %d and %d to yield %d", lhs, rhs, math.MinInt64+2)
491 }
492 if lhs, rhs := (math.MinInt64+2)/2, -2; !Int(lhs).Multiply(Int(rhs)).Equal(Int(math.MaxInt64 - 1)).(Bool) {
493 t.Errorf("Expected multiplying %d and %d to yield %d", lhs, rhs, math.MaxInt64-1)
494 }
495 if lhs, rhs := math.MinInt64, -1; !IsError(Int(lhs).Multiply(Int(rhs))) {
496 t.Errorf("Expected multiplying %d and %d result in overflow.", lhs, rhs)
497 }
498 }
499
500 func TestIntNegate(t *testing.T) {
501 if !Int(1).Negate().Equal(Int(-1)).(Bool) {
502 t.Error("Negating int value did not succeed")
503 }
504 if v := math.MinInt64; !IsError(Int(v).Negate()) {
505 t.Errorf("Expected negating %d to result in overflow.", v)
506 }
507 if v := math.MaxInt64; !Int(v).Negate().Equal(Int(math.MinInt64 + 1)).(Bool) {
508 t.Errorf("Expected negating %d to yield %d", v, math.MinInt64+1)
509 }
510 }
511
512 func TestIntSubtract(t *testing.T) {
513 if !Int(4).Subtract(Int(-3)).Equal(Int(7)).(Bool) {
514 t.Error("Subtracting two ints did not match expected value.")
515 }
516 if !IsError(Int(1).Subtract(Uint(1))) {
517 t.Error("Subtraction permitted without express type-conversion.")
518 }
519 if lhs, rhs := math.MaxInt64, -1; !IsError(Int(lhs).Subtract(Int(rhs))) {
520 t.Errorf("Expected subtracting %d and %d to result in overflow.", lhs, rhs)
521 }
522 if lhs, rhs := math.MinInt64, 1; !IsError(Int(lhs).Subtract(Int(rhs))) {
523 t.Errorf("Expected subtracting %d and %d to result in overflow.", lhs, rhs)
524 }
525 if lhs, rhs := math.MaxInt64-1, -1; !Int(lhs).Subtract(Int(rhs)).Equal(Int(math.MaxInt64)).(Bool) {
526 t.Errorf("Expected subtracting %d and %d to yield %d", lhs, rhs, math.MaxInt64)
527 }
528 if lhs, rhs := math.MinInt64+1, 1; !Int(lhs).Subtract(Int(rhs)).Equal(Int(math.MinInt64)).(Bool) {
529 t.Errorf("Expected subtracting %d and %d to yield %d", lhs, rhs, math.MinInt64)
530 }
531 }