Skip to content

Commit

Permalink
Allow unsafe_wrapping PointerWrappers to vectors (#82)
Browse files Browse the repository at this point in the history
* allow unsafe_wrapping PointerWrappers to vectors

* fix order in tests

* Update tests_basic.jl
  • Loading branch information
JoshuaLampert authored May 8, 2023
1 parent 4ae835e commit 0cbad1b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/pointerwrappers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ Base.unsafe_load(pw::PointerWrapper, i::Integer=1) = unsafe_load(pointer(pw), i)

# When `unsafe_wrap`ping a PointerWrapper object, we really want to wrap the underlying array
Base.unsafe_wrap(AType::Union{Type{Array},Type{Array{T}},Type{Array{T,N}}},
pw::PointerWrapper, dims::NTuple{N,Int}; own::Bool = false) where {T,N} = unsafe_wrap(AType, pointer(pw), dims; own)
pw::PointerWrapper, dims::Union{NTuple{N,Int}, Integer}; own::Bool = false) where {T,N} = unsafe_wrap(AType, pointer(pw), dims; own)

# If value is of the wrong type, try to convert it
Base.unsafe_store!(pw::PointerWrapper{T}, value, i::Integer=1) where T = unsafe_store!(pw, convert(T, value), i)
Expand Down
20 changes: 13 additions & 7 deletions test/tests_basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -85,17 +85,23 @@ end

# `unsafe_wrap`ping a `PointerWrapper`
n_vertices::Int = connectivity_pw.num_vertices[]
vertices = @test_nowarn unsafe_wrap(Array, connectivity_pw.vertices, (3, n_vertices))
@test vertices isa Array{Float64, 2}
# wrapping matrices
vertices_matrix = @test_nowarn unsafe_wrap(Array, connectivity_pw.vertices, (3, n_vertices))
@test vertices_matrix isa Array{Float64, 2}
@test unsafe_wrap(Array{Float64}, connectivity_pw.vertices, (3, n_vertices)) isa Array{Float64, 2}
@test unsafe_wrap(Array{Float64, 2}, connectivity_pw.vertices, (3, n_vertices)) isa Array{Float64, 2}

@test size(vertices) == (3, n_vertices)
@test vertices[1, 1] == connectivity_pw.vertices[1] == 0.0
@test_nowarn vertices[1, 1] = 1.0
@test vertices[1, 1] == connectivity_pw.vertices[1] == 1.0
@test size(vertices_matrix) == (3, n_vertices)
@test vertices_matrix[1, 1] == connectivity_pw.vertices[1] == 0.0
@test_nowarn vertices_matrix[1, 1] = 1.0
@test vertices_matrix[1, 1] == connectivity_pw.vertices[1] == 1.0
@test_nowarn connectivity_pw.vertices[1] = 2.0
@test vertices[1, 1] == connectivity_pw.vertices[1] == 2.0
@test vertices_matrix[1, 1] == connectivity_pw.vertices[1] == 2.0
# wrapping vectors
vertices_vector = @test_nowarn unsafe_wrap(Array, connectivity_pw.vertices, 3 * n_vertices)
@test vertices_vector isa Array{Float64, 1}
@test unsafe_wrap(Array{Float64}, connectivity_pw.vertices, 3 * n_vertices) isa Array{Float64, 1}
@test unsafe_wrap(Array{Float64, 1}, connectivity_pw.vertices, 3 * n_vertices) isa Array{Float64, 1}

@test_nowarn p4est_destroy(p4est_pw)
@test_nowarn p4est_connectivity_destroy(connectivity_pw)
Expand Down

0 comments on commit 0cbad1b

Please sign in to comment.