Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 1 addition & 15 deletions .github/workflows/Tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,7 @@ concurrency:

jobs:
tests:
name: "Tests"
strategy:
fail-fast: false
matrix:
version:
- "lts"
- "1"
- "pre"
os:
- ubuntu-latest
- macOS-latest
- windows-latest
uses: "SciML/.github/.github/workflows/tests.yml@v1"
uses: "SciML/.github/.github/workflows/grouped-tests.yml@v1"
with:
julia-version: "${{ matrix.version }}"
os: "${{ matrix.os }}"
coverage: true
secrets: "inherit"
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
*.jl.mem
/Manifest.toml
/docs/build/
/docs/Manifest.toml
/docs/Manifest.toml
/test/qa/Manifest.toml
13 changes: 8 additions & 5 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,20 @@ authors = ["ChrisRackauckas <contact@chrisrackauckas.com>"]
PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a"

[compat]
ExplicitImports = "1.14.0"
JET = "0.9, 0.10, 0.11"
PrecompileTools = "1.2"
Random = "1.10"
SafeTestsets = "0.1"
SciMLTesting = "1.6"
Statistics = "1.10"
Test = "1.10"
julia = "1.10"

[extras]
ExplicitImports = "7d51a73a-1435-4ff3-83d9-f097790105c7"
JET = "c3a54625-cd67-489e-a8e7-0a5a0ff4e31b"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f"
SciMLTesting = "09d9d899-5365-40a9-917a-5f67fddea283"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["ExplicitImports", "JET", "Random", "Statistics", "Test"]
test = ["Random", "SafeTestsets", "SciMLTesting", "Statistics", "Test"]
135 changes: 135 additions & 0 deletions test/core_tests.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
using LightweightStats
using Test

using LightweightStats: mean, median, std, var, cov, cor, quantile, middle

@testset "mean" begin
@test mean([1, 2, 3, 4, 5]) ≈ 3.0
@test mean([1.5, 2.5, 3.5]) ≈ 2.5
@test mean(Float32[1, 2, 3]) ≈ 2.0f0

@test mean(x -> x^2, [1, 2, 3]) ≈ 14 / 3

A = [1 2 3; 4 5 6]
@test mean(A) ≈ 3.5
@test mean(A; dims = 1) ≈ [2.5 3.5 4.5]
@test mean(A; dims = 2) ≈ reshape([2.0, 5.0], 2, 1)

@test_throws ArgumentError mean([])
end

@testset "median" begin
@test median([1, 2, 3, 4, 5]) == 3
@test median([1, 2, 3, 4]) == 2.5
@test median([3, 1, 2]) == 2
@test median([1]) == 1

A = [1 2 3; 4 5 6]
@test median(A) == 3.5
@test median(A; dims = 1) == [2.5 3.5 4.5]
@test median(A; dims = 2) == reshape([2.0, 5.0], 2, 1)

@test_throws ArgumentError median([])
end

@testset "var and std" begin
x = [1, 2, 3, 4, 5]
@test var(x) ≈ 2.5
@test var(x; corrected = false) ≈ 2.0
@test std(x) ≈ sqrt(2.5)
@test std(x; corrected = false) ≈ sqrt(2.0)

@test var(x; mean = 3) ≈ 2.5
@test std(x; mean = 3) ≈ sqrt(2.5)

A = [1 2 3; 4 5 6]
@test var(A) ≈ 3.5
@test std(A) ≈ sqrt(3.5)

@test isnan(var(Float64[]))
end

@testset "cov" begin
x = [1.0, 2.0, 3.0, 4.0, 5.0]
y = [2.0, 4.0, 6.0, 8.0, 10.0]

@test cov(x, y) ≈ 5.0
@test cov(x, y; corrected = false) ≈ 4.0
@test cov(x) ≈ var(x)

X = [1 2; 3 4; 5 6]
C = cov(X; dims = 1)
@test size(C) == (2, 2)
@test C[1, 1] ≈ var(X[:, 1])
@test C[2, 2] ≈ var(X[:, 2])
@test C[1, 2] ≈ C[2, 1]

C2 = cov(X; dims = 2)
@test size(C2) == (3, 3)

@test_throws DimensionMismatch cov([1, 2], [1, 2, 3])
end

@testset "cor" begin
x = [1.0, 2.0, 3.0, 4.0, 5.0]
y = [2.0, 4.0, 6.0, 8.0, 10.0]

@test cor(x, y) ≈ 1.0
@test cor(x, -y) ≈ -1.0

X = [1 2; 3 4; 5 6]
R = cor(X; dims = 1)
@test size(R) == (2, 2)
@test R[1, 1] ≈ 1.0
@test R[2, 2] ≈ 1.0
@test R[1, 2] ≈ R[2, 1]

@test isnan(cor([1, 1, 1], [1, 2, 3]))

@test_throws DimensionMismatch cor([1, 2], [1, 2, 3])
end

@testset "quantile" begin
v = [1, 2, 3, 4, 5]

@test quantile(v, 0.0) == 1
@test quantile(v, 0.25) ≈ 2.0
@test quantile(v, 0.5) ≈ 3.0
@test quantile(v, 0.75) ≈ 4.0
@test quantile(v, 1.0) == 5

@test quantile(v, [0.25, 0.5, 0.75]) ≈ [2.0, 3.0, 4.0]

@test_throws ArgumentError quantile(v, -0.1)
@test_throws ArgumentError quantile(v, 1.1)
@test_throws ArgumentError quantile([], 0.5)
end

@testset "middle" begin
@test middle(1, 5) == 3
@test middle(1.0, 5.0) == 3.0
@test middle([1, 2, 3, 4, 5]) == 3
@test middle([5, 1, 3]) == 3
@test middle(10) == 10

@test_throws ArgumentError middle([])
end

@testset "Type stability" begin
@test typeof(mean(Int[1, 2, 3])) == Float64
@test typeof(mean(Float32[1, 2, 3])) == Float32
@test typeof(std(Int[1, 2, 3])) == Float64
@test typeof(median(Int[1, 2, 3])) == Int
@test typeof(median(Float64[1, 2, 3])) == Float64
end

@testset "Edge cases" begin
@test mean([42]) == 42
@test median([42]) == 42
@test var([42]) |> isnan
@test std([42]) |> isnan

@test mean([1, 1, 1, 1]) == 1
@test var([1, 1, 1, 1]; corrected = false) == 0
@test std([1, 1, 1, 1]; corrected = false) == 0
end
8 changes: 0 additions & 8 deletions test/explicit_imports_tests.jl

This file was deleted.

126 changes: 0 additions & 126 deletions test/jet_tests.jl

This file was deleted.

16 changes: 16 additions & 0 deletions test/qa/Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[deps]
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
JET = "c3a54625-cd67-489e-a8e7-0a5a0ff4e31b"
LightweightStats = "6e4b0b68-1f2b-4a7e-8f0e-d3e9f3d5c8a1"
SciMLTesting = "09d9d899-5365-40a9-917a-5f67fddea283"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[sources]
LightweightStats = {path = "../.."}

[compat]
Aqua = "0.8"
JET = "0.9, 0.10, 0.11"
SciMLTesting = "1.6"
Test = "1"
julia = "1.10"
3 changes: 3 additions & 0 deletions test/qa/qa.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
using SciMLTesting, LightweightStats, JET, Test

run_qa(LightweightStats; explicit_imports = true)
Loading
Loading