Skip to content

Commit

Permalink
updating to reflect L6Testing
Browse files Browse the repository at this point in the history
  • Loading branch information
mauro3 committed Oct 31, 2023
1 parent 61b820a commit 168c9f4
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 45 deletions.
2 changes: 1 addition & 1 deletion subfolder/Project.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name = "L6Testing"
uuid = "44c933c6-7d10-4379-b2cf-b211c6f3d331"
uuid = "12a8c696-9afa-4891-b56f-a95e8a68aa4d"
authors = ["Mauro Werder <[email protected]>"]
version = "0.1.0"
20 changes: 15 additions & 5 deletions subfolder/README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
[![CI action](https://github.com/eth-vaw-glaciology/course-101-0250-00-L6Testing.jl/actions/workflows/ci.yml/badge.svg)](https://github.com/eth-vaw-glaciology/course-101-0250-00-L6Testing.jl/actions/workflows/ci.yml)

# Showcase unit and integration testing

[![CI action](https://github.com/eth-vaw-glaciology/course-101-0250-00-L6Testing-subfolder.jl/actions/workflows/CI.yml/badge.svg)](https://github.com/eth-vaw-glaciology/course-101-0250-00-L6Testing-subfolder.jl/actions/workflows/CI.yml)
## This demonstrates how to use CI for a Julia project in a subfolder

https://github.com/eth-vaw-glaciology/course-101-0250-00-L6Testing-subfolder.jl/actions/workflows/CI.yml/badge.svg
This is for Lecture 6 of course https://pde-on-gpu.vaw.ethz.ch

This is for Lecture 7 of course
https://eth-vaw-glaciology.github.io/course-101-0250-00/
It demonstrates a typical work-flow of mine which I committed such
that commit history retraces that workflow:
- start coding ([repo state](https://github.com/eth-vaw-glaciology/course-101-0250-00-L6Testing.jl/tree/8d3d875991e3010216b321413f34cb7ee01b8aa8))
- sprinkle a reference test within the script itself ([script
state](https://github.com/eth-vaw-glaciology/course-101-0250-00-L6Testing.jl/blob/580eebd9287fb7c547520b3e2863a3ce599e8207/scripts/car_travel.jl))
- refactor and sprinkle a unit-test within the script itself ([script
state](https://github.com/eth-vaw-glaciology/course-101-0250-00-L6Testing.jl/blob/33b669969b1227289b1059b9ce01baf3ced349e1/scripts/car_travel.jl))
- move tests to `test/` directory
([runtests.jl](https://github.com/eth-vaw-glaciology/course-101-0250-00-L6Testing.jl/blob/5b41f800039f77f4b1024393180733b396ee3c09/test/runtests.jl))

It demonstrates how to use CI for a Julia project in a subfolder.
Here a link to the commits:
https://github.com/eth-vaw-glaciology/course-101-0250-00-L6Testing.jl/commits/master
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# plotting not needed
# using Plots
# pyplot()

function update_position(X, V, dir, dt, L)
X = X + dir*V*dt
#X = X + eps(X)
if X > L
dir = -1 # if beyond L, go back (left)
elseif X < 0
Expand All @@ -11,7 +14,7 @@ end

function car_travel_1D()
# Physical parameters
V = 13.0 # speed, km/h
V = 113.0 # speed, km/h
L = 200.0 # length of segment, km
dir = 1 # switch 1 = go right, -1 = go left
ttot = 16.0 # total time, h
Expand All @@ -24,15 +27,13 @@ function car_travel_1D()
# Time loop
for it = 2:nt
T[it] = T[it-1] + dt
X[it], dir = update_position(X[it], V, dir, dt, L)
X[it], dir = update_position(X[it - 1], V, dir, dt, L)
end
# Visualisation
# display(scatter(T, X, markersize=5, xlabel="time, hrs", ylabel="distance, km", framestyle=:box, legend=:none))
return T, X
end

# Only run this in an interactive session:
if isinteractive()
println("Running model")
if isinteractive() # do not run this when including this script from other scripts
T, X = car_travel_1D()
end
2 changes: 1 addition & 1 deletion subfolder/src/L6Testing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ module L6Testing

greet() = print("Hello World!")

end # module
end # module L6Testing
2 changes: 0 additions & 2 deletions subfolder/test/Project.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
[deps]
BSON = "fbb218c0-5317-5bc6-957e-2ee96dd4b1f0"
ReferenceTests = "324d217c-45ce-50fc-942e-d289b448e8cf"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Binary file removed subfolder/test/reftest-files/X.bson
Binary file not shown.
41 changes: 11 additions & 30 deletions subfolder/test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,35 +1,16 @@
using Test, ReferenceTests, BSON
using Test

include("../scripts/car_travels.jl")
include("../scripts/car_travel.jl")

## Unit tests
@testset "update_position" begin
@test update_position(0.0, 10, 1, 1, 200)[1] 10.0
@test update_position(0.0, 10, 1, 1, 200)[2] == 1

@test update_position(0.0, 10, -1, 1, 200)[1] -10.0
@test update_position(0.0, 10, -1, 1, 200)[2] == 1

@test update_position(0.0, 10, -1, 1, 200)[1] -10.0
@test update_position(0.0, 10, -1, 1, 200)[2] == 1
end


## Reference Tests with ReferenceTests.jl
# We put both arrays X and T into a BSON.jl and then compare them

"Compare all dict entries"
comp(d1, d2) = keys(d1) == keys(d2) &&
all([ v1v2 for (v1,v2) in zip(values(d1), values(d2))])

# run the model
T, X = car_travel_1D()

# Test just at some random indices. As for larger models,
# storing the full output array would create really large files!
inds = [18, 27, 45, 68, 71, 71, 102, 110, 123, 144]
# a unit test
@test update_position(10, 0, 1, 123, 123) == (10, 1)

# a reference test:
# make inds with `sort(rand(1:length(X), 10)
inds = [10, 28, 36, 37, 39, 69, 80, 96, 128, 135]
# make vals with `X[inds]`
vals = [101.69999999999999, 101.7, 11.300000000000018, 1.7763568394002505e-14, 1.7763568394002505e-14, 67.80000000000001, 33.90000000000002, 192.10000000000005, 146.9, 180.80000000000004]

d = Dict(:X=> X[inds], :T=>T[inds])
@testset "Ref-tests" begin
@test_reference "reftest-files/X.bson" d by=comp
end
@test all(vals .≈ X[inds])

0 comments on commit 168c9f4

Please sign in to comment.