- First steps in doing unit tests of the C-part

git-svn-id: https://svn.code.sf.net/p/libusbjava/code/trunk@295 94ad28fe-ef68-46b1-9651-e7ae4fcf1c4c
This commit is contained in:
uniederer
2012-04-19 15:37:26 +00:00
parent 2730203a29
commit da72b7b39b
3 changed files with 46 additions and 16 deletions

View File

@@ -42,6 +42,7 @@
<option id="gnu.cpp.compiler.option.other.other.913778927" name="Other flags" superClass="gnu.cpp.compiler.option.other.other" value=" -mwindows -c -fmessage-length=0" valueType="string"/> <option id="gnu.cpp.compiler.option.other.other.913778927" name="Other flags" superClass="gnu.cpp.compiler.option.other.other" value=" -mwindows -c -fmessage-length=0" valueType="string"/>
<option id="gnu.cpp.compiler.option.preprocessor.def.1526583498" name="Defined symbols (-D)" superClass="gnu.cpp.compiler.option.preprocessor.def" valueType="definedSymbols"> <option id="gnu.cpp.compiler.option.preprocessor.def.1526583498" name="Defined symbols (-D)" superClass="gnu.cpp.compiler.option.preprocessor.def" valueType="definedSymbols">
<listOptionValue builtIn="false" value="_JNI_IMPLEMENTATION_"/> <listOptionValue builtIn="false" value="_JNI_IMPLEMENTATION_"/>
<listOptionValue builtIn="false" value="DO_UNIT_TEST=1"/>
</option> </option>
<inputType id="cdt.managedbuild.tool.gnu.cpp.compiler.input.728081923" superClass="cdt.managedbuild.tool.gnu.cpp.compiler.input"/> <inputType id="cdt.managedbuild.tool.gnu.cpp.compiler.input.728081923" superClass="cdt.managedbuild.tool.gnu.cpp.compiler.input"/>
</tool> </tool>
@@ -53,6 +54,9 @@
<listOptionValue builtIn="false" value="&quot;C:/Program Files/Java/jdk1.6.0_26/include&quot;"/> <listOptionValue builtIn="false" value="&quot;C:/Program Files/Java/jdk1.6.0_26/include&quot;"/>
<listOptionValue builtIn="false" value="&quot;C:/Program Files/Java/jdk1.6.0_26/include/win32&quot;"/> <listOptionValue builtIn="false" value="&quot;C:/Program Files/Java/jdk1.6.0_26/include/win32&quot;"/>
</option> </option>
<option id="gnu.c.compiler.option.preprocessor.def.symbols.645355643" superClass="gnu.c.compiler.option.preprocessor.def.symbols" valueType="definedSymbols">
<listOptionValue builtIn="false" value="DO_UNIT_TEST=1"/>
</option>
<inputType id="cdt.managedbuild.tool.gnu.c.compiler.input.1861606347" superClass="cdt.managedbuild.tool.gnu.c.compiler.input"/> <inputType id="cdt.managedbuild.tool.gnu.c.compiler.input.1861606347" superClass="cdt.managedbuild.tool.gnu.c.compiler.input"/>
</tool> </tool>
<tool id="cdt.managedbuild.tool.gnu.c.linker.mingw.so.debug.1427267537" name="MinGW C Linker" superClass="cdt.managedbuild.tool.gnu.c.linker.mingw.so.debug"> <tool id="cdt.managedbuild.tool.gnu.c.linker.mingw.so.debug.1427267537" name="MinGW C Linker" superClass="cdt.managedbuild.tool.gnu.c.linker.mingw.so.debug">
@@ -218,8 +222,8 @@
<parser enabled="false"/> <parser enabled="false"/>
</scannerInfoProvider> </scannerInfoProvider>
</profile> </profile>
<scannerConfigBuildInfo instanceId="cdt.managedbuild.config.gnu.mingw.so.debug.655628217;cdt.managedbuild.config.gnu.mingw.so.debug.655628217.;cdt.managedbuild.tool.gnu.cpp.compiler.mingw.so.debug.1164145566;cdt.managedbuild.tool.gnu.cpp.compiler.input.728081923"> <scannerConfigBuildInfo instanceId="cdt.managedbuild.config.gnu.mingw.so.release.1888654320;cdt.managedbuild.config.gnu.mingw.so.release.1888654320.;cdt.managedbuild.tool.gnu.c.compiler.mingw.so.release.913614476;cdt.managedbuild.tool.gnu.c.compiler.input.1931731652">
<autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId="org.eclipse.cdt.managedbuilder.core.GCCManagedMakePerProjectProfileCPP"/> <autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId="org.eclipse.cdt.managedbuilder.core.GCCManagedMakePerProjectProfileC"/>
<profile id="org.eclipse.cdt.make.core.GCCStandardMakePerProjectProfile"> <profile id="org.eclipse.cdt.make.core.GCCStandardMakePerProjectProfile">
<buildOutputProvider> <buildOutputProvider>
<openAction enabled="true" filePath=""/> <openAction enabled="true" filePath=""/>
@@ -261,8 +265,8 @@
</scannerInfoProvider> </scannerInfoProvider>
</profile> </profile>
</scannerConfigBuildInfo> </scannerConfigBuildInfo>
<scannerConfigBuildInfo instanceId="cdt.managedbuild.config.gnu.mingw.so.release.1888654320;cdt.managedbuild.config.gnu.mingw.so.release.1888654320.;cdt.managedbuild.tool.gnu.c.compiler.mingw.so.release.913614476;cdt.managedbuild.tool.gnu.c.compiler.input.1931731652"> <scannerConfigBuildInfo instanceId="cdt.managedbuild.config.gnu.mingw.so.debug.655628217;cdt.managedbuild.config.gnu.mingw.so.debug.655628217.;cdt.managedbuild.tool.gnu.cpp.compiler.mingw.so.debug.1164145566;cdt.managedbuild.tool.gnu.cpp.compiler.input.728081923">
<autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId="org.eclipse.cdt.managedbuilder.core.GCCManagedMakePerProjectProfileC"/> <autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId="org.eclipse.cdt.managedbuilder.core.GCCManagedMakePerProjectProfileCPP"/>
<profile id="org.eclipse.cdt.make.core.GCCStandardMakePerProjectProfile"> <profile id="org.eclipse.cdt.make.core.GCCStandardMakePerProjectProfile">
<buildOutputProvider> <buildOutputProvider>
<openAction enabled="true" filePath=""/> <openAction enabled="true" filePath=""/>

View File

@@ -194,7 +194,7 @@ JNIEXPORT void JNICALL Java_ch_ntb_inf_libusbJava_LibusbJava1_libusb_1exit(JNIEn
********************************************************************************************/ ********************************************************************************************/
JNIEXPORT jobject JNICALL Java_ch_ntb_inf_libusbJava_LibusbJava1_libusb_1get_1device_1list( JNIEnv *env, jclass obj, jlong ctx) { JNIEXPORT jobject JNICALL Java_ch_ntb_inf_libusbJava_LibusbJava1_libusb_1get_1device_1list( JNIEnv *env, jclass obj, jlong ctx) {
libusb_device **devs; libusb_device **devs;
libusb_context *context = NULL; libusb_context *context = (ctx != 0) ? (libusb_context*)ctx : NULL;
ssize_t cnt; ssize_t cnt;
int res, a, i; int res, a, i;
uint8_t c, h, e; uint8_t c, h, e;
@@ -405,12 +405,8 @@ JNIEXPORT jobject JNICALL Java_ch_ntb_inf_libusbJava_LibusbJava1_libusb_1get_1de
clearLibusbJavaError(); clearLibusbJavaError();
libusb_config_descriptor *conf_desc; libusb_config_descriptor *conf_desc;
if (ctx > 0) {
context = (libusb_context*) ((unsigned long) ctx); cnt = libusb_get_device_list(context, &devs);
cnt = libusb_get_device_list(context, &devs);
} else {
cnt = libusb_get_device_list(NULL, &devs);
}
if (cnt < 0) { if (cnt < 0) {
setLibusbJavaError("libusb_get_device_list: Error on Memory allocation"); setLibusbJavaError("libusb_get_device_list: Error on Memory allocation");
libusb_free_device_list(devs, 1); libusb_free_device_list(devs, 1);
@@ -1825,6 +1821,18 @@ JNIEXPORT void JNICALL Java_ch_ntb_inf_libusbJava_LibusbJava1_libusb_1exceptionT
ThrowLibusbError(env, code); ThrowLibusbError(env, code);
} }
/*
* Class: ch_ntb_inf_libusbJava_LibusbJava1
* Method: to_byteArrayTest
* Signature: (Ljava/lang/String;I)[B
*/
JNIEXPORT jbyteArray JNICALL Java_ch_ntb_inf_libusbJava_LibusbJava1_to_1byteArrayTest(JNIEnv *env, jclass obj, jstring str, jint size)
{
jbyteArray result = to_byteArray(env, env->GetStringUTFChars (str, NULL), size);
env->ReleaseStringUTFChars(str, NULL);
return result;
}
/******************************************************************************************** /********************************************************************************************
* Class: LibusbJava_1_0.cpp * Class: LibusbJava_1_0.cpp
* Method: transfer_callback * Method: transfer_callback
@@ -1868,16 +1876,21 @@ static void LIBUSB_CALL fd_removed_callback(int fd, void *user_data) {
* \return The pointer to the newly created byte array. NULL if an error occured * \return The pointer to the newly created byte array. NULL if an error occured
* *
* \note If NULL is returned, #libusbJavaError is set to a matching error string. * \note If NULL is returned, #libusbJavaError is set to a matching error string.
*
* \test Tested through the unit-test of the java part of this dll by passing a string
* with length 5 and checking the result.
* \test Tested through the Unit-Test of the java part of this dll by passing a string
* with length 0 and checking the result.
*/ */
static __inline jbyteArray JNICALL to_byteArray(JNIEnv *env, const void *data, size_t len) static __inline jbyteArray JNICALL to_byteArray(JNIEnv *env, const void *data, size_t len)
{ {
jbyteArray result = env->NewByteArray(len); jbyteArray result = env->NewByteArray(len);
if (result != NULL) { if (result != NULL) {
/* Using SetByteArrayRegion, we avoid that the JNI layer first copies the data already /* By using SetByteArrayRegion, we avoid that the JNI layer first copies the data of
* available in the array in our space just to overwrite them. As we just allocated the * the array in our space just to overwrite them. As we just allocated the
* byte aray with a length of "len", the set operation can never fail. The check for an * byte array with a length of "len", the set operation can never fail. The check for an
* exception can be omitted. */ * exception can therefore be omitted. */
env->SetByteArrayRegion(result, 0, len, (const signed char *)data); env->SetByteArrayRegion(result, 0, len, (const signed char *)data);
#if 0 /* No need to check for exceptions here... */ #if 0 /* No need to check for exceptions here... */
if (env->ExceptionOccurred()){ if (env->ExceptionOccurred()){
@@ -1902,7 +1915,10 @@ static __inline jbyteArray JNICALL to_byteArray(JNIEnv *env, const void *data, s
* environment. * environment.
* *
* \param env Java environment of the caller * \param env Java environment of the caller
* \param libusb_result Result code of the libusb call * \param libusb_result Result code of the libusb call. If the result is != 0, a
* LibusbError is thrown.
*
* \test Code-Review
*/ */
static __inline void JNICALL ThrowIfUnsuccessful(JNIEnv *env, int libusb_result) static __inline void JNICALL ThrowIfUnsuccessful(JNIEnv *env, int libusb_result)
{ {
@@ -1916,6 +1932,8 @@ static __inline void JNICALL ThrowIfUnsuccessful(JNIEnv *env, int libusb_result)
* *
* \param env Environment to throw the exception in * \param env Environment to throw the exception in
* \param code Error code that represents the cause of the exception * \param code Error code that represents the cause of the exception
*
* \test Tested through the unit-test of the java part of this dll
*/ */
static __inline void JNICALL ThrowLibusbError(JNIEnv *env, jint code) static __inline void JNICALL ThrowLibusbError(JNIEnv *env, jint code)
{ {

View File

@@ -487,6 +487,14 @@ JNIEXPORT jstring JNICALL Java_ch_ntb_inf_libusbJava_LibusbJava1_libusb_1strerro
JNIEXPORT void JNICALL Java_ch_ntb_inf_libusbJava_LibusbJava1_libusb_1exceptionTest JNIEXPORT void JNICALL Java_ch_ntb_inf_libusbJava_LibusbJava1_libusb_1exceptionTest
(JNIEnv *, jclass, jint); (JNIEnv *, jclass, jint);
/*
* Class: ch_ntb_inf_libusbJava_LibusbJava1
* Method: to_byteArrayTest
* Signature: (Ljava/lang/String;I)[B
*/
JNIEXPORT jbyteArray JNICALL Java_ch_ntb_inf_libusbJava_LibusbJava1_to_1byteArrayTest
(JNIEnv *, jclass, jstring, jint);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif