P.S.:
cuMemcpyHtoD() just wrap the native method cuMemcpyHtoDNative():
public static int cuMemcpyHtoD(CUdeviceptr dstDevice, Pointer srcHost, long ByteCount)
{
return checkResult(cuMemcpyHtoDNative(dstDevice, srcHost, ByteCount));
}
private static native int cuMemcpyHtoDNative(CUdeviceptr dstDevice, Pointer srcHost, long ByteCount);```
I’m not familiar with PowerMock. But where do the ‘ptr’ and ‘numArray’ come from? Are you sure that they are valid pointers/arrays, each containing 8 bytes of data?
there are an initialization statement with the array numArray and the CUDevice ptr:
float[] a = new float[2];
a[0] = 1;
a[1] = 2;
CUdeviceptr ptr = new CUdeviceptr();
But I’m trying to mock the method cuMemcpyHtoD. Therefore the native method cuMemcpyHtoDNative souhld not be call. When I try to mock the native method cuMemcpyHtoDNative I’ll get the same exception. How do you mock JCuda class or method for unit testing?
Although I have never been working with a Mocking Framework, and don’t know the subtleties of such a Framework and its usage: It seems that the Expectation Object that is created with
expect(JCudaDriver.cuMemcpyHtoD(ptr, pointer, 8)).andReturn(CUresult.CUDA_SUCCESS);
includes the method arguments, and the verification is done specifically for these arguments. That means that the arguments which are passed in there (ptr and pointer) must be identically the same as later in the actual method call. (Probably they only have to be “equals” to the ones in the call, but a Pointer.to(array) internally uses a ByteBuffer, which does not have a proper implementation of ‘equals’).
So ensuring that the arguments are identically the same, it should be possible to run a test:
import static org.easymock.EasyMock.expect;
import static org.powermock.api.easymock.PowerMock.*;
import jcuda.Pointer;
import jcuda.driver.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
class Tested
{
public void run(CUdeviceptr ptr, Pointer pointer)
{
JCudaDriver.cuMemcpyHtoD(ptr, pointer, 8);
}
}
@RunWith(PowerMockRunner.class)
@PrepareForTest(JCudaDriver.class)
public class MockTest
{
@Test
public void testMockStaticFinal() throws Exception
{
Tested tested = new Tested();
float[] a = new float[2];
CUdeviceptr ptr = new CUdeviceptr();
Pointer pointer = Pointer.to(a);
mockStaticPartial(JCudaDriver.class, "cuMemcpyHtoD",
CUdeviceptr.class, Pointer.class, Long.TYPE);
expect(JCudaDriver.cuMemcpyHtoD(ptr, pointer, 8)).
andReturn(CUresult.CUDA_SUCCESS);
replayAll();
tested.run(ptr, pointer);
verifyAll();
}
}
I’m not sure how to apply this in a practical tests, but maybe it can serve as a starting point.
bye
Marco
BTW: You may probably close the StackOverflow question, or link it here, or quote the hints from this post there…