Skip to content

Commit

Permalink
nightly update (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
thatstoasty committed Jun 17, 2024
1 parent 111082d commit 31b75c6
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 45 deletions.
3 changes: 2 additions & 1 deletion gojo/bytes/buffer.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ struct Buffer(
A List[Byte] struct containing the data up to and including the delimiter.
"""
var at_eof = False
var i = index_byte(self.buf[self.off : len(self.buf)], (delim))
var i = index_byte(self.buf[self.off : len(self.buf)], delim)
var end = self.off + i + 1

if i < 0:
Expand Down Expand Up @@ -591,6 +591,7 @@ struct Buffer(
var slice: List[Byte]
var err: Error
slice, err = self.read_slice(delim)
slice.append(0)
return String(slice), err


Expand Down
2 changes: 1 addition & 1 deletion gojo/net/fd.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ struct FileDescriptor(FileDescriptorBase):

fn write(inout self, src: List[Byte]) -> (Int, Error):
"""Write data from the buffer to the file descriptor."""
var header_pointer = Pointer[Int8](src.data.value).bitcast[UInt8]()
var header_pointer = Pointer[Int8](src.data.address).bitcast[UInt8]()

var bytes_sent = send(self.fd, header_pointer, strlen(header_pointer), 0)
if bytes_sent == -1:
Expand Down
2 changes: 1 addition & 1 deletion goodies/file.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ struct FileWrapper(io.ReadWriteSeeker, io.ByteReader):
try:
var copy = List[Byte](src)
var bytes_length = len(copy)
self.handle.write(StringRef(copy.steal_data().value, bytes_length))
self.handle.write(StringRef(copy.steal_data(), bytes_length))
return len(src), Error(io.EOF)
except e:
return 0, Error(e)
28 changes: 14 additions & 14 deletions tests/test_bufio.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn test_read_all() raises:
var buf = buffer.new_reader(s)
var reader = Reader(buf)
var result = read_all(reader)
var bytes = result.get[0]()
var bytes = result[0]
bytes.append(0)
test.assert_equal(String(bytes), "0123456789")

Expand Down Expand Up @@ -61,7 +61,7 @@ fn test_read_and_unread_byte() raises:
var reader = Reader(buf)
var buffer = List[Byte](capacity=512)
var result = reader.read_byte()
test.assert_equal(result.get[0](), 72)
test.assert_equal(int(result[0]), int(72))
var post_read_position = reader.read_pos

# Unread the first byte from the reader. Read position should be moved back by 1
Expand All @@ -75,8 +75,8 @@ fn test_read_slice() raises:
var reader = Reader(buf)

var result = reader.read_slice(ord(5))
var bytes = result.get[0]()
test.assert_equal(to_string(result.get[0]()), "012345")
var bytes = result[0]
test.assert_equal(to_string(result[0]), "012345")


fn test_read_bytes() raises:
Expand All @@ -85,7 +85,7 @@ fn test_read_bytes() raises:
var reader = Reader(buf)

var result = reader.read_bytes(ord("\n"))
test.assert_equal(to_string(result.get[0]()), "01234")
test.assert_equal(to_string(result[0]), "01234")


fn test_read_line() raises:
Expand All @@ -96,7 +96,7 @@ fn test_read_line() raises:
var line: List[Byte]
var b: Bool
line, b = reader.read_line()
test.assert_equal(line, "01234")
test.assert_equal(String(line), "01234")


fn test_peek() raises:
Expand All @@ -107,8 +107,8 @@ fn test_peek() raises:
# Peek doesn't advance the reader, so we should see the same content twice.
var result = reader.peek(5)
var second_result = reader.peek(5)
test.assert_equal(to_string(result.get[0]()), "01234")
test.assert_equal(to_string(second_result.get[0]()), "01234")
test.assert_equal(to_string(result[0]), "01234")
test.assert_equal(to_string(second_result[0]), "01234")


fn test_discard() raises:
Expand All @@ -117,11 +117,11 @@ fn test_discard() raises:
var reader = Reader(buf)

var result = reader.discard(5)
test.assert_equal(result.get[0](), 5)
test.assert_equal(result[0], 5)

# Peek doesn't advance the reader, so we should see the same content twice.
var second_result = reader.peek(5)
test.assert_equal(to_string(second_result.get[0]()), "56789")
test.assert_equal(to_string(second_result[0]), "56789")


fn test_write() raises:
Expand All @@ -136,7 +136,7 @@ fn test_write() raises:
var result = writer.write(src)
_ = writer.flush()

test.assert_equal(result.get[0](), 10)
test.assert_equal(result[0], 10)
test.assert_equal(str(writer.writer), "0123456789")


Expand Down Expand Up @@ -190,7 +190,7 @@ fn test_write_byte() raises:
var result = writer.write_byte(32)
_ = writer.flush()

test.assert_equal(result.get[0](), 1)
test.assert_equal(result[0], 1)
test.assert_equal(str(writer.writer), "Hello ")


Expand All @@ -205,7 +205,7 @@ fn test_write_string() raises:
var result = writer.write_string(" World!")
_ = writer.flush()

test.assert_equal(result.get[0](), 7)
test.assert_equal(result[0], 7)
test.assert_equal(str(writer.writer), "Hello World!")


Expand All @@ -222,7 +222,7 @@ fn test_read_from() raises:
var result = writer.read_from(reader_from)
_ = writer.flush()

test.assert_equal(result.get[0](), 7)
test.assert_equal(int(result[0]), 7)
test.assert_equal(str(writer.writer), "Hello World!")


Expand Down
2 changes: 1 addition & 1 deletion tests/test_bufio_scanner.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ fn test_scan_bytes() raises:
var j = 0

while scanner.scan():
test.assert_equal(scanner.current_token_as_bytes(), test_case[j].as_bytes())
test.assert_equal(String(scanner.current_token_as_bytes()), test_case[j])
j += 1


Expand Down
18 changes: 12 additions & 6 deletions tests/test_bytes_buffer.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ fn test_read_byte() raises:
var s: String = "Hello World!"
var buf = new_buffer(s)
var result = buf.read_byte()
test.assert_equal(result.get[0](), 72)
test.assert_equal(int(result[0]), 72)


fn test_unread_byte() raises:
var test = MojoTest("Testing bytes.Buffer.unread_byte")
var s: String = "Hello World!"
var buf = new_buffer(s)
var result = buf.read_byte()
test.assert_equal(result.get[0](), 72)
test.assert_equal(int(result[0]), 72)
test.assert_equal(buf.off, 1)

_ = buf.unread_byte()
Expand All @@ -38,29 +38,35 @@ fn test_read_bytes() raises:
var s: String = "Hello World!"
var buf = new_buffer(s)
var result = buf.read_bytes(ord("o"))
test.assert_equal(result.get[0](), String("Hello").as_bytes())
var text = result[0]
text.append(0)
test.assert_equal(String(text), String("Hello"))


fn test_read_slice() raises:
var test = MojoTest("Testing bytes.Buffer.read_slice")
var s: String = "Hello World!"
var buf = new_buffer(s)
var result = buf.read_slice(5)
test.assert_equal(result.get[0](), String("Hello").as_bytes())
var text = result[0]
text.append(0)
test.assert_equal(String(text), String("Hello"))


fn test_read_string() raises:
var test = MojoTest("Testing bytes.Buffer.read_string")
var s: String = "Hello World!"
var buf = new_buffer(s)
var result = buf.read_string(ord("o"))
test.assert_equal(result.get[0](), String("Hello").as_bytes())
test.assert_equal(String(result[0]), String("Hello"))


fn test_next() raises:
var test = MojoTest("Testing bytes.Buffer.next")
var buf = new_buffer("Hello World!")
test.assert_equal(buf.next(5), String("Hello").as_bytes())
var text = buf.next(5)
text.append(0)
test.assert_equal(String(text), String("Hello"))


fn test_write() raises:
Expand Down
12 changes: 7 additions & 5 deletions tests/test_bytes_reader.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ fn test_read_all() raises:
var test = MojoTest("Testing io.read_all with bytes.Reader")
var reader = reader.new_reader("0123456789")
var result = io.read_all(reader)
var bytes = result.get[0]()
var bytes = result[0]
bytes.append(0)
test.assert_equal(String(bytes), "0123456789")

Expand Down Expand Up @@ -118,15 +118,17 @@ fn test_read_and_unread_byte() raises:

# Read the first byte from the reader.
var buffer = List[Byte](capacity=128)
var result = reader.read_byte()
test.assert_equal(result.get[0](), 48)
var byte: Int8
var err: Error
byte, err = reader.read_byte()
test.assert_equal(int(byte), 48)
var post_read_position = reader.index

# Unread the first byte from the reader. Read position should be moved back by 1
var err = reader.unread_byte()
err = reader.unread_byte()
if err:
raise err
test.assert_equal(reader.index, post_read_position - 1)
test.assert_equal(int(reader.index), int(post_read_position - 1))


fn test_unread_byte_at_beginning() raises:
Expand Down
10 changes: 5 additions & 5 deletions tests/test_goodies_file.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn test_read_all() raises:
var test = MojoTest("Testing goodies.FileWrapper.read_all")
var file = FileWrapper("tests/data/test_big_file.txt", "r")
var result = file.read_all()
var bytes = result.get[0]()
var bytes = result[0]
test.assert_equal(len(bytes), 15358)
bytes.append(0)

Expand All @@ -30,7 +30,7 @@ fn test_io_read_all() raises:
var test = MojoTest("Testing io.read_all with goodies.FileWrapper")
var file = FileWrapper("tests/data/test_big_file.txt", "r")
var result = read_all(file)
var bytes = result.get[0]()
var bytes = result[0]
test.assert_equal(len(bytes), 15358)
bytes.append(0)

Expand All @@ -42,21 +42,21 @@ fn test_io_read_all() raises:
fn test_read_byte() raises:
var test = MojoTest("Testing goodies.FileWrapper.read_byte")
var file = FileWrapper("tests/data/test.txt", "r")
test.assert_equal(file.read_byte().get[0](), 49)
test.assert_equal(int(file.read_byte()[0]), 49)


fn test_write() raises:
var test = MojoTest("Testing goodies.FileWrapper.write")
var file = FileWrapper("tests/data/test_write.txt", "w")
var src = String("12345").as_bytes()
var bytes_written = file.write(src)
test.assert_equal(bytes_written.get[0](), 5)
test.assert_equal(bytes_written[0], 5)

var dest = List[Byte](capacity=128)
file = FileWrapper("tests/data/test_write.txt", "r")
var bytes_read = file.read(dest)
dest.append(0)
test.assert_equal(bytes_read.get[0](), 5)
test.assert_equal(bytes_read[0], 5)
test.assert_equal(String(dest), "12345")


Expand Down
8 changes: 4 additions & 4 deletions tests/test_strings_reader.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fn test_read() raises:
var bytes_read = reader.read(buffer)
buffer.append(0)

test.assert_equal(bytes_read.get[0](), len(example))
test.assert_equal(bytes_read[0], len(example))
test.assert_equal(String(buffer), "Hello, World!")


Expand All @@ -28,7 +28,7 @@ fn test_read_at() raises:
var bytes_read = reader.read_at(buffer, 7)
buffer.append(0)

test.assert_equal(bytes_read.get[0](), len(example[7:]))
test.assert_equal(bytes_read[0], len(example[7:]))
test.assert_equal(String(buffer), "World!")


Expand All @@ -39,7 +39,7 @@ fn test_seek() raises:

# Seek to the middle of the reader.
var position = reader.seek(5, io.SEEK_START)
test.assert_equal(position.get[0](), 5)
test.assert_equal(int(position[0]), 5)


fn test_read_and_unread_byte() raises:
Expand All @@ -50,7 +50,7 @@ fn test_read_and_unread_byte() raises:
# Read the first byte from the reader.
var buffer = List[Byte](capacity=512)
var byte = reader.read_byte()
test.assert_equal(byte.get[0](), 72)
test.assert_equal(int(byte[0]), 72)

# Unread the first byte from the reader. Remaining bytes to be read should be the same as the length of the example string.
_ = reader.unread_byte()
Expand Down
8 changes: 1 addition & 7 deletions tests/wrapper.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,7 @@ struct MojoTest:
except e:
print(e)

fn assert_equal(self, left: Int, right: Int):
try:
testing.assert_equal(left, right)
except e:
print(e)

fn assert_equal(self, left: String, right: String):
fn assert_equal[T: testing.Testable](self, left: T, right: T):
try:
testing.assert_equal(left, right)
except e:
Expand Down

0 comments on commit 31b75c6

Please sign in to comment.