Skip to content

Commit

Permalink
Improve coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobnissen committed Jun 22, 2024
1 parent a2b791c commit 7068804
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/documentation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ jobs:
- name: Install dependencies
run: julia --project=docs/ -e 'using Pkg; Pkg.develop(PackageSpec(path=pwd())); Pkg.instantiate()'
- name: Build and deploy
with:
julia-version: 'nightly'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # If authenticating with GitHub Actions token
DOCUMENTER_KEY: ${{ secrets.DOCUMENTER_KEY }} # If authenticating with SSH deploy key
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/unittest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ jobs:
julia-arch: x86
os: ubuntu-latest
experimental: true
- julia-version: 1
os: macOS-latest
experimental: false
# - julia-version: 1
# os: macOS-latest
# experimental: false
steps:
- name: Checkout Repository
uses: actions/checkout@v2
Expand Down
2 changes: 1 addition & 1 deletion src/basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Base.empty(T::Type{<:MemView{E}}) where E = T(memoryref(Memory{E}()), 0)
Base.pointer(x::MemView{T}) where {T} = Ptr{T}(pointer(x.ref))
Base.unsafe_convert(::Type{Ptr{T}}, v::MemView{T}) where {T} = pointer(v)
Base.elsize(::Type{<:MemView{T}}) where {T} = Base.elsize(Memory{T})
Base.sizeof(x::MemView) = sizeof(eltype(x)) * length(x)
Base.sizeof(x::MemView) = Base.elsize(typeof(x)) * length(x)
Base.strides(::MemView) = (1,)

function Base.getindex(v::MemView, idx::AbstractUnitRange)
Expand Down
114 changes: 114 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,40 @@ MUT_BACKINGS = Any[
]
@test_throws Exception MemView(nonmem)
end

@testset "Unsafe mutability" begin
v = [1.0, 2.0, 3.0]
m = ImmutableMemView(v)
m2 = MutableMemView(MemViews.unsafe, m)
m2[2] = 5.0
@test v == [1.0, 5.0, 3.0]
end
end

@testset "More construction" begin
mem = MemView([1,2,3])
@test MemView(mem) === mem

mem = MemView(view("abc", 2:3))
@test mem isa ImmutableMemView{UInt8}
@test mem == [0x62, 0x63]
end

@testset "Unsafe casting" begin
v = UInt32[htol(0x04030201), htol(0x08070605)]
mem = as_bytes(MemViews.unsafe, MemView(v))
@test mem == 0x01:08
mem[2] = 0x09
@test mem == [1, 9, 3, 4, 5, 6, 7, 8]

mem = MemView("abc")
@test as_bytes(MemViews.unsafe, mem) === mem
end

@testset "Immutable views are immutable" begin
mem = MemView("abc")
@test mem isa ImmutableMemView{UInt8}
@test ImmutableMemView(mem) === mem
mutmem = MemView(collect(codeunits("def")))

@test_throws Exception mem[1] = 2
Expand Down Expand Up @@ -122,6 +151,13 @@ end
@test mem2 == [10, 1, 8]
end

@testset "Views of memviews" begin
mem = MemView(rand(3, 4))
mem2 = view(mem, 4:7)
@test mem2 === mem[4:7]
mem2 .= [1.0, 2.0, 3.0, 4.0]
@test mem[4:7] == [1.0, 2.0, 3.0, 4.0]
end

@testset "setindex!" begin
v = Int16[32, 924, 231, 0, -145]
Expand Down Expand Up @@ -204,4 +240,82 @@ end
@test parentindices(mem) == 5:7
end

@testset "Similar and empty" begin
mem = MemView(Int16[6, 4, 3])
@test typeof(empty(mem)) == typeof(mem)
@test isempty(empty(mem))

mem2 = empty(mem, Int8)
@test isempty(mem2)
@test typeof(mem2) == MutableMemView{Int8}

mem = MemView("abc")
mem2 = similar(mem)
@test length(mem2) == length(mem)
@test typeof(mem2) == typeof(mem)

mem = MemView(String["", "", ""])
mem2 = similar(mem, Int, 4)
@test length(mem2) == 4
@test eltype(mem2) == Int
end

@testset "Sizeof" begin
@test sizeof(MemView("abc")) == 3
@test sizeof(MemView([1, 2, 3])) == 3 * sizeof(Int)
@test sizeof(MemView(String["", "", "", ""])) == 4 * sizeof(Int)
end

@testset "Copyto" begin
# Copy!
v1 = [5, 2, 1, 9, 8]
v2 = [0, 2, 6, 3, 9]
mem1 = MemView(v1)
mem2 = MemView(v2)
copy!(mem1, mem2)
@test v1 == v2
@test mem1 == mem2

@test_throws BoundsError copy!(MemView([1]), MemView([1, 2]))
@test_throws BoundsError copy!(MemView([1, 2]), MemView([1]))

# Copyto!
v1 = [4, 2, 6, 7, 9]
v2 = [1, 5, 2, 3]
copyto!(MemView(v1), MemView(v2))
@test v1 == [1, 5, 2, 3, 9]
@test_throws BoundsError copyto!(MemView(v2), MemView(v1))

# unsafe_copyto!
v1 = [3, 6, 2, 1]
v2 = [0, 9, 5]
unsafe_copyto!(MemView(v1), MemView(v2))
@test v1 == [0, 9, 5, 1]
v2 = rand(Int, 4)
unsafe_copyto!(MemView(v1), MemView(v2))
@test v2 == v1
end

@testset "Findnext" begin
mem = MemView([4,3,2])
@test findfirst(==(2), mem) == 3

mem = MemView(Int8[6, 2, 7, 0, 2])
@test findfirst(iszero, mem) == 4
@test findfirst(==(Int8(0)), mem) == 4
end
end

@testset "MemKind" begin
@test MemKind(Vector{Int16}) == IsMemory(MutableMemView{Int16})
@test MemKind(typeof(codeunits(view("abc", 2:3)))) == IsMemory(ImmutableMemView{UInt8})
@test MemKind(typeof(view(Memory{String}(undef, 3), Base.OneTo(2)))) == IsMemory(MutableMemView{String})
@test MemKind(Matrix{Nothing}) == IsMemory(MutableMemView{Nothing})
@test MemKind(Memory{Int32}) == IsMemory(MutableMemView{Int32})

@test MemKind(SubString{String}) == NotMemory()
@test MemKind(String) == NotMemory()
@test MemKind(Int) == NotMemory()
@test MemKind(Nothing) == NotMemory()
@test MemKind(Union{}) == NotMemory()
end

0 comments on commit 7068804

Please sign in to comment.