Skip to content

Commit

Permalink
Merge pull request #2047 from actonlang/u16-to-int
Browse files Browse the repository at this point in the history
Fix u16 to int conversion
  • Loading branch information
plajjan authored Dec 23, 2024
2 parents 8201fe4 + 2c50a00 commit 6f17178
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
4 changes: 2 additions & 2 deletions base/builtin/int.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ B_int B_intG_new(B_atom a, B_int base) {
return res;
}
}
if ($ISINSTANCE0(a,B_u32)) {
unsigned short v = ((B_u32)a)->val;
if ($ISINSTANCE0(a,B_u16)) {
unsigned short v = ((B_u16)a)->val;
if (v==0)
return to$int(0L);
else {
Expand Down
54 changes: 52 additions & 2 deletions test/builtins_auto/int.act
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ def test_i16():
raise ValueError("unexpected: bool(x) != 0")
if x != 0:
raise ValueError("unexpected: x != 0")
return True
if i16("1234") != 1234:
raise ValueError('i16("1234") gives wrong result')
if i16("0x1234") != 4660:
Expand All @@ -97,6 +96,7 @@ def test_i16():
raise ValueError('i16("0b1111") gives wrong result')
if i16("1234",7) != 466:
raise ValueError('i16("1234",7) gives wrong result')
return True

def test_i32():
test_i32_divzero()
Expand All @@ -105,6 +105,18 @@ def test_i32():
raise ValueError("unexpected: bool(x) != 0")
if x != 0:
raise ValueError("unexpected: x != 0")
if i32("1234") != 1234:
raise ValueError('i32("1234") gives wrong result')
if i32("0x1234") != 4660:
raise ValueError('i32("0x1234") gives wrong result')
if i32("0o1234") != 668:
raise ValueError('i32("0o1234") gives wrong result')
if i32("0b1111") != 15:
raise ValueError('i32("0b1111") gives wrong result')
if i32("1234", 7) != 466:
raise ValueError('i32("1234",7) gives wrong result')
if int(x) != 0:
raise ValueError("unexpected: int(x) != 0")
return True

def test_i64():
Expand All @@ -114,6 +126,18 @@ def test_i64():
raise ValueError("unexpected: bool(x) != 0")
if x != 0:
raise ValueError("unexpected: x != 0")
if i64("1234") != 1234:
raise ValueError('i64("1234") gives wrong result')
if i64("0x1234") != 4660:
raise ValueError('i64("0x1234") gives wrong result')
if i64("0o1234") != 668:
raise ValueError('i64("0o1234") gives wrong result')
if i64("0b1111") != 15:
raise ValueError('i64("0b1111") gives wrong result')
if i64("1234", 7) != 466:
raise ValueError('i64("1234",7) gives wrong result')
if int(x) != 0:
raise ValueError("unexpected: int(x) != 0")
return True

def test_u16():
Expand All @@ -123,6 +147,18 @@ def test_u16():
raise ValueError("unexpected: bool(x) != 0")
if x != 0:
raise ValueError("unexpected: x != 0")
if u16("1234") != 1234:
raise ValueError('u16("1234") gives wrong result')
if u16("0x1234") != 4660:
raise ValueError('u16("0x1234") gives wrong result')
if u16("0o1234") != 668:
raise ValueError('u16("0o1234") gives wrong result')
if u16("0b1111") != 15:
raise ValueError('u16("0b1111") gives wrong result')
if u16("1234", 7) != 466:
raise ValueError('u16("1234",7) gives wrong result')
if int(x) != 0:
raise ValueError("unexpected: int(x) != 0")
return True

def test_u32():
Expand All @@ -132,6 +168,18 @@ def test_u32():
raise ValueError("unexpected: bool(x) != 0")
if x != 0:
raise ValueError("unexpected: x != 0")
if u32("1234") != 1234:
raise ValueError('u32("1234") gives wrong result')
if u32("0x1234") != 4660:
raise ValueError('u32("0x1234") gives wrong result')
if u32("0o1234") != 668:
raise ValueError('u32("0o1234") gives wrong result')
if u32("0b1111") != 15:
raise ValueError('u32("0b1111") gives wrong result')
if u32("1234", 7) != 466:
raise ValueError('u32("1234",7) gives wrong result')
if int(x) != 0:
raise ValueError("unexpected: int(x) != 0")
return True

def test_u64():
Expand All @@ -141,7 +189,6 @@ def test_u64():
raise ValueError("unexpected: bool(x) != 0")
if x != 0:
raise ValueError("unexpected: x != 0")
return True
if u64("1234") != 1234:
raise ValueError('u64("1234") gives wrong result')
if u64("0x1234") != 4660:
Expand All @@ -152,6 +199,9 @@ def test_u64():
raise ValueError('u64("0b1111") gives wrong result')
if u64("1234",7) != 466:
raise ValueError('u64("1234",7) gives wrong result')
if int(x) != 0:
raise ValueError("unexpected: int(x) != 0")
return True



Expand Down

0 comments on commit 6f17178

Please sign in to comment.