Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
avik-pal committed Nov 25, 2023
1 parent 8536ca4 commit 98e8cdb
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 7 deletions.
1 change: 1 addition & 0 deletions .github/workflows/Downstream.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ jobs:
os: [ubuntu-latest]
package:
- {user: SciML, repo: SimpleNonlinearSolve.jl, group: All}
- {user: SciML, repo: NonlinearSolve.jl, group: All}
steps:
- uses: actions/checkout@v4
- uses: julia-actions/setup-julia@v1
Expand Down
48 changes: 46 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,48 @@
# MaybeInplace

[![Build Status](https://github.com/avik-pal/MaybeInplace.jl/actions/workflows/CI.yml/badge.svg?branch=main)](https://github.com/avik-pal/MaybeInplace.jl/actions/workflows/CI.yml?query=branch%3Amain)
[![Aqua](https://raw.githubusercontent.com/JuliaTesting/Aqua.jl/master/badge.svg)](https://github.com/JuliaTesting/Aqua.jl)
[![Join the chat at https://julialang.zulipchat.com #sciml-bridged](https://img.shields.io/static/v1?label=Zulip&message=chat&color=9558b2&labelColor=389826)](https://julialang.zulipchat.com/#narrow/stream/279055-sciml-bridged)

[![CI](https://github.com/avik-pal/MaybeInplace.jl/actions/workflows/CI.yml/badge.svg)](https://github.com/avik-pal/MaybeInplace.jl/actions/workflows/CI.yml)
[![codecov](https://codecov.io/gh/avik-pal/MaybeInplace.jl/branch/main/graph/badge.svg?)](https://codecov.io/gh/avik-pal/MaybeInplace.jl)
[![Package Downloads](https://shields.io/endpoint?url=https://pkgs.genieframework.com/api/v1/badge/MaybeInplace)](https://pkgs.genieframework.com?packages=MaybeInplace)
[![Aqua QA](https://raw.githubusercontent.com/JuliaTesting/Aqua.jl/master/badge.svg)](https://github.com/JuliaTesting/Aqua.jl)

[![ColPrac: Contributor's Guide on Collaborative Practices for Community Packages](https://img.shields.io/badge/ColPrac-Contributor%27s%20Guide-blueviolet)](https://github.com/SciML/ColPrac)
[![SciML Code Style](https://img.shields.io/static/v1?label=code%20style&message=SciML&color=9558b2&labelColor=389826)](https://github.com/SciML/SciMLStyle)

MaybeInplace.jl does a simple thing: If you write a code for mutable arrays, that won't work for immutable arrays. This package provides a macro `@bb` or `@bangbang` that will automatically convert the code to make it work for immutable arrays as well.

## Installation

<p>
MaybeInplace is a &nbsp;
<a href="https://julialang.org">
<img src="https://raw.githubusercontent.com/JuliaLang/julia-logo-graphics/master/images/julia.ico" width="16em">
Julia Language
</a>
&nbsp; package. To install Expronicon,
please <a href="https://docs.julialang.org/en/v1/manual/getting-started/">open
Julia's interactive session (known as REPL)</a> and press <kbd>]</kbd> key in the REPL to use the package mode, then type the following command
</p>

```julia
pkg> add MaybeInplace
```

## How This Works?

The code is simple enough to be self-explanatory. The basic idea is as follows, if you have the following code:

```julia
@bb @. x = y + z
```

This macro will convert it to:

```julia
if setindex_trait(x) === CanSetindex()
@. x = y + z
else
x = @. y + z
end
```
6 changes: 6 additions & 0 deletions src/MaybeInplace.jl
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,12 @@ abstract type AbstractMaybeSetindex end
struct CannotSetindex <: AbstractMaybeSetindex end
struct CanSetindex <: AbstractMaybeSetindex end

"""
setindex_trait(A)
Returns `CanSetindex()` if `A` can be setindex-ed else returns `CannotSetindex()`. This is
used by `@bangbang` to determine if an array can be setindex-ed or not.
"""
setindex_trait(::Number) = CannotSetindex()
setindex_trait(::Array) = CanSetindex()
setindex_trait(A::SubArray) = setindex_trait(parent(A))
Expand Down
41 changes: 37 additions & 4 deletions test/Manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

julia_version = "1.10.0-rc1"
manifest_format = "2.0"
project_hash = "c31c70e376fb0af6b22375790fe5a79d2c770ae3"
project_hash = "ef4bcf29fa4bfb90c2f3ea3c8493560500248130"

[[deps.Aqua]]
deps = ["Compat", "Pkg", "Test"]
Expand Down Expand Up @@ -31,13 +31,15 @@ deps = ["UUIDs"]
git-tree-sha1 = "8a62af3e248a8c4bad6b32cbbe663ae02275e32c"
uuid = "34da2185-b29b-5c13-b0c7-acf172513d20"
version = "4.10.0"
weakdeps = ["Dates", "LinearAlgebra"]

[deps.Compat.extensions]
CompatLinearAlgebraExt = "LinearAlgebra"

[deps.Compat.weakdeps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
[[deps.CompilerSupportLibraries_jll]]
deps = ["Artifacts", "Libdl"]
uuid = "e66e0078-7015-5450-92f7-15fbd957f2ae"
version = "1.0.5+1"

[[deps.Dates]]
deps = ["Printf"]
Expand Down Expand Up @@ -98,6 +100,10 @@ version = "1.11.0+1"
[[deps.Libdl]]
uuid = "8f399da3-3557-5675-b5ff-fb832c97cbdb"

[[deps.LinearAlgebra]]
deps = ["Libdl", "OpenBLAS_jll", "libblastrampoline_jll"]
uuid = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

[[deps.Logging]]
uuid = "56ddb016-857b-54e1-b83d-db4d58db5568"

Expand Down Expand Up @@ -130,6 +136,11 @@ version = "2023.1.10"
uuid = "ca575930-c2e3-43a9-ace4-1e988b2c1908"
version = "1.2.0"

[[deps.OpenBLAS_jll]]
deps = ["Artifacts", "CompilerSupportLibraries_jll", "Libdl"]
uuid = "4536629a-c528-5b80-bd46-f80d51c5b363"
version = "0.3.23+2"

[[deps.OrderedCollections]]
git-tree-sha1 = "dfdf5519f235516220579f949664f1bf44e741c5"
uuid = "bac558e1-5e72-5ebc-8fee-abe8a469f55d"
Expand Down Expand Up @@ -191,6 +202,23 @@ uuid = "9e88b42a-f829-5b0c-bbe9-9e923198166b"
[[deps.Sockets]]
uuid = "6462fe0b-24de-5631-8697-dd941f90decc"

[[deps.StaticArrays]]
deps = ["LinearAlgebra", "PrecompileTools", "Random", "StaticArraysCore"]
git-tree-sha1 = "5ef59aea6f18c25168842bded46b16662141ab87"
uuid = "90137ffa-7385-5640-81b9-e52037218182"
version = "1.7.0"

[deps.StaticArrays.extensions]
StaticArraysStatisticsExt = "Statistics"

[deps.StaticArrays.weakdeps]
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"

[[deps.StaticArraysCore]]
git-tree-sha1 = "36b3d696ce6366023a0ea192b4cd442268995a0d"
uuid = "1e83bf80-4336-4d27-bf5d-d5a4f845583c"
version = "1.4.2"

[[deps.TOML]]
deps = ["Dates"]
uuid = "fa267f1f-6049-4f14-aa54-33bafae1ed76"
Expand All @@ -217,6 +245,11 @@ deps = ["Libdl"]
uuid = "83775a58-1f1d-513f-b197-d71354ab007a"
version = "1.2.13+1"

[[deps.libblastrampoline_jll]]
deps = ["Artifacts", "Libdl"]
uuid = "8e850b90-86db-534c-a0d3-1478176c7d93"
version = "5.8.0+1"

[[deps.nghttp2_jll]]
deps = ["Artifacts", "Libdl"]
uuid = "8e850ede-7688-5339-a07c-302acd2aaf8d"
Expand Down
1 change: 1 addition & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
JET = "c3a54625-cd67-489e-a8e7-0a5a0ff4e31b"
SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
90 changes: 90 additions & 0 deletions test/basictests.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
using MaybeInplace, StaticArrays, Test

function copyto!!(y, x)
@bb copyto!(y, x)
return y
end

function eqop!!(y, x1, x2, x3, x4, x5)
@bb y .= x1
@bb y .+= x2
@bb y .*= x3
@bb y .-= x4
@bb y ./= x5
return y
end

function dotmacro!!(y, x, z)
@bb @. y = x * z
return y
end

function matmul!!(y, x, z)
@bb y = x × z
return y
end

@testset "copyto!" begin
x = [1.0, 1.0]
y = [0.0, 0.0]
@test copyto!!(y, x) == [1.0, 1.0]
@test y == [1.0, 1.0]

x = @SVector[1.0, 1.0]
y = @SVector[0.0, 0.0]
@test copyto!!(y, x) == @SVector[1.0, 1.0]
@test y == @SVector[0.0, 0.0]

x = @SMatrix[1.0 1.0; 1.0 1.0]
y = @SMatrix[0.0 0.0; 0.0 0.0]
@test copyto!!(y, x) == @SMatrix[1.0 1.0; 1.0 1.0]
@test y == @SMatrix[0.0 0.0; 0.0 0.0]
end

@testset "(_/+/-/*/div)=" begin
y = [0.0, 0.0]
x1 = [1.0, 1.0]
x2 = [1.0, 1.0]
x3 = [1.0, 1.0]
x4 = [1.0, 1.0]
x5 = [1.0, 1.0]
@test eqop!!(y, x1, x2, x3, x4, x5) == [1.0, 1.0]
@test y == [1.0, 1.0]

y = @SVector[0.0, 0.0]
x1 = @SVector[1.0, 1.0]
x2 = @SVector[1.0, 1.0]
x3 = @SVector[1.0, 1.0]
x4 = @SVector[1.0, 1.0]
x5 = @SVector[1.0, 1.0]
@test eqop!!(y, x1, x2, x3, x4, x5) == @SVector[1.0, 1.0]
@test y == @SVector[0.0, 0.0]
end

@testset "dot" begin
y = [0.0, 0.0]
x = [1.0, 1.0]
z = [1.0, 1.0]
@test dotmacro!!(y, x, z) == [1.0, 1.0]
@test y == [1.0, 1.0]

y = @SVector[0.0, 0.0]
x = @SVector[1.0, 1.0]
z = @SVector[1.0, 1.0]
@test dotmacro!!(y, x, z) == @SVector[1.0, 1.0]
@test y == @SVector[0.0, 0.0]
end

@testset "matmul" begin
y = [0.0, 0.0]
x = [1.0 1.0; 1.0 1.0]
z = [1.0, 1.0]
@test matmul!!(y, x, z) == [2.0, 2.0]
@test y == [2.0, 2.0]

y = @SVector[0.0, 0.0]
x = @SMatrix[1.0 1.0; 1.0 1.0]
z = @SVector[1.0, 1.0]
@test matmul!!(y, x, z) == @SVector[2.0, 2.0]
@test y == @SVector[0.0, 0.0]
end
4 changes: 3 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ using SafeTestsets, Test
JET.test_package(MaybeInplace; target_defined_modules = true)
end

# Write your tests here.
@safetestset "Core" begin
include("basictests.jl")
end
end

0 comments on commit 98e8cdb

Please sign in to comment.