Add a testcase for testing %native

This commit is contained in:
William S Fulton 2019-01-31 23:01:42 +00:00
parent 054a85c546
commit 50a80e6ee3
3 changed files with 66 additions and 0 deletions

View File

@ -314,6 +314,7 @@ CPP_TEST_CASES += \
namespace_virtual_method \
nspace \
nspace_extend \
native_directive \
naturalvar \
naturalvar_more \
naturalvar_onoff \

View File

@ -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");
}
}

View File

@ -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