Skip to content

Commit

Permalink
#to_signed(!)
Browse files Browse the repository at this point in the history
  • Loading branch information
HertzDevil committed Nov 7, 2023
1 parent e838701 commit c228930
Show file tree
Hide file tree
Showing 2 changed files with 329 additions and 0 deletions.
49 changes: 49 additions & 0 deletions spec/std/int_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,55 @@ describe "Int" do
end
end

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

(-123_i{{n}}).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 = 123_u{{n}}.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 = 123_i{{n}}.to_signed!
x.should be_a(Int{{n}})
x.should eq(123)

(-123_i{{n}}).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 = 123_u{{n}}.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 "#abs_unsigned" do
{% for int in Int::Signed.union_types %}
it "does for {{ int }}" do
Expand Down
Loading

0 comments on commit c228930

Please sign in to comment.