Skip to content

Commit

Permalink
Merge branch 'master' into refactor/llvm-ext-set-current-debug-location2
Browse files Browse the repository at this point in the history
  • Loading branch information
HertzDevil committed Nov 11, 2023
2 parents 4a42f51 + 4001a79 commit 5814ba4
Show file tree
Hide file tree
Showing 26 changed files with 1,159 additions and 549 deletions.
2 changes: 1 addition & 1 deletion spec/std/big/big_int_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ describe "BigInt" do

it "raises if factorial of 2^64" do
expect_raises ArgumentError do
(LibGMP::ULong::MAX.to_big_i + 1).factorial
(LibGMP::UI::MAX.to_big_i + 1).factorial
end
end

Expand Down
96 changes: 96 additions & 0 deletions spec/std/int_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,102 @@ describe "Int" do
end
end

describe "#to_signed" do
{% for n in [8, 16, 32, 64, 128] %}
it "does for Int{{n}}" do
x = Int{{n}}.new(123).to_signed
x.should be_a(Int{{n}})
x.should eq(123)

Int{{n}}.new(-123).to_signed.should eq(-123)
Int{{n}}::MIN.to_signed.should eq(Int{{n}}::MIN)
Int{{n}}::MAX.to_signed.should eq(Int{{n}}::MAX)
end

it "does for UInt{{n}}" do
x = UInt{{n}}.new(123).to_signed
x.should be_a(Int{{n}})
x.should eq(123)

UInt{{n}}::MIN.to_signed.should eq(0)
expect_raises(OverflowError) { UInt{{n}}::MAX.to_signed }
expect_raises(OverflowError) { (UInt{{n}}.new(Int{{n}}::MAX) + 1).to_signed }
end
{% end %}
end

describe "#to_signed!" do
{% for n in [8, 16, 32, 64, 128] %}
it "does for Int{{n}}" do
x = Int{{n}}.new(123).to_signed!
x.should be_a(Int{{n}})
x.should eq(123)

Int{{n}}.new(-123).to_signed!.should eq(-123)
Int{{n}}::MIN.to_signed!.should eq(Int{{n}}::MIN)
Int{{n}}::MAX.to_signed!.should eq(Int{{n}}::MAX)
end

it "does for UInt{{n}}" do
x = UInt{{n}}.new(123).to_signed!
x.should be_a(Int{{n}})
x.should eq(123)

UInt{{n}}::MIN.to_signed!.should eq(0)
UInt{{n}}::MAX.to_signed!.should eq(-1)
(UInt{{n}}::MAX - 122).to_signed!.should eq(-123)
(UInt{{n}}.new(Int{{n}}::MAX) + 1).to_signed!.should eq(Int{{n}}::MIN)
end
{% end %}
end

describe "#to_unsigned" do
{% for n in [8, 16, 32, 64, 128] %}
it "does for Int{{n}}" do
x = Int{{n}}.new(123).to_unsigned
x.should be_a(UInt{{n}})
x.should eq(123)

Int{{n}}.zero.to_unsigned.should eq(UInt{{n}}::MIN)
Int{{n}}::MAX.to_unsigned.should eq(UInt{{n}}.new(Int{{n}}::MAX))
expect_raises(OverflowError) { Int{{n}}::MIN.to_unsigned }
end

it "does for UInt{{n}}" do
x = UInt{{n}}.new(123).to_unsigned
x.should be_a(UInt{{n}})
x.should eq(123)

UInt{{n}}::MIN.to_unsigned.should eq(UInt{{n}}::MIN)
UInt{{n}}::MAX.to_unsigned.should eq(UInt{{n}}::MAX)
end
{% end %}
end

describe "#to_unsigned!" do
{% for n in [8, 16, 32, 64, 128] %}
it "does for Int{{n}}" do
x = Int{{n}}.new(123).to_unsigned!
x.should be_a(UInt{{n}})
x.should eq(123)

Int{{n}}.new(-123).to_unsigned!.should eq(UInt{{n}}::MAX - 122)
Int{{n}}::MIN.to_unsigned!.should eq(UInt{{n}}::MAX // 2 + 1)
Int{{n}}::MAX.to_unsigned!.should eq(UInt{{n}}::MAX // 2)
Int{{n}}.new(-1).to_unsigned!.should eq(UInt{{n}}::MAX)
end

it "does for UInt{{n}}" do
x = UInt{{n}}.new(123).to_unsigned!
x.should be_a(UInt{{n}})
x.should eq(123)

UInt{{n}}::MIN.to_unsigned!.should eq(UInt{{n}}::MIN)
UInt{{n}}::MAX.to_unsigned!.should eq(UInt{{n}}::MAX)
end
{% end %}
end

describe "#abs_unsigned" do
{% for int in Int::Signed.union_types %}
it "does for {{ int }}" do
Expand Down
9 changes: 9 additions & 0 deletions src/array.cr
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,21 @@ class Array(T)
#
# ```
# Array.new(3, 'a') # => ['a', 'a', 'a']
# ```
#
# WARNING: The initial value is filled into the array as-is. It gets neither
# duplicated nor cloned. For types with reference semantics this means every
# item will point to the *same* object.
#
# ```
# ary = Array.new(3, [1])
# ary # => [[1], [1], [1]]
# ary[0][0] = 2
# ary # => [[2], [2], [2]]
# ```
#
# * `.new(Int, & : Int32 -> T)` is an alternative that allows using a
# different initial value for each position.
def initialize(size : Int, value : T)
if size < 0
raise ArgumentError.new("Negative array size: #{size}")
Expand Down
51 changes: 21 additions & 30 deletions src/big/big_float.cr
Original file line number Diff line number Diff line change
Expand Up @@ -40,29 +40,16 @@ struct BigFloat < Float
LibGMP.mpf_set(self, num)
end

def initialize(num : Int8 | Int16 | Int32)
LibGMP.mpf_init_set_si(out @mpf, num)
end

def initialize(num : UInt8 | UInt16 | UInt32)
LibGMP.mpf_init_set_ui(out @mpf, num)
end

def initialize(num : Int64)
if LibGMP::Long == Int64
LibGMP.mpf_init_set_si(out @mpf, num)
else
LibGMP.mpf_init(out @mpf)
LibGMP.mpf_set_z(self, num.to_big_i)
end
end

def initialize(num : UInt64)
if LibGMP::ULong == UInt64
LibGMP.mpf_init_set_ui(out @mpf, num)
else
LibGMP.mpf_init(out @mpf)
LibGMP.mpf_set_z(self, num.to_big_i)
def initialize(num : Int)
Int.primitive_si_ui_check(num) do |si, ui, big_i|
{
si: LibGMP.mpf_init_set_si(out @mpf, {{ si }}),
ui: LibGMP.mpf_init_set_ui(out @mpf, {{ ui }}),
big_i: begin
LibGMP.mpf_init(out @mpf)
LibGMP.mpf_set_z(self, {{ big_i }})
end,
}
end
end

Expand Down Expand Up @@ -113,16 +100,20 @@ struct BigFloat < Float
LibGMP.mpf_cmp_d(self, other) unless other.nan?
end

def <=>(other : Number)
if other.is_a?(Int8 | Int16 | Int32) || (LibGMP::Long == Int64 && other.is_a?(Int64))
LibGMP.mpf_cmp_si(self, other)
elsif other.is_a?(UInt8 | UInt16 | UInt32) || (LibGMP::ULong == UInt64 && other.is_a?(UInt64))
LibGMP.mpf_cmp_ui(self, other)
else
LibGMP.mpf_cmp(self, other.to_big_f)
def <=>(other : Int)
Int.primitive_si_ui_check(other) do |si, ui, big_i|
{
si: LibGMP.mpf_cmp_si(self, {{ si }}),
ui: LibGMP.mpf_cmp_ui(self, {{ ui }}),
big_i: self <=> {{ big_i }},
}
end
end

def <=>(other : Number)
LibGMP.mpf_cmp(self, other.to_big_f)
end

def - : BigFloat
BigFloat.new { |mpf| LibGMP.mpf_neg(mpf, self) }
end
Expand Down
Loading

0 comments on commit 5814ba4

Please sign in to comment.