Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions builtin/builtin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -875,3 +875,36 @@ func TestBuiltin_recursion_custom_max_depth(t *testing.T) {
require.NoError(t, err)
})
}

func TestAbs_UnsignedIntegers(t *testing.T) {
// Test that abs() correctly handles unsigned integers
// Unsigned integers are always non-negative, so abs() should return them unchanged
tests := []struct {
name string
env map[string]any
expr string
want any
}{
{"uint", map[string]any{"x": uint(42)}, "abs(x)", uint(42)},
{"uint8", map[string]any{"x": uint8(42)}, "abs(x)", uint8(42)},
{"uint16", map[string]any{"x": uint16(42)}, "abs(x)", uint16(42)},
{"uint32", map[string]any{"x": uint32(42)}, "abs(x)", uint32(42)},
{"uint64", map[string]any{"x": uint64(42)}, "abs(x)", uint64(42)},
{"uint zero", map[string]any{"x": uint(0)}, "abs(x)", uint(0)},
{"uint8 zero", map[string]any{"x": uint8(0)}, "abs(x)", uint8(0)},
{"uint16 zero", map[string]any{"x": uint16(0)}, "abs(x)", uint16(0)},
{"uint32 zero", map[string]any{"x": uint32(0)}, "abs(x)", uint32(0)},
{"uint64 zero", map[string]any{"x": uint64(0)}, "abs(x)", uint64(0)},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
program, err := expr.Compile(tt.expr, expr.Env(tt.env))
require.NoError(t, err)

result, err := expr.Run(program, tt.env)
require.NoError(t, err)
assert.Equal(t, tt.want, result)
})
}
}
30 changes: 5 additions & 25 deletions builtin/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,35 +102,15 @@ func Abs(x any) any {
return x
}
case uint:
if x < 0 {
return -x
} else {
return x
}
return x
case uint8:
if x < 0 {
return -x
} else {
return x
}
return x
case uint16:
if x < 0 {
return -x
} else {
return x
}
return x
case uint32:
if x < 0 {
return -x
} else {
return x
}
return x
case uint64:
if x < 0 {
return -x
} else {
return x
}
return x
}
panic(fmt.Sprintf("invalid argument for abs (type %T)", x))
}
Expand Down
Loading