Skip to content

Commit 40aa0a8

Browse files
committed
Remove some custom test macros.
1 parent fa12be0 commit 40aa0a8

File tree

4 files changed

+32
-70
lines changed

4 files changed

+32
-70
lines changed

lib/cudadrv/error.jl

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,37 @@
33
export CuError
44

55

6+
# an optional struct, used to represent e.g. optional error logs.
7+
# this is to make CuErrors with/without additional logs compare equal
8+
# (so that we can simply reuse `@test_throws CuError(code)`).
9+
struct Optional{T}
10+
data::Union{Nothing,T}
11+
Optional{T}(data::Union{Nothing,T}=nothing) where {T} = new{T}(data)
12+
end
13+
Base.getindex(s::Optional) = s.data
14+
function Base.isequal(a::Optional, b::Optional)
15+
if a.data === nothing || b.data === nothing
16+
return true
17+
else
18+
return isequal(a.data, b.data)
19+
end
20+
end
21+
Base.convert(::Type{Optional{T}}, ::Nothing) where T = Optional{T}()
22+
Base.convert(::Type{Optional{T}}, x) where T = Optional{T}(convert(T, x))
23+
24+
625
"""
726
CuError(code)
8-
CuError(code, meta)
27+
CuError(code, details)
928
10-
Create a CUDA error object with error code `code`. The optional `meta` parameter indicates
11-
whether extra information, such as error logs, is known.
29+
Create a CUDA error object with error code `code`. The optional `details` parameter
30+
indicates whether extra information, such as error logs, is known.
1231
"""
1332
struct CuError <: Exception
1433
code::CUresult
15-
meta::Any
34+
details::Optional{String}
1635

17-
CuError(code, meta=nothing) = new(code, meta)
36+
CuError(code, details=nothing) = new(code, details)
1837
end
1938

2039
Base.convert(::Type{CUresult}, err::CuError) = err.code
@@ -63,9 +82,9 @@ function Base.showerror(io::IO, err::CuError)
6382
print(io, "CUDA error: $(description(err)) (code $(reinterpret(Int32, err.code)), $(name(err)))")
6483
end
6584

66-
if err.meta !== nothing
85+
if err.details[] !== nothing
6786
print(io, "\n")
68-
print(io, err.meta)
87+
print(io, err.details[])
6988
end
7089
end
7190

test/core/cudadrv.jl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,7 @@ let
705705
@test md != md2
706706
end
707707

708-
@test_throws_cuerror CUDA.ERROR_INVALID_IMAGE CuModule("foobar")
708+
@test_throws CuError(CUDA.ERROR_INVALID_IMAGE) CuModule("foobar")
709709

710710
@testset "globals" begin
711711
md = CuModuleFile(joinpath(@__DIR__, "ptx/global.ptx"))
@@ -735,7 +735,7 @@ end
735735
# TODO: test with valid object code
736736
# NOTE: apparently, on Windows cuLinkAddData! _does_ accept object data containing \0
737737
if !Sys.iswindows()
738-
@test_throws_cuerror CUDA.ERROR_UNKNOWN add_data!(link, "vadd_parent", UInt8[0])
738+
@test_throws CuError(CUDA.ERROR_UNKNOWN) add_data!(link, "vadd_parent", UInt8[0])
739739
end
740740
end
741741

@@ -744,10 +744,12 @@ end
744744
.version 3.1
745745
.target sm_999
746746
.address_size 64"""
747-
@test_throws_message contains("ptxas fatal") CuError CuModule(invalid_code)
747+
@test_throws CuError(CUDA.ERROR_INVALID_PTX) CuModule(invalid_code)
748+
@test_throws "ptxas fatal" CuModule(invalid_code)
748749

749750
link = CuLink()
750-
@test_throws_message contains("ptxas fatal") CuError add_data!(link, "dummy", invalid_code)
751+
@test_throws CuError(CUDA.ERROR_INVALID_PTX) add_data!(link, "dummy", invalid_code)
752+
@test_throws "ptxas fatal" add_data!(link, "dummy", invalid_code)
751753
end
752754

753755
let

test/core/utils.jl

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,6 @@
11
using BFloat16s: BFloat16
22
import CUDA: cudaDataType
33

4-
@testset "test utilities" begin
5-
mutable struct NoThrowTestSet <: Test.AbstractTestSet
6-
results::Vector
7-
NoThrowTestSet(desc) = new([])
8-
end
9-
Test.record(ts::NoThrowTestSet, t::Test.Result) = (push!(ts.results, t); t)
10-
Test.finish(ts::NoThrowTestSet) = ts.results
11-
fails = @testset NoThrowTestSet begin
12-
# OK
13-
@test_throws_cuerror CUDA.ERROR_UNKNOWN throw(CuError(CUDA.ERROR_UNKNOWN))
14-
# Fail, wrong CuError
15-
@test_throws_cuerror CUDA.ERROR_UNKNOWN throw(CuError(CUDA.ERROR_INVALID_VALUE))
16-
# Fail, wrong Exception
17-
@test_throws_cuerror CUDA.ERROR_UNKNOWN error()
18-
end
19-
@test isa(fails[1], Test.Pass)
20-
@test isa(fails[2], Test.Fail)
21-
@test isa(fails[3], Test.Fail)
22-
end
23-
244
@testset "@sync" begin
255
t = Base.@elapsed ret = CUDA.@sync begin
266
# TODO: do something that takes a while on the GPU

test/setup.jl

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -136,45 +136,6 @@ macro grab_output(ex)
136136
end
137137
end
138138

139-
# variant on @test_throws that checks the CuError error code
140-
macro test_throws_cuerror(code, ex)
141-
# generate a test only returning CuError if it is the correct one
142-
test = quote
143-
try
144-
$(esc(ex))
145-
catch err
146-
isa(err, CuError) || rethrow()
147-
err.code == $code || error("Wrong CuError: ", err.code, " instead of ", $code)
148-
rethrow()
149-
end
150-
end
151-
152-
# now re-use @test_throws (which ties into @testset, etc)
153-
quote
154-
@test_throws CuError $test
155-
end
156-
end
157-
158-
# @test_throw, with additional testing for the exception message
159-
macro test_throws_message(f, typ, ex...)
160-
@gensym msg
161-
quote
162-
$msg = ""
163-
@test_throws $(esc(typ)) try
164-
$(esc(ex...))
165-
catch err
166-
$msg = sprint(showerror, err)
167-
rethrow()
168-
end
169-
170-
if !$(esc(f))($msg)
171-
# @test should return its result, but doesn't
172-
@error "Failed to validate error message\n" * $msg
173-
end
174-
@test $(esc(f))($msg)
175-
end
176-
end
177-
178139
# Run some code on-device
179140
macro on_device(ex...)
180141
code = ex[end]

0 commit comments

Comments
 (0)