Skip to content

Commit fa553a2

Browse files
authored
Tests for CUSTATEVEC errors (#2716)
* Tests for CUSTATEVEC errors * Add return statements
1 parent 1f533f4 commit fa553a2

2 files changed

Lines changed: 27 additions & 13 deletions

File tree

lib/custatevec/src/error.jl

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,30 @@ name(err) = string(err.code)
1313

1414
function description(err)
1515
if err.code == CUSTATEVEC_STATUS_SUCCESS
16-
"the operation completed successfully"
16+
return "the operation completed successfully"
1717
elseif err.code == CUSTATEVEC_STATUS_NOT_INITIALIZED
18-
"the library was not initialized"
18+
return "the library was not initialized"
1919
elseif err.code == CUSTATEVEC_STATUS_ALLOC_FAILED
20-
"the resource allocation failed"
20+
return "the resource allocation failed"
2121
elseif err.code == CUSTATEVEC_STATUS_INVALID_VALUE
22-
"an invalid value was used as an argument"
22+
return "an invalid value was used as an argument"
2323
elseif err.code == CUSTATEVEC_STATUS_ARCH_MISMATCH
24-
"an absent device architectural feature is required"
24+
return "an absent device architectural feature is required"
2525
elseif err.code == CUSTATEVEC_STATUS_EXECUTION_FAILED
26-
"the GPU program failed to execute"
26+
return "the GPU program failed to execute"
2727
elseif err.code == CUSTATEVEC_STATUS_INTERNAL_ERROR
28-
"an internal operation failed"
28+
return "an internal operation failed"
2929
elseif err.code == CUSTATEVEC_STATUS_NOT_SUPPORTED
30-
"the API is not supported by the backend."
30+
return "the API is not supported by the backend."
3131
elseif err.code == CUSTATEVEC_STATUS_INSUFFICIENT_WORKSPACE
32-
"the workspace on the device is too small to execute."
32+
return "the workspace on the device is too small to execute."
3333
elseif err.code == CUSTATEVEC_STATUS_SAMPLER_NOT_PREPROCESSED
34-
"the sampler was called prior to preprocessing."
34+
return "the sampler was called prior to preprocessing."
3535
elseif err.code == CUSTATEVEC_STATUS_NO_DEVICE_ALLOCATOR
36-
"the device memory pool was not set."
36+
return "the device memory pool was not set."
3737
elseif err.code == CUSTATEVEC_STATUS_DEVICE_ALLOCATOR_ERROR
38-
"operation with the device memory pool failed"
38+
return "operation with the device memory pool failed"
3939
else
40-
"no description for this error"
40+
return "no description for this error"
4141
end
4242
end

lib/custatevec/test/runtests.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,20 @@ using cuStateVec
1010
@testset "cuStateVec" begin
1111
import cuStateVec: CuStateVec, applyMatrix!, applyMatrixBatched!, applyPauliExp!, applyGeneralizedPermutationMatrix!, expectation, expectationsOnPauliBasis, sample, testMatrixType, Pauli, PauliX, PauliY, PauliZ, PauliI, measureOnZBasis!, swapIndexBits!, abs2SumOnZBasis, collapseOnZBasis!, batchMeasure!, batchMeasureWithOffset!, abs2SumArray, collapseByBitString!, abs2SumArrayBatched, collapseByBitStringBatched!, accessorSet!, accessorGet, CuStateVecAccessor
1212

13+
@testset "Errors" begin
14+
@test sprint(showerror, cuStateVec.CUSTATEVECError(cuStateVec.CUSTATEVEC_STATUS_SUCCESS)) == "CUSTATEVECError: the operation completed successfully (code 0, CUSTATEVEC_STATUS_SUCCESS)"
15+
@test cuStateVec.description(cuStateVec.CUSTATEVECError(cuStateVec.CUSTATEVEC_STATUS_NOT_INITIALIZED)) == "the library was not initialized"
16+
@test cuStateVec.description(cuStateVec.CUSTATEVECError(cuStateVec.CUSTATEVEC_STATUS_ALLOC_FAILED)) == "the resource allocation failed"
17+
@test cuStateVec.description(cuStateVec.CUSTATEVECError(cuStateVec.CUSTATEVEC_STATUS_INVALID_VALUE)) == "an invalid value was used as an argument"
18+
@test cuStateVec.description(cuStateVec.CUSTATEVECError(cuStateVec.CUSTATEVEC_STATUS_ARCH_MISMATCH)) == "an absent device architectural feature is required"
19+
@test cuStateVec.description(cuStateVec.CUSTATEVECError(cuStateVec.CUSTATEVEC_STATUS_EXECUTION_FAILED)) == "the GPU program failed to execute"
20+
@test cuStateVec.description(cuStateVec.CUSTATEVECError(cuStateVec.CUSTATEVEC_STATUS_INTERNAL_ERROR)) == "an internal operation failed"
21+
@test cuStateVec.description(cuStateVec.CUSTATEVECError(cuStateVec.CUSTATEVEC_STATUS_NOT_SUPPORTED)) == "the API is not supported by the backend."
22+
@test cuStateVec.description(cuStateVec.CUSTATEVECError(cuStateVec.CUSTATEVEC_STATUS_INSUFFICIENT_WORKSPACE)) == "the workspace on the device is too small to execute."
23+
@test cuStateVec.description(cuStateVec.CUSTATEVECError(cuStateVec.CUSTATEVEC_STATUS_SAMPLER_NOT_PREPROCESSED)) == "the sampler was called prior to preprocessing."
24+
@test cuStateVec.description(cuStateVec.CUSTATEVECError(cuStateVec.CUSTATEVEC_STATUS_NO_DEVICE_ALLOCATOR)) == "the device memory pool was not set."
25+
@test cuStateVec.description(cuStateVec.CUSTATEVECError(cuStateVec.CUSTATEVEC_STATUS_DEVICE_ALLOCATOR_ERROR)) == "operation with the device memory pool failed"
26+
end
1327
@testset "applyMatrix! and expectation" begin
1428
# build a simple state and compute expectations
1529
n_q = 2

0 commit comments

Comments
 (0)