Skip to content

Commit

Permalink
fix byte comparisons
Browse files Browse the repository at this point in the history
  • Loading branch information
saviorand committed Feb 16, 2025
1 parent d26d30b commit 39eacda
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 27 deletions.
14 changes: 13 additions & 1 deletion lightbug_http/io/bytes.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ struct ByteWriter(Writer):

@always_inline
fn consuming_write(mut self, owned s: String):
# kind of cursed but seems to work?
# kind of cursed but seems to work? pops the null terminator
_ = s._buffer.pop()
self._inner.extend(s._buffer^)
s._buffer = s._buffer_type()
Expand Down Expand Up @@ -144,6 +144,18 @@ struct ByteView[origin: Origin](Testable):
if self[i] != other[i]:
return False
return True

fn __eq__(self, other: Bytes) -> Bool:
# Check if lengths match
if len(self) != len(other):
return False

# Compare each byte
for i in range(len(self)):
if self[i] != other[i]:
return False
return True


fn __ne__(self, other: Self) -> Bool:
return not self == other
Expand Down
25 changes: 12 additions & 13 deletions tests/lightbug_http/io/test_byte_reader.mojo
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import testing
from lightbug_http.io.bytes import Bytes, ByteReader, EndOfReaderError
from lightbug_http.strings import to_string

alias example = "Hello, World!"

Expand All @@ -15,61 +16,59 @@ def test_peek():
r.read_pos = 1
with testing.assert_raises(contains="No more bytes to read."):
_ = r.peek()


def test_read_until():
var r = ByteReader(example.as_bytes())
testing.assert_equal(r.read_pos, 0)
testing.assert_equal(r.read_until(ord(",")).to_bytes(), Bytes(72, 101, 108, 108, 111))
testing.assert_equal(to_string(r.read_until(ord(",")).to_bytes()), to_string(Bytes(72, 101, 108, 108, 111)))
testing.assert_equal(r.read_pos, 5)


def test_read_bytes():
var r = ByteReader(example.as_bytes())
testing.assert_equal(r.read_bytes().to_bytes(), Bytes(72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33))
testing.assert_equal(to_string(r.read_bytes().to_bytes()), to_string(Bytes(72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33)))

r = ByteReader(example.as_bytes())
testing.assert_equal(r.read_bytes(7).to_bytes(), Bytes(72, 101, 108, 108, 111, 44, 32))
testing.assert_equal(r.read_bytes().to_bytes(), Bytes(87, 111, 114, 108, 100, 33))
testing.assert_equal(to_string(r.read_bytes(7).to_bytes()), to_string(Bytes(72, 101, 108, 108, 111, 44, 32)))
testing.assert_equal(to_string(r.read_bytes().to_bytes()), to_string(Bytes(87, 111, 114, 108, 100, 33)))


def test_read_word():
var r = ByteReader(example.as_bytes())
testing.assert_equal(r.read_pos, 0)
testing.assert_equal(r.read_word().to_bytes(), Bytes(72, 101, 108, 108, 111, 44))
testing.assert_equal(to_string(r.read_word().to_bytes()), to_string(Bytes(72, 101, 108, 108, 111, 44)))
testing.assert_equal(r.read_pos, 6)


def test_read_line():
# No newline, go to end of line
var r = ByteReader(example.as_bytes())
testing.assert_equal(r.read_pos, 0)
testing.assert_equal(r.read_line().to_bytes(), Bytes(72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33))
testing.assert_equal(to_string(r.read_line().to_bytes()), to_string(Bytes(72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33)))
testing.assert_equal(r.read_pos, 13)

# Newline, go to end of line. Should cover carriage return and newline
var r2 = ByteReader("Hello\r\nWorld\n!".as_bytes())
testing.assert_equal(r2.read_pos, 0)
testing.assert_equal(r2.read_line().to_bytes(), Bytes(72, 101, 108, 108, 111))
testing.assert_equal(to_string(r2.read_line().to_bytes()), to_string(Bytes(72, 101, 108, 108, 111)))
testing.assert_equal(r2.read_pos, 7)
testing.assert_equal(r2.read_line().to_bytes(), Bytes(87, 111, 114, 108, 100))
testing.assert_equal(to_string(r2.read_line().to_bytes()), to_string(Bytes(87, 111, 114, 108, 100)))
testing.assert_equal(r2.read_pos, 13)


def test_skip_whitespace():
var r = ByteReader(" Hola".as_bytes())
r.skip_whitespace()
testing.assert_equal(r.read_pos, 1)
testing.assert_equal(r.read_word().to_bytes(), Bytes(72, 111, 108, 97))
testing.assert_equal(to_string(r.read_word().to_bytes()), to_string(Bytes(72, 111, 108, 97)))


def test_skip_carriage_return():
var r = ByteReader("\r\nHola".as_bytes())
r.skip_carriage_return()
testing.assert_equal(r.read_pos, 2)
testing.assert_equal(r.read_bytes(4).to_bytes(), Bytes(72, 111, 108, 97))
testing.assert_equal(to_string(r.read_bytes(4).to_bytes()), to_string(Bytes(72, 111, 108, 97)))


def test_consume():
var r = ByteReader(example.as_bytes())
testing.assert_equal(r^.consume(), Bytes(72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33))
testing.assert_equal(to_string(r^.consume()), to_string(Bytes(72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33)))
19 changes: 8 additions & 11 deletions tests/lightbug_http/io/test_byte_writer.mojo
Original file line number Diff line number Diff line change
@@ -1,33 +1,30 @@
import testing
from lightbug_http.io.bytes import Bytes, ByteWriter

from lightbug_http.strings import to_string

def test_write_byte():
var w = ByteWriter()
w.write_byte(0x01)
testing.assert_equal(w^.consume(), Bytes(0x01))
testing.assert_equal(to_string(w^.consume()), to_string(Bytes(0x01)))

w = ByteWriter()
w.write_byte(2)
testing.assert_equal(w^.consume(), Bytes(2))
testing.assert_equal(to_string(w^.consume()), to_string(Bytes(2)))


def test_consuming_write():
var w = ByteWriter()
var my_string: String = "World"
w.consuming_write("Hello ")
w.consuming_write(my_string^)
testing.assert_equal(w^.consume(), Bytes(72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100))

w = ByteWriter()
var my_bytes = Bytes(72, 101, 108, 108, 111, 32)
w.consuming_write(my_bytes^)
w.consuming_write(Bytes(87, 111, 114, 108, 10))
testing.assert_equal(w^.consume(), Bytes(72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100))

var result = w^.consume()

testing.assert_equal(to_string(result), "Hello World")


def test_write():
var w = ByteWriter()
w.write("Hello", ", ")
w.write_bytes("World!".as_bytes())
testing.assert_equal(w^.consume(), Bytes(72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33))
testing.assert_equal(to_string(w^.consume()), to_string(Bytes(72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33)))
5 changes: 3 additions & 2 deletions tests/lightbug_http/io/test_bytes.mojo
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import testing
from collections import Dict, List
from lightbug_http.io.bytes import Bytes, ByteView, bytes
from lightbug_http.strings import to_string


fn test_string_literal_to_bytes() raises:
Expand All @@ -17,7 +18,7 @@ fn test_string_literal_to_bytes() raises:
)

for c in cases.items():
testing.assert_equal(Bytes(c[].key.as_bytes()), c[].value)
testing.assert_equal(to_string(Bytes(c[].key.as_bytes())), to_string(c[].value))


fn test_string_to_bytes() raises:
Expand All @@ -34,5 +35,5 @@ fn test_string_to_bytes() raises:
)

for c in cases.items():
testing.assert_equal(Bytes(c[].key.as_bytes()), c[].value)
testing.assert_equal(to_string(Bytes(c[].key.as_bytes())), to_string(c[].value))

0 comments on commit 39eacda

Please sign in to comment.