MCPcopy Create free account
hub / github.com/brimdata/super / TestArithmetic

Function TestArithmetic

runtime/sam/expr/expr_test.go:345–459  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

343}
344
345func TestArithmetic(t *testing.T) {
346 record := "{x:10::int32,f:2.5}"
347
348 // Test integer arithmetic
349 testSuccessful(t, "100 + 23", record, "123")
350 testSuccessful(t, "x + 5", record, "15")
351 testSuccessful(t, "5 + x", record, "15")
352 testSuccessful(t, "x - 5", record, "5")
353 testSuccessful(t, "0 - x", record, "-10")
354 testSuccessful(t, "x + 5 - 3", record, "12")
355 testSuccessful(t, "x*2", record, "20")
356 testSuccessful(t, "5*x*2", record, "100")
357 testSuccessful(t, "x/3", record, "3")
358 testSuccessful(t, "20/x", record, "2")
359
360 // Test precedence of arithmetic operations
361 testSuccessful(t, "x + 1 * 10", record, "20")
362 testSuccessful(t, "(x + 1) * 10", record, "110")
363
364 // Test arithmetic with floats
365 testSuccessful(t, "f + 1.0", record, "3.5")
366 testSuccessful(t, "1.0 + f", record, "3.5")
367 testSuccessful(t, "f - 1.0", record, "1.5")
368 testSuccessful(t, "0.0 - f", record, "-2.5")
369 testSuccessful(t, "f * 1.5", record, "3.75")
370 testSuccessful(t, "1.5 * f", record, "3.75")
371 testSuccessful(t, "f / 1.25", record, "2.")
372 testSuccessful(t, "5.0 / f", record, "2.")
373
374 // Union values are dereferenced when doing arithmetic on them.
375 val := "10::(int64|string)"
376 testSuccessful(t, "this + 1", val, "11")
377 testSuccessful(t, "this - 1", val, "9")
378 testSuccessful(t, "this * 2", val, "20")
379 testSuccessful(t, "this / 2", val, "5")
380 testSuccessful(t, "this % 3", val, "1")
381
382 // Test arithmetic with null values
383 testSuccessful(t, "null + 1", "", "null")
384 testSuccessful(t, "(1)::uint64 + null", "", "null")
385 testSuccessful(t, "null + 1.0", "", "null")
386 testSuccessful(t, "1. - null", "", "null")
387 testSuccessful(t, "(1)::uint64 * null", "", "null")
388 testSuccessful(t, "null / 1.", "", "null")
389 testSuccessful(t, "1 / null", "", "null")
390 testSuccessful(t, "null % 1", "", "null")
391
392 // Difference of two times is a duration
393 testSuccessful(t, "a - b", "{a:2022-09-22T00:00:01Z,b:2022-09-22T00:00:00Z}", "1s")
394
395 // Difference of time and duration is a time
396 testSuccessful(t, "2022-09-22T00:00:01Z - 1h", "", "2022-09-21T23:00:01Z")
397
398 width := func(id int) int {
399 switch id {
400 case super.IDInt8, super.IDUint8:
401 return 8
402 case super.IDInt16, super.IDUint16:

Callers

nothing calls this directly

Calls 4

LookupPrimitiveFunction · 0.92
IsSignedFunction · 0.92
testSuccessfulFunction · 0.85
IDMethod · 0.65

Tested by

no test coverage detected