Skip to content

Commit

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

describe "#to_unsigned" do
{% for n in [8, 16, 32, 64, 128] %}
it "does for Int{{n}}" do
x = 123_i{{n}}.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 = 123_u{{n}}.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)

(-123_i{{n}}).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)
(-1_i{{n}}).to_unsigned!.should eq(UInt{{n}}::MAX)
end

it "does for UInt{{n}}" do
x = 123_u{{n}}.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
Loading

0 comments on commit 5b6c7c1

Please sign in to comment.