Skip to content

Commit 9a467f6

Browse files
authored
Add more tests (#41)
Add missing tests for specific behaviour
1 parent 362c872 commit 9a467f6

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "MemoryViews"
22
uuid = "a791c907-b98b-4e44-8f4d-e4c2362c6b2f"
3-
version = "0.4.0"
3+
version = "0.4.1"
44
authors = ["Jakob Nybo Nissen <jakobnybonissen@gmail.com>"]
55

66
[deps]

test/runtests.jl

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,20 @@ end
107107
@test !Base.mightalias(v1, v2)
108108
@test Base.mightalias(MemoryView(v1)[2:2], v1)
109109
@test Base.mightalias(view(v1, 2:3), MemoryView(v1))
110+
111+
# Different element types can never alias
112+
@test !Base.mightalias(MemoryView(Int[1, 2]), MemoryView(UInt[1, 2]))
113+
@test !Base.mightalias(MemoryView(UInt8[1]), MemoryView(Int8[1]))
114+
@test !Base.mightalias(MemoryView(Float32[1.0]), MemoryView(Int32[1]))
115+
@test !Base.mightalias(ImmutableMemoryView(Int[1]), MemoryView(UInt[1]))
116+
117+
# Empty views of the same type never alias
118+
m1 = MemoryView(Int[])
119+
m2 = MemoryView([1, 2, 3])
120+
@test !Base.mightalias(m1, m2)
121+
@test !Base.mightalias(m2, m1)
122+
@test !Base.mightalias(m1, m1)
123+
@test !Base.mightalias(m2[1:0], m2)
110124
end
111125

112126
@testset "Pointer" begin
@@ -121,6 +135,15 @@ end
121135
end
122136
end
123137

138+
@testset "Array interface" begin
139+
for mem in Any[MemoryView([1, 2, 3]), MemoryView("abc"), MemoryView(Float32[1.0])]
140+
@test strides(mem) === (1,)
141+
@test IndexStyle(typeof(mem)) === Base.IndexLinear()
142+
@test Base.elsize(typeof(mem)) === Base.elsize(typeof(parent(mem)))
143+
end
144+
@test strides(MemoryView(Int[])) === (1,)
145+
end
146+
124147
# Span of views
125148
@testset "Span of views" begin
126149
mem = MemoryView("abc")
@@ -619,6 +642,41 @@ end
619642
@test findnext(==(0x04), mem, 3) === nothing
620643
end
621644
end
645+
646+
@testset "Cmp same ref fast path" begin
647+
v = UInt8[3, 1, 4, 1, 5]
648+
m = MemoryView(v)
649+
# Same ref, same length
650+
@test cmp(m, m) == 0
651+
# Same ref, different length (slices share the ref)
652+
@test cmp(m[1:3], m[1:5]) == -1
653+
@test cmp(m[1:5], m[1:3]) == 1
654+
end
655+
656+
@testset "Equality with singleton eltypes" begin
657+
m1 = MemoryView(fill(nothing, 3))
658+
m2 = MemoryView(fill(nothing, 3))
659+
@test m1 == m2
660+
m2 = MemoryView(fill(nothing, 2))
661+
@test m1 != m2
662+
end
663+
664+
@testset "Slicing preserves mutability" begin
665+
mut = MemoryView([1, 2, 3, 4, 5])
666+
imm = ImmutableMemoryView([1, 2, 3, 4, 5])
667+
for idx in Any[2:4, UInt(2):UInt(4), Base.OneTo(3), :]
668+
@test mut[idx] isa MutableMemoryView{Int}
669+
@test imm[idx] isa ImmutableMemoryView{Int}
670+
end
671+
end
672+
673+
@testset "Find on split results" begin
674+
mem = MemoryView(UInt8[1, 0, 2, 0, 3])
675+
(_, rest) = split_first(mem)
676+
@test findfirst(iszero, rest) == 1
677+
(head, _) = split_at(mem, 4)
678+
@test findlast(iszero, head) == 2
679+
end
622680
end
623681

624682
@testset "memset" begin

0 commit comments

Comments
 (0)