Skip to content
Closed
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
6 changes: 2 additions & 4 deletions spec/compiler/codegen/arithmetics_spec.cr
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
require "../../spec_helper"

{% if flag?(:darwin) %}
# Int128 and UInt128 specs do not pass on win32 because of missing symbols
{% unless flag?(:win32) %}
SupportedInts = [UInt8, UInt16, UInt32, UInt64, UInt128, Int8, Int16, Int32, Int64, Int128]
SupportedIntsConversions = {
to_i8: Int8, to_i16: Int16, to_i32: Int32, to_i64: Int64, to_i128: Int128,
to_u8: UInt8, to_u16: UInt16, to_u32: UInt32, to_u64: UInt64, to_u128: UInt128,
}
{% else %}
# Skip Int128 and UInt128 on linux platforms due to compiler-rt dependency.
# PreviewOverflowFlags includes compiler_rt flag to support Int64 overflow
# detection in 32 bits platforms.
SupportedInts = [UInt8, UInt16, UInt32, UInt64, Int8, Int16, Int32, Int64]
SupportedIntsConversions = {
to_i8: Int8, to_i16: Int16, to_i32: Int32, to_i64: Int64,
Expand Down
29 changes: 19 additions & 10 deletions spec/compiler/lexer/lexer_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ describe "Lexer" do
["+1.0f32", "+1.0"], ["-1.0f32", "-1.0"], ["-0.0f32", "-0.0"], ["1_234.567_890_f32", "1234.567890"]]
it_lexes_f64 ["1.0", ["1.0hello", "1.0"], "+1.0", "-1.0", ["1_234.567_890", "1234.567890"]]
it_lexes_f32 [["1e+23_f32", "1e+23"], ["1.2e+23_f32", "1.2e+23"]]
it_lexes_f64 ["1e23", "1e-23", "1e+23", "1.2e+23", ["1e23f64", "1e23"], ["1.2e+23_f64", "1.2e+23"]]
it_lexes_f64 ["1e23", "1e-23", "1e+23", "1.2e+23", ["1e23f64", "1e23"], ["1.2e+23_f64", "1.2e+23"], "0e40"]

it_lexes_number :i8, ["1i8", "1"]
it_lexes_number :i8, ["1_i8", "1"]
Expand Down Expand Up @@ -299,17 +299,24 @@ describe "Lexer" do
assert_syntax_error "18446744073709551616_i32", "18446744073709551616 doesn't fit in an Int32"
assert_syntax_error "9999999999999999999_i32", "9999999999999999999 doesn't fit in an Int32"

assert_syntax_error "-9999999999999999999", "-9999999999999999999 doesn't fit in an Int64"
assert_syntax_error "-99999999999999999999", "-99999999999999999999 doesn't fit in an Int64"
assert_syntax_error "-11111111111111111111", "-11111111111111111111 doesn't fit in an Int64"
assert_syntax_error "-9223372036854775809", "-9223372036854775809 doesn't fit in an Int64"
assert_syntax_error "18446744073709551616", "18446744073709551616 doesn't fit in an UInt64"
assert_syntax_error "-9999999999999999999_i64", "-9999999999999999999 doesn't fit in an Int64"
assert_syntax_error "-99999999999999999999_i64", "-99999999999999999999 doesn't fit in an Int64"
assert_syntax_error "-11111111111111111111_i64", "-11111111111111111111 doesn't fit in an Int64"
assert_syntax_error "-9223372036854775809_i64", "-9223372036854775809 doesn't fit in an Int64"
assert_syntax_error "18446744073709551616_u64", "18446744073709551616 doesn't fit in an UInt64"

assert_syntax_error "340282366920938463463374607431768211456", "340282366920938463463374607431768211456 doesn't fit in an UInt128"
assert_syntax_error "-170141183460469231731687303715884105729", "-170141183460469231731687303715884105729 doesn't fit in an Int128"
assert_syntax_error "-999999999999999999999999999999999999999", "-999999999999999999999999999999999999999 doesn't fit in an Int128"

assert_syntax_error "9223372036854775808_i128", "9223372036854775808 doesn't fit in an Int64. Int128 literals that don't fit in an Int64 are currently not supported"
assert_syntax_error "-9223372036854775809_i128", "-9223372036854775809 doesn't fit in an Int64. Int128 literals that don't fit in an Int64 are currently not supported"
assert_syntax_error "118446744073709551616_u128", "118446744073709551616 doesn't fit in an UInt64. UInt128 literals that don't fit in an UInt64 are currently not supported"
assert_syntax_error "18446744073709551616_u128", "18446744073709551616 doesn't fit in an UInt64. UInt128 literals that don't fit in an UInt64 are currently not supported"
assert_syntax_error "-1_u128", "Invalid negative value -1 for UInt128"
assert_syntax_error "-0_u128", "Invalid negative value -0 for UInt128"
assert_syntax_error "-0u128", "Invalid negative value -0 for UInt128"

assert_syntax_error "1__1", "trailing '_' in number"
assert_syntax_error "-3_", "trailing '_' in number"

assert_syntax_error "0_12", "octal constants should be prefixed with 0o"

assert_syntax_error "0xFF_i8", "255 doesn't fit in an Int8"
assert_syntax_error "0o200_i8", "128 doesn't fit in an Int8"
Expand All @@ -330,6 +337,8 @@ describe "Lexer" do
assert_syntax_error ".42", ".1 style number literal is not supported, put 0 before dot"
assert_syntax_error "-.42", ".1 style number literal is not supported, put 0 before dot"

assert_syntax_error "-0_e12", "trailing '_' in number"

it "lexes not instance var" do
lexer = Lexer.new "!@foo"
token = lexer.next_token
Expand Down
89 changes: 89 additions & 0 deletions spec/std/crystal/compiler_rt/divmod128_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
require "spec"

# TODO: Remove these helpers in PR part 2

private def make_ti(a : Int128, b : Int128)
(a << 64) + b
end

private def make_tu(a : UInt128, b : UInt128)
(a << 64) + b
end

# Specs ported from compiler-rt

private def test__divti3(a : Int128, b : Int128, expected : Int128, file = __FILE__, line = __LINE__)
it "passes compiler-rt builtins unit tests" do
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this is the same design as in the existing mulodi4_spec.cr, so don't feel obliged to change this. But I don't think there's much reason for having many individual examples (with non-descriptive names). I'd propose to place all related expecations in a single example.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Placing it all in the one example will show just the first fail instead of all.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, but that doesn't really matter much. They're all expected to pass anyways.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course they are :D I'm saying is that you want to know exactly which ones are failing if they don't.

actual = __divti3(a, b)
actual.should eq(expected), file: file, line: line
end
end

private def test__modti3(a : Int128, b : Int128, expected : Int128, file = __FILE__, line = __LINE__)
it "passes compiler-rt builtins unit tests" do
actual = __modti3(a, b)
actual.should eq(expected), file: file, line: line
end
end

private def test__udivti3(a : UInt128, b : UInt128, expected : UInt128, file = __FILE__, line = __LINE__)
it "passes compiler-rt builtins unit tests" do
actual = __udivti3(a, b)
actual.should eq(expected), file: file, line: line
end
end

private def test__umodti3(a : UInt128, b : UInt128, expected : UInt128, file = __FILE__, line = __LINE__)
it "passes compiler-rt builtins unit tests" do
actual = __umodti3(a, b)
actual.should eq(expected), file: file, line: line
end
end

describe "__divti3" do
test__divti3(0, 1, 0)
test__divti3(0, -1, 0)
test__divti3(2, 1, 2)
test__divti3(2, -1, -2)
test__divti3(-2, 1, -2)
test__divti3(-2, -1, 2)
test__divti3(make_ti(-9223372036854775808, 0x0), 1, make_ti(-9223372036854775808, 0x0))
test__divti3(make_ti(-9223372036854775808, 0x0), -1, make_ti(-9223372036854775808, 0x0))
test__divti3(make_ti(-9223372036854775808, 0x0), -2, make_ti(0x4000000000000000, 0x0))
test__divti3(make_ti(-9223372036854775808, 0x0), 2, make_ti(-0x4000000000000000, 0x0))
end

describe "__modti3" do
test__modti3(0, 1, 0)
test__modti3(0, -1, 0)

test__modti3(5, 3, 2)
test__modti3(5, -3, 2)
test__modti3(-5, 3, -2)
test__modti3(-5, -3, -2)

test__modti3(make_ti(-9223372036854775808, 0x0), 1, 0)
test__modti3(make_ti(-9223372036854775808, 0x0), -1, 0)
test__modti3(make_ti(-9223372036854775808, 0x0), 2, 0)
test__modti3(make_ti(-9223372036854775808, 0x0), -2, 0)
test__modti3(make_ti(-9223372036854775808, 0x0), 3, -2)
test__modti3(make_ti(-9223372036854775808, 0x0), -3, -2)
end

describe "__udivti3" do
test__udivti3(0, 1, 0)
test__udivti3(2, 1, 2)

test__udivti3(make_tu(0x0, 0x8000000000000000), 1, make_tu(0x0, 0x8000000000000000))
test__udivti3(make_tu(0x0, 0x8000000000000000), 2, make_tu(0x0, 0x4000000000000000))
test__udivti3(make_tu(0xffffffffffffffff, 0xffffffffffffffff), 2, make_tu(0x7fffffffffffffff, 0xffffffffffffffff))
end

describe "__umodti3" do
test__umodti3(0, 1, 0)
test__umodti3(2, 1, 0)

test__umodti3(make_tu(0x0, 0x8000000000000000), 1, 0)
test__umodti3(make_tu(0x0, 0x8000000000000000), 2, 0)
test__umodti3(make_tu(0xffffffffffffffff, 0xffffffffffffffff), 2, 1)
end
76 changes: 76 additions & 0 deletions spec/std/crystal/compiler_rt/mulosi4_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
require "spec"

# Ported from compiler-rt:test/builtins/Unit/mulosi4_test.c

private def test__mulosi4(a : Int32, b : Int32, expected : Int32, expected_overflow : Int32, file = __FILE__, line = __LINE__)
it "passes compiler-rt builtins unit tests" do
actual_overflow : Int32 = 0
actual = __mulosi4(a, b, pointerof(actual_overflow))
actual_overflow.should eq(expected_overflow), file: file, line: line
if !expected_overflow
actual.should eq(expected), file: file, line: line
end
end
end

describe "__mulosi4" do
test__mulosi4(0, 0, 0, 0)
test__mulosi4(0, 1, 0, 0)
test__mulosi4(1, 0, 0, 0)
test__mulosi4(0, 10, 0, 0)
test__mulosi4(10, 0, 0, 0)
test__mulosi4(0, 0x1234567, 0, 0)
test__mulosi4(0x1234567, 0, 0, 0)

test__mulosi4(0, -1, 0, 0)
test__mulosi4(-1, 0, 0, 0)
test__mulosi4(0, -10, 0, 0)
test__mulosi4(-10, 0, 0, 0)
test__mulosi4(0, 0x1234567, 0, 0)
test__mulosi4(0x1234567, 0, 0, 0)

test__mulosi4(1, 1, 1, 0)
test__mulosi4(1, 10, 10, 0)
test__mulosi4(10, 1, 10, 0)
test__mulosi4(1, 0x1234567, 0x1234567, 0)
test__mulosi4(0x1234567, 1, 0x1234567, 0)

test__mulosi4(1, -1, -1, 0)
test__mulosi4(1, -10, -10, 0)
test__mulosi4(-10, 1, -10, 0)
test__mulosi4(1, -0x1234567, -0x1234567, 0)
test__mulosi4(-0x1234567, 1, -0x1234567, 0)

test__mulosi4(0x7FFFFFFF, -2, -0x7fffffff, 1)
test__mulosi4(-2, 0x7FFFFFFF, -0x7fffffff, 1)
test__mulosi4(0x7FFFFFFF, -1, -0x7fffffff, 0)
test__mulosi4(-1, 0x7FFFFFFF, -0x7fffffff, 0)
test__mulosi4(0x7FFFFFFF, 0, 0, 0)
test__mulosi4(0, 0x7FFFFFFF, 0, 0)
test__mulosi4(0x7FFFFFFF, 1, 0x7FFFFFFF, 0)
test__mulosi4(1, 0x7FFFFFFF, 0x7FFFFFFF, 0)
test__mulosi4(0x7FFFFFFF, 2, -0x7fffffff, 1)
test__mulosi4(2, 0x7FFFFFFF, -0x7fffffff, 1)

test__mulosi4(-0x80000000, -2, -0x80000000, 1)
test__mulosi4(-2, -0x80000000, -0x80000000, 1)
test__mulosi4(-0x80000000, -1, -0x80000000, 1)
test__mulosi4(-1, -0x80000000, -0x80000000, 1)
test__mulosi4(-0x80000000, 0, 0, 0)
test__mulosi4(0, -0x80000000, 0, 0)
test__mulosi4(-0x80000000, 1, -0x80000000, 0)
test__mulosi4(1, -0x80000000, -0x80000000, 0)
test__mulosi4(-0x80000000, 2, -0x80000000, 1)
test__mulosi4(2, -0x80000000, -0x80000000, 1)

test__mulosi4(-0x7fffffff, -2, -0x7fffffff, 1)
test__mulosi4(-2, -0x7fffffff, -0x7fffffff, 1)
test__mulosi4(-0x7fffffff, -1, 0x7FFFFFFF, 0)
test__mulosi4(-1, -0x7fffffff, 0x7FFFFFFF, 0)
test__mulosi4(-0x7fffffff, 0, 0, 0)
test__mulosi4(0, -0x7fffffff, 0, 0)
test__mulosi4(-0x7fffffff, 1, -0x7fffffff, 0)
test__mulosi4(1, -0x7fffffff, -0x7fffffff, 0)
test__mulosi4(-0x7fffffff, 2, -0x80000000, 1)
test__mulosi4(2, -0x7fffffff, -0x80000000, 1)
end
151 changes: 151 additions & 0 deletions spec/std/crystal/compiler_rt/muloti4_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
require "spec"

# Ported from compiler-rt:test/builtins/Unit/muloti4_test.c

private def test__muloti4(a : Int128, b : Int128, expected : Int128, expected_overflow : Int32, file = __FILE__, line = __LINE__)
it "passes compiler-rt builtins unit tests" do
actual_overflow : Int32 = 0
actual = __muloti4(a, b, pointerof(actual_overflow))
actual_overflow.should eq(expected_overflow), file: file, line: line
if !expected_overflow
actual.should eq(expected), file: file, line: line
end
end
end

# TODO: Remove this helper in PR part 2

private def make_ti(a : Int128, b : Int128)
(a << 64) + b
end

describe "__muloti4" do
test__muloti4(0, 0, 0, 0)
test__muloti4(0, 1, 0, 0)
test__muloti4(1, 0, 0, 0)
test__muloti4(0, 10, 0, 0)
test__muloti4(10, 0, 0, 0)
test__muloti4(0, 81985529216486895, 0, 0)
test__muloti4(81985529216486895, 0, 0, 0)
test__muloti4(0, -1, 0, 0)
test__muloti4(-1, 0, 0, 0)
test__muloti4(0, -10, 0, 0)
test__muloti4(-10, 0, 0, 0)
test__muloti4(0, -81985529216486895, 0, 0)
test__muloti4(-81985529216486895, 0, 0, 0)
test__muloti4(1, 1, 1, 0)
test__muloti4(1, 10, 10, 0)
test__muloti4(10, 1, 10, 0)
test__muloti4(1, 81985529216486895, 81985529216486895, 0)
test__muloti4(81985529216486895, 1, 81985529216486895, 0)
test__muloti4(1, -1, -1, 0)
test__muloti4(1, -10, -10, 0)
test__muloti4(-10, 1, -10, 0)
test__muloti4(1, -81985529216486895, -81985529216486895, 0)
test__muloti4(-81985529216486895, 1, -81985529216486895, 0)
test__muloti4(3037000499, 3037000499, 9223372030926249001, 0)
test__muloti4(-3037000499, 3037000499, -9223372030926249001, 0)
test__muloti4(3037000499, -3037000499, -9223372030926249001, 0)
test__muloti4(-3037000499, -3037000499, 9223372030926249001, 0)
test__muloti4(4398046511103, 2097152, 9223372036852678656, 0)
test__muloti4(-4398046511103, 2097152, -9223372036852678656, 0)
test__muloti4(4398046511103, -2097152, -9223372036852678656, 0)
test__muloti4(-4398046511103, -2097152, 9223372036852678656, 0)
test__muloti4(2097152, 4398046511103, 9223372036852678656, 0)
test__muloti4(-2097152, 4398046511103, -9223372036852678656, 0)
test__muloti4(2097152, -4398046511103, -9223372036852678656, 0)
test__muloti4(-2097152, -4398046511103, 9223372036852678656, 0)
test__muloti4(make_ti(0x00000000000000B5, 0x04F333F9DE5BE000),
make_ti(0x0000000000000000, 0x00B504F333F9DE5B),
make_ti(0x7FFFFFFFFFFFF328, 0xDF915DA296E8A000), 0)
test__muloti4(make_ti(0x7FFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF),
-2,
make_ti(0x8000000000000000, 0x0000000000000001), 1)
test__muloti4(-2,
make_ti(0x7FFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF),
make_ti(0x8000000000000000, 0x0000000000000001), 1)
test__muloti4(make_ti(0x7FFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF),
-1,
make_ti(0x8000000000000000, 0x0000000000000001), 0)
test__muloti4(-1,
make_ti(0x7FFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF),
make_ti(0x8000000000000000, 0x0000000000000001), 0)
test__muloti4(make_ti(0x7FFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF),
0,
0, 0)
test__muloti4(0,
make_ti(0x7FFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF),
0, 0)
test__muloti4(make_ti(0x7FFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF),
1,
make_ti(0x7FFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF), 0)
test__muloti4(1,
make_ti(0x7FFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF),
make_ti(0x7FFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF), 0)
test__muloti4(make_ti(0x7FFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF),
2,
make_ti(0x8000000000000000, 0x0000000000000001), 1)
test__muloti4(2,
make_ti(0x7FFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF),
make_ti(0x8000000000000000, 0x0000000000000001), 1)
test__muloti4(make_ti(0x8000000000000000, 0x0000000000000000),
-2,
make_ti(0x8000000000000000, 0x0000000000000000), 1)
test__muloti4(-2,
make_ti(0x8000000000000000, 0x0000000000000000),
make_ti(0x8000000000000000, 0x0000000000000000), 1)
test__muloti4(make_ti(0x8000000000000000, 0x0000000000000000),
-1,
make_ti(0x8000000000000000, 0x0000000000000000), 1)
test__muloti4(-1,
make_ti(0x8000000000000000, 0x0000000000000000),
make_ti(0x8000000000000000, 0x0000000000000000), 1)
test__muloti4(make_ti(0x8000000000000000, 0x0000000000000000),
0,
0, 0)
test__muloti4(0,
make_ti(0x8000000000000000, 0x0000000000000000),
0, 0)
test__muloti4(make_ti(0x8000000000000000, 0x0000000000000000),
1,
make_ti(0x8000000000000000, 0x0000000000000000), 0)
test__muloti4(1,
make_ti(0x8000000000000000, 0x0000000000000000),
make_ti(0x8000000000000000, 0x0000000000000000), 0)
test__muloti4(make_ti(0x8000000000000000, 0x0000000000000000),
2,
make_ti(0x8000000000000000, 0x0000000000000000), 1)
test__muloti4(2,
make_ti(0x8000000000000000, 0x0000000000000000),
make_ti(0x8000000000000000, 0x0000000000000000), 1)
test__muloti4(make_ti(0x8000000000000000, 0x0000000000000001),
-2,
make_ti(0x8000000000000000, 0x0000000000000001), 1)
test__muloti4(-2,
make_ti(0x8000000000000000, 0x0000000000000001),
make_ti(0x8000000000000000, 0x0000000000000001), 1)
test__muloti4(make_ti(0x8000000000000000, 0x0000000000000001),
-1,
make_ti(0x7FFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF), 0)
test__muloti4(-1,
make_ti(0x8000000000000000, 0x0000000000000001),
make_ti(0x7FFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF), 0)
test__muloti4(make_ti(0x8000000000000000, 0x0000000000000001),
0,
0, 0)
test__muloti4(0,
make_ti(0x8000000000000000, 0x0000000000000001),
0, 0)
test__muloti4(make_ti(0x8000000000000000, 0x0000000000000001),
1,
make_ti(0x8000000000000000, 0x0000000000000001), 0)
test__muloti4(1,
make_ti(0x8000000000000000, 0x0000000000000001),
make_ti(0x8000000000000000, 0x0000000000000001), 0)
test__muloti4(make_ti(0x8000000000000000, 0x0000000000000001),
2,
make_ti(0x8000000000000000, 0x0000000000000000), 1)
test__muloti4(2,
make_ti(0x8000000000000000, 0x0000000000000001),
make_ti(0x8000000000000000, 0x0000000000000000), 1)
end
Loading