Skip to content

Commit 3d902d9

Browse files
committed
Add tests for the warning/erroring behavior of indexing.
1 parent ff4cc4a commit 3d902d9

File tree

1 file changed

+45
-8
lines changed

1 file changed

+45
-8
lines changed

test/testsuite/indexing.jl

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,44 @@
11
function test_indexing(AT)
2-
# TODO: more fine-grained allowscalar within test_indexing
3-
GPUArrays.@allowscalar @testset "indexing" begin
4-
for T in (Float32, Int32)
2+
@testset "indexing" begin
3+
@allowscalar @testset "errors and warnings" begin
4+
x = AT([0])
5+
6+
allowscalar(true, false)
7+
x[1] = 1
8+
@test x[1] == 1
9+
10+
@disallowscalar begin
11+
@test_throws ErrorException x[1]
12+
@test_throws ErrorException x[1] = 1
13+
end
14+
15+
x[1] = 2
16+
@test x[1] == 2
17+
18+
allowscalar(false)
19+
@test_throws ErrorException x[1]
20+
@test_throws ErrorException x[1] = 1
21+
22+
@allowscalar begin
23+
x[1] = 3
24+
@test x[1] == 3
25+
end
26+
27+
@test_throws ErrorException x[1]
28+
@test_throws ErrorException x[1] = 1
29+
30+
allowscalar(true, false)
31+
x[1]
32+
33+
allowscalar(true, true)
34+
@test_logs (:warn, r"Performing scalar operations on GPU arrays: .*") x[1]
35+
@test_logs x[1]
36+
37+
# NOTE: this inner testset _needs_ to be wrapped with allowscalar
38+
# to make sure its original value is restored.
39+
end
40+
41+
@allowscalar for T in (Float32, Int32)
542
@testset "Indexing with $T" begin
643
x = rand(T, 32)
744
src = AT(x)
@@ -27,7 +64,7 @@ function test_indexing(AT)
2764
end
2865
end
2966

30-
for T in (Float32, Int32)
67+
@allowscalar for T in (Float32, Int32)
3168
@testset "Indexing with $T" begin
3269
x = fill(zero(T), 7)
3370
src = AT(x)
@@ -43,7 +80,7 @@ function test_indexing(AT)
4380
end
4481
end
4582

46-
for T in (Float32, Int32)
83+
@allowscalar for T in (Float32, Int32)
4784
@testset "issue #42 with $T" begin
4885
Ac = rand(Float32, 2, 2)
4986
A = AT(Ac)
@@ -52,7 +89,7 @@ function test_indexing(AT)
5289
@test A[1, 1] == Ac[1, 1]
5390
end
5491
end
55-
for T in (Float32, Int32)
92+
@allowscalar for T in (Float32, Int32)
5693
@testset "Colon() $T" begin
5794
Ac = rand(T, 10)
5895
A = AT(Ac)
@@ -63,8 +100,8 @@ function test_indexing(AT)
63100
end
64101
end
65102

66-
@testset "get/setindex!" begin
67-
# literal calls to get/setindex! have differen return types
103+
@allowscalar @testset "get/setindex!" begin
104+
# literal calls to get/setindex! have different return types
68105
@test compare(x->getindex(x,1), AT, zeros(Int, 2))
69106
@test compare(x->setindex!(x,1,1), AT, zeros(Int, 2))
70107
end

0 commit comments

Comments
 (0)