mirror of https://github.com/swig/swig
Add a testcase for testing %native
This commit is contained in:
parent
054a85c546
commit
50a80e6ee3
|
@ -314,6 +314,7 @@ CPP_TEST_CASES += \
|
|||
namespace_virtual_method \
|
||||
nspace \
|
||||
nspace_extend \
|
||||
native_directive \
|
||||
naturalvar \
|
||||
naturalvar_more \
|
||||
naturalvar_onoff \
|
||||
|
|
|
@ -0,0 +1,22 @@
|
|||
import native_directive.*;
|
||||
|
||||
public class native_directive_runme {
|
||||
|
||||
static {
|
||||
try {
|
||||
System.loadLibrary("native_directive");
|
||||
} catch (UnsatisfiedLinkError e) {
|
||||
System.err.println("Native code library failed to load. See the chapter on Dynamic Linking Problems in the SWIG Java documentation for help.\n" + e);
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String argv[]) {
|
||||
String s = "abc.DEF-123";
|
||||
if (native_directive.CountAlphas(s) != 6)
|
||||
throw new RuntimeException("CountAlphas failed");
|
||||
if (native_directive.CountAlphaCharacters(s) != 6)
|
||||
throw new RuntimeException("CountAlphaCharacters failed");
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,43 @@
|
|||
%module native_directive
|
||||
|
||||
%{
|
||||
#include <ctype.h>
|
||||
int alpha_count(const char *instring) {
|
||||
int count = 0;
|
||||
const char *s = instring;
|
||||
while (s && *s) {
|
||||
if (isalpha((int)*s))
|
||||
count++;
|
||||
s++;
|
||||
};
|
||||
return count;
|
||||
}
|
||||
%}
|
||||
|
||||
%inline %{
|
||||
int CountAlphas(const char *instring) {
|
||||
return alpha_count(instring);
|
||||
}
|
||||
%}
|
||||
|
||||
// Languages that support %native should code up language specific implementations below
|
||||
|
||||
#if defined(SWIGJAVA)
|
||||
%native(CountAlphaCharacters) int alpha_count(const char *inputString);
|
||||
%{
|
||||
extern "C" JNIEXPORT jint JNICALL Java_native_1directive_native_1directiveJNI_CountAlphaCharacters(JNIEnv *jenv, jclass jcls, jstring instring) {
|
||||
jint jresult = 0 ;
|
||||
(void)jcls;
|
||||
|
||||
if (instring) {
|
||||
const char *s = (char *)jenv->GetStringUTFChars(instring, 0);
|
||||
if (s) {
|
||||
jresult = (jint)alpha_count(s);
|
||||
jenv->ReleaseStringUTFChars(instring, s);
|
||||
}
|
||||
}
|
||||
return jresult;
|
||||
}
|
||||
%}
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue