Skip to content
This repository has been archived by the owner on Feb 14, 2025. It is now read-only.

Commit

Permalink
reorganizing repo to move towards using mojopkg more
Browse files Browse the repository at this point in the history
  • Loading branch information
thatstoasty committed Aug 29, 2024
1 parent 3d8958d commit 22b7b66
Show file tree
Hide file tree
Showing 76 changed files with 961 additions and 1,108 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@ jobs:
curl https://get.modular.com | MODULAR_AUTH=${{ secrets.MODULAR_AUTH }} sh -
modular auth ${{ secrets.MODULAR_AUTH }}
modular install mojo
pip install pytest
pip install git+https://github.com/guidorice/mojo-pytest.git
- name: Unit Tests
run: |
export MODULAR_HOME="/home/runner/.modular"
export PATH="/home/runner/.modular/pkg/packages.modular.com_mojo/bin:$PATH"
pytest
bash scripts/run_tests.sh
Binary file added benchmarks/gojo.mojopkg
Binary file not shown.
6 changes: 5 additions & 1 deletion scripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,13 @@ build_dependencies() {
}

if [ "$1" == "package" ]; then
mojo package gojo
mojo package src/gojo
elif [ "$1" == "dependencies" ]; then
echo "No dependencies to build"
elif [ "$1" == "tests" ]; then
mojo package src/gojo
cp gojo.mojopkg benchmarks
mv gojo.mojopkg test
else
echo "Invalid argument. Use 'package' to package the project or 'dependencies' to build only the dependencies."
fi
14 changes: 8 additions & 6 deletions scripts/run_tests.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
#!/bin/bash
mkdir -p tmp

TEMP_DIR=~/tmp
mkdir -p $TEMP_DIR

echo -e "Building gojo package and copying tests."
./scripts/build.sh package
mv gojo.mojopkg tmp/
cp -R tests/ tmp/tests/
mv gojo.mojopkg $TEMP_DIR/
cp -R test/ $TEMP_DIR/test/

echo -e "\nBuilding binaries for all examples."
cd tmp
pytest tests
cd $TEMP_DIR
mojo test test
cd ..

echo -e "Cleaning up the test directory."
rm -R tmp
rm -R $TEMP_DIR
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
13 changes: 7 additions & 6 deletions gojo/bytes/buffer.mojo → src/gojo/bytes/buffer.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ underlying buffer."""
alias ERR_TOO_LARGE = "buffer.Buffer: too large"
"""ERR_TOO_LARGE is passed to panic if memory cannot be allocated to store data in a buffer."""
alias ERR_NEGATIVE_READ = "buffer.Buffer: reader returned negative count from read"
alias ERR_SHORTwrite = "short write"
alias ERR_SHORT_WRITE = "short write"


struct Buffer(
Expand Down Expand Up @@ -99,15 +99,16 @@ struct Buffer(
self.offset = 0
self.last_read = OP_INVALID

fn __init__(inout self, owned buf: String):
fn __init__(inout self, buf: String):
"""Creates a new buffer with String provided.
Args:
buf: The String to initialize the buffer with.
"""
self._capacity = buf._buffer.capacity
self._size = len(buf)
self._data = buf._steal_ptr()
var bytes = buf.as_bytes()
self._capacity = bytes.capacity
self._size = bytes.size
self._data = bytes.steal_data()
self.offset = 0
self.last_read = OP_INVALID

Expand Down Expand Up @@ -489,7 +490,7 @@ struct Buffer(

# all bytes should have been written, by definition of write method in io.Writer
if bytes_written != bytes_to_write:
return total_bytes_written, Error(ERR_SHORTwrite)
return total_bytes_written, Error(ERR_SHORT_WRITE)

# Buffer is now empty; reset.
self.reset()
Expand Down
13 changes: 13 additions & 0 deletions gojo/bytes/reader.mojo → src/gojo/bytes/reader.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,19 @@ struct Reader(
self.index = 0
self.prev_rune = -1

fn __init__(inout self, text: String):
"""Initializes a new `Reader` with the given String.
Args:
text: The String to initialize the `Reader` with.
"""
var bytes = text.as_bytes()
self._capacity = bytes.capacity
self._size = bytes.size
self._data = bytes.steal_data()
self.index = 0
self.prev_rune = -1

fn __moveinit__(inout self, owned other: Reader):
self._capacity = other._capacity
self._size = other._size
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
82 changes: 40 additions & 42 deletions gojo/strings/reader.mojo → src/gojo/strings/reader.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,25 @@ from ..builtins import copy, panic


@value
# TODO: Uncomment write_to and write_buf once the bug with the trait's Span argument is fixed.
struct Reader(
Sized,
io.Reader,
io.ReaderAt,
io.ByteReader,
io.ByteScanner,
io.Seeker,
# io.WriterTo,
io.WriterTo,
):
"""A Reader that implements the [io.Reader], [io.ReaderAt], [io.ByteReader], [io.ByteScanner], [io.Seeker], and [io.WriterTo] traits
"""A Reader that implements the `io.Reader`, `io.ReaderAt`, `io.ByteReader`, `io.ByteScanner`, `io.Seeker`, and `io.WriterTo` traits
by reading from a string. The zero value for Reader operates like a Reader of an empty string.
"""

var string: String
var read_pos: Int # current reading index
var prev_rune: Int # index of previous rune; or < 0
"""Internal string to read from."""
var read_pos: Int
"""Current reading index."""
var prev_rune: Int
"""Index of previous rune; or < 0."""

fn __init__(inout self, string: String = ""):
self.string = string
Expand All @@ -36,7 +38,7 @@ struct Reader(

fn size(self) -> Int:
"""Returns the original length of the underlying string.
size is the number of bytes available for reading via [Reader.read_at].
`size` is the number of bytes available for reading via `Reader.read_at`.
The returned value is always the same and is not affected by calls
to any other method.
Expand All @@ -46,12 +48,11 @@ struct Reader(
return len(self.string)

fn _read(inout self, inout dest: UnsafePointer[UInt8], capacity: Int) -> (Int, Error):
"""Reads from the underlying string into the provided List[UInt8, True] object.
Implements the [io.Reader] trait.
"""Reads from the underlying string into the provided `dest` buffer.
Args:
dest: The destination List[UInt8, True] object to read into.
capacity: The capacity of the destination List[UInt8, True] object.
dest: The destination buffer to read into.
capacity: The capacity of the destination buffer.
Returns:
The number of bytes read into dest.
Expand All @@ -69,11 +70,10 @@ struct Reader(
return bytes_written, Error()

fn read(inout self, inout dest: List[UInt8, True]) -> (Int, Error):
"""Reads from the underlying string into the provided List[UInt8, True] object.
Implements the [io.Reader] trait.
"""Reads from the underlying string into the provided `dest` buffer.
Args:
dest: The destination List[UInt8, True] object to read into.
dest: The destination buffer to read into.
Returns:
The number of bytes read into dest.
Expand All @@ -91,16 +91,15 @@ struct Reader(
return bytes_read, err

fn _read_at(self, inout dest: Span[UInt8], off: Int, capacity: Int) -> (Int, Error):
"""Reads from the Reader into the dest List[UInt8, True] starting at the offset off.
It returns the number of bytes read into dest and an error if any.
"""Reads from the Reader into the `dest` buffer starting at the offset `off`.
Args:
dest: The destination List[UInt8, True] object to read into.
dest: The destination buffer to read into.
off: The byte offset to start reading from.
capacity: The capacity of the destination List[UInt8, True] object.
capacity: The capacity of the destination buffer.
Returns:
The number of bytes read into dest.
It returns the number of bytes read into `dest` and an error if any.
"""
# cannot modify state - see io.ReaderAt
if off < 0:
Expand All @@ -119,11 +118,11 @@ struct Reader(
return copied_elements_count, error

fn read_at(self, inout dest: List[UInt8, True], off: Int) -> (Int, Error):
"""Reads from the Reader into the dest List[UInt8, True] starting at the offset off.
"""Reads from the Reader into the `dest` buffer starting at the offset off.
It returns the number of bytes read into dest and an error if any.
Args:
dest: The destination List[UInt8, True] object to read into.
dest: The destination buffer to read into.
off: The byte offset to start reading from.
Returns:
Expand Down Expand Up @@ -190,7 +189,7 @@ struct Reader(
Args:
offset: The offset to seek to.
whence: The seek mode. It can be one of [io.SEEK_START], [io.SEEK_CURRENT], or [io.SEEK_END].
whence: The seek mode. It can be one of `io.SEEK_START`, `io.SEEK_CURRENT`, or `io.SEEK_END`.
Returns:
The new position in the string.
Expand All @@ -213,32 +212,31 @@ struct Reader(
self.read_pos = position
return position, Error()

# fn write_to[W: io.Writer](inout self, inout writer: W) -> (Int, Error):
# """Writes the remaining portion of the underlying string to the provided writer.
# Implements the [io.WriterTo] trait.
fn write_to[W: io.Writer](inout self, inout writer: W) -> (Int, Error):
"""Writes the remaining portion of the underlying string to the provided writer.
# Args:
# writer: The writer to write the remaining portion of the string to.
Args:
writer: The writer to write the remaining portion of the string to.
# Returns:
# The number of bytes written to the writer.
# """
# self.prev_rune = -1
# var err = Error()
# if self.read_pos >= len(self.string):
# return Int(0), err
Returns:
The number of bytes written to the writer.
"""
self.prev_rune = -1
var err = Error()
if self.read_pos >= len(self.string):
return Int(0), err

# var chunk_to_write = self.string.as_bytes_slice()[self.read_pos :]
# var bytes_written: Int
# bytes_written, err = writer.write(chunk_to_write)
# if bytes_written > len(chunk_to_write):
# panic("strings.Reader.write_to: invalid write_string count")
var chunk_to_write = self.string.as_bytes_slice()[self.read_pos :]
var bytes_written: Int
bytes_written, err = writer.write(chunk_to_write)
if bytes_written > len(chunk_to_write):
panic("strings.Reader.write_to: invalid write_string count")

# self.read_pos += bytes_written
# if bytes_written != len(chunk_to_write) and not err:
# err = Error(io.ERR_SHORT_WRITE)
self.read_pos += bytes_written
if bytes_written != len(chunk_to_write) and not err:
err = str(io.ERR_SHORT_WRITE)

# return bytes_written, err
return bytes_written, err

# # TODO: How can I differentiate between the two write_to methods when the writer implements both traits?
# fn write_to[W: io.StringWriter](inout self, inout writer: W) raises -> Int:
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion gojo/syscall/net.mojo → src/gojo/syscall/net.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ fn inet_ntoa(addr: in_addr) -> UnsafePointer[UInt8]:
Fn signature: `char *inet_ntoa(struct in_addr in)`.
Args:
in: A pointer to a string containing the address.
addr: A pointer to a string containing the address.
Returns:
The address in network byte order.
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions test/data/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
12345
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ Quis imperdiet massa tincidunt nunc pulvinar. Eget lorem dolor sed viverra ipsum
Egestas congue quisque egestas diam in arcu. Et magnis dis parturient montes nascetur. Dolor sit amet consectetur adipiscing. Ut tristique et egestas quis ipsum. Turpis egestas sed tempus urna. Euismod elementum nisi quis eleifend quam adipiscing vitae. Nisl vel pretium lectus quam id leo. Proin sed libero enim sed faucibus turpis. Mi quis hendrerit dolor magna eget. Suspendisse ultrices gravida dictum fusce ut placerat. Habitasse platea dictumst quisque sagittis purus. Curabitur gravida arcu ac tortor. Commodo nulla facilisi nullam vehicula ipsum a.
Sagittis vitae et leo duis ut diam quam. Pretium nibh ipsum consequat nisl vel pretium lectus. Arcu cursus euismod quis viverra. Risus nullam eget felis eget nunc lobortis. Aliquet eget sit amet tellus cras adipiscing enim eu. In mollis nunc sed id semper risus in. Sed egestas egestas fringilla phasellus faucibus scelerisque. A diam sollicitudin tempor id. Lacus laoreet non curabitur gravida. A diam maecenas sed enim ut. Id nibh tortor id aliquet lectus proin.
Eget mi proin sed libero. Maecenas pharetra convallis posuere morbi leo urna. Id donec ultrices tincidunt arcu. Urna et pharetra pharetra massa massa ultricies. Pulvinar sapien et ligula ullamcorper malesuada proin libero. Nec nam aliquam sem et tortor consequat id porta. In vitae turpis massa sed. Praesent semper feugiat nibh sed pulvinar. Nascetur ridiculus mus mauris vitae ultricies. Ut aliquam purus sit amet luctus venenatis lectus magna fringilla. Sit amet mattis vulputate enim. Orci a scelerisque purus semper eget duis at tellus at.
Platea dictumst vestibulum rhoncus est pellentesque elit ullamcorper dignissim cras. Gravida arcu ac tortor dignissim convallis aenean et tortor. Ornare suspendisse sed nisi lacus sed viverra tellus in. Turpis egestas maecenas pharetra convallis posuere morbi. Elementum nisi quis eleifend quam adipiscing vitae. Maecenas sed enim ut sem. Feugiat in fermentum posuere urna nec tincidunt praesent. Suspendisse sed nisi lacus sed. Scelerisque in dictum non consectetur. Mauris commodo quis imperdiet massa.
Platea dictumst vestibulum rhoncus est pellentesque elit ullamcorper dignissim cras. Gravida arcu ac tortor dignissim convallis aenean et tortor. Ornare suspendisse sed nisi lacus sed viverra tellus in. Turpis egestas maecenas pharetra convallis posuere morbi. Elementum nisi quis eleifend quam adipiscing vitae. Maecenas sed enim ut sem. Feugiat in fermentum posuere urna nec tincidunt praesent. Suspendisse sed nisi lacus sed. Scelerisque in dictum non consectetur. Mauris commodo quis imperdiet massa.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
22222
33333
44444
55555
55555
2 changes: 1 addition & 1 deletion tests/data/test_read.csv → test/data/test_read.csv
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Hello,World,I am here
Goodbye,World,I was here
Lorem,Ipsum,Dolor
Lorem,Ipsum,Dolor
File renamed without changes.
1 change: 1 addition & 0 deletions test/data/test_write.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
12345
Binary file added test/gojo.mojopkg
Binary file not shown.
Loading

0 comments on commit 22b7b66

Please sign in to comment.