Skip to content

Commit

Permalink
feat: i32 to u64 (#1118)
Browse files Browse the repository at this point in the history
  • Loading branch information
qazxcdswe123 authored Oct 14, 2024
1 parent 09c26b2 commit 25f3c2d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions builtin/builtin.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,7 @@ impl Int {
to_json(Int) -> Json
to_string(Int) -> String
to_uint(Int) -> UInt //deprecated
to_uint64(Int) -> UInt64
until(Int, Int, ~step : Int = .., ~inclusive : Bool = ..) -> Iter[Int]
upto(Int, Int, ~inclusive : Bool = ..) -> Iter[Int] //deprecated
}
Expand Down
4 changes: 4 additions & 0 deletions builtin/intrinsics.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ pub fn Int::reinterpret_as_uint(self : Int) -> UInt = "%i32.to_u32_reinterpret"
pub fn Int::to_uint(self : Int) -> UInt = "%i32.to_u32_reinterpret"
// Double primitive ops

pub fn Int::to_uint64(self : Int) -> UInt64 {
self.to_int64().reinterpret_as_uint64()
}

pub fn Double::op_neg(self : Double) -> Double = "%f64_neg"

pub fn Double::op_add(self : Double, other : Double) -> Double = "%f64_add"
Expand Down
25 changes: 25 additions & 0 deletions builtin/intrinsics_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -384,3 +384,28 @@ test "Char::to_uint - Unicode Characters" {
inspect!(Char::to_uint('⚡'), content="9889")
inspect!(Char::to_uint('♫'), content="9835")
}

test "Int::to_uint64 - Boundary Cases" {
inspect!(Int::to_uint64(0), content="0")
inspect!(Int::to_uint64(1), content="1")
inspect!(Int::to_uint64(-1), content="18446744073709551615")
inspect!(Int::to_uint64(2147483647), content="2147483647")
inspect!(Int::to_uint64(-2147483648), content="18446744071562067968")
}

test "Int::to_uint64 - Random Cases" {
inspect!(Int::to_uint64(123456789), content="123456789")
inspect!(Int::to_uint64(-987654321), content="18446744072721897295")
inspect!(Int::to_uint64(987654321), content="987654321")
inspect!(Int::to_uint64(-123456789), content="18446744073586094827")
}

test "Int::to_uint64 - Edge Cases with Zero" {
inspect!(Int::to_uint64(0), content="0")
inspect!(Int::to_uint64(-0), content="0")
}

test "Int::to_uint64 - Random Values" {
inspect!(Int::to_uint64(1234567890), content="1234567890")
inspect!(Int::to_uint64(-1234567890), content="18446744072474983726")
}

0 comments on commit 25f3c2d

Please sign in to comment.