Skip to content

Commit a29e7b8

Browse files
authored
Minor code refactor and more test cases for TArray (#49)
1 parent 8118bc4 commit a29e7b8

File tree

4 files changed

+48
-9
lines changed

4 files changed

+48
-9
lines changed

src/tarray.jl

+14-8
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ function Base.getindex(S::TArray{T, N}, I::Vararg{Int,N}) where {T, N}
5353
return d[I...]
5454
end
5555

56-
function Base.setindex!(S::TArray{T, N}, x, I::Vararg{Int,N}) where {T, N}
56+
function Base.setindex!(S::TArray{T, N}, x::T, I::Vararg{Int,N}) where {T, N}
5757
n, d = task_local_storage(S.ref)
5858
cn = n_copies()
5959
newd = d
@@ -65,7 +65,7 @@ function Base.setindex!(S::TArray{T, N}, x, I::Vararg{Int,N}) where {T, N}
6565
newd[I...] = x
6666
end
6767

68-
function Base.push!(S::TArray, x)
68+
function Base.push!(S::TArray{T}, x::T) where T
6969
n, d = task_local_storage(S.ref)
7070
cn = n_copies()
7171
newd = d
@@ -102,21 +102,27 @@ end
102102

103103
function Base.display(S::TArray)
104104
arr = S.orig_task.storage[S.ref][2]
105-
@warn "display(::TArray) prints the originating task's storage, not the current task's storage. Please use show(::TArray) to display the current task's version of a TArray."
105+
@warn "display(::TArray) prints the originating task's storage, " *
106+
"not the current task's storage. " *
107+
"Please use show(::TArray) to display the current task's version of a TArray."
106108
display(arr)
107109
end
108110

109111
Base.show(io::IO, S::TArray) = Base.show(io::IO, task_local_storage(S.ref)[2])
110-
Base.size(S::TArray) = Base.size(task_local_storage(S.ref)[2])
111-
Base.ndims(S::TArray) = Base.ndims(task_local_storage(S.ref)[2])
112112

113113
# Base.get(t::Task, S) = S
114114
# Base.get(t::Task, S::TArray) = (t.storage[S.ref][2])
115115
Base.get(S::TArray) = (current_task().storage[S.ref][2])
116116

117-
# Implements eltype, firstindex, lastindex, and iterate
118-
# functions.
119-
for F in (:eltype, :firstindex, :lastindex, :iterate)
117+
##
118+
# Iterator Interface
119+
IteratorSize(::Type{TArray{T, N}}) where {T, N} = HasShape{N}()
120+
IteratorEltype(::Type{TArray}) = HasEltype()
121+
122+
# Implements iterate, eltype, length, and size functions,
123+
# as well as firstindex, lastindex, ndims, and axes
124+
for F in (:iterate, :eltype, :length, :size,
125+
:firstindex, :lastindex, :ndims, :axes)
120126
@eval Base.$F(a::TArray, args...) = $F(get(a), args...)
121127
end
122128

src/tref.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
##########
2-
# TRef #
2+
# TRef #
33
##########
44

55
"""

test/runtests.jl

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ include("clonetask.jl")
22
include("brokentask.jl")
33
include("tarray.jl")
44
include("tarray2.jl")
5+
include("tarray3.jl")

test/tarray3.jl

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using Libtask
2+
using Test
3+
4+
@testset "TArray Copying Test" begin
5+
DATA = Dict{Task, Array}()
6+
7+
function f_ct()
8+
ta = tzeros(UInt64, 4);
9+
for i in 1:4
10+
ta[i] = hash(current_task())
11+
DATA[current_task()] = convert(Array, ta)
12+
produce(ta[i])
13+
end
14+
end
15+
16+
t = CTask(f_ct)
17+
18+
@test consume(t) == hash(t) # index = 1
19+
@test consume(t) == hash(t) # index = 2
20+
21+
a = copy(t);
22+
23+
@test consume(a) == hash(a) # index = 3
24+
@test consume(a) == hash(a) # index = 4
25+
26+
@test consume(t) == hash(t) # index = 3
27+
28+
29+
@test DATA[t] == [hash(t), hash(t), hash(t), 0]
30+
@test DATA[a] == [hash(t), hash(t), hash(a), hash(a)]
31+
32+
end

0 commit comments

Comments
 (0)