Skip to content

Commit

Permalink
feat: char to uint (#1120)
Browse files Browse the repository at this point in the history
  • Loading branch information
qazxcdswe123 authored Oct 14, 2024
1 parent b458397 commit 09c26b2
Show file tree
Hide file tree
Showing 3 changed files with 46 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 @@ -394,6 +394,7 @@ impl Char {
to_int(Char) -> Int
to_json(Char) -> Json
to_string(Char) -> String
to_uint(Char) -> UInt
}
impl Int {
asr(Int, Int) -> 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 @@ -138,6 +138,10 @@ pub fn Double::convert_uint(val : UInt) -> Double = "%u32.to_f64"

pub fn Char::to_int(self : Char) -> Int = "%char_to_int"

pub fn Char::to_uint(self : Char) -> UInt {
self.to_int().reinterpret_as_uint()
}

pub fn Char::from_int(val : Int) -> Char = "%char_from_int"

pub fn Char::op_equal(self : Char, other : Char) -> Bool = "%char_eq"
Expand Down
41 changes: 41 additions & 0 deletions builtin/intrinsics_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -343,3 +343,44 @@ test "grouped test for random cases" {
inspect!(Float::reinterpret_as_uint(1.0e+30), content="1900671690")
inspect!(Float::reinterpret_as_uint(-1.0e+30), content="4048155338")
}

test "Char::to_uint - Boundary Cases" {
// Test for the boundary values of Char
inspect!(Char::to_uint('\u{0}'), content="0")
inspect!(Char::to_uint('\u{FFFF}'), content="65535")
}

test "Char::to_uint - Random Cases" {
// Test for some random values of Char
inspect!(Char::to_uint('a'), content="97")
inspect!(Char::to_uint('A'), content="65")
inspect!(Char::to_uint('1'), content="49")
inspect!(Char::to_uint('!'), content="33")
inspect!(Char::to_uint(' '), content="32")
inspect!(Char::to_uint('中'), content="20013")
}

test "Char::to_uint - Edge Cases" {
// Test for some edge cases
inspect!(Char::to_uint('\u{0001}'), content="1")
inspect!(Char::to_uint('\u{00FF}'), content="255")
inspect!(Char::to_uint('\u{0100}'), content="256")
inspect!(Char::to_uint('\u{FFFE}'), content="65534")
}

test "Char::to_uint - Special Characters" {
// Test for special characters
inspect!(Char::to_uint('\n'), content="10")
inspect!(Char::to_uint('\t'), content="9")
inspect!(Char::to_uint('\r'), content="13")
inspect!(Char::to_uint('\\'), content="92")
inspect!(Char::to_uint('\''), content="39")
}

test "Char::to_uint - Unicode Characters" {
// Test for some Unicode characters
inspect!(Char::to_uint('😀'), content="128512")
inspect!(Char::to_uint('🌍'), content="127757")
inspect!(Char::to_uint('⚡'), content="9889")
inspect!(Char::to_uint('♫'), content="9835")
}

0 comments on commit 09c26b2

Please sign in to comment.