Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: improve coverage for builtin/bytes.mbt #1591

Merged
merged 1 commit into from
Feb 5, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions builtin/bytes_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,57 @@ test "fixedarray_byte_blit_from_bytes" {
arr.blit_from_bytes(0, bytes, 0, bytes.length())
inspect!(arr, content="[b'\\x48', b'\\x65', b'\\x6C', b'\\x6C', b'\\x6F']")
}

///|
test "Bytes::op_equal with differing bytes" {
let bytes1 = b"\x01\x02\x03"
let bytes2 = b"\x01\x02\xFF" // Same length as bytes1 but last byte differs
inspect!(bytes1 == bytes2, content="false")
}

///|
test "blit_from_bytesview basic case" {
let arr = FixedArray::make(4, b'\x00')
let view = b"\x01\x02\x03"[1:] // view contains "\x02\x03"
arr.blit_from_bytesview(1, view)
inspect!(arr, content="[b'\\x00', b'\\x02', b'\\x03', b'\\x00']")
}

///|
test "panic FixedArray::set_utf8_char/invalid_code_point" {
let buf = FixedArray::make(4, b'\x00')
// Creating a character with code point greater than 0x10FFFF (1114111)
let invalid_char = Char::from_int(0x110001)
// This should trigger the "Char out of range" panic
let _ = buf.set_utf8_char(0, invalid_char)

}

///|
test "set_utf16be_char with surrogate pairs" {
let buf = FixedArray::make(4, b'\x00')
// Test surrogate pair encoding for a supplementary plane character (U+10437)
let c = '\u{10437}'
let len = buf.set_utf16be_char(0, c)
inspect!(len, content="4")
// Check the generated bytes for correct UTF-16BE encoding
// High surrogate: D801, Low surrogate: DC37
inspect!(buf[0], content="b'\\xD8'")
inspect!(buf[1], content="b'\\x01'")
inspect!(buf[2], content="b'\\xDC'")
inspect!(buf[3], content="b'\\x37'")
}

///|
test "panic set_utf16be_char with out of range code point" {
let buf = FixedArray::make(4, b'\x00')
ignore(buf.set_utf16be_char(0, Char::from_int(0x110000)))
}

///|
test "Bytes::makei with non-positive length" {
let bytes = Bytes::makei(0, fn(_i) { b'\x00' })
inspect!(bytes, content="b\"\"")
let bytes2 = Bytes::makei(-1, fn(_i) { b'\x00' })
inspect!(bytes2, content="b\"\"")
}