From da72b7b39bb075d76810c90c2efc41caaee6da1e Mon Sep 17 00:00:00 2001 From: uniederer Date: Thu, 19 Apr 2012 15:37:26 +0000 Subject: [PATCH] - 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 --- LibusbJava/.cproject | 12 +++++++---- LibusbJava/LibusbJava.cpp | 42 ++++++++++++++++++++++++++++----------- LibusbJava/LibusbJava.h | 8 ++++++++ 3 files changed, 46 insertions(+), 16 deletions(-) diff --git a/LibusbJava/.cproject b/LibusbJava/.cproject index 44e4849..be2bb51 100644 --- a/LibusbJava/.cproject +++ b/LibusbJava/.cproject @@ -42,6 +42,7 @@ @@ -53,6 +54,9 @@ + @@ -218,8 +222,8 @@ - - + + @@ -261,8 +265,8 @@ - - + + diff --git a/LibusbJava/LibusbJava.cpp b/LibusbJava/LibusbJava.cpp index f224d66..9dbab72 100644 --- a/LibusbJava/LibusbJava.cpp +++ b/LibusbJava/LibusbJava.cpp @@ -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) { libusb_device **devs; - libusb_context *context = NULL; + libusb_context *context = (ctx != 0) ? (libusb_context*)ctx : NULL; ssize_t cnt; int res, a, i; uint8_t c, h, e; @@ -405,12 +405,8 @@ JNIEXPORT jobject JNICALL Java_ch_ntb_inf_libusbJava_LibusbJava1_libusb_1get_1de clearLibusbJavaError(); libusb_config_descriptor *conf_desc; - if (ctx > 0) { - context = (libusb_context*) ((unsigned long) ctx); - cnt = libusb_get_device_list(context, &devs); - } else { - cnt = libusb_get_device_list(NULL, &devs); - } + + cnt = libusb_get_device_list(context, &devs); if (cnt < 0) { setLibusbJavaError("libusb_get_device_list: Error on Memory allocation"); libusb_free_device_list(devs, 1); @@ -1825,6 +1821,18 @@ JNIEXPORT void JNICALL Java_ch_ntb_inf_libusbJava_LibusbJava1_libusb_1exceptionT 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 * 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 * * \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) { jbyteArray result = env->NewByteArray(len); if (result != NULL) { - /* Using SetByteArrayRegion, we avoid that the JNI layer first copies the data already - * available in 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 - * exception can be omitted. */ + /* By using SetByteArrayRegion, we avoid that the JNI layer first copies the data of + * the array in our space just to overwrite them. As we just allocated the + * byte array with a length of "len", the set operation can never fail. The check for an + * exception can therefore be omitted. */ env->SetByteArrayRegion(result, 0, len, (const signed char *)data); #if 0 /* No need to check for exceptions here... */ if (env->ExceptionOccurred()){ @@ -1902,7 +1915,10 @@ static __inline jbyteArray JNICALL to_byteArray(JNIEnv *env, const void *data, s * environment. * * \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) { @@ -1916,6 +1932,8 @@ static __inline void JNICALL ThrowIfUnsuccessful(JNIEnv *env, int libusb_result) * * \param env Environment to throw the exception in * \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) { diff --git a/LibusbJava/LibusbJava.h b/LibusbJava/LibusbJava.h index fe3348a..d3c5960 100644 --- a/LibusbJava/LibusbJava.h +++ b/LibusbJava/LibusbJava.h @@ -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 (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 } #endif