From fad70b479c896cdb690be3c08dd9b1e530d18e7a Mon Sep 17 00:00:00 2001 From: Mikhail Tavarez Date: Wed, 24 Apr 2024 14:07:34 -0500 Subject: [PATCH] nightly update --- gojo/bytes/buffer.mojo | 3 ++- gojo/net/fd.mojo | 2 +- goodies/file.mojo | 2 +- tests/test_bufio.mojo | 28 ++++++++++++++-------------- tests/test_bufio_scanner.mojo | 2 +- tests/test_bytes_buffer.mojo | 18 ++++++++++++------ tests/test_bytes_reader.mojo | 12 +++++++----- tests/test_goodies_file.mojo | 10 +++++----- tests/test_strings_reader.mojo | 8 ++++---- tests/wrapper.mojo | 8 +------- 10 files changed, 48 insertions(+), 45 deletions(-) diff --git a/gojo/bytes/buffer.mojo b/gojo/bytes/buffer.mojo index 89582d3..13f2df9 100644 --- a/gojo/bytes/buffer.mojo +++ b/gojo/bytes/buffer.mojo @@ -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: @@ -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 diff --git a/gojo/net/fd.mojo b/gojo/net/fd.mojo index a4bce25..6e4fc62 100644 --- a/gojo/net/fd.mojo +++ b/gojo/net/fd.mojo @@ -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: diff --git a/goodies/file.mojo b/goodies/file.mojo index 3e671de..a0a00d6 100644 --- a/goodies/file.mojo +++ b/goodies/file.mojo @@ -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) diff --git a/tests/test_bufio.mojo b/tests/test_bufio.mojo index 27b0424..baa7c3d 100644 --- a/tests/test_bufio.mojo +++ b/tests/test_bufio.mojo @@ -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") @@ -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 @@ -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: @@ -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: @@ -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: @@ -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: @@ -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: @@ -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") @@ -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 ") @@ -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!") @@ -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!") diff --git a/tests/test_bufio_scanner.mojo b/tests/test_bufio_scanner.mojo index 259b1dd..a83041f 100644 --- a/tests/test_bufio_scanner.mojo +++ b/tests/test_bufio_scanner.mojo @@ -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 diff --git a/tests/test_bytes_buffer.mojo b/tests/test_bytes_buffer.mojo index fa3e7c4..fdebb5e 100644 --- a/tests/test_bytes_buffer.mojo +++ b/tests/test_bytes_buffer.mojo @@ -18,7 +18,7 @@ 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: @@ -26,7 +26,7 @@ fn test_unread_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) test.assert_equal(buf.off, 1) _ = buf.unread_byte() @@ -38,7 +38,9 @@ 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: @@ -46,7 +48,9 @@ fn test_read_slice() raises: 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: @@ -54,13 +58,15 @@ fn test_read_string() raises: 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: diff --git a/tests/test_bytes_reader.mojo b/tests/test_bytes_reader.mojo index 378f2a3..2992e7e 100644 --- a/tests/test_bytes_reader.mojo +++ b/tests/test_bytes_reader.mojo @@ -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") @@ -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: diff --git a/tests/test_goodies_file.mojo b/tests/test_goodies_file.mojo index c652931..2d50d52 100644 --- a/tests/test_goodies_file.mojo +++ b/tests/test_goodies_file.mojo @@ -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) @@ -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) @@ -42,7 +42,7 @@ 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: @@ -50,13 +50,13 @@ fn test_write() raises: 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") diff --git a/tests/test_strings_reader.mojo b/tests/test_strings_reader.mojo index 3f99c41..4168a02 100644 --- a/tests/test_strings_reader.mojo +++ b/tests/test_strings_reader.mojo @@ -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!") @@ -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!") @@ -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: @@ -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() diff --git a/tests/wrapper.mojo b/tests/wrapper.mojo index 5bbffe9..bdf3f87 100644 --- a/tests/wrapper.mojo +++ b/tests/wrapper.mojo @@ -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: