Skip to content

Commit fd32e26

Browse files
authored
Merge pull request #95 from sandstromviktor/main
Adding workflows with unit tests and linting
2 parents 80705fe + 667408e commit fd32e26

25 files changed

+522
-353
lines changed

.github/workflows/run_tests.yaml

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
name: NuMojo Unit Tests
2+
on:
3+
pull_request:
4+
workflow_dispatch:
5+
6+
permissions:
7+
contents: read
8+
pull-requests: read
9+
10+
jobs:
11+
testing-numojo:
12+
name: with ${{ matrix.os }}
13+
strategy:
14+
fail-fast: false
15+
matrix:
16+
os: ["ubuntu-latest", "macos-14"]
17+
18+
runs-on: ${{ matrix.os }}
19+
timeout-minutes: 30
20+
21+
defaults:
22+
run:
23+
shell: bash
24+
env:
25+
DEBIAN_FRONTEND: noninteractive
26+
27+
steps:
28+
- name: Checkout repo
29+
uses: actions/checkout@v4
30+
31+
- name: Download Modular installer
32+
run: |
33+
curl -s https://get.modular.com | sh -
34+
35+
- name: Activate virtualenv
36+
run: |
37+
python3 -m venv $HOME/venv/
38+
. $HOME/venv/bin/activate
39+
echo PATH=$PATH >> $GITHUB_ENV
40+
41+
- name: Install Mojo
42+
run: |
43+
modular install mojo
44+
45+
- name: Set path Mojo
46+
run: |
47+
echo "MODULAR_HOME=$HOME/.modular" >> $GITHUB_ENV
48+
echo "$HOME/.modular/pkg/packages.modular.com_mojo/bin" >> $GITHUB_PATH
49+
50+
- name: Run tests
51+
run: |
52+
pip install numpy
53+
mojo test tests -I .
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
name: Run pre-commit
2+
on:
3+
# Run pre-commit on pull requests
4+
pull_request:
5+
# Add a workflow_dispatch event to run pre-commit manually
6+
workflow_dispatch:
7+
8+
permissions:
9+
contents: read
10+
pull-requests: read
11+
12+
jobs:
13+
lint:
14+
runs-on: "ubuntu-latest"
15+
timeout-minutes: 30
16+
17+
defaults:
18+
run:
19+
shell: bash
20+
env:
21+
DEBIAN_FRONTEND: noninteractive
22+
23+
steps:
24+
- name: Checkout repo
25+
uses: actions/checkout@v4
26+
27+
- name: Download Modular installer
28+
run: |
29+
curl -s https://get.modular.com | sh -
30+
31+
- name: Set path Mojo
32+
run: |
33+
modular install mojo
34+
echo "MODULAR_HOME=/home/runner/.modular" >> $GITHUB_ENV
35+
echo "/home/runner/.modular/pkg/packages.modular.com_mojo/bin" >> $GITHUB_PATH
36+
37+
- name: Install pre-commit
38+
run: |
39+
pip install pre-commit
40+
pre-commit install
41+
42+
- name: Run pre-commit
43+
run: pre-commit run --all-files

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ mojo
1111
numojo.mojopkg
1212
.gitignore
1313
bench.mojo
14-
test_ndarray.ipynb
14+
test_ndarray.ipynb
15+
/venv

.pre-commit-config.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
repos:
2+
- repo: local
3+
hooks:
4+
- id: mojo-format
5+
name: mojo-format
6+
entry: mojo format
7+
language: system
8+
files: '\.(mojo|🔥|py)$'
9+
stages: [commit]

numojo/core/array_creation_routines.mojo

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ from .utility_funcs import is_inttype, is_floattype
2525
# Arranged Value NDArray generation
2626
# ===------------------------------------------------------------------------===#
2727
fn arange[
28-
dtype:DType = DType.float64
28+
dtype: DType = DType.float64
2929
](
3030
start: Scalar[dtype],
3131
stop: Scalar[dtype],
@@ -54,17 +54,11 @@ fn arange[
5454
# raise Error(
5555
# "Both input and output datatypes cannot be integers. If the input is a float, the output must also be a float."
5656
# )
57-
58-
5957

6058
var num: Int = ((stop - start) / step).__int__()
61-
var result: NDArray[dtype] = NDArray[dtype](
62-
NDArrayShape(num, size=num)
63-
)
59+
var result: NDArray[dtype] = NDArray[dtype](NDArrayShape(num, size=num))
6460
for idx in range(num):
65-
result.data[idx] = (
66-
start + step * idx
67-
)
61+
result.data[idx] = start + step * idx
6862

6963
return result
7064

@@ -112,13 +106,9 @@ fn linspace[
112106
# )
113107
constrained[not dtype.is_integral()]()
114108
if parallel:
115-
return _linspace_parallel[dtype](
116-
start, stop, num, endpoint
117-
)
109+
return _linspace_parallel[dtype](start, stop, num, endpoint)
118110
else:
119-
return _linspace_serial[dtype](
120-
start, stop, num, endpoint
121-
)
111+
return _linspace_serial[dtype](start, stop, num, endpoint)
122112

123113

124114
fn _linspace_serial[
@@ -386,18 +376,14 @@ fn geomspace[
386376

387377
if endpoint:
388378
var result: NDArray[dtype] = NDArray[dtype](NDArrayShape(num))
389-
var r: Scalar[dtype] = (
390-
stop / start
391-
) ** (1 / (num - 1))
379+
var r: Scalar[dtype] = (stop / start) ** (1 / (num - 1))
392380
for i in range(num):
393381
result.data[i] = a * r**i
394382
return result
395383

396384
else:
397385
var result: NDArray[dtype] = NDArray[dtype](NDArrayShape(num))
398-
var r: Scalar[dtype] = (
399-
stop / start
400-
) ** (1 / (num))
386+
var r: Scalar[dtype] = (stop / start) ** (1 / (num))
401387
for i in range(num):
402388
result.data[i] = a * r**i
403389
return result
@@ -541,7 +527,9 @@ fn full[
541527
return NDArray[dtype](shape, fill=tens_value)
542528

543529

544-
fn diagflat[dtype: DType](inout v: NDArray[dtype], k: Int = 0) raises -> NDArray[dtype]:
530+
fn diagflat[
531+
dtype: DType
532+
](inout v: NDArray[dtype], k: Int = 0) raises -> NDArray[dtype]:
545533
"""
546534
Generate a 2-D NDArray with the flattened input as the diagonal.
547535
@@ -556,12 +544,12 @@ fn diagflat[dtype: DType](inout v: NDArray[dtype], k: Int = 0) raises -> NDArray
556544
A 2-D NDArray with the flattened input as the diagonal.
557545
"""
558546
v.reshape(v.ndshape.ndsize, 1)
559-
var n: Int= v.ndshape.ndsize + abs(k)
560-
var result: NDArray[dtype]= NDArray[dtype](n, n, random=False)
547+
var n: Int = v.ndshape.ndsize + abs(k)
548+
var result: NDArray[dtype] = NDArray[dtype](n, n, random=False)
561549

562550
for i in range(n):
563-
print(n*i + i + k)
564-
result.store(n*i + i + k, v.data[i])
551+
print(n * i + i + k)
552+
result.store(n * i + i + k, v.data[i])
565553
return result
566554

567555

numojo/core/array_manipulation_routines.mojo

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ Array manipulation routines.
77
# ===----------------------------------------------------------------------=== #
88

99

10-
1110
fn copyto():
1211
pass
1312

numojo/core/constants.mojo

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ Constants
77
# ===----------------------------------------------------------------------=== #
88

99

10-
1110
@value
1211
struct Constants(AnyType):
1312
"""Define constants.

numojo/core/ndarray.mojo

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -948,7 +948,7 @@ struct NDArray[dtype: DType = DType.float64](
948948
```
949949
A is an array with shape 2 x 2 and randomly values between 0 and 10.
950950
The output goes as follows.
951-
951+
952952
```console
953953
[[ 6.046875 6.98046875 ]
954954
[ 6.6484375 1.736328125 ]]
@@ -965,16 +965,17 @@ struct NDArray[dtype: DType = DType.float64](
965965
if dtype.is_floating_point():
966966
for i in range(self.ndshape.ndsize):
967967
self.data.store(
968-
i,
969-
random_float64(min.cast[DType.float64](), max.cast[DType.float64]()).cast[dtype]()
968+
i,
969+
random_float64(
970+
min.cast[DType.float64](), max.cast[DType.float64]()
971+
).cast[dtype](),
970972
)
971973
elif dtype.is_integral():
972974
for i in range(self.ndshape.ndsize):
973975
self.data.store(
974-
i,
975-
random_si64(int(min), int(max)).cast[dtype]()
976+
i, random_si64(int(min), int(max)).cast[dtype]()
976977
)
977-
978+
978979
@always_inline("nodebug")
979980
fn __init__(
980981
inout self,
@@ -1001,7 +1002,7 @@ struct NDArray[dtype: DType = DType.float64](
10011002
```
10021003
A is an array with shape 2 x 2 and randomly values between 0 and 10.
10031004
The output goes as follows.
1004-
1005+
10051006
```console
10061007
[[ 6.046875 6.98046875 ]
10071008
[ 6.6484375 1.736328125 ]]
@@ -1018,14 +1019,15 @@ struct NDArray[dtype: DType = DType.float64](
10181019
if dtype.is_floating_point():
10191020
for i in range(self.ndshape.ndsize):
10201021
self.data.store(
1021-
i,
1022-
random_float64(min.cast[DType.float64](), max.cast[DType.float64]()).cast[dtype]()
1022+
i,
1023+
random_float64(
1024+
min.cast[DType.float64](), max.cast[DType.float64]()
1025+
).cast[dtype](),
10231026
)
10241027
elif dtype.is_integral():
10251028
for i in range(self.ndshape.ndsize):
10261029
self.data.store(
1027-
i,
1028-
random_si64(int(min), int(max)).cast[dtype]()
1030+
i, random_si64(int(min), int(max)).cast[dtype]()
10291031
)
10301032

10311033
fn __init__(

numojo/core/random.mojo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Random values array generation.
33
"""
44
# ===----------------------------------------------------------------------=== #
5-
# Implements RANDOM
5+
# Implements RANDOM
66
# Last updated: 2024-06-18
77
# ===----------------------------------------------------------------------=== #
88

numojo/core/sort.mojo

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,8 @@ fn quick_sort[
164164

165165

166166
fn binary_sort[
167-
dtype: DType = DType.float64
168-
](array: NDArray[ dtype]) raises -> NDArray[dtype]:
167+
dtype: DType = DType.float64
168+
](array: NDArray[dtype]) raises -> NDArray[dtype]:
169169
"""
170170
Binary sorting of NDArray.
171171
@@ -185,6 +185,7 @@ fn binary_sort[
185185
Returns:
186186
The sorted NDArray of type `dtype`.
187187
"""
188+
188189
@parameter
189190
if dtype != array.dtype:
190191
alias dtype = array.dtype

0 commit comments

Comments
 (0)