Skip to content

Commit

Permalink
Fixup equality
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobnissen committed Jul 11, 2024
1 parent e234975 commit b188cdb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ function Base.:(==)(a::MemoryView{T}, b::MemoryView{T}) where {T <: Bits}
a.ref === b.ref && return true
GC.@preserve a b begin
aptr = Ptr{Nothing}(pointer(a))
bptr = Ptr{Nothing}(pointer(a))
bptr = Ptr{Nothing}(pointer(b))
y = @ccall memcmp(aptr::Ptr{Nothing}, bptr::Ptr{Nothing}, length(a)::Int)::Cint
end
iszero(y)
Expand Down
25 changes: 21 additions & 4 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ using Test
using MemoryViews
using Aqua

MemoryViews.MemoryView(s::GenericString) = MemoryView(s.string)

MUT_BACKINGS = Any[
# Arrays
UInt8[1, 2],
Expand Down Expand Up @@ -53,6 +55,10 @@ end
@test m isa ImmutableMemoryView{UInt8}
@test isempty(m)
end

s = Test.GenericString("abγδf")
@test codeunits(s) == MemoryView(s)
@test codeunits(s[2:end-2]) == MemoryView(s)[2:end-1]
end

@testset "Immutable views are immutable" begin
Expand Down Expand Up @@ -83,10 +89,6 @@ end
v = view(view(rand(UInt16, 19), 2:11), 3:9)
mem = MemoryView(v)
@test mem == v

s = SubString(Test.GenericString("dslkjad"), 2:5)
# This is not implemented
@test_throws Exception MemoryView(s)
end

memlen(x) = length(MemoryView(x))
Expand Down Expand Up @@ -301,6 +303,21 @@ end
end
end

@testset "Equality" begin
v = rand(UInt, 10)
m1 = MemoryView(v)
m2 = MemoryView(copy(v))
@test m1 == m2
@test m1 !== m2

m2 = m2[1:end-1]
@test m1 != m2
m1 = m1[1:end-2]
@test m1 != m2
m2 = m2[1:end-1]
@test m1 == m2
end

@testset "MemoryKind" begin
@test MemoryKind(Vector{Int16}) == IsMemory(MutableMemoryView{Int16})
@test MemoryKind(typeof(codeunits(view("abc", 2:3)))) ==
Expand Down

0 comments on commit b188cdb

Please sign in to comment.