Skip to content

Commit

Permalink
Tests for SIMD.from_bytes and SIMD.as_bytes
Browse files Browse the repository at this point in the history
Signed-off-by: Manuel Saelices <[email protected]>
  • Loading branch information
msaelices committed Dec 16, 2024
1 parent 0e5d293 commit 6e144d5
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions stdlib/test/builtin/test_simd.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -1818,6 +1818,48 @@ def test_float_conversion():
assert_almost_equal(float(UInt64(36)), 36.0)


def test_from_bytes_as_bytes():
alias Bytes = List[Byte]

assert_equal(Int16.from_bytes[big_endian=True](Bytes(0, 16)), 16)
assert_equal(Int16.from_bytes[big_endian=False](Bytes(0, 16)), 4096)
assert_equal(Int16.from_bytes[big_endian=True](Bytes(252, 0)), -1024)
assert_equal(UInt16.from_bytes[big_endian=True](Bytes(252, 0)), 64512)
assert_equal(Int16.from_bytes[big_endian=False](Bytes(252, 0)), 252)
assert_equal(Int32.from_bytes[big_endian=True](Bytes(0, 0, 0, 1)), 1)
assert_equal(
Int32.from_bytes[big_endian=False](Bytes(0, 0, 0, 1)),
16777216,
)
assert_equal(
Int32.from_bytes[big_endian=True](Bytes(1, 0, 0, 0)),
16777216,
)
assert_equal(
Int32.from_bytes[big_endian=True](Bytes(1, 0, 0, 1)),
16777217,
)
assert_equal(
Int32.from_bytes[big_endian=False](Bytes(1, 0, 0, 1)),
16777217,
)
assert_equal(
Int32.from_bytes[big_endian=True](Bytes(255, 0, 0, 0)),
-16777216,
)
for x_ref in List[Int16](10, 100, -12, 0, 1, -1, 1000, -1000):
x = x_ref[]

@parameter
for b in range(2):
assert_equal(
Int16.from_bytes[big_endian=b](
Int16(x).as_bytes[big_endian=b]()
),
x,
)


def test_reversed():
fn test[D: DType]() raises:
assert_equal(SIMD[D, 4](1, 2, 3, 4).reversed(), SIMD[D, 4](4, 3, 2, 1))
Expand Down Expand Up @@ -1847,6 +1889,7 @@ def main():
test_extract()
test_floor()
test_floordiv()
test_from_bytes_as_bytes()
test_iadd()
test_indexing()
test_insert()
Expand Down

0 comments on commit 6e144d5

Please sign in to comment.