From 2abb40b0ca9a6a06bdbe3e66b4235301ed15c693 Mon Sep 17 00:00:00 2001 From: Mathieu Féry Date: Fri, 4 Aug 2023 19:37:14 +0200 Subject: wip(test_case): Example of test case for issue related of 927bbc7160a812bb29c0e7120d4a3009bfb13bbf Almost done --- doc/GlueGen_Mapping.html | 2 +- doc/GlueGen_Mapping.md | 2 +- make/build-test.xml | 72 +- make/scripts/runtest.sh | 9 +- src/antlr/com/jogamp/gluegen/jgram/JavaParser.g | 205 ++- .../com/jogamp/gluegen/JavaCallbackEmitter.java | 2 + src/java/com/jogamp/gluegen/JavaConfiguration.java | 10 + src/java/com/jogamp/gluegen/JavaEmitter.java | 70 +- .../test/junit/generation/BaseTest2FuncPtr.java | 126 ++ .../test/junit/generation/BaseTest3PtrStorage.java | 98 + .../junit/generation/BaseTest4JavaCallback.java | 1852 +++++++++++++++++++ .../test/junit/generation/BindingJNILibLoader.java | 14 +- .../test/junit/generation/Test2FuncPtr.java | 169 -- .../test/junit/generation/Test2p1FuncPtr.java | 75 + .../test/junit/generation/Test2p2FuncPtr.java | 77 + .../test/junit/generation/Test3PtrStorage.java | 139 -- .../test/junit/generation/Test3p1PtrStorage.java | 75 + .../test/junit/generation/Test3p2PtrStorage.java | 77 + .../test/junit/generation/Test4JavaCallback.java | 1914 -------------------- .../test/junit/generation/Test4p1JavaCallback.java | 125 ++ .../test/junit/generation/Test4p2JavaCallback.java | 127 ++ .../gluegen/test/junit/generation/test2-common.cfg | 210 +++ .../test/junit/generation/test2-gluegen.cfg | 11 + .../jogamp/gluegen/test/junit/generation/test2.cfg | 236 --- .../gluegen/test/junit/generation/test2p1.cfg | 16 + .../gluegen/test/junit/generation/test2p2.cfg | 36 + 26 files changed, 3169 insertions(+), 2580 deletions(-) create mode 100644 src/junit/com/jogamp/gluegen/test/junit/generation/BaseTest2FuncPtr.java create mode 100644 src/junit/com/jogamp/gluegen/test/junit/generation/BaseTest3PtrStorage.java create mode 100644 src/junit/com/jogamp/gluegen/test/junit/generation/BaseTest4JavaCallback.java delete mode 100644 src/junit/com/jogamp/gluegen/test/junit/generation/Test2FuncPtr.java create mode 100644 src/junit/com/jogamp/gluegen/test/junit/generation/Test2p1FuncPtr.java create mode 100644 src/junit/com/jogamp/gluegen/test/junit/generation/Test2p2FuncPtr.java delete mode 100644 src/junit/com/jogamp/gluegen/test/junit/generation/Test3PtrStorage.java create mode 100644 src/junit/com/jogamp/gluegen/test/junit/generation/Test3p1PtrStorage.java create mode 100644 src/junit/com/jogamp/gluegen/test/junit/generation/Test3p2PtrStorage.java delete mode 100644 src/junit/com/jogamp/gluegen/test/junit/generation/Test4JavaCallback.java create mode 100644 src/junit/com/jogamp/gluegen/test/junit/generation/Test4p1JavaCallback.java create mode 100644 src/junit/com/jogamp/gluegen/test/junit/generation/Test4p2JavaCallback.java create mode 100644 src/junit/com/jogamp/gluegen/test/junit/generation/test2-common.cfg create mode 100644 src/junit/com/jogamp/gluegen/test/junit/generation/test2-gluegen.cfg delete mode 100644 src/junit/com/jogamp/gluegen/test/junit/generation/test2.cfg create mode 100644 src/junit/com/jogamp/gluegen/test/junit/generation/test2p1.cfg create mode 100644 src/junit/com/jogamp/gluegen/test/junit/generation/test2p2.cfg diff --git a/doc/GlueGen_Mapping.html b/doc/GlueGen_Mapping.html index 43f9219..4ea6b76 100644 --- a/doc/GlueGen_Mapping.html +++ b/doc/GlueGen_Mapping.html @@ -2415,7 +2415,7 @@ accommodating a different key-parameter order between

GlueGen configuration snippet with the added option attribute for the Callback-KeyClass in directive JavaCallbackDef.

-
JavaCallbackDef  alBufferCallback1 0 ALBUFFERCALLBACKTYPESOFT 1 ALCcontext com.jogamp.gluegen.test.junit.generation.Test4JavaCallback.CustomAlBufferCallback1Key
+
JavaCallbackDef  alBufferCallback1 0 ALBUFFERCALLBACKTYPESOFT 1 ALCcontext com.jogamp.gluegen.test.junit.generation.BaseTest4JavaCallback.CustomAlBufferCallback1Key
 JavaCallbackKey  alBufferCallback1 1 ALBUFFERCALLBACKTYPESOFT 0
 

Implementation utilizes a custom Callback-KeyClass diff --git a/doc/GlueGen_Mapping.md b/doc/GlueGen_Mapping.md index 1262082..45892db 100644 --- a/doc/GlueGen_Mapping.md +++ b/doc/GlueGen_Mapping.md @@ -1108,7 +1108,7 @@ C-API Header snipped GlueGen configuration snippet with the added option attribute for the `Callback-KeyClass` in directive `JavaCallbackDef`. ``` -JavaCallbackDef alBufferCallback1 0 ALBUFFERCALLBACKTYPESOFT 1 ALCcontext com.jogamp.gluegen.test.junit.generation.Test4JavaCallback.CustomAlBufferCallback1Key +JavaCallbackDef alBufferCallback1 0 ALBUFFERCALLBACKTYPESOFT 1 ALCcontext com.jogamp.gluegen.test.junit.generation.BaseTest4JavaCallback.CustomAlBufferCallback1Key JavaCallbackKey alBufferCallback1 1 ALBUFFERCALLBACKTYPESOFT 0 ``` diff --git a/make/build-test.xml b/make/build-test.xml index a791c7c..f472063 100644 --- a/make/build-test.xml +++ b/make/build-test.xml @@ -671,44 +671,90 @@ chmod 644 ${results}/* \${line.separator} + + + + + + + + - - - - + + + + + + - - + + + + + + + + + + + + + + + + + + - - - + + + - diff --git a/make/scripts/runtest.sh b/make/scripts/runtest.sh index e20c09e..ef5a879 100755 --- a/make/scripts/runtest.sh +++ b/make/scripts/runtest.sh @@ -152,9 +152,12 @@ function onetest() { #onetest com.jogamp.gluegen.test.junit.generation.Test1p2ProcAddressEmitter 2>&1 | tee -a $LOG #onetest com.jogamp.gluegen.test.junit.generation.Test1p2LoadJNIAndImplLib 2>&1 | tee -a $LOG onetest com.jogamp.gluegen.test.junit.generation.Test1p2DynamicLibraryBundle 2>&1 | tee -a $LOG -onetest com.jogamp.gluegen.test.junit.generation.Test2FuncPtr 2>&1 | tee -a $LOG -onetest com.jogamp.gluegen.test.junit.generation.Test3PtrStorage 2>&1 | tee -a $LOG -onetest com.jogamp.gluegen.test.junit.generation.Test4JavaCallback 2>&1 | tee -a $LOG +onetest com.jogamp.gluegen.test.junit.generation.Test2p1FuncPtr 2>&1 | tee -a $LOG +onetest com.jogamp.gluegen.test.junit.generation.Test2p2FuncPtr 2>&1 | tee -a $LOG +onetest com.jogamp.gluegen.test.junit.generation.Test3p1PtrStorage 2>&1 | tee -a $LOG +onetest com.jogamp.gluegen.test.junit.generation.Test3p2PtrStorage 2>&1 | tee -a $LOG +onetest com.jogamp.gluegen.test.junit.generation.Test4p1JavaCallback 2>&1 | tee -a $LOG +onetest com.jogamp.gluegen.test.junit.generation.Test4p2JavaCallback 2>&1 | tee -a $LOG #onetest com.jogamp.gluegen.test.junit.structgen.TestStructGen01 2>&1 | tee -a $LOG #onetest com.jogamp.gluegen.test.junit.structgen.TestStructGen02 2>&1 | tee -a $LOG diff --git a/src/antlr/com/jogamp/gluegen/jgram/JavaParser.g b/src/antlr/com/jogamp/gluegen/jgram/JavaParser.g index 1c06bfd..a4d8c72 100644 --- a/src/antlr/com/jogamp/gluegen/jgram/JavaParser.g +++ b/src/antlr/com/jogamp/gluegen/jgram/JavaParser.g @@ -171,9 +171,35 @@ tokens { return functionNames; } + /** Clears the list of inner interfaces this HeaderParser has parsed. + Useful when reusing the same HeaderParser for more than one + header file. */ + public void clearParsedInnerInterfacesNames() { + innerInterfacesNames.clear(); + } + + /** Returns the list of inner interfaces this HeaderParser has parsed. */ + public Set getParsedInnerInterfacesNames() { + return innerInterfacesNames; + } + + /** Clears the list of inner classes this HeaderParser has parsed. + Useful when reusing the same HeaderParser for more than one + header file. */ + public void clearParsedInnerClassesNames() { + innerClassesNames.clear(); + } + + /** Returns the list of inner classes this HeaderParser has parsed. */ + public Set getParsedInnerClassesNames() { + return innerClassesNames; + } + private Set functionNames = new HashSet(); // hash from name of an enumerated value to the EnumType to which it belongs private Set enumNames = new HashSet(); + private Set innerInterfacesNames = new HashSet(); + private Set innerClassesNames = new HashSet(); private int blockDepth = 0; } @@ -213,9 +239,11 @@ importDefinition // A type definition in a file is either a class or interface definition. typeDefinition options {defaultErrorHandler = true;} - : m:modifiers! - ( classDefinition[#m] - | interfaceDefinition[#m] + : antsBefore:annotations + m:modifiers! + antsAfter:annotations + ( classDefinition[#antsBefore,#m,#antsAfter] + | interfaceDefinition[#antsBefore,#m,#antsAfter] ) | SEMI! ; @@ -224,7 +252,7 @@ typeDefinition * Create a separate Type/Var tree for each var in the var list. */ declaration! - : m:modifiers t:typeSpec[false] v:variableDefinitions[#m,#t] + : antsBefore:annotations m:modifiers antsAfter:annotations t:typeSpec[false] v:variableDefinitions[#antsBefore,#m,#antsAfter,#t] {#declaration = #v;} ; @@ -238,7 +266,7 @@ typeSpec[boolean addImagNode] // A class type specification is a class type with possible brackets afterwards // (which would make it an array type). classTypeSpec[boolean addImagNode] - : identifier (lb:LBRACK^ {#lb.setType(ARRAY_DECLARATOR);} RBRACK!)* + : identifier (LT gen:classTypeSpec[false] GT)? (lb:LBRACK^ {#lb.setType(ARRAY_DECLARATOR);} RBRACK!)* { if ( addImagNode ) { #classTypeSpec = #(#[TYPE,"TYPE"], #classTypeSpec); @@ -316,8 +344,8 @@ modifier ; // Definition of a Java class -classDefinition![AST modifiers] - : "class" IDENT +classDefinition![AST antsBefore, AST modifiers, AST antsAfter] + : "class" cn:IDENT // it _might_ have a superclass... sc:superClassClause // it might implement some interfaces... @@ -325,7 +353,9 @@ classDefinition![AST modifiers] // now parse the body of the class cb:classBlock {#classDefinition = #(#[CLASS_DEF,"CLASS_DEF"], - modifiers,IDENT,sc,ic,cb);} + antsBefore,modifiers,antsAfter,cn,sc,ic,cb); + if(blockDepth==1) { + innerClassesNames.add(cn.getText()); } } ; superClassClause! @@ -334,14 +364,16 @@ superClassClause! ; // Definition of a Java Interface -interfaceDefinition![AST modifiers] - : "interface" IDENT +interfaceDefinition![AST antsBefore, AST modifiers, AST antsAfter] + : "interface" in:IDENT // it might extend some other interfaces ie:interfaceExtends // now parse the body of the interface (looks like a class...) cb:classBlock {#interfaceDefinition = #(#[INTERFACE_DEF,"INTERFACE_DEF"], - modifiers,IDENT,ie,cb);} + antsBefore,modifiers,antsAfter,in,ie,cb); + if(blockDepth==1) { + innerInterfacesNames.add(in.getText()); } } ; @@ -379,14 +411,16 @@ implementsClause // need to be some semantic checks to make sure we're doing the right thing... field! : // method, constructor, or variable declaration + antsBefore:annotations mods:modifiers + antsAfter:annotations ( h:ctorHead s:constructorBody // constructor - {#field = #(#[CTOR_DEF,"CTOR_DEF"], mods, h, s);} + {#field = #(#[CTOR_DEF,"CTOR_DEF"], antsBefore, mods, antsAfter, h, s);} - | cd:classDefinition[#mods] // inner class + | cd:classDefinition[#antsBefore,#mods,#antsAfter] // inner class {#field = #cd;} - | id:interfaceDefinition[#mods] // inner interface + | id:interfaceDefinition[#antsBefore,#mods,#antsAfter] // inner interface {#field = #id;} | t:typeSpec[false] // method or variable declaration(s) @@ -403,7 +437,9 @@ field! ( s2:compoundStatement | SEMI ) {#field = #(#[METHOD_DEF,"METHOD_DEF"], + antsBefore, mods, + antsAfter, #(#[TYPE,"TYPE"],rt), fn, param, @@ -411,7 +447,7 @@ field! s2); if(blockDepth==1) { functionNames.add(fn.getText()); } } - | v:variableDefinitions[#mods,#t] SEMI + | v:variableDefinitions[#antsBefore,#mods,#antsAfter,#t] SEMI // {#field = #(#[VARIABLE_DEF,"VARIABLE_DEF"], v);} {#field = #v;} ) @@ -441,11 +477,15 @@ explicitConstructorInvocation {#lp2.setType(SUPER_CTOR_CALL);} ; -variableDefinitions[AST mods, AST t] - : variableDeclarator[getASTFactory().dupTree(mods), - getASTFactory().dupTree(t)] +variableDefinitions[AST antsBefore, AST mods, AST antsAfter, AST t] + : variableDeclarator[getASTFactory().dupTree(antsBefore), + getASTFactory().dupTree(mods), + getASTFactory().dupTree(antsAfter), + getASTFactory().dupTree(t)] ( COMMA! - variableDeclarator[getASTFactory().dupTree(mods), + variableDeclarator[getASTFactory().dupTree(antsBefore), + getASTFactory().dupTree(mods), + getASTFactory().dupTree(antsAfter), getASTFactory().dupTree(t)] )* ; @@ -454,11 +494,11 @@ variableDefinitions[AST mods, AST t] * or a local variable in a method * It can also include possible initialization. */ -variableDeclarator![AST mods, AST t] +variableDeclarator![AST antsBefore, AST mods, AST antsAfter, AST t] : id:IDENT d:declaratorBrackets[t] v:varInitializer - {#variableDeclarator = #(#[VARIABLE_DEF,"VARIABLE_DEF"], mods, #(#[TYPE,"TYPE"],d), id, v); - if(blockDepth==1) { - enumNames.add(id.getText()); + {#variableDeclarator = #(#[VARIABLE_DEF,"VARIABLE_DEF"], antsBefore, mods, antsAfter, #(#[TYPE,"TYPE"],d), id, v); + if(blockDepth==1) { + enumNames.add(id.getText()); } } ; @@ -528,10 +568,11 @@ parameterDeclarationList // A formal parameter. parameterDeclaration! - : pm:parameterModifier t:typeSpec[false] id:IDENT + : antsBefore:annotations pm:parameterModifier antsAfter:annotations + t:typeSpec[false] id:IDENT pd:declaratorBrackets[#t] {#parameterDeclaration = #(#[PARAMETER_DEF,"PARAMETER_DEF"], - pm, #([TYPE,"TYPE"],pd), id);} + antsBefore, pm, antsAfter, #([TYPE,"TYPE"],pd), id);} ; parameterModifier @@ -572,7 +613,7 @@ statement | expression SEMI! // class definition - | m:modifiers! classDefinition[#m] + | antsBefore:annotations m:modifiers! antsAfter:annotations classDefinition[#antsBefore,#m,#antsAfter] // Attach a label to the front of a statement | IDENT c:COLON^ {#c.setType(LABELED_STAT);} statement @@ -695,6 +736,27 @@ handler : "catch"^ LPAREN! parameterDeclaration RPAREN! compoundStatement ; +annotations + : ( annotation )* + ; + +annotation + : ( AT type:typeSpec[false] ( + LPAREN + ( + content:primaryExpression + | + identPrimary ASSIGN primaryExpression + ( + COMMA + identPrimary ASSIGN primaryExpression + )* + ) + RPAREN + )? + ) + ; + // expressions // Note that most of these expressions follow the pattern @@ -1079,52 +1141,53 @@ options { } // OPERATORS -QUESTION : '?' ; -LPAREN : '(' ; -RPAREN : ')' ; -LBRACK : '[' ; -RBRACK : ']' ; -LCURLY : '{' ; -RCURLY : '}' ; -COLON : ':' ; -COMMA : ',' ; -//DOT : '.' ; -ASSIGN : '=' ; -EQUAL : "==" ; -LNOT : '!' ; -BNOT : '~' ; -NOT_EQUAL : "!=" ; -DIV : '/' ; -DIV_ASSIGN : "/=" ; -PLUS : '+' ; -PLUS_ASSIGN : "+=" ; -INC : "++" ; -MINUS : '-' ; -MINUS_ASSIGN : "-=" ; -DEC : "--" ; -STAR : '*' ; -STAR_ASSIGN : "*=" ; -MOD : '%' ; -MOD_ASSIGN : "%=" ; -SR : ">>" ; -SR_ASSIGN : ">>=" ; -BSR : ">>>" ; +QUESTION : '?' ; +LPAREN : '(' ; +RPAREN : ')' ; +LBRACK : '[' ; +RBRACK : ']' ; +LCURLY : '{' ; +RCURLY : '}' ; +COLON : ':' ; +COMMA : ',' ; +//DOT : '.' ; +ASSIGN : '=' ; +EQUAL : "==" ; +LNOT : '!' ; +BNOT : '~' ; +NOT_EQUAL : "!=" ; +DIV : '/' ; +DIV_ASSIGN : "/=" ; +PLUS : '+' ; +PLUS_ASSIGN : "+=" ; +INC : "++" ; +MINUS : '-' ; +MINUS_ASSIGN : "-=" ; +DEC : "--" ; +STAR : '*' ; +STAR_ASSIGN : "*=" ; +MOD : '%' ; +MOD_ASSIGN : "%=" ; +SR : ">>" ; +SR_ASSIGN : ">>=" ; +BSR : ">>>" ; BSR_ASSIGN : ">>>=" ; -GE : ">=" ; -GT : ">" ; -SL : "<<" ; -SL_ASSIGN : "<<=" ; -LE : "<=" ; -LT : '<' ; -BXOR : '^' ; -BXOR_ASSIGN : "^=" ; -BOR : '|' ; -BOR_ASSIGN : "|=" ; -LOR : "||" ; -BAND : '&' ; -BAND_ASSIGN : "&=" ; -LAND : "&&" ; -SEMI : ';' ; +GE : ">=" ; +GT : ">" ; +SL : "<<" ; +SL_ASSIGN : "<<=" ; +LE : "<=" ; +LT : '<' ; +BXOR : '^' ; +BXOR_ASSIGN : "^=" ; +BOR : '|' ; +BOR_ASSIGN : "|=" ; +LOR : "||" ; +BAND : '&' ; +BAND_ASSIGN : "&=" ; +LAND : "&&" ; +SEMI : ';' ; +AT : '@' ; // Whitespace -- ignored diff --git a/src/java/com/jogamp/gluegen/JavaCallbackEmitter.java b/src/java/com/jogamp/gluegen/JavaCallbackEmitter.java index 01ffe8f..3359c1c 100644 --- a/src/java/com/jogamp/gluegen/JavaCallbackEmitter.java +++ b/src/java/com/jogamp/gluegen/JavaCallbackEmitter.java @@ -348,6 +348,8 @@ public final class JavaCallbackEmitter { } private final void emitJavaKeyClass(final CodeUnit unit) { + if( cfg.shouldIgnoreInInterface(KeyClassName) ) return; + emitJavaBriefAPIDoc(unit, "", "", "", "for "); unit.emitln(" public static class "+KeyClassName+" {"); binding.forEachParameter( ( final int idx, final int consumedCount, final Type cType, final JavaType jType, final String name ) -> { diff --git a/src/java/com/jogamp/gluegen/JavaConfiguration.java b/src/java/com/jogamp/gluegen/JavaConfiguration.java index 107b8c3..870b708 100644 --- a/src/java/com/jogamp/gluegen/JavaConfiguration.java +++ b/src/java/com/jogamp/gluegen/JavaConfiguration.java @@ -1808,23 +1808,33 @@ public class JavaConfiguration { final Set parsedEnumNames = parser.getParsedEnumNames(); final Set parsedFuncNames = parser.getParsedFunctionNames(); + final Set parsedInnerInterfaces = parser.getParsedInnerInterfacesNames(); + final Set parsedInnerClasses = parser.getParsedInnerClassesNames(); if(forInterface) { if(onlyList) { extendedIntfSymbolsOnly.addAll(parsedEnumNames); extendedIntfSymbolsOnly.addAll(parsedFuncNames); + extendedIntfSymbolsOnly.addAll(parsedInnerInterfaces); + extendedIntfSymbolsOnly.addAll(parsedInnerClasses); } else { extendedIntfSymbolsIgnore.addAll(parsedEnumNames); extendedIntfSymbolsIgnore.addAll(parsedFuncNames); + extendedIntfSymbolsIgnore.addAll(parsedInnerInterfaces); + extendedIntfSymbolsIgnore.addAll(parsedInnerClasses); } } if(forImplementation) { if(onlyList) { extendedImplSymbolsOnly.addAll(parsedEnumNames); extendedImplSymbolsOnly.addAll(parsedFuncNames); + extendedImplSymbolsOnly.addAll(parsedInnerInterfaces); + extendedImplSymbolsOnly.addAll(parsedInnerClasses); } else { extendedImplSymbolsIgnore.addAll(parsedEnumNames); extendedImplSymbolsIgnore.addAll(parsedFuncNames); + extendedImplSymbolsIgnore.addAll(parsedInnerInterfaces); + extendedImplSymbolsIgnore.addAll(parsedInnerClasses); } } } diff --git a/src/java/com/jogamp/gluegen/JavaEmitter.java b/src/java/com/jogamp/gluegen/JavaEmitter.java index 0936536..6e79b6c 100644 --- a/src/java/com/jogamp/gluegen/JavaEmitter.java +++ b/src/java/com/jogamp/gluegen/JavaEmitter.java @@ -1494,11 +1494,17 @@ public class JavaEmitter implements GlueEmitter { LOG.log(INFO, "JavaCallbackInfo: Reusing {0} -> {1}", jcbd.setFuncName, jcbi0); } else { final StringBuilder cbMethodSignature = new StringBuilder(); - javaUnit.emitln(" /** JavaCallback interface: "+jcbd.cbFuncTypeName+" -> "+funcType.toString(jcbd.cbFuncTypeName, false, true)+" */"); - javaUnit.emitln(" public static interface "+cbSimpleClazzName+" {"); - final List mbs = generateFunctionInterfaceCode(javaUnit, funcSym, jcbd, cbMethodSignature); - javaUnit.emitln(" }"); - javaUnit.emitln(); + final List mbs; + if( !cfg.shouldIgnoreInInterface(jcbd.cbFuncTypeName) ) { + javaUnit.emitln(" /** JavaCallback interface: "+jcbd.cbFuncTypeName+" -> "+funcType.toString(jcbd.cbFuncTypeName, false, true)+" */"); + javaUnit.emitln(" public static interface "+cbSimpleClazzName+" {"); + mbs = generateFunctionInterfaceCode(javaUnit, funcSym, jcbd, cbMethodSignature); + javaUnit.emitln(" }"); + javaUnit.emitln(); + } else { + LOG.log(WARNING, "JavaCallbackInfo: Java Configuration indicate current JavaCallback must be ignored so assume JavaCallback meet presents requirements of {0}", jcbd.setFuncName); + mbs = generateFunctionInterfaceCode(null, funcSym, jcbd, cbMethodSignature); + } if( 1 != mbs.size() ) { throw new UnsupportedOperationException("Multiple bindings generated where only 1 is allowed for func "+funcType.toString(jcbd.cbFuncTypeName, false, true)); } @@ -1508,9 +1514,9 @@ public class JavaEmitter implements GlueEmitter { cbFuncBinding.getJavaReturnType()+", func "+funcType.toString(jcbd.cbFuncTypeName, false, true)); } final JavaCallbackInfo jcbi1 = new JavaCallbackInfo(jcbd.cbFuncTypeName, cbSimpleClazzName, cbFQClazzName, cbMethodSignature.toString(), - funcType, cbFuncBinding, jcbd.cbFuncUserParamIdx, jcbd.cbFuncKeyIndices, - jcbd.setFuncName, jcbd.setFuncUserParamIdx, jcbd.setFuncKeyIndices, - jcbd.userParamClassName, jcbd.customKeyClassName); + funcType, cbFuncBinding, jcbd.cbFuncUserParamIdx, jcbd.cbFuncKeyIndices, + jcbd.setFuncName, jcbd.setFuncUserParamIdx, jcbd.setFuncKeyIndices, + jcbd.userParamClassName, jcbd.customKeyClassName); cfg.setFuncToJavaCallbackMap.put(jcbd.setFuncName, jcbi1); javaCallbackInterfaceMap.put(cbFQClazzName, jcbi1); LOG.log(INFO, "JavaCallbackInfo: Added {0} -> {1}", jcbd.setFuncName, jcbi1); @@ -1556,29 +1562,31 @@ public class JavaEmitter implements GlueEmitter { final boolean useNIOOnly = true; final boolean useNIODirectOnly = true; - for (final MethodBinding binding : bindings) { - // Emit public Java entry point for calling this function pointer - final JavaMethodBindingEmitter emitter = new JavaMethodBindingEmitter(binding, - javaUnit, - cfg.runtimeExceptionType(), - cfg.unsupportedExceptionType(), - false, // emitBody - cfg.tagNativeBinding(), - false, // eraseBufferAndArrayTypes - useNIOOnly, - useNIODirectOnly, - false, // forDirectBufferImplementation - false, // forIndirectBufferAndArrayImplementation - true, // isUnimplemented - true, // isInterface - false, // isNativeMethod - false, // isPrivateNativeMethod - cfg) { - @Override - protected String getBaseIndentString() { return " "; } - }; - emitter.addModifier(JavaMethodBindingEmitter.PUBLIC); - emitter.emit(); + if( null != javaUnit) { + for (final MethodBinding binding : bindings) { + // Emit public Java entry point for calling this function pointer + final JavaMethodBindingEmitter emitter = new JavaMethodBindingEmitter(binding, + javaUnit, + cfg.runtimeExceptionType(), + cfg.unsupportedExceptionType(), + false, // emitBody + cfg.tagNativeBinding(), + false, // eraseBufferAndArrayTypes + useNIOOnly, + useNIODirectOnly, + false, // forDirectBufferImplementation + false, // forIndirectBufferAndArrayImplementation + true, // isUnimplemented + true, // isInterface + false, // isNativeMethod + false, // isPrivateNativeMethod + cfg) { + @Override + protected String getBaseIndentString() { return " "; } + }; + emitter.addModifier(JavaMethodBindingEmitter.PUBLIC); + emitter.emit(); + } } return bindings; } diff --git a/src/junit/com/jogamp/gluegen/test/junit/generation/BaseTest2FuncPtr.java b/src/junit/com/jogamp/gluegen/test/junit/generation/BaseTest2FuncPtr.java new file mode 100644 index 0000000..595ecbc --- /dev/null +++ b/src/junit/com/jogamp/gluegen/test/junit/generation/BaseTest2FuncPtr.java @@ -0,0 +1,126 @@ +/** + * Copyright 2023 JogAmp Community. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are + * permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are those of the + * authors and should not be interpreted as representing official policies, either expressed + * or implied, of JogAmp Community. + */ + +package com.jogamp.gluegen.test.junit.generation; + +import org.junit.Assert; + +/** + * Test {@link Bindingtest2} with {@link T2_InitializeOptions} instance and function pointer... + */ +public class BaseTest2FuncPtr extends BaseClass { + + /** + * Test Bindingtest2 with T2_InitializeOptions instance and function pointer + */ + public void chapter01(final Bindingtest2 bt2) throws Exception { + final T2_InitializeOptions options = T2_InitializeOptions.create(); + Assert.assertEquals(true, options.isOverrideThreadAffinityNull()); + Assert.assertEquals(true, options.isProductNameNull()); + Assert.assertEquals(true, options.isProductVersionNull()); + Assert.assertEquals(0, options.getCustomFuncA1()); + Assert.assertEquals(0, options.getCustomFuncA2()); + Assert.assertEquals(0, options.getCustomFuncB1()); + Assert.assertEquals(0, options.getCustomFuncB2()); + + bt2.Initialize(options); + Assert.assertEquals(true, options.isOverrideThreadAffinityNull()); + Assert.assertEquals(false, options.isProductNameNull()); + Assert.assertEquals(false, options.isProductVersionNull()); + Assert.assertNotEquals(0, options.getCustomFuncA1()); + Assert.assertNotEquals(0, options.getCustomFuncA2()); + Assert.assertNotEquals(0, options.getCustomFuncB1()); + Assert.assertNotEquals(0, options.getCustomFuncB2()); + Assert.assertEquals(1, options.getApiVersion()); + // dropped: Assert.assertEquals(1, T2_InitializeOptions.getReserved1ElemCount()); + BaseClass.assertAPTR(0x0000CAFFEEBEEFL, options.getReserved1()); + { + options.setReserved1(0xBEEFCAFFEE0DADL); + BaseClass.assertAPTR(0xBEEFCAFFEE0DADL, options.getReserved1()); + } + Assert.assertEquals("Product Name", options.getProductName()); + Assert.assertEquals("Product Version", options.getProductVersion()); + Assert.assertEquals(0xa001, options.CustomFuncA1(0)); + Assert.assertEquals(0xa002, options.CustomFuncA2(0)); + final T2_UserData ud1 = T2_UserData.create(); + { + ud1.setBalance(101); + ud1.setName("John Doe"); + Assert.assertEquals(101, ud1.getBalance()); + Assert.assertEquals("John Doe", ud1.getName()); + } + final T2_UserData ud2 = T2_UserData.create(); + { + ud2.setBalance(404); + ud2.setName("Jane Doe"); + Assert.assertEquals(404, ud2.getBalance()); + Assert.assertEquals("Jane Doe", ud2.getName()); + } + // Check func-ptr are original + { + final long[] funcBOrigs = options.getCustomFuncBVariants(0, new long[2], 0, 2); + final long funcB1 = options.getCustomFuncB1(); + final long funcB2 = options.getCustomFuncB2(); + Assert.assertEquals(funcBOrigs[0], funcB1); + Assert.assertEquals(funcBOrigs[1], funcB2); + } + Assert.assertEquals(101, options.CustomFuncB1(ud1)); + Assert.assertEquals(404, options.CustomFuncB1(ud2)); + Assert.assertEquals(-101, options.CustomFuncB2(ud1)); + Assert.assertEquals(-404, options.CustomFuncB2(ud2)); + // switch functions + { + final long funcB1 = options.getCustomFuncB1(); + final long funcB2 = options.getCustomFuncB2(); + options.setCustomFuncB1(funcB2); + options.setCustomFuncB2(funcB1); + } + // Check func-ptr are switched + { + final long[] funcBOrigs = options.getCustomFuncBVariants(0, new long[2], 0, 2); + final long funcB1 = options.getCustomFuncB1(); + final long funcB2 = options.getCustomFuncB2(); + Assert.assertEquals(funcBOrigs[1], funcB1); + Assert.assertEquals(funcBOrigs[0], funcB2); + } + Assert.assertEquals(-101, options.CustomFuncB1(ud1)); + Assert.assertEquals(-404, options.CustomFuncB1(ud2)); + Assert.assertEquals(101, options.CustomFuncB2(ud1)); + Assert.assertEquals(404, options.CustomFuncB2(ud2)); + + bt2.Release(options); + Assert.assertEquals(true, options.isOverrideThreadAffinityNull()); + Assert.assertEquals(true, options.isProductNameNull()); + Assert.assertEquals(true, options.isProductVersionNull()); + Assert.assertEquals(0, options.getCustomFuncA1()); + // const Assert.assertEquals(0, options.getCustomFuncA2()); + Assert.assertEquals(0, options.getCustomFuncB1()); + Assert.assertEquals(0, options.getCustomFuncB2()); + } + +} diff --git a/src/junit/com/jogamp/gluegen/test/junit/generation/BaseTest3PtrStorage.java b/src/junit/com/jogamp/gluegen/test/junit/generation/BaseTest3PtrStorage.java new file mode 100644 index 0000000..f255c31 --- /dev/null +++ b/src/junit/com/jogamp/gluegen/test/junit/generation/BaseTest3PtrStorage.java @@ -0,0 +1,98 @@ +/** + * Copyright 2023 JogAmp Community. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are + * permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are those of the + * authors and should not be interpreted as representing official policies, either expressed + * or implied, of JogAmp Community. + */ + +package com.jogamp.gluegen.test.junit.generation; + +import java.nio.IntBuffer; + +import com.jogamp.common.nio.Buffers; +import com.jogamp.common.nio.ElementBuffer; + +import org.junit.Assert; + +/** + * Test {@link Bindingtest2} with {@link T2_PointerStorage} instance and pointer pointer.. + */ +public class BaseTest3PtrStorage extends BaseClass { + + /** + * Test {@link Bindingtest2} with {@link T2_PointerStorage} instance and pointer pointer + */ + public void chapter01(final Bindingtest2 bt2) throws Exception { + Assert.assertEquals(false, T2_PointerStorage.usesNativeCode()); + + final T2_PointerStorage store = bt2.createT2PointerStorage(); + // final T2_PointerStorage store = T2_PointerStorage.create(); + final long[] int32PtrArray = store.getInt32PtrArray(0, new long[10], 0, 10); // 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 + { + Assert.assertEquals(10, int32PtrArray.length); + System.err.print("int32PtrArray[10] = { "); + for(int i=0; i 3, 4, 5, 6, 7, 8, 9 + store.setInt32PtrPtr(true, int32PtrArray, 8, 3, 2); // -> 3, 4, 5, 8, 9, 8, 9 + store.setInt32PtrPtr(true, int32PtrArray, 0, 5, 2); // -> 3, 4, 5, 8, 9, 0, 1 + final long[] int32PtrPtr = store.getInt32PtrPtr(0, new long[7], 0, 7); // 3, 4, 5, 8, 9, 0, 1 + { + System.err.print("int32PtrPtr[7] = { "); + for(int i=0; i myCallback01, context01 + bt2.alBufferCallback0(buffer1, 0, 0, myCallback01, context01); + Assert.assertEquals(true, bt2.isAlBufferCallback0Mapped(buffer1Key)); + Assert.assertEquals(false, bt2.isAlBufferCallback0Mapped(buffer2Key)); + Assert.assertEquals(false, bt2.isAlBufferCallback0Mapped(buffer3Key)); + Assert.assertEquals(context01, bt2.getAlBufferCallback0UserParam(buffer1Key)); + Assert.assertEquals(null, bt2.getAlBufferCallback0UserParam(buffer2Key)); + Assert.assertEquals(null, bt2.getAlBufferCallback0UserParam(buffer3Key)); + Assert.assertEquals(myCallback01, bt2.getAlBufferCallback0(buffer1Key)); + Assert.assertEquals(null, bt2.getAlBufferCallback0(buffer2Key)); + Assert.assertEquals(null, bt2.getAlBufferCallback0(buffer3Key)); + Assert.assertEquals(1, bt2.getAlBufferCallback0Keys().size()); + { + final Set keys = bt2.getAlBufferCallback0Keys(); + Assert.assertEquals(true, keys.contains(buffer1Key)); + Assert.assertEquals(false, keys.contains(buffer2Key)); + Assert.assertEquals(false, keys.contains(buffer3Key)); + } + + // 2nd mapping: buffer2 -> myCallback02, context02 + bt2.alBufferCallback0(buffer2, 0, 0, myCallback02, context02); + Assert.assertEquals(true, bt2.isAlBufferCallback0Mapped(buffer1Key)); + Assert.assertEquals(true, bt2.isAlBufferCallback0Mapped(buffer2Key)); + Assert.assertEquals(false, bt2.isAlBufferCallback0Mapped(buffer3Key)); + Assert.assertEquals(context01, bt2.getAlBufferCallback0UserParam(buffer1Key)); + Assert.assertEquals(context02, bt2.getAlBufferCallback0UserParam(buffer2Key)); + Assert.assertEquals(null, bt2.getAlBufferCallback0UserParam(buffer3Key)); + Assert.assertEquals(myCallback01, bt2.getAlBufferCallback0(buffer1Key)); + Assert.assertEquals(myCallback02, bt2.getAlBufferCallback0(buffer2Key)); + Assert.assertEquals(null, bt2.getAlBufferCallback0(buffer3Key)); + Assert.assertEquals(2, bt2.getAlBufferCallback0Keys().size()); + { + final Set keys = bt2.getAlBufferCallback0Keys(); + Assert.assertEquals(true, keys.contains(buffer1Key)); + Assert.assertEquals(true, keys.contains(buffer2Key)); + Assert.assertEquals(false, keys.contains(buffer3Key)); + } + + { + final Thread thread = new Thread(new Runnable() { + @Override + public void run() { + bt2.alBufferCallback0Inject(buffer1, 10, 100); // buffer1 -> myCallback01, context01 + } + }); + thread.start(); + thread.join(); + Assert.assertEquals(10+100+1, id_res[0]); + Assert.assertEquals( 1, context01.i); + Assert.assertEquals(10+100+1, context01.j); + Assert.assertEquals( 1, context01.buffer); + Assert.assertEquals( 2, context02.i); + Assert.assertEquals( 0, context02.j); + Assert.assertEquals( 0, context02.buffer); + } + { + bt2.alBufferCallback0Inject(buffer2, 10, 100); // buffer2 -> myCallback02, context02 + Assert.assertEquals(10*100+2, id_res[0]); + Assert.assertEquals( 1, context01.i); + Assert.assertEquals(10+100+1, context01.j); + Assert.assertEquals( 1, context01.buffer); + Assert.assertEquals( 2, context02.i); + Assert.assertEquals(10*100+2, context02.j); + Assert.assertEquals( 2, context02.buffer); + } + + // Switch the callback function for buffer2 -> myCallback01, context02 + bt2.alBufferCallback0(buffer2, 0, 0, myCallback01, context02); + Assert.assertEquals(true, bt2.isAlBufferCallback0Mapped(buffer1Key)); + Assert.assertEquals(true, bt2.isAlBufferCallback0Mapped(buffer2Key)); + Assert.assertEquals(false, bt2.isAlBufferCallback0Mapped(buffer3Key)); + Assert.assertEquals(context01, bt2.getAlBufferCallback0UserParam(buffer1Key)); + Assert.assertEquals(context02, bt2.getAlBufferCallback0UserParam(buffer2Key)); + Assert.assertEquals(null, bt2.getAlBufferCallback0UserParam(buffer3Key)); + Assert.assertEquals(myCallback01, bt2.getAlBufferCallback0(buffer1Key)); + Assert.assertEquals(myCallback01, bt2.getAlBufferCallback0(buffer2Key)); + Assert.assertEquals(null, bt2.getAlBufferCallback0(buffer3Key)); + + { + bt2.alBufferCallback0Inject(buffer1, 11, 101); // buffer1 -> myCallback01, context01 + Assert.assertEquals(11+101+1, id_res[0]); + Assert.assertEquals( 1, context01.i); + Assert.assertEquals(11+101+1, context01.j); + Assert.assertEquals( 1, context01.buffer); + Assert.assertEquals( 2, context02.i); + Assert.assertEquals(10*100+2, context02.j); + Assert.assertEquals( 2, context02.buffer); + } + { + final Thread thread = new Thread(new Runnable() { + @Override + public void run() { + bt2.alBufferCallback0Inject(buffer2, 1, 10); // buffer2 -> myCallback01, context02 + } + }); + thread.start(); + thread.join(); + Assert.assertEquals( 1+ 10+2, id_res[0]); + Assert.assertEquals( 1, context01.i); + Assert.assertEquals(11+101+1, context01.j); + Assert.assertEquals( 1, context01.buffer); + Assert.assertEquals( 2, context02.i); + Assert.assertEquals( 1+ 10+2, context02.j); + Assert.assertEquals( 2, context02.buffer); + } + + // Just release the buffer2 callback and mapped resources + bt2.alBufferCallback0(buffer2, 0, 0, null, context02); // usrptr is not key, only buffer is key! + Assert.assertEquals(true, bt2.isAlBufferCallback0Mapped(buffer1Key)); + Assert.assertEquals(false, bt2.isAlBufferCallback0Mapped(buffer2Key)); + Assert.assertEquals(false, bt2.isAlBufferCallback0Mapped(buffer3Key)); + Assert.assertEquals(context01, bt2.getAlBufferCallback0UserParam(buffer1Key)); + Assert.assertEquals(null, bt2.getAlBufferCallback0UserParam(buffer2Key)); + Assert.assertEquals(null, bt2.getAlBufferCallback0UserParam(buffer3Key)); + Assert.assertEquals(myCallback01, bt2.getAlBufferCallback0(buffer1Key)); + Assert.assertEquals(null, bt2.getAlBufferCallback0(buffer2Key)); + Assert.assertEquals(null, bt2.getAlBufferCallback0(buffer3Key)); + Assert.assertEquals(1, bt2.getAlBufferCallback0Keys().size()); + { + final Set keys = bt2.getAlBufferCallback0Keys(); + Assert.assertEquals(true, keys.contains(buffer1Key)); + Assert.assertEquals(false, keys.contains(buffer2Key)); + Assert.assertEquals(false, keys.contains(buffer3Key)); + } + + // Switch the callback function for buffer2 -> myCallback01, context02 + { + // pre-condition + Assert.assertEquals(true, bt2.isAlBufferCallback0Mapped(buffer1Key)); + Assert.assertEquals(false, bt2.isAlBufferCallback0Mapped(buffer2Key)); + Assert.assertEquals(false, bt2.isAlBufferCallback0Mapped(buffer3Key)); + Assert.assertEquals(context01, bt2.getAlBufferCallback0UserParam(buffer1Key)); + Assert.assertEquals(null, bt2.getAlBufferCallback0UserParam(buffer2Key)); + Assert.assertEquals(null, bt2.getAlBufferCallback0UserParam(buffer3Key)); + Assert.assertEquals(myCallback01, bt2.getAlBufferCallback0(buffer1Key)); + Assert.assertEquals(null, bt2.getAlBufferCallback0(buffer2Key)); + Assert.assertEquals(null, bt2.getAlBufferCallback0(buffer3Key)); + Assert.assertEquals(1, bt2.getAlBufferCallback0Keys().size()); + } + bt2.alBufferCallback0(buffer1, 0, 0, myCallback02, context02); + { + // post-state + Assert.assertEquals(true, bt2.isAlBufferCallback0Mapped(buffer1Key)); + Assert.assertEquals(false, bt2.isAlBufferCallback0Mapped(buffer2Key)); + Assert.assertEquals(false, bt2.isAlBufferCallback0Mapped(buffer3Key)); + Assert.assertEquals(context02, bt2.getAlBufferCallback0UserParam(buffer1Key)); + Assert.assertEquals(null, bt2.getAlBufferCallback0UserParam(buffer2Key)); + Assert.assertEquals(null, bt2.getAlBufferCallback0UserParam(buffer3Key)); + Assert.assertEquals(myCallback02, bt2.getAlBufferCallback0(buffer1Key)); + Assert.assertEquals(null, bt2.getAlBufferCallback0(buffer2Key)); + Assert.assertEquals(null, bt2.getAlBufferCallback0(buffer3Key)); + } + { + context01.j = 0; + context01.buffer = 0; + context02.j = 0; + context02.buffer = 0; + bt2.alBufferCallback0Inject(buffer1, 2, 10); // buffer1 -> myCallback01, context01 + Assert.assertEquals( 2*10+2, id_res[0]); + Assert.assertEquals( 1, context01.i); + Assert.assertEquals( 0, context01.j); + Assert.assertEquals( 0, context01.buffer); + Assert.assertEquals( 2, context02.i); + Assert.assertEquals( 2*10+2, context02.j); + Assert.assertEquals( 1, context02.buffer); + } + + // Just release the buffer1 callback and mapped resources + bt2.alBufferCallback0(buffer1, 0, 0, null, null); // usrptr is not key, only buffer is key! + Assert.assertEquals(false, bt2.isAlBufferCallback0Mapped(buffer1Key)); + Assert.assertEquals(false, bt2.isAlBufferCallback0Mapped(buffer2Key)); + Assert.assertEquals(false, bt2.isAlBufferCallback0Mapped(buffer3Key)); + Assert.assertEquals(null, bt2.getAlBufferCallback0UserParam(buffer1Key)); + Assert.assertEquals(null, bt2.getAlBufferCallback0UserParam(buffer2Key)); + Assert.assertEquals(null, bt2.getAlBufferCallback0UserParam(buffer3Key)); + Assert.assertEquals(null, bt2.getAlBufferCallback0(buffer1Key)); + Assert.assertEquals(null, bt2.getAlBufferCallback0(buffer2Key)); + Assert.assertEquals(null, bt2.getAlBufferCallback0(buffer3Key)); + Assert.assertEquals(0, bt2.getAlBufferCallback0Keys().size()); + { + final Set keys = bt2.getAlBufferCallback0Keys(); + Assert.assertEquals(false, keys.contains(buffer1Key)); + Assert.assertEquals(false, keys.contains(buffer2Key)); + Assert.assertEquals(false, keys.contains(buffer3Key)); + } + + { + id_res[0] = 0; + context01.j = 0; + context01.buffer = 0; + context02.j = 0; + context02.buffer = 0; + bt2.alBufferCallback0Inject(buffer2, 1, 10); // unmapped, no change in data + Assert.assertEquals( 0, id_res[0]); + Assert.assertEquals( 1, context01.i); + Assert.assertEquals( 0, context01.j); + Assert.assertEquals( 0, context01.buffer); + Assert.assertEquals( 2, context02.i); + Assert.assertEquals( 0, context02.j); + Assert.assertEquals( 0, context02.buffer); + } + } + + /** + * Test Bindingtest2 with ALBUFFERCALLBACKTYPESOFT JavaCallback via alBufferCallback1() + * using our custom CustomAlBufferCallback1Key class. + */ + public void chapter03(final Bindingtest2 bt2) throws Exception { + final long[] id_res = { -1 }; + final ALBUFFERCALLBACKTYPESOFT myCallback01 = new ALBUFFERCALLBACKTYPESOFT() { + @Override + public void callback(final int buffer, final ALCcontext context, final int sampledata, final int numbytes) { + final long res = sampledata + numbytes + context.i; + id_res[0] = res; + context.j = res;; + context.buffer = buffer; + System.err.println("chapter03.myCallback01: buffer "+buffer+", sampledata "+sampledata+", numbytes "+numbytes); + } + }; + final ALBUFFERCALLBACKTYPESOFT myCallback02 = new ALBUFFERCALLBACKTYPESOFT() { + @Override + public void callback(final int buffer, final ALCcontext context, final int sampledata, final int numbytes) { + final long res = sampledata * numbytes + context.i; + id_res[0] = res; + context.j = res; + context.buffer = buffer; + System.err.println("chapter03.myCallback02: buffer "+buffer+", sampledata "+sampledata+", numbytes "+numbytes); + } + }; + final int buffer1 = 1; + final int buffer2 = 2; + final int buffer3 = 3; + final CustomAlBufferCallback1Key buffer1Key = new CustomAlBufferCallback1Key(buffer1); + final CustomAlBufferCallback1Key buffer2Key = new CustomAlBufferCallback1Key(buffer2); + final CustomAlBufferCallback1Key buffer3Key = new CustomAlBufferCallback1Key(buffer3); + final ALCcontext context01 = new ALCcontext( 1); + final ALCcontext context02 = new ALCcontext( 2); + Assert.assertEquals( 1, context01.i); + Assert.assertEquals( 0, context01.j); + Assert.assertEquals( 0, context01.buffer); + Assert.assertEquals( 2, context02.i); + Assert.assertEquals( 0, context02.j); + Assert.assertEquals( 0, context02.buffer); + + Assert.assertEquals(false, bt2.isAlBufferCallback1Mapped(buffer1Key)); + Assert.assertEquals(false, bt2.isAlBufferCallback1Mapped(buffer2Key)); + Assert.assertEquals(false, bt2.isAlBufferCallback1Mapped(buffer3Key)); + Assert.assertEquals(0, bt2.getAlBufferCallback1Keys().size()); + + // 1st mapping: buffer1 -> myCallback01, context01 + bt2.alBufferCallback1(context01, buffer1, 0, 0, myCallback01); + Assert.assertEquals(true, bt2.isAlBufferCallback1Mapped(buffer1Key)); + Assert.assertEquals(false, bt2.isAlBufferCallback1Mapped(buffer2Key)); + Assert.assertEquals(false, bt2.isAlBufferCallback1Mapped(buffer3Key)); + Assert.assertEquals(context01, bt2.getAlBufferCallback1UserParam(buffer1Key)); + Assert.assertEquals(null, bt2.getAlBufferCallback1UserParam(buffer2Key)); + Assert.assertEquals(null, bt2.getAlBufferCallback1UserParam(buffer3Key)); + Assert.assertEquals(myCallback01, bt2.getAlBufferCallback1(buffer1Key)); + Assert.assertEquals(null, bt2.getAlBufferCallback1(buffer2Key)); + Assert.assertEquals(null, bt2.getAlBufferCallback1(buffer3Key)); + Assert.assertEquals(1, bt2.getAlBufferCallback1Keys().size()); + { + final Set keys = bt2.getAlBufferCallback1Keys(); + Assert.assertEquals(true, keys.contains(buffer1Key)); + Assert.assertEquals(false, keys.contains(buffer2Key)); + Assert.assertEquals(false, keys.contains(buffer3Key)); + } + + // 2nd mapping: buffer2 -> myCallback02, context02 + bt2.alBufferCallback1(context02, buffer2, 0, 0, myCallback02); + Assert.assertEquals(true, bt2.isAlBufferCallback1Mapped(buffer1Key)); + Assert.assertEquals(true, bt2.isAlBufferCallback1Mapped(buffer2Key)); + Assert.assertEquals(false, bt2.isAlBufferCallback1Mapped(buffer3Key)); + Assert.assertEquals(context01, bt2.getAlBufferCallback1UserParam(buffer1Key)); + Assert.assertEquals(context02, bt2.getAlBufferCallback1UserParam(buffer2Key)); + Assert.assertEquals(null, bt2.getAlBufferCallback1UserParam(buffer3Key)); + Assert.assertEquals(myCallback01, bt2.getAlBufferCallback1(buffer1Key)); + Assert.assertEquals(myCallback02, bt2.getAlBufferCallback1(buffer2Key)); + Assert.assertEquals(null, bt2.getAlBufferCallback1(buffer3Key)); + Assert.assertEquals(2, bt2.getAlBufferCallback1Keys().size()); + { + final Set keys = bt2.getAlBufferCallback1Keys(); + Assert.assertEquals(true, keys.contains(buffer1Key)); + Assert.assertEquals(true, keys.contains(buffer2Key)); + Assert.assertEquals(false, keys.contains(buffer3Key)); + } + + { + bt2.alBufferCallback1Inject(buffer1, 10, 100); // buffer1 -> myCallback01, context01 + Assert.assertEquals(10+100+1, id_res[0]); + Assert.assertEquals( 1, context01.i); + Assert.assertEquals(10+100+1, context01.j); + Assert.assertEquals( 1, context01.buffer); + Assert.assertEquals( 2, context02.i); + Assert.assertEquals( 0, context02.j); + Assert.assertEquals( 0, context02.buffer); + } + { + final Thread thread = new Thread(new Runnable() { + @Override + public void run() { + bt2.alBufferCallback1Inject(buffer2, 10, 100); // buffer2 -> myCallback02, context02 + } + }); + thread.start(); + thread.join(); + Assert.assertEquals(10*100+2, id_res[0]); + Assert.assertEquals( 1, context01.i); + Assert.assertEquals(10+100+1, context01.j); + Assert.assertEquals( 1, context01.buffer); + Assert.assertEquals( 2, context02.i); + Assert.assertEquals(10*100+2, context02.j); + Assert.assertEquals( 2, context02.buffer); + } + + // Switch the callback function for buffer2 -> myCallback01, context02 + bt2.alBufferCallback1(context02, buffer2, 0, 0, myCallback01); + Assert.assertEquals(true, bt2.isAlBufferCallback1Mapped(buffer1Key)); + Assert.assertEquals(true, bt2.isAlBufferCallback1Mapped(buffer2Key)); + Assert.assertEquals(false, bt2.isAlBufferCallback1Mapped(buffer3Key)); + Assert.assertEquals(context01, bt2.getAlBufferCallback1UserParam(buffer1Key)); + Assert.assertEquals(context02, bt2.getAlBufferCallback1UserParam(buffer2Key)); + Assert.assertEquals(null, bt2.getAlBufferCallback1UserParam(buffer3Key)); + Assert.assertEquals(myCallback01, bt2.getAlBufferCallback1(buffer1Key)); + Assert.assertEquals(myCallback01, bt2.getAlBufferCallback1(buffer2Key)); + Assert.assertEquals(null, bt2.getAlBufferCallback1(buffer3Key)); + + { + final Thread thread = new Thread(new Runnable() { + @Override + public void run() { + bt2.alBufferCallback1Inject(buffer1, 11, 101); // buffer1 -> myCallback01, context01 + } + }); + thread.start(); + thread.join(); + Assert.assertEquals(11+101+1, id_res[0]); + Assert.assertEquals( 1, context01.i); + Assert.assertEquals(11+101+1, context01.j); + Assert.assertEquals( 1, context01.buffer); + Assert.assertEquals( 2, context02.i); + Assert.assertEquals(10*100+2, context02.j); + Assert.assertEquals( 2, context02.buffer); + } + { + bt2.alBufferCallback1Inject(buffer2, 1, 10); // buffer2 -> myCallback01, context02 + Assert.assertEquals( 1+ 10+2, id_res[0]); + Assert.assertEquals( 1, context01.i); + Assert.assertEquals(11+101+1, context01.j); + Assert.assertEquals( 1, context01.buffer); + Assert.assertEquals( 2, context02.i); + Assert.assertEquals( 1+ 10+2, context02.j); + Assert.assertEquals( 2, context02.buffer); + } + + // Just release the buffer2 callback and mapped resources + bt2.alBufferCallback1(context02, buffer2, 0, 0, null); // usrptr is not key, only buffer is key! + Assert.assertEquals(true, bt2.isAlBufferCallback1Mapped(buffer1Key)); + Assert.assertEquals(false, bt2.isAlBufferCallback1Mapped(buffer2Key)); + Assert.assertEquals(false, bt2.isAlBufferCallback1Mapped(buffer3Key)); + Assert.assertEquals(context01, bt2.getAlBufferCallback1UserParam(buffer1Key)); + Assert.assertEquals(null, bt2.getAlBufferCallback1UserParam(buffer2Key)); + Assert.assertEquals(null, bt2.getAlBufferCallback1UserParam(buffer3Key)); + Assert.assertEquals(myCallback01, bt2.getAlBufferCallback1(buffer1Key)); + Assert.assertEquals(null, bt2.getAlBufferCallback1(buffer2Key)); + Assert.assertEquals(null, bt2.getAlBufferCallback1(buffer3Key)); + Assert.assertEquals(1, bt2.getAlBufferCallback1Keys().size()); + { + final Set keys = bt2.getAlBufferCallback1Keys(); + Assert.assertEquals(true, keys.contains(buffer1Key)); + Assert.assertEquals(false, keys.contains(buffer2Key)); + Assert.assertEquals(false, keys.contains(buffer3Key)); + } + + // Just release the buffer1 callback and mapped resources + bt2.alBufferCallback1(null, buffer1, 0, 0, null); // usrptr is not key, only buffer is key! + Assert.assertEquals(false, bt2.isAlBufferCallback1Mapped(buffer1Key)); + Assert.assertEquals(false, bt2.isAlBufferCallback1Mapped(buffer2Key)); + Assert.assertEquals(false, bt2.isAlBufferCallback1Mapped(buffer3Key)); + Assert.assertEquals(null, bt2.getAlBufferCallback1UserParam(buffer1Key)); + Assert.assertEquals(null, bt2.getAlBufferCallback1UserParam(buffer2Key)); + Assert.assertEquals(null, bt2.getAlBufferCallback1UserParam(buffer3Key)); + Assert.assertEquals(null, bt2.getAlBufferCallback1(buffer1Key)); + Assert.assertEquals(null, bt2.getAlBufferCallback1(buffer2Key)); + Assert.assertEquals(null, bt2.getAlBufferCallback1(buffer3Key)); + Assert.assertEquals(0, bt2.getAlBufferCallback1Keys().size()); + { + final Set keys = bt2.getAlBufferCallback1Keys(); + Assert.assertEquals(false, keys.contains(buffer1Key)); + Assert.assertEquals(false, keys.contains(buffer2Key)); + Assert.assertEquals(false, keys.contains(buffer3Key)); + } + + { + bt2.alBufferCallback1Inject(buffer2, 1, 10); // unmapped, no change in data + Assert.assertEquals( 1+ 10+2, id_res[0]); + Assert.assertEquals( 1, context01.i); + Assert.assertEquals(11+101+1, context01.j); + Assert.assertEquals( 1, context01.buffer); + Assert.assertEquals( 2, context02.i); + Assert.assertEquals( 1+ 10+2, context02.j); + Assert.assertEquals( 2, context02.buffer); + } + } + + /** + * Test in depth lifecycle of Bindingtest2 with ALBUFFERCALLBACKTYPESOFT JavaCallback via alBufferCallback1() + * using the default AlBufferCallback1Key class. + */ + public void chapter04(final Bindingtest2 bt2) throws Exception { + final long[] id_res = { -1 }; + final ALBUFFERCALLBACKTYPESOFT myCallback01 = new ALBUFFERCALLBACKTYPESOFT() { + @Override + public void callback(final int buffer, final ALCcontext context, final int sampledata, final int numbytes) { + if( context.throwPreAction ) { + context.throwPreAction = false; + throw new RuntimeException("Exception test.pre: chapter04.myCallback01"); + } + final long res = sampledata + numbytes + context.i; + id_res[0] = res; + context.j = res; + context.buffer = buffer; + System.err.println("chapter04.myCallback01: buffer "+buffer+", sampledata "+sampledata+", numbytes "+numbytes); + if( context.throwPostAction ) { + context.throwPostAction = false; + throw new RuntimeException("Exception test.post: chapter04.myCallback01"); + } + } + }; + final ALBUFFERCALLBACKTYPESOFT myCallback02 = new ALBUFFERCALLBACKTYPESOFT() { + @Override + public void callback(final int buffer, final ALCcontext context, final int sampledata, final int numbytes) { + if( context.throwPreAction ) { + context.throwPreAction = false; + throw new RuntimeException("Exception test.pre: chapter04.myCallback02"); + } + final long res = sampledata * numbytes + context.i; + id_res[0] = res; + context.j = res; + context.buffer = buffer; + System.err.println("chapter04.myCallback02: buffer "+buffer+", sampledata "+sampledata+", numbytes "+numbytes); + if( context.throwPostAction ) { + context.throwPostAction = false; + throw new RuntimeException("Exception test.post: chapter04.myCallback02"); + } + } + }; + final int buffer1 = 1; + final int buffer2 = 2; + final int buffer3 = 3; + final AlBufferCallback0Key buffer1Key = new AlBufferCallback0Key(buffer1); + final AlBufferCallback0Key buffer2Key = new AlBufferCallback0Key(buffer2); + final AlBufferCallback0Key buffer3Key = new AlBufferCallback0Key(buffer3); + final ALCcontext context01 = new ALCcontext( 1); + final ALCcontext context02 = new ALCcontext( 2); + Assert.assertEquals( 1, context01.i); + Assert.assertEquals( 0, context01.j); + Assert.assertEquals( 0, context01.buffer); + Assert.assertEquals( 2, context02.i); + Assert.assertEquals( 0, context02.j); + Assert.assertEquals( 0, context02.buffer); + + Assert.assertEquals(false, bt2.isAlBufferCallback0Mapped(buffer1Key)); + Assert.assertEquals(false, bt2.isAlBufferCallback0Mapped(buffer2Key)); + Assert.assertEquals(false, bt2.isAlBufferCallback0Mapped(buffer3Key)); + Assert.assertEquals(0, bt2.getAlBufferCallback0Keys().size()); + + // 1st mapping: buffer1 -> myCallback01, context01 + bt2.alBufferCallback0(buffer1, 0, 0, myCallback01, context01); + Assert.assertEquals(true, bt2.isAlBufferCallback0Mapped(buffer1Key)); + Assert.assertEquals(false, bt2.isAlBufferCallback0Mapped(buffer2Key)); + Assert.assertEquals(false, bt2.isAlBufferCallback0Mapped(buffer3Key)); + Assert.assertEquals(context01, bt2.getAlBufferCallback0UserParam(buffer1Key)); + Assert.assertEquals(null, bt2.getAlBufferCallback0UserParam(buffer2Key)); + Assert.assertEquals(null, bt2.getAlBufferCallback0UserParam(buffer3Key)); + Assert.assertEquals(myCallback01, bt2.getAlBufferCallback0(buffer1Key)); + Assert.assertEquals(null, bt2.getAlBufferCallback0(buffer2Key)); + Assert.assertEquals(null, bt2.getAlBufferCallback0(buffer3Key)); + Assert.assertEquals(1, bt2.getAlBufferCallback0Keys().size()); + { + final Set keys = bt2.getAlBufferCallback0Keys(); + Assert.assertEquals(true, keys.contains(buffer1Key)); + Assert.assertEquals(false, keys.contains(buffer2Key)); + Assert.assertEquals(false, keys.contains(buffer3Key)); + } + + // 2nd mapping: buffer2 -> myCallback02, context02 + bt2.alBufferCallback0(buffer2, 0, 0, myCallback02, context02); + Assert.assertEquals(true, bt2.isAlBufferCallback0Mapped(buffer1Key)); + Assert.assertEquals(true, bt2.isAlBufferCallback0Mapped(buffer2Key)); + Assert.assertEquals(false, bt2.isAlBufferCallback0Mapped(buffer3Key)); + Assert.assertEquals(context01, bt2.getAlBufferCallback0UserParam(buffer1Key)); + Assert.assertEquals(context02, bt2.getAlBufferCallback0UserParam(buffer2Key)); + Assert.assertEquals(null, bt2.getAlBufferCallback0UserParam(buffer3Key)); + Assert.assertEquals(myCallback01, bt2.getAlBufferCallback0(buffer1Key)); + Assert.assertEquals(myCallback02, bt2.getAlBufferCallback0(buffer2Key)); + Assert.assertEquals(null, bt2.getAlBufferCallback0(buffer3Key)); + Assert.assertEquals(2, bt2.getAlBufferCallback0Keys().size()); + { + final Set keys = bt2.getAlBufferCallback0Keys(); + Assert.assertEquals(true, keys.contains(buffer1Key)); + Assert.assertEquals(true, keys.contains(buffer2Key)); + Assert.assertEquals(false, keys.contains(buffer3Key)); + } + + // Exception text post action, i.e. result as expected + // Continuous program flow + id_res[0] = -1; + { + context01.throwPostAction = true; + bt2.alBufferCallback0Inject(buffer1, 10, 100); // buffer1 -> myCallback01, context01 + Assert.assertEquals(10+100+1, id_res[0]); + Assert.assertEquals( 1, context01.i); + Assert.assertEquals(10+100+1, context01.j); + Assert.assertEquals( 1, context01.buffer); + Assert.assertEquals( 2, context02.i); + Assert.assertEquals( 0, context02.j); + Assert.assertEquals( 0, context02.buffer); + } + { + context02.throwPostAction = true; + final Thread thread = new Thread(new Runnable() { + @Override + public void run() { + bt2.alBufferCallback0Inject(buffer2, 10, 100); // buffer2 -> myCallback02, context02 + } + }); + thread.start(); + thread.join(); + Assert.assertEquals(10*100+2, id_res[0]); + Assert.assertEquals( 1, context01.i); + Assert.assertEquals(10+100+1, context01.j); + Assert.assertEquals( 1, context01.buffer); + Assert.assertEquals( 2, context02.i); + Assert.assertEquals(10*100+2, context02.j); + Assert.assertEquals( 2, context02.buffer); + } + + // Exception text pre action, i.e. result NOT as expected (unchanged) + // Continuous program flow + id_res[0] = -1; + { + context01.throwPreAction = true; + bt2.alBufferCallback0Inject(buffer1, 20, 200); // buffer1 -> myCallback01, context01 + Assert.assertEquals( -1, id_res[0]); + Assert.assertEquals( 1, context01.i); + Assert.assertEquals(10+100+1, context01.j); + Assert.assertEquals( 1, context01.buffer); + Assert.assertEquals( 2, context02.i); + Assert.assertEquals(10*100+2, context02.j); + Assert.assertEquals( 2, context02.buffer); + } + { + context02.throwPreAction = true; + final Thread thread = new Thread(new Runnable() { + @Override + public void run() { + bt2.alBufferCallback0Inject(buffer2, 20, 200); // buffer2 -> myCallback02, context02 + } + }); + thread.start(); + thread.join(); + Assert.assertEquals( -1, id_res[0]); + Assert.assertEquals( 1, context01.i); + Assert.assertEquals(10+100+1, context01.j); + Assert.assertEquals( 1, context01.buffer); + Assert.assertEquals( 2, context02.i); + Assert.assertEquals(10*100+2, context02.j); + Assert.assertEquals( 2, context02.buffer); + } + + // Just release the buffer2 callback and mapped resources + bt2.releaseAlBufferCallback0(buffer2Key); + // bt2.alBufferCallback0(buffer2, 0, 0, null, context02); // usrptr is not key, only buffer is key! + Assert.assertEquals(true, bt2.isAlBufferCallback0Mapped(buffer1Key)); + Assert.assertEquals(false, bt2.isAlBufferCallback0Mapped(buffer2Key)); + Assert.assertEquals(false, bt2.isAlBufferCallback0Mapped(buffer3Key)); + Assert.assertEquals(context01, bt2.getAlBufferCallback0UserParam(buffer1Key)); + Assert.assertEquals(null, bt2.getAlBufferCallback0UserParam(buffer2Key)); + Assert.assertEquals(null, bt2.getAlBufferCallback0UserParam(buffer3Key)); + Assert.assertEquals(myCallback01, bt2.getAlBufferCallback0(buffer1Key)); + Assert.assertEquals(null, bt2.getAlBufferCallback0(buffer2Key)); + Assert.assertEquals(null, bt2.getAlBufferCallback0(buffer3Key)); + Assert.assertEquals(1, bt2.getAlBufferCallback0Keys().size()); + { + final Set keys = bt2.getAlBufferCallback0Keys(); + Assert.assertEquals(true, keys.contains(buffer1Key)); + Assert.assertEquals(false, keys.contains(buffer2Key)); + Assert.assertEquals(false, keys.contains(buffer3Key)); + } + + // Just release the buffer1 callback and mapped resources + bt2.releaseAlBufferCallback0(buffer1Key); + // bt2.alBufferCallback0(buffer1, 0, 0, null, null); // usrptr is not key, only buffer is key! + Assert.assertEquals(false, bt2.isAlBufferCallback0Mapped(buffer1Key)); + Assert.assertEquals(false, bt2.isAlBufferCallback0Mapped(buffer2Key)); + Assert.assertEquals(false, bt2.isAlBufferCallback0Mapped(buffer3Key)); + Assert.assertEquals(null, bt2.getAlBufferCallback0UserParam(buffer1Key)); + Assert.assertEquals(null, bt2.getAlBufferCallback0UserParam(buffer2Key)); + Assert.assertEquals(null, bt2.getAlBufferCallback0UserParam(buffer3Key)); + Assert.assertEquals(null, bt2.getAlBufferCallback0(buffer1Key)); + Assert.assertEquals(null, bt2.getAlBufferCallback0(buffer2Key)); + Assert.assertEquals(null, bt2.getAlBufferCallback0(buffer3Key)); + Assert.assertEquals(0, bt2.getAlBufferCallback0Keys().size()); + { + final Set keys = bt2.getAlBufferCallback0Keys(); + Assert.assertEquals(false, keys.contains(buffer1Key)); + Assert.assertEquals(false, keys.contains(buffer2Key)); + Assert.assertEquals(false, keys.contains(buffer3Key)); + } + + // The native callback is still registered, + // we 'just' pulled the resource via release*()! + // + // So we no only test no-change in data + // but also whether the native callback handles this case well, + // i.e. detects the released data-resource and *NOT* issuing the java callback. + // The latter would end up in a SIGSEGV otherwise! + // + // Note: After successfully checking a correct jobject reference, + // the native callback also enters and leaves the monitor (Object sync/lock). + id_res[0] = -1; + bt2.alBufferCallback0Inject(buffer2, 1, 10); + Assert.assertEquals( -1, id_res[0]); + Assert.assertEquals( 1, context01.i); + Assert.assertEquals(10+100+1, context01.j); + Assert.assertEquals( 1, context01.buffer); + Assert.assertEquals( 2, context02.i); + Assert.assertEquals(10*100+2, context02.j); + Assert.assertEquals( 2, context02.buffer); + } + + public static class CustomAlBufferCallback1Key { + private final int buffer; + public CustomAlBufferCallback1Key(final int buffer) { + this.buffer = buffer; + } + @Override + public boolean equals(final Object o) { + if( this == o ) { + return true; + } + if( !(o instanceof CustomAlBufferCallback1Key) ) { + return false; + } + final CustomAlBufferCallback1Key o2 = (CustomAlBufferCallback1Key)o; + return buffer == o2.buffer; + } + @Override + public int hashCode() { + return buffer; + } + @Override + public String toString() { + return "CustomALKey[this "+toHexString(System.identityHashCode(this))+", buffer "+buffer+"]"; + } + } + + /** + * Test Bindingtest2 with ALEVENTPROCSOFT JavaCallback + * on alEventCallback0(..) having the 'Object userParam` as single key. + */ + public void chapter05a(final Bindingtest2 bt2) throws Exception { + final int[] id_res = { -1 }; + final String[] msg_res = { null }; + final ALEVENTPROCSOFT myCallback01 = new ALEVENTPROCSOFT() { + @Override + public void callback(final int eventType, final int object, final int param, final int length, final String message, final ALCcontext context) { + id_res[0] = object; + msg_res[0] = message; + System.err.println("chapter05a.myCallback01: type "+eventType+", obj "+object+", param "+param+", '"+message+"', userParam 0x"+ + Integer.toHexString(System.identityHashCode(context))); + } + }; + final ALEVENTPROCSOFT myCallback02 = new ALEVENTPROCSOFT() { + @Override + public void callback(final int eventType, final int object, final int param, final int length, final String message, final ALCcontext context) { + id_res[0] = 1000 * object; + msg_res[0] = message; + System.err.println("chapter05a.myCallback02: type "+eventType+", obj "+object+", param "+param+", '"+message+"', userParam 0x"+ + Integer.toHexString(System.identityHashCode(context))); + } + }; + final ALCcontext context01 = new ALCcontext(); + final ALCcontext context02 = new ALCcontext(); + final AlEventCallback0Key myKey01 = new AlEventCallback0Key(context01); + final AlEventCallback0Key myKey02 = new AlEventCallback0Key(context02); + Assert.assertEquals(false, bt2.isAlEventCallback0Mapped(myKey01)); + Assert.assertEquals(false, bt2.isAlEventCallback0Mapped(myKey02)); + + bt2.alEventCallback0(myCallback01, context01); + Assert.assertEquals(true, bt2.isAlEventCallback0Mapped(myKey01)); + Assert.assertEquals(false, bt2.isAlEventCallback0Mapped(myKey02)); + { + final Set keys = bt2.getAlEventCallback0Keys(); + Assert.assertEquals(1, keys.size()); + Assert.assertEquals(true, keys.contains(myKey01)); + Assert.assertEquals(false, keys.contains(myKey02)); + } + Assert.assertEquals(myCallback01, bt2.getAlEventCallback0(myKey01)); + Assert.assertEquals(null, bt2.getAlEventCallback0(myKey02)); + Assert.assertEquals(-1, id_res[0]); + Assert.assertEquals(null, msg_res[0]); + + { + final String msgNo1 = "First message"; + id_res[0] = -1; + msg_res[0] = null; + bt2.alEventCallback0Inject(context01, 0, 1, 0, msgNo1); + Assert.assertEquals( 1, id_res[0]); + Assert.assertEquals(msgNo1, msg_res[0]); + } + { + final String msgNo2 = "Second message"; + id_res[0] = -1; + msg_res[0] = null; + bt2.alEventCallback0Inject(context02, 0, 2, 0, msgNo2); + Assert.assertEquals( -1, id_res[0]); + Assert.assertEquals( null, msg_res[0]); + + bt2.alEventCallback0Inject(context01, 0, 2, 0, msgNo2); + Assert.assertEquals( 2, id_res[0]); + Assert.assertEquals( msgNo2, msg_res[0]); + } + + // Switch the callback function + // The previously mapped myCallback01 (context01) gets released + // and remapped to myCallback02 + ( context01 )(key) + bt2.alEventCallback0(myCallback02, context01); + Assert.assertEquals(true, bt2.isAlEventCallback0Mapped(myKey01)); + Assert.assertEquals(false, bt2.isAlEventCallback0Mapped(myKey02)); + { + final Set keys = bt2.getAlEventCallback0Keys(); + Assert.assertEquals(1, keys.size()); + Assert.assertEquals(true, keys.contains(myKey01)); + Assert.assertEquals(false, keys.contains(myKey02)); + } + Assert.assertEquals(myCallback02, bt2.getAlEventCallback0(myKey01)); + Assert.assertEquals(null, bt2.getAlEventCallback0(myKey02)); + + { + final String msgNo3 = "Third message"; + id_res[0] = -1; + msg_res[0] = null; + bt2.alEventCallback0Inject(context02, 0, 3, 0, msgNo3); + Assert.assertEquals( -1, id_res[0]); + Assert.assertEquals( null, msg_res[0]); + + bt2.alEventCallback0Inject(context01, 0, 3, 0, msgNo3); + Assert.assertEquals( 3000, id_res[0]); + Assert.assertEquals( msgNo3, msg_res[0]); + } + + // Fake release (wrong key) + bt2.alEventCallback0(null, context02); + Assert.assertEquals(true, bt2.isAlEventCallback0Mapped(myKey01)); + Assert.assertEquals(false, bt2.isAlEventCallback0Mapped(myKey02)); + + // Just release the callback and mapped context01 + bt2.alEventCallback0(null, context01); + Assert.assertEquals(false, bt2.isAlEventCallback0Mapped(myKey01)); + Assert.assertEquals(false, bt2.isAlEventCallback0Mapped(myKey02)); + Assert.assertEquals(0, bt2.getAlEventCallback0Keys().size()); + + { + final String msgNo4 = "Forth message"; + id_res[0] = -1; + msg_res[0] = null; + bt2.alEventCallback0Inject(context01, 0, 4, 0, msgNo4); + Assert.assertEquals( -1, id_res[0]); + Assert.assertEquals( null, msg_res[0]); + + bt2.alEventCallback0Inject(context02, 0, 4, 0, msgNo4); + Assert.assertEquals( -1, id_res[0]); + Assert.assertEquals( null, msg_res[0]); + } + } + + /** + * Test Bindingtest2 with ALEVENTPROCSOFT JavaCallback + * on alEventCallback0(..) having the 'Object userParam` and `int object` as keys. + */ + public void chapter05b(final Bindingtest2 bt2) throws Exception { + final int[] id_res = { -1 }; + final String[] msg_res = { null }; + final ALEVENTPROCSOFT myCallback01 = new ALEVENTPROCSOFT() { + @Override + public void callback(final int eventType, final int object, final int param, final int length, final String message, final ALCcontext context) { + id_res[0] = object; + msg_res[0] = message; + System.err.println("chapter05.myCallback01: type "+eventType+", obj "+object+", param "+param+", '"+message+"', userParam 0x"+ + Integer.toHexString(System.identityHashCode(context))); + } + }; + final ALEVENTPROCSOFT myCallback02 = new ALEVENTPROCSOFT() { + @Override + public void callback(final int eventType, final int object, final int param, final int length, final String message, final ALCcontext context) { + id_res[0] = 1000 * object; + msg_res[0] = message; + System.err.println("chapter05.myCallback02: type "+eventType+", obj "+object+", param "+param+", '"+message+"', userParam 0x"+ + Integer.toHexString(System.identityHashCode(context))); + } + }; + final ALCcontext context01 = new ALCcontext(); + final ALCcontext context02 = new ALCcontext(); + final AlEventCallback1Key myKey01 = new AlEventCallback1Key(1, context01); + final AlEventCallback1Key myKey02 = new AlEventCallback1Key(2, context02); + Assert.assertEquals(false, bt2.isAlEventCallback1Mapped(myKey01)); + Assert.assertEquals(false, bt2.isAlEventCallback1Mapped(myKey02)); + + bt2.alEventCallback1(1, myCallback01, context01); + Assert.assertEquals(true, bt2.isAlEventCallback1Mapped(myKey01)); + Assert.assertEquals(false, bt2.isAlEventCallback1Mapped(myKey02)); + { + final Set keys = bt2.getAlEventCallback1Keys(); + Assert.assertEquals(1, keys.size()); + Assert.assertEquals(true, keys.contains(myKey01)); + Assert.assertEquals(false, keys.contains(myKey02)); + } + Assert.assertEquals(myCallback01, bt2.getAlEventCallback1(myKey01)); + Assert.assertEquals(null, bt2.getAlEventCallback1(myKey02)); + Assert.assertEquals(-1, id_res[0]); + Assert.assertEquals(null, msg_res[0]); + + { + final String msgNo1 = "First message"; + id_res[0] = -1; + msg_res[0] = null; + bt2.alEventCallback1Inject(context01, 0, 1, 0, msgNo1); + Assert.assertEquals( 1, id_res[0]); + Assert.assertEquals(msgNo1, msg_res[0]); + } + { + final String msgNo2 = "Second message"; + id_res[0] = -1; + msg_res[0] = null; + bt2.alEventCallback1Inject(context02, 0, 2, 0, msgNo2); + Assert.assertEquals( -1, id_res[0]); + Assert.assertEquals( null, msg_res[0]); + + bt2.alEventCallback1Inject(context01, 0, 2, 0, msgNo2); + Assert.assertEquals( -1, id_res[0]); + Assert.assertEquals( null, msg_res[0]); + + bt2.alEventCallback1Inject(context01, 0, 1, 0, msgNo2); + Assert.assertEquals( 1, id_res[0]); + Assert.assertEquals( msgNo2, msg_res[0]); + } + + // Switch the callback function + // The previously mapped myCallback01 (1, context01) gets released + // and remapped to myCallback02 + ( 1, context01 )(key) + bt2.alEventCallback1(1, myCallback02, context01); + Assert.assertEquals(true, bt2.isAlEventCallback1Mapped(myKey01)); + Assert.assertEquals(false, bt2.isAlEventCallback1Mapped(myKey02)); + { + final Set keys = bt2.getAlEventCallback1Keys(); + Assert.assertEquals(1, keys.size()); + Assert.assertEquals(true, keys.contains(myKey01)); + Assert.assertEquals(false, keys.contains(myKey02)); + } + Assert.assertEquals(myCallback02, bt2.getAlEventCallback1(myKey01)); + Assert.assertEquals(null, bt2.getAlEventCallback1(myKey02)); + + { + final String msgNo3 = "Third message"; + id_res[0] = -1; + msg_res[0] = null; + bt2.alEventCallback1Inject(context02, 0, 2, 0, msgNo3); + Assert.assertEquals( -1, id_res[0]); + Assert.assertEquals( null, msg_res[0]); + + bt2.alEventCallback1Inject(context01, 0, 2, 0, msgNo3); + Assert.assertEquals( -1, id_res[0]); + Assert.assertEquals( null, msg_res[0]); + + bt2.alEventCallback1Inject(context01, 0, 1, 0, msgNo3); + Assert.assertEquals( 1000, id_res[0]); + Assert.assertEquals( msgNo3, msg_res[0]); + } + + // Fake release (wrong key) + bt2.alEventCallback1(2, null, context02); + Assert.assertEquals(true, bt2.isAlEventCallback1Mapped(myKey01)); + Assert.assertEquals(false, bt2.isAlEventCallback1Mapped(myKey02)); + + bt2.alEventCallback1(2, null, context01); + Assert.assertEquals(true, bt2.isAlEventCallback1Mapped(myKey01)); + Assert.assertEquals(false, bt2.isAlEventCallback1Mapped(myKey02)); + + // Just release the callback and mapped context01 + bt2.alEventCallback1(1, null, context01); + Assert.assertEquals(false, bt2.isAlEventCallback1Mapped(myKey01)); + Assert.assertEquals(false, bt2.isAlEventCallback1Mapped(myKey02)); + Assert.assertEquals(0, bt2.getAlEventCallback1Keys().size()); + + { + final String msgNo4 = "Forth message"; + id_res[0] = -1; + msg_res[0] = null; + bt2.alEventCallback1Inject(context01, 0, 4, 0, msgNo4); + Assert.assertEquals( -1, id_res[0]); + Assert.assertEquals( null, msg_res[0]); + + bt2.alEventCallback1Inject(context02, 0, 4, 0, msgNo4); + Assert.assertEquals( -1, id_res[0]); + Assert.assertEquals( null, msg_res[0]); + } + } + + /** + * Test Bindingtest2 with T2_CallbackFunc11 JavaCallback via MessageCallback11a() + * using the default MessageCallback11aKey class. + */ + public void chapter11a(final Bindingtest2 bt2) throws Exception { + final long userParam01Ptr = 0xAFFEBEAFC0FFEEL; + final long userParam02Ptr = 0xC0FFEEDEADBEAFL; + + final long[] id_res = { -1 }; + final T2_CallbackFunc11 myCallback01 = new T2_CallbackFunc11() { + @Override + public void callback(final long id, final T2_Callback11UserType usrParam, final long val) { + Assert.assertEquals(42, usrParam.getApiVersion()); // native toolkit should have set API version + if( 1 == id ) { + BaseClass.assertAPTR(userParam01Ptr, usrParam.getData()); + } else if( 2 == id ) { + BaseClass.assertAPTR(userParam02Ptr, usrParam.getData()); + } + final long res = val + usrParam.getI(); + id_res[0] = res; + usrParam.setR(res); + usrParam.setId(id); + System.err.println("chapter11a.myCallback01: id "+id+", val "+val); + } + }; + final T2_CallbackFunc11 myCallback02 = new T2_CallbackFunc11() { + @Override + public void callback(final long id, final T2_Callback11UserType usrParam, final long val) { + Assert.assertEquals(42, usrParam.getApiVersion()); // native toolkit should have set API version + if( 1 == id ) { + BaseClass.assertAPTR(userParam01Ptr, usrParam.getData()); + } else if( 2 == id ) { + BaseClass.assertAPTR(userParam02Ptr, usrParam.getData()); + } + final long res = val * usrParam.getI(); + id_res[0] = res; + usrParam.setR(res); + usrParam.setId(id); + System.err.println("chapter11a.myCallback02: id "+id+", val "+val); + } + }; + final int id1 = 1; + final int id2 = 2; + final int id3 = 3; + final MessageCallback11aKey id1Key = new MessageCallback11aKey(id1); + final MessageCallback11aKey id2Key = new MessageCallback11aKey(id2); + final MessageCallback11aKey id3Key = new MessageCallback11aKey(id3); + final T2_Callback11UserType userParam01 = T2_Callback11UserType.create(); // native toolkit should set API version + userParam01.setData(userParam01Ptr); + userParam01.setI(1); + final T2_Callback11UserType userParam02 = T2_Callback11UserType.create(); // native toolkit should set API version + userParam02.setData(userParam02Ptr); + userParam02.setI(2); + Assert.assertEquals( 1, userParam01.getI()); + Assert.assertEquals( 0, userParam01.getR()); + Assert.assertEquals( 0, userParam01.getId()); + Assert.assertEquals( 2, userParam02.getI()); + Assert.assertEquals( 0, userParam02.getR()); + Assert.assertEquals( 0, userParam02.getId()); + + Assert.assertEquals(false, bt2.isMessageCallback11aMapped(id1Key)); + Assert.assertEquals(false, bt2.isMessageCallback11aMapped(id2Key)); + Assert.assertEquals(false, bt2.isMessageCallback11aMapped(id3Key)); + Assert.assertEquals(0, bt2.getMessageCallback11aKeys().size()); + + // 1st mapping: buffer1 -> myCallback01, userParam01 + bt2.MessageCallback11a(id1, myCallback01, userParam01); + Assert.assertEquals(true, bt2.isMessageCallback11aMapped(id1Key)); + Assert.assertEquals(false, bt2.isMessageCallback11aMapped(id2Key)); + Assert.assertEquals(false, bt2.isMessageCallback11aMapped(id3Key)); + Assert.assertEquals(userParam01, bt2.getMessageCallback11aUserParam(id1Key)); + Assert.assertEquals(null, bt2.getMessageCallback11aUserParam(id2Key)); + Assert.assertEquals(null, bt2.getMessageCallback11aUserParam(id3Key)); + Assert.assertEquals(myCallback01, bt2.getMessageCallback11a(id1Key)); + Assert.assertEquals(null, bt2.getMessageCallback11a(id2Key)); + Assert.assertEquals(null, bt2.getMessageCallback11a(id3Key)); + Assert.assertEquals(1, bt2.getMessageCallback11aKeys().size()); + { + final Set keys = bt2.getMessageCallback11aKeys(); + Assert.assertEquals(true, keys.contains(id1Key)); + Assert.assertEquals(false, keys.contains(id2Key)); + Assert.assertEquals(false, keys.contains(id3Key)); + } + + // 2nd mapping: buffer2 -> myCallback02, userParam02 + bt2.MessageCallback11a(id2, myCallback02, userParam02); + Assert.assertEquals(true, bt2.isMessageCallback11aMapped(id1Key)); + Assert.assertEquals(true, bt2.isMessageCallback11aMapped(id2Key)); + Assert.assertEquals(false, bt2.isMessageCallback11aMapped(id3Key)); + Assert.assertEquals(userParam01, bt2.getMessageCallback11aUserParam(id1Key)); + Assert.assertEquals(userParam02, bt2.getMessageCallback11aUserParam(id2Key)); + Assert.assertEquals(null, bt2.getMessageCallback11aUserParam(id3Key)); + Assert.assertEquals(myCallback01, bt2.getMessageCallback11a(id1Key)); + Assert.assertEquals(myCallback02, bt2.getMessageCallback11a(id2Key)); + Assert.assertEquals(null, bt2.getMessageCallback11a(id3Key)); + Assert.assertEquals(2, bt2.getMessageCallback11aKeys().size()); + { + final Set keys = bt2.getMessageCallback11aKeys(); + Assert.assertEquals(true, keys.contains(id1Key)); + Assert.assertEquals(true, keys.contains(id2Key)); + Assert.assertEquals(false, keys.contains(id3Key)); + } + + { + final Thread thread = new Thread(new Runnable() { + @Override + public void run() { + bt2.MessageCallback11aInject(id1, 10); // buffer1 -> myCallback01, userParam01 + } + }); + thread.start(); + thread.join(); + Assert.assertEquals( 10+1, id_res[0]); + Assert.assertEquals( 1, userParam01.getI()); + Assert.assertEquals( 10+1, userParam01.getR()); + Assert.assertEquals( 1, userParam01.getId()); + Assert.assertEquals( 2, userParam02.getI()); + Assert.assertEquals( 0, userParam02.getR()); + Assert.assertEquals( 0, userParam02.getId()); + } + { + bt2.MessageCallback11aInject(id2, 10); // buffer2 -> myCallback02, userParam02 + Assert.assertEquals( 10*2, id_res[0]); + Assert.assertEquals( 1, userParam01.getI()); + Assert.assertEquals( 10+1, userParam01.getR()); + Assert.assertEquals( 1, userParam01.getId()); + Assert.assertEquals( 2, userParam02.getI()); + Assert.assertEquals( 10*2, userParam02.getR()); + Assert.assertEquals( 2, userParam02.getId()); + } + + // Switch the callback function for buffer2 -> myCallback01, userParam02 + bt2.MessageCallback11a(id2, myCallback01, userParam02); + Assert.assertEquals(true, bt2.isMessageCallback11aMapped(id1Key)); + Assert.assertEquals(true, bt2.isMessageCallback11aMapped(id2Key)); + Assert.assertEquals(false, bt2.isMessageCallback11aMapped(id3Key)); + Assert.assertEquals(userParam01, bt2.getMessageCallback11aUserParam(id1Key)); + Assert.assertEquals(userParam02, bt2.getMessageCallback11aUserParam(id2Key)); + Assert.assertEquals(null, bt2.getMessageCallback11aUserParam(id3Key)); + Assert.assertEquals(myCallback01, bt2.getMessageCallback11a(id1Key)); + Assert.assertEquals(myCallback01, bt2.getMessageCallback11a(id2Key)); + Assert.assertEquals(null, bt2.getMessageCallback11a(id3Key)); + + { + bt2.MessageCallback11aInject(id1, 11); // buffer1 -> myCallback01, userParam01 + Assert.assertEquals( 11+1, id_res[0]); + Assert.assertEquals( 1, userParam01.getI()); + Assert.assertEquals( 11+1, userParam01.getR()); + Assert.assertEquals( 1, userParam01.getId()); + Assert.assertEquals( 2, userParam02.getI()); + Assert.assertEquals( 10*2, userParam02.getR()); + Assert.assertEquals( 2, userParam02.getId()); + } + { + final Thread thread = new Thread(new Runnable() { + @Override + public void run() { + bt2.MessageCallback11aInject(id2, 22); // buffer2 -> myCallback01, userParam02 + } + }); + thread.start(); + thread.join(); + Assert.assertEquals( 22+2, id_res[0]); + Assert.assertEquals( 1, userParam01.getI()); + Assert.assertEquals( 11+1, userParam01.getR()); + Assert.assertEquals( 1, userParam01.getId()); + Assert.assertEquals( 2, userParam02.getI()); + Assert.assertEquals( 22+2, userParam02.getR()); + Assert.assertEquals( 2, userParam02.getId()); + } + + // Just release the buffer2 callback and mapped resources + bt2.MessageCallback11a(id2, null, userParam02); // usrptr is not key, only id is key! + Assert.assertEquals(true, bt2.isMessageCallback11aMapped(id1Key)); + Assert.assertEquals(false, bt2.isMessageCallback11aMapped(id2Key)); + Assert.assertEquals(false, bt2.isMessageCallback11aMapped(id3Key)); + Assert.assertEquals(userParam01, bt2.getMessageCallback11aUserParam(id1Key)); + Assert.assertEquals(null, bt2.getMessageCallback11aUserParam(id2Key)); + Assert.assertEquals(null, bt2.getMessageCallback11aUserParam(id3Key)); + Assert.assertEquals(myCallback01, bt2.getMessageCallback11a(id1Key)); + Assert.assertEquals(null, bt2.getMessageCallback11a(id2Key)); + Assert.assertEquals(null, bt2.getMessageCallback11a(id3Key)); + Assert.assertEquals(1, bt2.getMessageCallback11aKeys().size()); + { + final Set keys = bt2.getMessageCallback11aKeys(); + Assert.assertEquals(true, keys.contains(id1Key)); + Assert.assertEquals(false, keys.contains(id2Key)); + Assert.assertEquals(false, keys.contains(id3Key)); + } + + // Just release the buffer1 callback and mapped resources + bt2.MessageCallback11a(id1, null, null); // usrptr is not key, only id is key! + Assert.assertEquals(false, bt2.isMessageCallback11aMapped(id1Key)); + Assert.assertEquals(false, bt2.isMessageCallback11aMapped(id2Key)); + Assert.assertEquals(false, bt2.isMessageCallback11aMapped(id3Key)); + Assert.assertEquals(null, bt2.getMessageCallback11aUserParam(id1Key)); + Assert.assertEquals(null, bt2.getMessageCallback11aUserParam(id2Key)); + Assert.assertEquals(null, bt2.getMessageCallback11aUserParam(id3Key)); + Assert.assertEquals(null, bt2.getMessageCallback11a(id1Key)); + Assert.assertEquals(null, bt2.getMessageCallback11a(id2Key)); + Assert.assertEquals(null, bt2.getMessageCallback11a(id3Key)); + Assert.assertEquals(0, bt2.getMessageCallback11aKeys().size()); + { + final Set keys = bt2.getMessageCallback11aKeys(); + Assert.assertEquals(false, keys.contains(id1Key)); + Assert.assertEquals(false, keys.contains(id2Key)); + Assert.assertEquals(false, keys.contains(id3Key)); + } + + { + bt2.MessageCallback11aInject(id2, 5); // unmapped, no change in data + Assert.assertEquals( 22+2, id_res[0]); + Assert.assertEquals( 1, userParam01.getI()); + Assert.assertEquals( 11+1, userParam01.getR()); + Assert.assertEquals( 1, userParam01.getId()); + Assert.assertEquals( 2, userParam02.getI()); + Assert.assertEquals( 22+2, userParam02.getR()); + Assert.assertEquals( 2, userParam02.getId()); + } + } + + /** + * Test Bindingtest2 with T2_CallbackFunc11 JavaCallback via MessageCallback11b() + * using the default MessageCallback11bKey class. + */ + public void chapter11b(final Bindingtest2 bt2) throws Exception { + final long userParam01Ptr = 0xAFFEBEAFC0FFEEL; + final long userParam02Ptr = 0xC0FFEEDEADBEAFL; + + final long[] id_res = { -1 }; + final T2_CallbackFunc11 myCallback01 = new T2_CallbackFunc11() { + @Override + public void callback(final long id, final T2_Callback11UserType usrParam, final long val) { + Assert.assertEquals(42, usrParam.getApiVersion()); // native toolkit should have set API version + if( 1 == id ) { + BaseClass.assertAPTR(userParam01Ptr, usrParam.getData()); + } else if( 2 == id ) { + BaseClass.assertAPTR(userParam02Ptr, usrParam.getData()); + } + final long res = val + id; + id_res[0] = res; + usrParam.setR(res); + usrParam.setId(id); + System.err.println("chapter11b.myCallback01: id "+id+", val "+val); + } + }; + final T2_CallbackFunc11 myCallback02 = new T2_CallbackFunc11() { + @Override + public void callback(final long id, final T2_Callback11UserType usrParam, final long val) { + Assert.assertEquals(42, usrParam.getApiVersion()); // native toolkit should have set API version + if( 1 == id ) { + BaseClass.assertAPTR(userParam01Ptr, usrParam.getData()); + } else if( 2 == id ) { + BaseClass.assertAPTR(userParam02Ptr, usrParam.getData()); + } + final long res = val * id; + id_res[0] = res; + usrParam.setR(res); + usrParam.setId(id); + System.err.println("chapter11b.myCallback02: id "+id+", val "+val); + } + }; + final int id1 = 1; + final int id2 = 2; + final int id3 = 3; + final MessageCallback11bKey id1Key = new MessageCallback11bKey(id1); + final MessageCallback11bKey id2Key = new MessageCallback11bKey(id2); + final MessageCallback11bKey id3Key = new MessageCallback11bKey(id3); + + Assert.assertEquals(false, bt2.isMessageCallback11bMapped(id1Key)); + Assert.assertEquals(false, bt2.isMessageCallback11bMapped(id2Key)); + Assert.assertEquals(false, bt2.isMessageCallback11bMapped(id3Key)); + Assert.assertEquals(0, bt2.getMessageCallback11bKeys().size()); + + // 1st mapping: buffer1 -> myCallback01, userParam01Ptr + bt2.MessageCallback11b(id1, myCallback01, userParam01Ptr); + Assert.assertEquals(true, bt2.isMessageCallback11bMapped(id1Key)); + Assert.assertEquals(false, bt2.isMessageCallback11bMapped(id2Key)); + Assert.assertEquals(false, bt2.isMessageCallback11bMapped(id3Key)); + Assert.assertEquals(null, bt2.getMessageCallback11bUserParam(id2Key)); + Assert.assertEquals(null, bt2.getMessageCallback11bUserParam(id3Key)); + Assert.assertEquals(myCallback01, bt2.getMessageCallback11b(id1Key)); + Assert.assertEquals(null, bt2.getMessageCallback11b(id2Key)); + Assert.assertEquals(null, bt2.getMessageCallback11b(id3Key)); + Assert.assertEquals(1, bt2.getMessageCallback11bKeys().size()); + { + final Set keys = bt2.getMessageCallback11bKeys(); + Assert.assertEquals(true, keys.contains(id1Key)); + Assert.assertEquals(false, keys.contains(id2Key)); + Assert.assertEquals(false, keys.contains(id3Key)); + } + + // 2nd mapping: buffer2 -> myCallback02, userParam02Ptr + bt2.MessageCallback11b(id2, myCallback02, userParam02Ptr); + Assert.assertEquals(true, bt2.isMessageCallback11bMapped(id1Key)); + Assert.assertEquals(true, bt2.isMessageCallback11bMapped(id2Key)); + Assert.assertEquals(false, bt2.isMessageCallback11bMapped(id3Key)); + Assert.assertEquals(null, bt2.getMessageCallback11bUserParam(id3Key)); + Assert.assertEquals(null, bt2.getMessageCallback11b(id3Key)); + Assert.assertEquals(2, bt2.getMessageCallback11bKeys().size()); + { + final Set keys = bt2.getMessageCallback11bKeys(); + Assert.assertEquals(true, keys.contains(id1Key)); + Assert.assertEquals(true, keys.contains(id2Key)); + Assert.assertEquals(false, keys.contains(id3Key)); + } + + { + final Thread thread = new Thread(new Runnable() { + @Override + public void run() { + bt2.MessageCallback11bInject(id1, 10); // buffer1 -> myCallback01, userParam01 + } + }); + thread.start(); + thread.join(); + Assert.assertEquals( 10+1, id_res[0]); + } + { + bt2.MessageCallback11bInject(id2, 10); // buffer2 -> myCallback02, userParam02 + Assert.assertEquals( 10*2, id_res[0]); + } + + // Switch the callback function for buffer2 -> myCallback01, userParam02Ptr + bt2.MessageCallback11b(id2, myCallback01, userParam02Ptr); + Assert.assertEquals(true, bt2.isMessageCallback11bMapped(id1Key)); + Assert.assertEquals(true, bt2.isMessageCallback11bMapped(id2Key)); + Assert.assertEquals(false, bt2.isMessageCallback11bMapped(id3Key)); + Assert.assertEquals(null, bt2.getMessageCallback11bUserParam(id3Key)); + Assert.assertEquals(myCallback01, bt2.getMessageCallback11b(id1Key)); + Assert.assertEquals(myCallback01, bt2.getMessageCallback11b(id2Key)); + Assert.assertEquals(null, bt2.getMessageCallback11b(id3Key)); + + { + bt2.MessageCallback11bInject(id1, 11); // buffer1 -> myCallback01, userParam01 + Assert.assertEquals( 11+1, id_res[0]); + } + { + final Thread thread = new Thread(new Runnable() { + @Override + public void run() { + bt2.MessageCallback11bInject(id2, 22); // buffer2 -> myCallback01, userParam02 + } + }); + thread.start(); + thread.join(); + Assert.assertEquals( 22+2, id_res[0]); + } + + // Just release the buffer2 callback and mapped resources + bt2.MessageCallback11b(id2, null, userParam02Ptr); // usrptr is not key, only id is key! + Assert.assertEquals(true, bt2.isMessageCallback11bMapped(id1Key)); + Assert.assertEquals(false, bt2.isMessageCallback11bMapped(id2Key)); + Assert.assertEquals(false, bt2.isMessageCallback11bMapped(id3Key)); + Assert.assertEquals(null, bt2.getMessageCallback11bUserParam(id2Key)); + Assert.assertEquals(null, bt2.getMessageCallback11bUserParam(id3Key)); + Assert.assertEquals(myCallback01, bt2.getMessageCallback11b(id1Key)); + Assert.assertEquals(null, bt2.getMessageCallback11b(id2Key)); + Assert.assertEquals(null, bt2.getMessageCallback11b(id3Key)); + Assert.assertEquals(1, bt2.getMessageCallback11bKeys().size()); + { + final Set keys = bt2.getMessageCallback11bKeys(); + Assert.assertEquals(true, keys.contains(id1Key)); + Assert.assertEquals(false, keys.contains(id2Key)); + Assert.assertEquals(false, keys.contains(id3Key)); + } + + // Just release the buffer1 callback and mapped resources + bt2.MessageCallback11b(id1, null, 0); // usrptr is not key, only id is key! + Assert.assertEquals(false, bt2.isMessageCallback11bMapped(id1Key)); + Assert.assertEquals(false, bt2.isMessageCallback11bMapped(id2Key)); + Assert.assertEquals(false, bt2.isMessageCallback11bMapped(id3Key)); + Assert.assertEquals(null, bt2.getMessageCallback11bUserParam(id1Key)); + Assert.assertEquals(null, bt2.getMessageCallback11bUserParam(id2Key)); + Assert.assertEquals(null, bt2.getMessageCallback11bUserParam(id3Key)); + Assert.assertEquals(null, bt2.getMessageCallback11b(id1Key)); + Assert.assertEquals(null, bt2.getMessageCallback11b(id2Key)); + Assert.assertEquals(null, bt2.getMessageCallback11b(id3Key)); + Assert.assertEquals(0, bt2.getMessageCallback11bKeys().size()); + { + final Set keys = bt2.getMessageCallback11bKeys(); + Assert.assertEquals(false, keys.contains(id1Key)); + Assert.assertEquals(false, keys.contains(id2Key)); + Assert.assertEquals(false, keys.contains(id3Key)); + } + + { + bt2.MessageCallback11bInject(id2, 5); // unmapped, no change in data + Assert.assertEquals( 22+2, id_res[0]); + } + } + + public static class CustomMessageCallback11Key { + public CustomMessageCallback11Key() { + } + @Override + public boolean equals(final Object o) { + if( this == o ) { + return true; + } + if( !(o instanceof CustomAlBufferCallback1Key) ) { + return false; + } + final CustomAlBufferCallback1Key o2 = (CustomAlBufferCallback1Key)o; + return true; + } + @Override + public int hashCode() { + return 0; + } + @Override + public String toString() { + return "CustomMessageCallback11Key[this "+toHexString(System.identityHashCode(this))+"]"; + } + } + + /** + * Test Bindingtest2 with T2_CallbackFunc12a JavaCallback via SetLogCallBack12a() + */ + public void chapter12a(final Bindingtest2 bt2) throws Exception { + final AtomicReference messageExpected = new AtomicReference<>(null); + final AtomicReference messageReturned = new AtomicReference<>(null); + final T2_CallbackFunc12a logCallBack = new T2_CallbackFunc12a() { + @Override + public void callback(final T2_Callback12LogMessage usrParam) { + assertEquals(messageExpected.get(), usrParam); // compare value, not object hash value (reference) + messageReturned.set("Result-"+usrParam.getMessage()); + } + }; + + bt2.SetLogCallBack12a(logCallBack); + messageReturned.set(null); + { + final T2_Callback12LogMessage logMessage = T2_Callback12LogMessage.create(); + logMessage.setCategory("TEST"); + logMessage.setMessage("Example"); + logMessage.setLevel(Bindingtest2.LOG_Info); + messageExpected.set(logMessage); + + bt2.LogCallBack12aInject(logMessage); + Assert.assertEquals(messageReturned.get(), "Result-Example"); + } + + messageReturned.set(null); + { + final T2_Callback12LogMessage logMessage = T2_Callback12LogMessage.create(); + logMessage.setCategory("IDK"); + logMessage.setMessage("John Doe is absent."); + logMessage.setLevel(Bindingtest2.LOG_Warning); + messageExpected.set(logMessage); + + bt2.LogCallBack12aInject(logMessage); + Assert.assertEquals(messageReturned.get(), "Result-John Doe is absent."); + } + + bt2.SetLogCallBack12a(null); + messageReturned.set(null); + { + final T2_Callback12LogMessage logMessage = T2_Callback12LogMessage.create(); + logMessage.setCategory("SANITY"); + logMessage.setMessage("Callback is now unset"); + logMessage.setLevel(Bindingtest2.LOG_Fatal); + messageExpected.set(logMessage); + + bt2.LogCallBack12aInject(logMessage); + Assert.assertEquals(messageReturned.get(), null); + } + } + /** + * Test Bindingtest2 with T2_CallbackFunc12a JavaCallback via SetLogCallBack12a() + */ + public void chapter12b(final Bindingtest2 bt2) throws Exception { + final AtomicReference expMessage = new AtomicReference<>(null); + final AtomicReference hasReturnedMsg = new AtomicReference<>(null); + final T2_CallbackFunc12b logCallBack = new T2_CallbackFunc12b() { + int expParam0 = 1; + @Override + public void callback(final int param0, final T2_Callback12LogMessage usrParam) { + assertEquals(expMessage.get(), usrParam); // compare value, not object hash value (reference) + hasReturnedMsg.set("Result-"+usrParam.getMessage()); + Assert.assertEquals(expParam0++, param0); + } + }; + + bt2.SetLogCallBack12b(logCallBack); + hasReturnedMsg.set(null); + { + final T2_Callback12LogMessage logMessage = T2_Callback12LogMessage.create(); + logMessage.setCategory("TEST"); + logMessage.setMessage("Example"); + logMessage.setLevel(Bindingtest2.LOG_Info); + expMessage.set(logMessage); + + bt2.LogCallBack12bInject(logMessage, 1); + Assert.assertEquals("Result-Example", hasReturnedMsg.get()); + } + + hasReturnedMsg.set(null); + { + final T2_Callback12LogMessage logMessage = T2_Callback12LogMessage.create(); + logMessage.setCategory("IDK"); + logMessage.setMessage("John Doe is absent."); + logMessage.setLevel(Bindingtest2.LOG_Warning); + expMessage.set(logMessage); + + bt2.LogCallBack12bInject(logMessage, 2); + Assert.assertEquals("Result-John Doe is absent.", hasReturnedMsg.get()); + } + + bt2.SetLogCallBack12b(null); + hasReturnedMsg.set(null); + { + final T2_Callback12LogMessage logMessage = T2_Callback12LogMessage.create(); + logMessage.setCategory("SANITY"); + logMessage.setMessage("Callback is now unset"); + logMessage.setLevel(Bindingtest2.LOG_Fatal); + expMessage.set(logMessage); + + bt2.LogCallBack12bInject(logMessage, 3); + Assert.assertEquals(null, hasReturnedMsg.get()); + } + } + private static void assertEquals(final T2_Callback12LogMessage exp, final T2_Callback12LogMessage has) { + if( null == exp ) { + Assert.assertNull(has); + } else { + Assert.assertNotNull(has); + } + Assert.assertEquals(exp.getCategory(), has.getCategory()); + Assert.assertEquals(exp.getMessage(), has.getMessage()); + Assert.assertEquals(exp.getLevel(), has.getLevel()); + } + + /** + * Test Bindingtest2 with T2_CallbackFunc13 JavaCallback via MessageCallback13() + */ + public void chapter13(final Bindingtest2 bt2) throws Exception { + // + // Key 1 + // + final AtomicReference hasReturnedMsgKey1 = new AtomicReference<>(null); + final T2_CallbackFunc13 callbackKey1 = new T2_CallbackFunc13() { + int localCallCount = 1; + + @Override + public void callback(final String msg1, final T2_Callback13UserType info, final String msg2, final T2_Callback13UserKey1 usrParamKey1, final long usrKey2) { + Assert.assertEquals(localCallCount, info.getANumber()); + final String strKey1 = String.valueOf( usrParamKey1.getKeyValue1() ); + final String strKey2 = String.valueOf( usrKey2 ); + Assert.assertEquals(strKey1, msg1); + Assert.assertEquals(strKey2, msg2); + hasReturnedMsgKey1.set("Result1-"+localCallCount+"-"+strKey1+"."+strKey2); + System.err.println("Callback: "+hasReturnedMsgKey1.get()); + localCallCount++; + } + }; + + // key 1 + 1 = ibdex 2 + final T2_Callback13UserKey1 key1a = T2_Callback13UserKey1.create(); + key1a.setKeyValue1(1); + final long key1b = 1; + final MessageCallback13Key expKey1 = new MessageCallback13Key(key1a, key1b); + bt2.MessageCallback13("a debug message - key2", callbackKey1, key1a, key1b); + + // + // Key 2 + // + final AtomicReference hasReturnedMsgKey2 = new AtomicReference<>(null); + final T2_CallbackFunc13 callbackKey2 = new T2_CallbackFunc13() { + int localCallCount = 1; + + @Override + public void callback(final String msg1, final T2_Callback13UserType info, final String msg2, final T2_Callback13UserKey1 usrParamKey2, final long usrKey2) { + Assert.assertEquals(localCallCount, info.getANumber()); + final String strKey1 = String.valueOf( usrParamKey2.getKeyValue1() ); + final String strKey2 = String.valueOf( usrKey2 ); + Assert.assertEquals(strKey1, msg1); + Assert.assertEquals(strKey2, msg2); + hasReturnedMsgKey2.set("Result2-"+localCallCount+"-"+strKey1+"."+strKey2); + System.err.println("Callback: "+hasReturnedMsgKey2.get()); + localCallCount++; + } + }; + + // key 2 + 2 = ibdex 4 + final T2_Callback13UserKey1 key2a = T2_Callback13UserKey1.create(); + key2a.setKeyValue1(2); + final long key2b = 2; + final MessageCallback13Key expKey2 = new MessageCallback13Key(key2a, key2b); + bt2.MessageCallback13("a debug message - key2", callbackKey2, key2a, key2b); + + // Check keys + final Set keys = bt2.getMessageCallback13Keys(); + Assert.assertNotNull(keys); + Assert.assertEquals(2, keys.size()); + { + System.err.println("XXX expKey1[key1a 0x"+Long.toHexString(key1a.getDirectBufferAddress())+", hash 0x"+Integer.toHexString(System.identityHashCode(key1a))+"]"); + System.err.println("XXX expKey1 hash 0x"+Integer.toHexString(expKey1.hashCode())); + + System.err.println("XXX expKey2[key1a 0x"+Long.toHexString(key2a.getDirectBufferAddress())+", hash 0x"+Integer.toHexString(System.identityHashCode(key2a))+"]"); + System.err.println("XXX expKey2 hash 0x"+Integer.toHexString(expKey2.hashCode())); + + final MessageCallback13Key[] keys0 = keys.toArray(new MessageCallback13Key[2]); + Assert.assertEquals(2, keys0.length); + final MessageCallback13Key hasKey1, hasKey2; + if( 1 == keys0[0].usrKey2 ) { + // keys0[0] -> hasKey1, keys0[1] -> hasKey2 + hasKey1 = keys0[0]; + hasKey2 = keys0[1]; + } else { + // keys0[0] -> hasKey2, keys0[1] -> hasKey1 + hasKey1 = keys0[1]; + hasKey2 = keys0[0]; + } + System.err.println("XXX hasKey1 hash 0x"+Integer.toHexString(hasKey1.hashCode())); + System.err.println("XXX hasKey2 hash 0x"+Integer.toHexString(hasKey2.hashCode())); + + Assert.assertEquals(key1a.getDirectBufferAddress(), hasKey1.usrParamKey1.getDirectBufferAddress()); + Assert.assertEquals(key1b, hasKey1.usrKey2); + Assert.assertEquals(expKey1, hasKey1); + Assert.assertEquals(expKey1.hashCode(), hasKey1.hashCode()); + final T2_CallbackFunc13 cb1 = bt2.getMessageCallback13(hasKey1); + Assert.assertNotNull(cb1); + final T2_Callback13UserKey1 up1 = bt2.getMessageCallback13UserParam(hasKey1); + Assert.assertNotNull(up1); + + Assert.assertEquals(key2a.getDirectBufferAddress(), hasKey2.usrParamKey1.getDirectBufferAddress()); + Assert.assertEquals(key2b, hasKey2.usrKey2); + Assert.assertEquals(expKey2, hasKey2); + Assert.assertEquals(expKey2.hashCode(), hasKey2.hashCode()); + final T2_CallbackFunc13 cb2 = bt2.getMessageCallback13(hasKey2); + Assert.assertNotNull(cb2); + final T2_Callback13UserKey1 up2 = bt2.getMessageCallback13UserParam(hasKey2); + Assert.assertNotNull(up2); + } + + // Send -> Key1 + int key1CallCount = 1; + final T2_Callback13UserType info1 = T2_Callback13UserType.create(); + hasReturnedMsgKey1.set(null); + { + final String expReturnedMsg = "Result1-"+key1CallCount+"-"+String.valueOf(key1a.getKeyValue1())+"."+String.valueOf(key1b); + info1.setANumber(key1CallCount); + bt2.InjectMessageCallback13(String.valueOf(key1a.getKeyValue1()), info1, String.valueOf(key1b), key1a, key1b); + Assert.assertEquals(expReturnedMsg, hasReturnedMsgKey1.get()); + key1CallCount++; + } + // Send -> Key2 + int key2CallCount = 1; + final T2_Callback13UserType info2 = T2_Callback13UserType.create(); + hasReturnedMsgKey2.set(null); + { + final String expReturnedMsg = "Result2-"+key2CallCount+"-"+String.valueOf(key2a.getKeyValue1())+"."+String.valueOf(key2b); + info2.setANumber(key2CallCount); + bt2.InjectMessageCallback13(String.valueOf(key2a.getKeyValue1()), info2, String.valueOf(key2b), key2a, key2b); + Assert.assertEquals(expReturnedMsg, hasReturnedMsgKey2.get()); + key2CallCount++; + } + + // Send -> Key1 + hasReturnedMsgKey1.set(null); + { + final String expReturnedMsg = "Result1-"+key1CallCount+"-"+String.valueOf(key1a.getKeyValue1())+"."+String.valueOf(key1b); + info1.setANumber(key1CallCount); + bt2.InjectMessageCallback13(String.valueOf(key1a.getKeyValue1()), info1, String.valueOf(key1b), key1a, key1b); + Assert.assertEquals(expReturnedMsg, hasReturnedMsgKey1.get()); + key1CallCount++; + } + // Send -> Key2 + hasReturnedMsgKey2.set(null); + { + final String expReturnedMsg = "Result2-"+key2CallCount+"-"+String.valueOf(key2a.getKeyValue1())+"."+String.valueOf(key2b); + info2.setANumber(key2CallCount); + bt2.InjectMessageCallback13(String.valueOf(key2a.getKeyValue1()), info2, String.valueOf(key2b), key2a, key2b); + Assert.assertEquals(expReturnedMsg, hasReturnedMsgKey2.get()); + key2CallCount++; + } + + // Send -> Key1 -> nil + bt2.MessageCallback13("turned off - key1", null, key1a, key1b); + hasReturnedMsgKey1.set(null); + { + final String expReturnedMsg = null; + info1.setANumber(key1CallCount); + bt2.InjectMessageCallback13(String.valueOf(key1a.getKeyValue1()), info1, String.valueOf(key1b), key1a, key1b); + Assert.assertEquals(expReturnedMsg, hasReturnedMsgKey1.get()); + } + // Send -> Key2 -> nil + bt2.MessageCallback13("turned off - key1", null, key2a, key2b); + hasReturnedMsgKey2.set(null); + { + final String expReturnedMsg = null; + info2.setANumber(key2CallCount); + bt2.InjectMessageCallback13(String.valueOf(key2a.getKeyValue1()), info2, String.valueOf(key2b), key2a, key2b); + Assert.assertEquals(expReturnedMsg, hasReturnedMsgKey2.get()); + } + } + + static private String toHexString(final int v) { return "0x"+Integer.toHexString(v); } + +} diff --git a/src/junit/com/jogamp/gluegen/test/junit/generation/BindingJNILibLoader.java b/src/junit/com/jogamp/gluegen/test/junit/generation/BindingJNILibLoader.java index 67f4918..8180204 100644 --- a/src/junit/com/jogamp/gluegen/test/junit/generation/BindingJNILibLoader.java +++ b/src/junit/com/jogamp/gluegen/test/junit/generation/BindingJNILibLoader.java @@ -55,11 +55,21 @@ public class BindingJNILibLoader extends JNILibLoaderBase { }); } - public static void loadBindingtest2() { + public static void loadBindingtest2p1() { SecurityUtil.doPrivileged(new PrivilegedAction() { @Override public Object run() { - loadLibrary("Bindingtest2", null, true, BindingJNILibLoader.class.getClassLoader()); + loadLibrary("Bindingtest2p1", null, true, BindingJNILibLoader.class.getClassLoader()); + return null; + } + }); + } + + public static void loadBindingtest2p2() { + SecurityUtil.doPrivileged(new PrivilegedAction() { + @Override + public Object run() { + loadLibrary("Bindingtest2p2", null, true, BindingJNILibLoader.class.getClassLoader()); return null; } }); diff --git a/src/junit/com/jogamp/gluegen/test/junit/generation/Test2FuncPtr.java b/src/junit/com/jogamp/gluegen/test/junit/generation/Test2FuncPtr.java deleted file mode 100644 index 915b3eb..0000000 --- a/src/junit/com/jogamp/gluegen/test/junit/generation/Test2FuncPtr.java +++ /dev/null @@ -1,169 +0,0 @@ -/** - * Copyright 2023 JogAmp Community. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modification, are - * permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this list of - * conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, this list - * of conditions and the following disclaimer in the documentation and/or other materials - * provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND - * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON - * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - * The views and conclusions contained in the software and documentation are those of the - * authors and should not be interpreted as representing official policies, either expressed - * or implied, of JogAmp Community. - */ - -package com.jogamp.gluegen.test.junit.generation; - -import java.io.IOException; - -import com.jogamp.gluegen.test.junit.generation.impl.Bindingtest2Impl; -import com.jogamp.common.os.NativeLibrary; - -import org.junit.AfterClass; -import org.junit.Assert; -import org.junit.BeforeClass; -import org.junit.Test; - -import org.junit.FixMethodOrder; -import org.junit.runners.MethodSorters; - -/** - * Test {@link Bindingtest2} with {@link T2_InitializeOptions} instance and function pointer... - */ -@FixMethodOrder(MethodSorters.NAME_ASCENDING) -public class Test2FuncPtr extends BaseClass { - - static NativeLibrary dynamicLookupHelper; - - /** - * Verifies loading of the new library. - */ - @BeforeClass - public static void chapter__TestLoadLibrary() throws Exception { - BindingJNILibLoader.loadBindingtest2(); - dynamicLookupHelper = NativeLibrary.open("test2", false, false, Test2FuncPtr.class.getClassLoader(), true); - Assert.assertNotNull("NativeLibrary.open(test2) failed", dynamicLookupHelper); - - Bindingtest2Impl.resetProcAddressTable(dynamicLookupHelper); - } - - /** - * Verifies unloading of the new library. - */ - @AfterClass - public static void chapter0XTestUnloadLibrary() throws Exception { - Assert.assertNotNull(dynamicLookupHelper); - dynamicLookupHelper.close(); - dynamicLookupHelper = null; - } - - /** - * Test Bindingtest2 with T2_InitializeOptions instance and function pointer - */ - @Test - public void chapter01() throws Exception { - final Bindingtest2 bt2 = new Bindingtest2Impl(); - - final T2_InitializeOptions options = T2_InitializeOptions.create(); - Assert.assertEquals(true, options.isOverrideThreadAffinityNull()); - Assert.assertEquals(true, options.isProductNameNull()); - Assert.assertEquals(true, options.isProductVersionNull()); - Assert.assertEquals(0, options.getCustomFuncA1()); - Assert.assertEquals(0, options.getCustomFuncA2()); - Assert.assertEquals(0, options.getCustomFuncB1()); - Assert.assertEquals(0, options.getCustomFuncB2()); - - bt2.Initialize(options); - Assert.assertEquals(true, options.isOverrideThreadAffinityNull()); - Assert.assertEquals(false, options.isProductNameNull()); - Assert.assertEquals(false, options.isProductVersionNull()); - Assert.assertNotEquals(0, options.getCustomFuncA1()); - Assert.assertNotEquals(0, options.getCustomFuncA2()); - Assert.assertNotEquals(0, options.getCustomFuncB1()); - Assert.assertNotEquals(0, options.getCustomFuncB2()); - Assert.assertEquals(1, options.getApiVersion()); - // dropped: Assert.assertEquals(1, T2_InitializeOptions.getReserved1ElemCount()); - BaseClass.assertAPTR(0x0000CAFFEEBEEFL, options.getReserved1()); - { - options.setReserved1(0xBEEFCAFFEE0DADL); - BaseClass.assertAPTR(0xBEEFCAFFEE0DADL, options.getReserved1()); - } - Assert.assertEquals("Product Name", options.getProductName()); - Assert.assertEquals("Product Version", options.getProductVersion()); - Assert.assertEquals(0xa001, options.CustomFuncA1(0)); - Assert.assertEquals(0xa002, options.CustomFuncA2(0)); - final T2_UserData ud1 = T2_UserData.create(); - { - ud1.setBalance(101); - ud1.setName("John Doe"); - Assert.assertEquals(101, ud1.getBalance()); - Assert.assertEquals("John Doe", ud1.getName()); - } - final T2_UserData ud2 = T2_UserData.create(); - { - ud2.setBalance(404); - ud2.setName("Jane Doe"); - Assert.assertEquals(404, ud2.getBalance()); - Assert.assertEquals("Jane Doe", ud2.getName()); - } - // Check func-ptr are original - { - final long[] funcBOrigs = options.getCustomFuncBVariants(0, new long[2], 0, 2); - final long funcB1 = options.getCustomFuncB1(); - final long funcB2 = options.getCustomFuncB2(); - Assert.assertEquals(funcBOrigs[0], funcB1); - Assert.assertEquals(funcBOrigs[1], funcB2); - } - Assert.assertEquals(101, options.CustomFuncB1(ud1)); - Assert.assertEquals(404, options.CustomFuncB1(ud2)); - Assert.assertEquals(-101, options.CustomFuncB2(ud1)); - Assert.assertEquals(-404, options.CustomFuncB2(ud2)); - // switch functions - { - final long funcB1 = options.getCustomFuncB1(); - final long funcB2 = options.getCustomFuncB2(); - options.setCustomFuncB1(funcB2); - options.setCustomFuncB2(funcB1); - } - // Check func-ptr are switched - { - final long[] funcBOrigs = options.getCustomFuncBVariants(0, new long[2], 0, 2); - final long funcB1 = options.getCustomFuncB1(); - final long funcB2 = options.getCustomFuncB2(); - Assert.assertEquals(funcBOrigs[1], funcB1); - Assert.assertEquals(funcBOrigs[0], funcB2); - } - Assert.assertEquals(-101, options.CustomFuncB1(ud1)); - Assert.assertEquals(-404, options.CustomFuncB1(ud2)); - Assert.assertEquals(101, options.CustomFuncB2(ud1)); - Assert.assertEquals(404, options.CustomFuncB2(ud2)); - - bt2.Release(options); - Assert.assertEquals(true, options.isOverrideThreadAffinityNull()); - Assert.assertEquals(true, options.isProductNameNull()); - Assert.assertEquals(true, options.isProductVersionNull()); - Assert.assertEquals(0, options.getCustomFuncA1()); - // const Assert.assertEquals(0, options.getCustomFuncA2()); - Assert.assertEquals(0, options.getCustomFuncB1()); - Assert.assertEquals(0, options.getCustomFuncB2()); - } - - public static void main(final String args[]) throws IOException { - final String tstname = Test2FuncPtr.class.getName(); - org.junit.runner.JUnitCore.main(tstname); - } -} diff --git a/src/junit/com/jogamp/gluegen/test/junit/generation/Test2p1FuncPtr.java b/src/junit/com/jogamp/gluegen/test/junit/generation/Test2p1FuncPtr.java new file mode 100644 index 0000000..82247ce --- /dev/null +++ b/src/junit/com/jogamp/gluegen/test/junit/generation/Test2p1FuncPtr.java @@ -0,0 +1,75 @@ +/** + * Copyright 2023 JogAmp Community. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are + * permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are those of the + * authors and should not be interpreted as representing official policies, either expressed + * or implied, of JogAmp Community. + */ + +package com.jogamp.gluegen.test.junit.generation; + +import com.jogamp.common.os.NativeLibrary; +import com.jogamp.gluegen.test.junit.generation.impl.Bindingtest2p1Impl; +import org.junit.*; +import org.junit.runners.MethodSorters; + +import java.io.IOException; + +/** + * Test {@link Bindingtest2p1} with {@link T2_InitializeOptions} instance and function pointer... + */ +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +public class Test2p1FuncPtr extends BaseTest2FuncPtr { + + static NativeLibrary dynamicLookupHelper; + + /** + * Verifies loading of the new library. + */ + @BeforeClass + public static void chapter__TestLoadLibrary() throws Exception { + BindingJNILibLoader.loadBindingtest2p1(); + dynamicLookupHelper = NativeLibrary.open("test2", false, false, Test2p1FuncPtr.class.getClassLoader(), true); + Assert.assertNotNull("NativeLibrary.open(test2) failed", dynamicLookupHelper); + } + + /** + * Verifies unloading of the new library. + */ + @AfterClass + public static void chapter0XTestUnloadLibrary() throws Exception { + Assert.assertNotNull(dynamicLookupHelper); + dynamicLookupHelper.close(); + dynamicLookupHelper = null; + } + + @Test + public void chapter01() throws Exception { + chapter01(new Bindingtest2p1Impl()); + } + + public static void main(final String args[]) throws IOException { + final String tstname = Test2p1FuncPtr.class.getName(); + org.junit.runner.JUnitCore.main(tstname); + } +} diff --git a/src/junit/com/jogamp/gluegen/test/junit/generation/Test2p2FuncPtr.java b/src/junit/com/jogamp/gluegen/test/junit/generation/Test2p2FuncPtr.java new file mode 100644 index 0000000..6a553d8 --- /dev/null +++ b/src/junit/com/jogamp/gluegen/test/junit/generation/Test2p2FuncPtr.java @@ -0,0 +1,77 @@ +/** + * Copyright 2023 JogAmp Community. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are + * permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are those of the + * authors and should not be interpreted as representing official policies, either expressed + * or implied, of JogAmp Community. + */ + +package com.jogamp.gluegen.test.junit.generation; + +import com.jogamp.common.os.NativeLibrary; +import com.jogamp.gluegen.test.junit.generation.impl.Bindingtest2p2Impl; +import org.junit.*; +import org.junit.runners.MethodSorters; + +import java.io.IOException; + +/** + * Test {@link Bindingtest2p1} with {@link T2_InitializeOptions} instance and function pointer... + */ +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +public class Test2p2FuncPtr extends BaseTest2FuncPtr { + + static NativeLibrary dynamicLookupHelper; + + /** + * Verifies loading of the new library. + */ + @BeforeClass + public static void chapter__TestLoadLibrary() throws Exception { + BindingJNILibLoader.loadBindingtest2p2(); + dynamicLookupHelper = NativeLibrary.open("test2", false, false, Test2p2FuncPtr.class.getClassLoader(), true); + Assert.assertNotNull("NativeLibrary.open(test2) failed", dynamicLookupHelper); + + Bindingtest2p2Impl.resetProcAddressTable(dynamicLookupHelper); + } + + /** + * Verifies unloading of the new library. + */ + @AfterClass + public static void chapter0XTestUnloadLibrary() throws Exception { + Assert.assertNotNull(dynamicLookupHelper); + dynamicLookupHelper.close(); + dynamicLookupHelper = null; + } + + @Test + public void chapter01() throws Exception { + chapter01(new Bindingtest2p2Impl()); + } + + public static void main(final String args[]) throws IOException { + final String tstname = Test2p2FuncPtr.class.getName(); + org.junit.runner.JUnitCore.main(tstname); + } +} diff --git a/src/junit/com/jogamp/gluegen/test/junit/generation/Test3PtrStorage.java b/src/junit/com/jogamp/gluegen/test/junit/generation/Test3PtrStorage.java deleted file mode 100644 index 2bf82a8..0000000 --- a/src/junit/com/jogamp/gluegen/test/junit/generation/Test3PtrStorage.java +++ /dev/null @@ -1,139 +0,0 @@ -/** - * Copyright 2023 JogAmp Community. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modification, are - * permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this list of - * conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, this list - * of conditions and the following disclaimer in the documentation and/or other materials - * provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND - * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON - * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - * The views and conclusions contained in the software and documentation are those of the - * authors and should not be interpreted as representing official policies, either expressed - * or implied, of JogAmp Community. - */ - -package com.jogamp.gluegen.test.junit.generation; - -import java.io.IOException; -import java.nio.IntBuffer; - -import com.jogamp.common.nio.Buffers; -import com.jogamp.common.nio.ElementBuffer; -import com.jogamp.common.os.NativeLibrary; -import com.jogamp.gluegen.test.junit.generation.impl.Bindingtest2Impl; - -import org.junit.AfterClass; -import org.junit.Assert; -import org.junit.BeforeClass; -import org.junit.Test; - -import org.junit.FixMethodOrder; -import org.junit.runners.MethodSorters; - -/** - * Test {@link Bindingtest2} with {@link T2_PointerStorage} instance and pointer pointer.. - */ -@FixMethodOrder(MethodSorters.NAME_ASCENDING) -public class Test3PtrStorage extends BaseClass { - - static NativeLibrary dynamicLookupHelper; - - /** - * Verifies loading of the new library. - */ - @BeforeClass - public static void chapter__TestLoadLibrary() throws Exception { - BindingJNILibLoader.loadBindingtest2(); - dynamicLookupHelper = NativeLibrary.open("test2", false, false, Test2FuncPtr.class.getClassLoader(), true); - Assert.assertNotNull("NativeLibrary.open(test2) failed", dynamicLookupHelper); - - Bindingtest2Impl.resetProcAddressTable(dynamicLookupHelper); - } - - /** - * Verifies unloading of the new library. - */ - @AfterClass - public static void chapter0XTestUnloadLibrary() throws Exception { - Assert.assertNotNull(dynamicLookupHelper); - dynamicLookupHelper.close(); - dynamicLookupHelper = null; - } - - - /** - * Test {@link Bindingtest2} with {@link T2_PointerStorage} instance and pointer pointer - */ - @Test - public void chapter01() throws Exception { - Assert.assertEquals(false, T2_PointerStorage.usesNativeCode()); - - final Bindingtest2 bt2 = new Bindingtest2Impl(); - final T2_PointerStorage store = bt2.createT2PointerStorage(); - // final T2_PointerStorage store = T2_PointerStorage.create(); - final long[] int32PtrArray = store.getInt32PtrArray(0, new long[10], 0, 10); // 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 - { - Assert.assertEquals(10, int32PtrArray.length); - System.err.print("int32PtrArray[10] = { "); - for(int i=0; i 3, 4, 5, 6, 7, 8, 9 - store.setInt32PtrPtr(true, int32PtrArray, 8, 3, 2); // -> 3, 4, 5, 8, 9, 8, 9 - store.setInt32PtrPtr(true, int32PtrArray, 0, 5, 2); // -> 3, 4, 5, 8, 9, 0, 1 - final long[] int32PtrPtr = store.getInt32PtrPtr(0, new long[7], 0, 7); // 3, 4, 5, 8, 9, 0, 1 - { - System.err.print("int32PtrPtr[7] = { "); - for(int i=0; i myCallback01, context01 - bt2.alBufferCallback0(buffer1, 0, 0, myCallback01, context01); - Assert.assertEquals(true, bt2.isAlBufferCallback0Mapped(buffer1Key)); - Assert.assertEquals(false, bt2.isAlBufferCallback0Mapped(buffer2Key)); - Assert.assertEquals(false, bt2.isAlBufferCallback0Mapped(buffer3Key)); - Assert.assertEquals(context01, bt2.getAlBufferCallback0UserParam(buffer1Key)); - Assert.assertEquals(null, bt2.getAlBufferCallback0UserParam(buffer2Key)); - Assert.assertEquals(null, bt2.getAlBufferCallback0UserParam(buffer3Key)); - Assert.assertEquals(myCallback01, bt2.getAlBufferCallback0(buffer1Key)); - Assert.assertEquals(null, bt2.getAlBufferCallback0(buffer2Key)); - Assert.assertEquals(null, bt2.getAlBufferCallback0(buffer3Key)); - Assert.assertEquals(1, bt2.getAlBufferCallback0Keys().size()); - { - final Set keys = bt2.getAlBufferCallback0Keys(); - Assert.assertEquals(true, keys.contains(buffer1Key)); - Assert.assertEquals(false, keys.contains(buffer2Key)); - Assert.assertEquals(false, keys.contains(buffer3Key)); - } - - // 2nd mapping: buffer2 -> myCallback02, context02 - bt2.alBufferCallback0(buffer2, 0, 0, myCallback02, context02); - Assert.assertEquals(true, bt2.isAlBufferCallback0Mapped(buffer1Key)); - Assert.assertEquals(true, bt2.isAlBufferCallback0Mapped(buffer2Key)); - Assert.assertEquals(false, bt2.isAlBufferCallback0Mapped(buffer3Key)); - Assert.assertEquals(context01, bt2.getAlBufferCallback0UserParam(buffer1Key)); - Assert.assertEquals(context02, bt2.getAlBufferCallback0UserParam(buffer2Key)); - Assert.assertEquals(null, bt2.getAlBufferCallback0UserParam(buffer3Key)); - Assert.assertEquals(myCallback01, bt2.getAlBufferCallback0(buffer1Key)); - Assert.assertEquals(myCallback02, bt2.getAlBufferCallback0(buffer2Key)); - Assert.assertEquals(null, bt2.getAlBufferCallback0(buffer3Key)); - Assert.assertEquals(2, bt2.getAlBufferCallback0Keys().size()); - { - final Set keys = bt2.getAlBufferCallback0Keys(); - Assert.assertEquals(true, keys.contains(buffer1Key)); - Assert.assertEquals(true, keys.contains(buffer2Key)); - Assert.assertEquals(false, keys.contains(buffer3Key)); - } - - { - final Thread thread = new Thread(new Runnable() { - @Override - public void run() { - bt2.alBufferCallback0Inject(buffer1, 10, 100); // buffer1 -> myCallback01, context01 - } - }); - thread.start(); - thread.join(); - Assert.assertEquals(10+100+1, id_res[0]); - Assert.assertEquals( 1, context01.i); - Assert.assertEquals(10+100+1, context01.j); - Assert.assertEquals( 1, context01.buffer); - Assert.assertEquals( 2, context02.i); - Assert.assertEquals( 0, context02.j); - Assert.assertEquals( 0, context02.buffer); - } - { - bt2.alBufferCallback0Inject(buffer2, 10, 100); // buffer2 -> myCallback02, context02 - Assert.assertEquals(10*100+2, id_res[0]); - Assert.assertEquals( 1, context01.i); - Assert.assertEquals(10+100+1, context01.j); - Assert.assertEquals( 1, context01.buffer); - Assert.assertEquals( 2, context02.i); - Assert.assertEquals(10*100+2, context02.j); - Assert.assertEquals( 2, context02.buffer); - } - - // Switch the callback function for buffer2 -> myCallback01, context02 - bt2.alBufferCallback0(buffer2, 0, 0, myCallback01, context02); - Assert.assertEquals(true, bt2.isAlBufferCallback0Mapped(buffer1Key)); - Assert.assertEquals(true, bt2.isAlBufferCallback0Mapped(buffer2Key)); - Assert.assertEquals(false, bt2.isAlBufferCallback0Mapped(buffer3Key)); - Assert.assertEquals(context01, bt2.getAlBufferCallback0UserParam(buffer1Key)); - Assert.assertEquals(context02, bt2.getAlBufferCallback0UserParam(buffer2Key)); - Assert.assertEquals(null, bt2.getAlBufferCallback0UserParam(buffer3Key)); - Assert.assertEquals(myCallback01, bt2.getAlBufferCallback0(buffer1Key)); - Assert.assertEquals(myCallback01, bt2.getAlBufferCallback0(buffer2Key)); - Assert.assertEquals(null, bt2.getAlBufferCallback0(buffer3Key)); - - { - bt2.alBufferCallback0Inject(buffer1, 11, 101); // buffer1 -> myCallback01, context01 - Assert.assertEquals(11+101+1, id_res[0]); - Assert.assertEquals( 1, context01.i); - Assert.assertEquals(11+101+1, context01.j); - Assert.assertEquals( 1, context01.buffer); - Assert.assertEquals( 2, context02.i); - Assert.assertEquals(10*100+2, context02.j); - Assert.assertEquals( 2, context02.buffer); - } - { - final Thread thread = new Thread(new Runnable() { - @Override - public void run() { - bt2.alBufferCallback0Inject(buffer2, 1, 10); // buffer2 -> myCallback01, context02 - } - }); - thread.start(); - thread.join(); - Assert.assertEquals( 1+ 10+2, id_res[0]); - Assert.assertEquals( 1, context01.i); - Assert.assertEquals(11+101+1, context01.j); - Assert.assertEquals( 1, context01.buffer); - Assert.assertEquals( 2, context02.i); - Assert.assertEquals( 1+ 10+2, context02.j); - Assert.assertEquals( 2, context02.buffer); - } - - // Just release the buffer2 callback and mapped resources - bt2.alBufferCallback0(buffer2, 0, 0, null, context02); // usrptr is not key, only buffer is key! - Assert.assertEquals(true, bt2.isAlBufferCallback0Mapped(buffer1Key)); - Assert.assertEquals(false, bt2.isAlBufferCallback0Mapped(buffer2Key)); - Assert.assertEquals(false, bt2.isAlBufferCallback0Mapped(buffer3Key)); - Assert.assertEquals(context01, bt2.getAlBufferCallback0UserParam(buffer1Key)); - Assert.assertEquals(null, bt2.getAlBufferCallback0UserParam(buffer2Key)); - Assert.assertEquals(null, bt2.getAlBufferCallback0UserParam(buffer3Key)); - Assert.assertEquals(myCallback01, bt2.getAlBufferCallback0(buffer1Key)); - Assert.assertEquals(null, bt2.getAlBufferCallback0(buffer2Key)); - Assert.assertEquals(null, bt2.getAlBufferCallback0(buffer3Key)); - Assert.assertEquals(1, bt2.getAlBufferCallback0Keys().size()); - { - final Set keys = bt2.getAlBufferCallback0Keys(); - Assert.assertEquals(true, keys.contains(buffer1Key)); - Assert.assertEquals(false, keys.contains(buffer2Key)); - Assert.assertEquals(false, keys.contains(buffer3Key)); - } - - // Switch the callback function for buffer2 -> myCallback01, context02 - { - // pre-condition - Assert.assertEquals(true, bt2.isAlBufferCallback0Mapped(buffer1Key)); - Assert.assertEquals(false, bt2.isAlBufferCallback0Mapped(buffer2Key)); - Assert.assertEquals(false, bt2.isAlBufferCallback0Mapped(buffer3Key)); - Assert.assertEquals(context01, bt2.getAlBufferCallback0UserParam(buffer1Key)); - Assert.assertEquals(null, bt2.getAlBufferCallback0UserParam(buffer2Key)); - Assert.assertEquals(null, bt2.getAlBufferCallback0UserParam(buffer3Key)); - Assert.assertEquals(myCallback01, bt2.getAlBufferCallback0(buffer1Key)); - Assert.assertEquals(null, bt2.getAlBufferCallback0(buffer2Key)); - Assert.assertEquals(null, bt2.getAlBufferCallback0(buffer3Key)); - Assert.assertEquals(1, bt2.getAlBufferCallback0Keys().size()); - } - bt2.alBufferCallback0(buffer1, 0, 0, myCallback02, context02); - { - // post-state - Assert.assertEquals(true, bt2.isAlBufferCallback0Mapped(buffer1Key)); - Assert.assertEquals(false, bt2.isAlBufferCallback0Mapped(buffer2Key)); - Assert.assertEquals(false, bt2.isAlBufferCallback0Mapped(buffer3Key)); - Assert.assertEquals(context02, bt2.getAlBufferCallback0UserParam(buffer1Key)); - Assert.assertEquals(null, bt2.getAlBufferCallback0UserParam(buffer2Key)); - Assert.assertEquals(null, bt2.getAlBufferCallback0UserParam(buffer3Key)); - Assert.assertEquals(myCallback02, bt2.getAlBufferCallback0(buffer1Key)); - Assert.assertEquals(null, bt2.getAlBufferCallback0(buffer2Key)); - Assert.assertEquals(null, bt2.getAlBufferCallback0(buffer3Key)); - } - { - context01.j = 0; - context01.buffer = 0; - context02.j = 0; - context02.buffer = 0; - bt2.alBufferCallback0Inject(buffer1, 2, 10); // buffer1 -> myCallback01, context01 - Assert.assertEquals( 2*10+2, id_res[0]); - Assert.assertEquals( 1, context01.i); - Assert.assertEquals( 0, context01.j); - Assert.assertEquals( 0, context01.buffer); - Assert.assertEquals( 2, context02.i); - Assert.assertEquals( 2*10+2, context02.j); - Assert.assertEquals( 1, context02.buffer); - } - - // Just release the buffer1 callback and mapped resources - bt2.alBufferCallback0(buffer1, 0, 0, null, null); // usrptr is not key, only buffer is key! - Assert.assertEquals(false, bt2.isAlBufferCallback0Mapped(buffer1Key)); - Assert.assertEquals(false, bt2.isAlBufferCallback0Mapped(buffer2Key)); - Assert.assertEquals(false, bt2.isAlBufferCallback0Mapped(buffer3Key)); - Assert.assertEquals(null, bt2.getAlBufferCallback0UserParam(buffer1Key)); - Assert.assertEquals(null, bt2.getAlBufferCallback0UserParam(buffer2Key)); - Assert.assertEquals(null, bt2.getAlBufferCallback0UserParam(buffer3Key)); - Assert.assertEquals(null, bt2.getAlBufferCallback0(buffer1Key)); - Assert.assertEquals(null, bt2.getAlBufferCallback0(buffer2Key)); - Assert.assertEquals(null, bt2.getAlBufferCallback0(buffer3Key)); - Assert.assertEquals(0, bt2.getAlBufferCallback0Keys().size()); - { - final Set keys = bt2.getAlBufferCallback0Keys(); - Assert.assertEquals(false, keys.contains(buffer1Key)); - Assert.assertEquals(false, keys.contains(buffer2Key)); - Assert.assertEquals(false, keys.contains(buffer3Key)); - } - - { - id_res[0] = 0; - context01.j = 0; - context01.buffer = 0; - context02.j = 0; - context02.buffer = 0; - bt2.alBufferCallback0Inject(buffer2, 1, 10); // unmapped, no change in data - Assert.assertEquals( 0, id_res[0]); - Assert.assertEquals( 1, context01.i); - Assert.assertEquals( 0, context01.j); - Assert.assertEquals( 0, context01.buffer); - Assert.assertEquals( 2, context02.i); - Assert.assertEquals( 0, context02.j); - Assert.assertEquals( 0, context02.buffer); - } - } - - /** - * Test Bindingtest2 with ALBUFFERCALLBACKTYPESOFT JavaCallback via alBufferCallback1() - * using our custom CustomAlBufferCallback1Key class. - */ - @Test - public void chapter03() throws Exception { - final Bindingtest2 bt2 = new Bindingtest2Impl(); - - final long[] id_res = { -1 }; - final ALBUFFERCALLBACKTYPESOFT myCallback01 = new ALBUFFERCALLBACKTYPESOFT() { - @Override - public void callback(final int buffer, final ALCcontext context, final int sampledata, final int numbytes) { - final long res = sampledata + numbytes + context.i; - id_res[0] = res; - context.j = res;; - context.buffer = buffer; - System.err.println("chapter03.myCallback01: buffer "+buffer+", sampledata "+sampledata+", numbytes "+numbytes); - } - }; - final ALBUFFERCALLBACKTYPESOFT myCallback02 = new ALBUFFERCALLBACKTYPESOFT() { - @Override - public void callback(final int buffer, final ALCcontext context, final int sampledata, final int numbytes) { - final long res = sampledata * numbytes + context.i; - id_res[0] = res; - context.j = res; - context.buffer = buffer; - System.err.println("chapter03.myCallback02: buffer "+buffer+", sampledata "+sampledata+", numbytes "+numbytes); - } - }; - final int buffer1 = 1; - final int buffer2 = 2; - final int buffer3 = 3; - final CustomAlBufferCallback1Key buffer1Key = new CustomAlBufferCallback1Key(buffer1); - final CustomAlBufferCallback1Key buffer2Key = new CustomAlBufferCallback1Key(buffer2); - final CustomAlBufferCallback1Key buffer3Key = new CustomAlBufferCallback1Key(buffer3); - final ALCcontext context01 = new ALCcontext( 1); - final ALCcontext context02 = new ALCcontext( 2); - Assert.assertEquals( 1, context01.i); - Assert.assertEquals( 0, context01.j); - Assert.assertEquals( 0, context01.buffer); - Assert.assertEquals( 2, context02.i); - Assert.assertEquals( 0, context02.j); - Assert.assertEquals( 0, context02.buffer); - - Assert.assertEquals(false, bt2.isAlBufferCallback1Mapped(buffer1Key)); - Assert.assertEquals(false, bt2.isAlBufferCallback1Mapped(buffer2Key)); - Assert.assertEquals(false, bt2.isAlBufferCallback1Mapped(buffer3Key)); - Assert.assertEquals(0, bt2.getAlBufferCallback1Keys().size()); - - // 1st mapping: buffer1 -> myCallback01, context01 - bt2.alBufferCallback1(context01, buffer1, 0, 0, myCallback01); - Assert.assertEquals(true, bt2.isAlBufferCallback1Mapped(buffer1Key)); - Assert.assertEquals(false, bt2.isAlBufferCallback1Mapped(buffer2Key)); - Assert.assertEquals(false, bt2.isAlBufferCallback1Mapped(buffer3Key)); - Assert.assertEquals(context01, bt2.getAlBufferCallback1UserParam(buffer1Key)); - Assert.assertEquals(null, bt2.getAlBufferCallback1UserParam(buffer2Key)); - Assert.assertEquals(null, bt2.getAlBufferCallback1UserParam(buffer3Key)); - Assert.assertEquals(myCallback01, bt2.getAlBufferCallback1(buffer1Key)); - Assert.assertEquals(null, bt2.getAlBufferCallback1(buffer2Key)); - Assert.assertEquals(null, bt2.getAlBufferCallback1(buffer3Key)); - Assert.assertEquals(1, bt2.getAlBufferCallback1Keys().size()); - { - final Set keys = bt2.getAlBufferCallback1Keys(); - Assert.assertEquals(true, keys.contains(buffer1Key)); - Assert.assertEquals(false, keys.contains(buffer2Key)); - Assert.assertEquals(false, keys.contains(buffer3Key)); - } - - // 2nd mapping: buffer2 -> myCallback02, context02 - bt2.alBufferCallback1(context02, buffer2, 0, 0, myCallback02); - Assert.assertEquals(true, bt2.isAlBufferCallback1Mapped(buffer1Key)); - Assert.assertEquals(true, bt2.isAlBufferCallback1Mapped(buffer2Key)); - Assert.assertEquals(false, bt2.isAlBufferCallback1Mapped(buffer3Key)); - Assert.assertEquals(context01, bt2.getAlBufferCallback1UserParam(buffer1Key)); - Assert.assertEquals(context02, bt2.getAlBufferCallback1UserParam(buffer2Key)); - Assert.assertEquals(null, bt2.getAlBufferCallback1UserParam(buffer3Key)); - Assert.assertEquals(myCallback01, bt2.getAlBufferCallback1(buffer1Key)); - Assert.assertEquals(myCallback02, bt2.getAlBufferCallback1(buffer2Key)); - Assert.assertEquals(null, bt2.getAlBufferCallback1(buffer3Key)); - Assert.assertEquals(2, bt2.getAlBufferCallback1Keys().size()); - { - final Set keys = bt2.getAlBufferCallback1Keys(); - Assert.assertEquals(true, keys.contains(buffer1Key)); - Assert.assertEquals(true, keys.contains(buffer2Key)); - Assert.assertEquals(false, keys.contains(buffer3Key)); - } - - { - bt2.alBufferCallback1Inject(buffer1, 10, 100); // buffer1 -> myCallback01, context01 - Assert.assertEquals(10+100+1, id_res[0]); - Assert.assertEquals( 1, context01.i); - Assert.assertEquals(10+100+1, context01.j); - Assert.assertEquals( 1, context01.buffer); - Assert.assertEquals( 2, context02.i); - Assert.assertEquals( 0, context02.j); - Assert.assertEquals( 0, context02.buffer); - } - { - final Thread thread = new Thread(new Runnable() { - @Override - public void run() { - bt2.alBufferCallback1Inject(buffer2, 10, 100); // buffer2 -> myCallback02, context02 - } - }); - thread.start(); - thread.join(); - Assert.assertEquals(10*100+2, id_res[0]); - Assert.assertEquals( 1, context01.i); - Assert.assertEquals(10+100+1, context01.j); - Assert.assertEquals( 1, context01.buffer); - Assert.assertEquals( 2, context02.i); - Assert.assertEquals(10*100+2, context02.j); - Assert.assertEquals( 2, context02.buffer); - } - - // Switch the callback function for buffer2 -> myCallback01, context02 - bt2.alBufferCallback1(context02, buffer2, 0, 0, myCallback01); - Assert.assertEquals(true, bt2.isAlBufferCallback1Mapped(buffer1Key)); - Assert.assertEquals(true, bt2.isAlBufferCallback1Mapped(buffer2Key)); - Assert.assertEquals(false, bt2.isAlBufferCallback1Mapped(buffer3Key)); - Assert.assertEquals(context01, bt2.getAlBufferCallback1UserParam(buffer1Key)); - Assert.assertEquals(context02, bt2.getAlBufferCallback1UserParam(buffer2Key)); - Assert.assertEquals(null, bt2.getAlBufferCallback1UserParam(buffer3Key)); - Assert.assertEquals(myCallback01, bt2.getAlBufferCallback1(buffer1Key)); - Assert.assertEquals(myCallback01, bt2.getAlBufferCallback1(buffer2Key)); - Assert.assertEquals(null, bt2.getAlBufferCallback1(buffer3Key)); - - { - final Thread thread = new Thread(new Runnable() { - @Override - public void run() { - bt2.alBufferCallback1Inject(buffer1, 11, 101); // buffer1 -> myCallback01, context01 - } - }); - thread.start(); - thread.join(); - Assert.assertEquals(11+101+1, id_res[0]); - Assert.assertEquals( 1, context01.i); - Assert.assertEquals(11+101+1, context01.j); - Assert.assertEquals( 1, context01.buffer); - Assert.assertEquals( 2, context02.i); - Assert.assertEquals(10*100+2, context02.j); - Assert.assertEquals( 2, context02.buffer); - } - { - bt2.alBufferCallback1Inject(buffer2, 1, 10); // buffer2 -> myCallback01, context02 - Assert.assertEquals( 1+ 10+2, id_res[0]); - Assert.assertEquals( 1, context01.i); - Assert.assertEquals(11+101+1, context01.j); - Assert.assertEquals( 1, context01.buffer); - Assert.assertEquals( 2, context02.i); - Assert.assertEquals( 1+ 10+2, context02.j); - Assert.assertEquals( 2, context02.buffer); - } - - // Just release the buffer2 callback and mapped resources - bt2.alBufferCallback1(context02, buffer2, 0, 0, null); // usrptr is not key, only buffer is key! - Assert.assertEquals(true, bt2.isAlBufferCallback1Mapped(buffer1Key)); - Assert.assertEquals(false, bt2.isAlBufferCallback1Mapped(buffer2Key)); - Assert.assertEquals(false, bt2.isAlBufferCallback1Mapped(buffer3Key)); - Assert.assertEquals(context01, bt2.getAlBufferCallback1UserParam(buffer1Key)); - Assert.assertEquals(null, bt2.getAlBufferCallback1UserParam(buffer2Key)); - Assert.assertEquals(null, bt2.getAlBufferCallback1UserParam(buffer3Key)); - Assert.assertEquals(myCallback01, bt2.getAlBufferCallback1(buffer1Key)); - Assert.assertEquals(null, bt2.getAlBufferCallback1(buffer2Key)); - Assert.assertEquals(null, bt2.getAlBufferCallback1(buffer3Key)); - Assert.assertEquals(1, bt2.getAlBufferCallback1Keys().size()); - { - final Set keys = bt2.getAlBufferCallback1Keys(); - Assert.assertEquals(true, keys.contains(buffer1Key)); - Assert.assertEquals(false, keys.contains(buffer2Key)); - Assert.assertEquals(false, keys.contains(buffer3Key)); - } - - // Just release the buffer1 callback and mapped resources - bt2.alBufferCallback1(null, buffer1, 0, 0, null); // usrptr is not key, only buffer is key! - Assert.assertEquals(false, bt2.isAlBufferCallback1Mapped(buffer1Key)); - Assert.assertEquals(false, bt2.isAlBufferCallback1Mapped(buffer2Key)); - Assert.assertEquals(false, bt2.isAlBufferCallback1Mapped(buffer3Key)); - Assert.assertEquals(null, bt2.getAlBufferCallback1UserParam(buffer1Key)); - Assert.assertEquals(null, bt2.getAlBufferCallback1UserParam(buffer2Key)); - Assert.assertEquals(null, bt2.getAlBufferCallback1UserParam(buffer3Key)); - Assert.assertEquals(null, bt2.getAlBufferCallback1(buffer1Key)); - Assert.assertEquals(null, bt2.getAlBufferCallback1(buffer2Key)); - Assert.assertEquals(null, bt2.getAlBufferCallback1(buffer3Key)); - Assert.assertEquals(0, bt2.getAlBufferCallback1Keys().size()); - { - final Set keys = bt2.getAlBufferCallback1Keys(); - Assert.assertEquals(false, keys.contains(buffer1Key)); - Assert.assertEquals(false, keys.contains(buffer2Key)); - Assert.assertEquals(false, keys.contains(buffer3Key)); - } - - { - bt2.alBufferCallback1Inject(buffer2, 1, 10); // unmapped, no change in data - Assert.assertEquals( 1+ 10+2, id_res[0]); - Assert.assertEquals( 1, context01.i); - Assert.assertEquals(11+101+1, context01.j); - Assert.assertEquals( 1, context01.buffer); - Assert.assertEquals( 2, context02.i); - Assert.assertEquals( 1+ 10+2, context02.j); - Assert.assertEquals( 2, context02.buffer); - } - } - - /** - * Test in depth lifecycle of Bindingtest2 with ALBUFFERCALLBACKTYPESOFT JavaCallback via alBufferCallback1() - * using the default AlBufferCallback1Key class. - */ - @Test - public void chapter04() throws Exception { - final Bindingtest2 bt2 = new Bindingtest2Impl(); - - final long[] id_res = { -1 }; - final ALBUFFERCALLBACKTYPESOFT myCallback01 = new ALBUFFERCALLBACKTYPESOFT() { - @Override - public void callback(final int buffer, final ALCcontext context, final int sampledata, final int numbytes) { - if( context.throwPreAction ) { - context.throwPreAction = false; - throw new RuntimeException("Exception test.pre: chapter04.myCallback01"); - } - final long res = sampledata + numbytes + context.i; - id_res[0] = res; - context.j = res; - context.buffer = buffer; - System.err.println("chapter04.myCallback01: buffer "+buffer+", sampledata "+sampledata+", numbytes "+numbytes); - if( context.throwPostAction ) { - context.throwPostAction = false; - throw new RuntimeException("Exception test.post: chapter04.myCallback01"); - } - } - }; - final ALBUFFERCALLBACKTYPESOFT myCallback02 = new ALBUFFERCALLBACKTYPESOFT() { - @Override - public void callback(final int buffer, final ALCcontext context, final int sampledata, final int numbytes) { - if( context.throwPreAction ) { - context.throwPreAction = false; - throw new RuntimeException("Exception test.pre: chapter04.myCallback02"); - } - final long res = sampledata * numbytes + context.i; - id_res[0] = res; - context.j = res; - context.buffer = buffer; - System.err.println("chapter04.myCallback02: buffer "+buffer+", sampledata "+sampledata+", numbytes "+numbytes); - if( context.throwPostAction ) { - context.throwPostAction = false; - throw new RuntimeException("Exception test.post: chapter04.myCallback02"); - } - } - }; - final int buffer1 = 1; - final int buffer2 = 2; - final int buffer3 = 3; - final AlBufferCallback0Key buffer1Key = new AlBufferCallback0Key(buffer1); - final AlBufferCallback0Key buffer2Key = new AlBufferCallback0Key(buffer2); - final AlBufferCallback0Key buffer3Key = new AlBufferCallback0Key(buffer3); - final ALCcontext context01 = new ALCcontext( 1); - final ALCcontext context02 = new ALCcontext( 2); - Assert.assertEquals( 1, context01.i); - Assert.assertEquals( 0, context01.j); - Assert.assertEquals( 0, context01.buffer); - Assert.assertEquals( 2, context02.i); - Assert.assertEquals( 0, context02.j); - Assert.assertEquals( 0, context02.buffer); - - Assert.assertEquals(false, bt2.isAlBufferCallback0Mapped(buffer1Key)); - Assert.assertEquals(false, bt2.isAlBufferCallback0Mapped(buffer2Key)); - Assert.assertEquals(false, bt2.isAlBufferCallback0Mapped(buffer3Key)); - Assert.assertEquals(0, bt2.getAlBufferCallback0Keys().size()); - - // 1st mapping: buffer1 -> myCallback01, context01 - bt2.alBufferCallback0(buffer1, 0, 0, myCallback01, context01); - Assert.assertEquals(true, bt2.isAlBufferCallback0Mapped(buffer1Key)); - Assert.assertEquals(false, bt2.isAlBufferCallback0Mapped(buffer2Key)); - Assert.assertEquals(false, bt2.isAlBufferCallback0Mapped(buffer3Key)); - Assert.assertEquals(context01, bt2.getAlBufferCallback0UserParam(buffer1Key)); - Assert.assertEquals(null, bt2.getAlBufferCallback0UserParam(buffer2Key)); - Assert.assertEquals(null, bt2.getAlBufferCallback0UserParam(buffer3Key)); - Assert.assertEquals(myCallback01, bt2.getAlBufferCallback0(buffer1Key)); - Assert.assertEquals(null, bt2.getAlBufferCallback0(buffer2Key)); - Assert.assertEquals(null, bt2.getAlBufferCallback0(buffer3Key)); - Assert.assertEquals(1, bt2.getAlBufferCallback0Keys().size()); - { - final Set keys = bt2.getAlBufferCallback0Keys(); - Assert.assertEquals(true, keys.contains(buffer1Key)); - Assert.assertEquals(false, keys.contains(buffer2Key)); - Assert.assertEquals(false, keys.contains(buffer3Key)); - } - - // 2nd mapping: buffer2 -> myCallback02, context02 - bt2.alBufferCallback0(buffer2, 0, 0, myCallback02, context02); - Assert.assertEquals(true, bt2.isAlBufferCallback0Mapped(buffer1Key)); - Assert.assertEquals(true, bt2.isAlBufferCallback0Mapped(buffer2Key)); - Assert.assertEquals(false, bt2.isAlBufferCallback0Mapped(buffer3Key)); - Assert.assertEquals(context01, bt2.getAlBufferCallback0UserParam(buffer1Key)); - Assert.assertEquals(context02, bt2.getAlBufferCallback0UserParam(buffer2Key)); - Assert.assertEquals(null, bt2.getAlBufferCallback0UserParam(buffer3Key)); - Assert.assertEquals(myCallback01, bt2.getAlBufferCallback0(buffer1Key)); - Assert.assertEquals(myCallback02, bt2.getAlBufferCallback0(buffer2Key)); - Assert.assertEquals(null, bt2.getAlBufferCallback0(buffer3Key)); - Assert.assertEquals(2, bt2.getAlBufferCallback0Keys().size()); - { - final Set keys = bt2.getAlBufferCallback0Keys(); - Assert.assertEquals(true, keys.contains(buffer1Key)); - Assert.assertEquals(true, keys.contains(buffer2Key)); - Assert.assertEquals(false, keys.contains(buffer3Key)); - } - - // Exception text post action, i.e. result as expected - // Continuous program flow - id_res[0] = -1; - { - context01.throwPostAction = true; - bt2.alBufferCallback0Inject(buffer1, 10, 100); // buffer1 -> myCallback01, context01 - Assert.assertEquals(10+100+1, id_res[0]); - Assert.assertEquals( 1, context01.i); - Assert.assertEquals(10+100+1, context01.j); - Assert.assertEquals( 1, context01.buffer); - Assert.assertEquals( 2, context02.i); - Assert.assertEquals( 0, context02.j); - Assert.assertEquals( 0, context02.buffer); - } - { - context02.throwPostAction = true; - final Thread thread = new Thread(new Runnable() { - @Override - public void run() { - bt2.alBufferCallback0Inject(buffer2, 10, 100); // buffer2 -> myCallback02, context02 - } - }); - thread.start(); - thread.join(); - Assert.assertEquals(10*100+2, id_res[0]); - Assert.assertEquals( 1, context01.i); - Assert.assertEquals(10+100+1, context01.j); - Assert.assertEquals( 1, context01.buffer); - Assert.assertEquals( 2, context02.i); - Assert.assertEquals(10*100+2, context02.j); - Assert.assertEquals( 2, context02.buffer); - } - - // Exception text pre action, i.e. result NOT as expected (unchanged) - // Continuous program flow - id_res[0] = -1; - { - context01.throwPreAction = true; - bt2.alBufferCallback0Inject(buffer1, 20, 200); // buffer1 -> myCallback01, context01 - Assert.assertEquals( -1, id_res[0]); - Assert.assertEquals( 1, context01.i); - Assert.assertEquals(10+100+1, context01.j); - Assert.assertEquals( 1, context01.buffer); - Assert.assertEquals( 2, context02.i); - Assert.assertEquals(10*100+2, context02.j); - Assert.assertEquals( 2, context02.buffer); - } - { - context02.throwPreAction = true; - final Thread thread = new Thread(new Runnable() { - @Override - public void run() { - bt2.alBufferCallback0Inject(buffer2, 20, 200); // buffer2 -> myCallback02, context02 - } - }); - thread.start(); - thread.join(); - Assert.assertEquals( -1, id_res[0]); - Assert.assertEquals( 1, context01.i); - Assert.assertEquals(10+100+1, context01.j); - Assert.assertEquals( 1, context01.buffer); - Assert.assertEquals( 2, context02.i); - Assert.assertEquals(10*100+2, context02.j); - Assert.assertEquals( 2, context02.buffer); - } - - // Just release the buffer2 callback and mapped resources - bt2.releaseAlBufferCallback0(buffer2Key); - // bt2.alBufferCallback0(buffer2, 0, 0, null, context02); // usrptr is not key, only buffer is key! - Assert.assertEquals(true, bt2.isAlBufferCallback0Mapped(buffer1Key)); - Assert.assertEquals(false, bt2.isAlBufferCallback0Mapped(buffer2Key)); - Assert.assertEquals(false, bt2.isAlBufferCallback0Mapped(buffer3Key)); - Assert.assertEquals(context01, bt2.getAlBufferCallback0UserParam(buffer1Key)); - Assert.assertEquals(null, bt2.getAlBufferCallback0UserParam(buffer2Key)); - Assert.assertEquals(null, bt2.getAlBufferCallback0UserParam(buffer3Key)); - Assert.assertEquals(myCallback01, bt2.getAlBufferCallback0(buffer1Key)); - Assert.assertEquals(null, bt2.getAlBufferCallback0(buffer2Key)); - Assert.assertEquals(null, bt2.getAlBufferCallback0(buffer3Key)); - Assert.assertEquals(1, bt2.getAlBufferCallback0Keys().size()); - { - final Set keys = bt2.getAlBufferCallback0Keys(); - Assert.assertEquals(true, keys.contains(buffer1Key)); - Assert.assertEquals(false, keys.contains(buffer2Key)); - Assert.assertEquals(false, keys.contains(buffer3Key)); - } - - // Just release the buffer1 callback and mapped resources - bt2.releaseAlBufferCallback0(buffer1Key); - // bt2.alBufferCallback0(buffer1, 0, 0, null, null); // usrptr is not key, only buffer is key! - Assert.assertEquals(false, bt2.isAlBufferCallback0Mapped(buffer1Key)); - Assert.assertEquals(false, bt2.isAlBufferCallback0Mapped(buffer2Key)); - Assert.assertEquals(false, bt2.isAlBufferCallback0Mapped(buffer3Key)); - Assert.assertEquals(null, bt2.getAlBufferCallback0UserParam(buffer1Key)); - Assert.assertEquals(null, bt2.getAlBufferCallback0UserParam(buffer2Key)); - Assert.assertEquals(null, bt2.getAlBufferCallback0UserParam(buffer3Key)); - Assert.assertEquals(null, bt2.getAlBufferCallback0(buffer1Key)); - Assert.assertEquals(null, bt2.getAlBufferCallback0(buffer2Key)); - Assert.assertEquals(null, bt2.getAlBufferCallback0(buffer3Key)); - Assert.assertEquals(0, bt2.getAlBufferCallback0Keys().size()); - { - final Set keys = bt2.getAlBufferCallback0Keys(); - Assert.assertEquals(false, keys.contains(buffer1Key)); - Assert.assertEquals(false, keys.contains(buffer2Key)); - Assert.assertEquals(false, keys.contains(buffer3Key)); - } - - // The native callback is still registered, - // we 'just' pulled the resource via release*()! - // - // So we no only test no-change in data - // but also whether the native callback handles this case well, - // i.e. detects the released data-resource and *NOT* issuing the java callback. - // The latter would end up in a SIGSEGV otherwise! - // - // Note: After successfully checking a correct jobject reference, - // the native callback also enters and leaves the monitor (Object sync/lock). - id_res[0] = -1; - bt2.alBufferCallback0Inject(buffer2, 1, 10); - Assert.assertEquals( -1, id_res[0]); - Assert.assertEquals( 1, context01.i); - Assert.assertEquals(10+100+1, context01.j); - Assert.assertEquals( 1, context01.buffer); - Assert.assertEquals( 2, context02.i); - Assert.assertEquals(10*100+2, context02.j); - Assert.assertEquals( 2, context02.buffer); - } - - public static class CustomAlBufferCallback1Key { - private final int buffer; - public CustomAlBufferCallback1Key(final int buffer) { - this.buffer = buffer; - } - @Override - public boolean equals(final Object o) { - if( this == o ) { - return true; - } - if( !(o instanceof CustomAlBufferCallback1Key) ) { - return false; - } - final CustomAlBufferCallback1Key o2 = (CustomAlBufferCallback1Key)o; - return buffer == o2.buffer; - } - @Override - public int hashCode() { - return buffer; - } - @Override - public String toString() { - return "CustomALKey[this "+toHexString(System.identityHashCode(this))+", buffer "+buffer+"]"; - } - } - - /** - * Test Bindingtest2 with ALEVENTPROCSOFT JavaCallback - * on alEventCallback0(..) having the 'Object userParam` as single key. - */ - @Test - public void chapter05a() throws Exception { - final Bindingtest2 bt2 = new Bindingtest2Impl(); - - final int[] id_res = { -1 }; - final String[] msg_res = { null }; - final ALEVENTPROCSOFT myCallback01 = new ALEVENTPROCSOFT() { - @Override - public void callback(final int eventType, final int object, final int param, final int length, final String message, final ALCcontext context) { - id_res[0] = object; - msg_res[0] = message; - System.err.println("chapter05a.myCallback01: type "+eventType+", obj "+object+", param "+param+", '"+message+"', userParam 0x"+ - Integer.toHexString(System.identityHashCode(context))); - } - }; - final ALEVENTPROCSOFT myCallback02 = new ALEVENTPROCSOFT() { - @Override - public void callback(final int eventType, final int object, final int param, final int length, final String message, final ALCcontext context) { - id_res[0] = 1000 * object; - msg_res[0] = message; - System.err.println("chapter05a.myCallback02: type "+eventType+", obj "+object+", param "+param+", '"+message+"', userParam 0x"+ - Integer.toHexString(System.identityHashCode(context))); - } - }; - final ALCcontext context01 = new ALCcontext(); - final ALCcontext context02 = new ALCcontext(); - final AlEventCallback0Key myKey01 = new AlEventCallback0Key(context01); - final AlEventCallback0Key myKey02 = new AlEventCallback0Key(context02); - Assert.assertEquals(false, bt2.isAlEventCallback0Mapped(myKey01)); - Assert.assertEquals(false, bt2.isAlEventCallback0Mapped(myKey02)); - - bt2.alEventCallback0(myCallback01, context01); - Assert.assertEquals(true, bt2.isAlEventCallback0Mapped(myKey01)); - Assert.assertEquals(false, bt2.isAlEventCallback0Mapped(myKey02)); - { - final Set keys = bt2.getAlEventCallback0Keys(); - Assert.assertEquals(1, keys.size()); - Assert.assertEquals(true, keys.contains(myKey01)); - Assert.assertEquals(false, keys.contains(myKey02)); - } - Assert.assertEquals(myCallback01, bt2.getAlEventCallback0(myKey01)); - Assert.assertEquals(null, bt2.getAlEventCallback0(myKey02)); - Assert.assertEquals(-1, id_res[0]); - Assert.assertEquals(null, msg_res[0]); - - { - final String msgNo1 = "First message"; - id_res[0] = -1; - msg_res[0] = null; - bt2.alEventCallback0Inject(context01, 0, 1, 0, msgNo1); - Assert.assertEquals( 1, id_res[0]); - Assert.assertEquals(msgNo1, msg_res[0]); - } - { - final String msgNo2 = "Second message"; - id_res[0] = -1; - msg_res[0] = null; - bt2.alEventCallback0Inject(context02, 0, 2, 0, msgNo2); - Assert.assertEquals( -1, id_res[0]); - Assert.assertEquals( null, msg_res[0]); - - bt2.alEventCallback0Inject(context01, 0, 2, 0, msgNo2); - Assert.assertEquals( 2, id_res[0]); - Assert.assertEquals( msgNo2, msg_res[0]); - } - - // Switch the callback function - // The previously mapped myCallback01 (context01) gets released - // and remapped to myCallback02 + ( context01 )(key) - bt2.alEventCallback0(myCallback02, context01); - Assert.assertEquals(true, bt2.isAlEventCallback0Mapped(myKey01)); - Assert.assertEquals(false, bt2.isAlEventCallback0Mapped(myKey02)); - { - final Set keys = bt2.getAlEventCallback0Keys(); - Assert.assertEquals(1, keys.size()); - Assert.assertEquals(true, keys.contains(myKey01)); - Assert.assertEquals(false, keys.contains(myKey02)); - } - Assert.assertEquals(myCallback02, bt2.getAlEventCallback0(myKey01)); - Assert.assertEquals(null, bt2.getAlEventCallback0(myKey02)); - - { - final String msgNo3 = "Third message"; - id_res[0] = -1; - msg_res[0] = null; - bt2.alEventCallback0Inject(context02, 0, 3, 0, msgNo3); - Assert.assertEquals( -1, id_res[0]); - Assert.assertEquals( null, msg_res[0]); - - bt2.alEventCallback0Inject(context01, 0, 3, 0, msgNo3); - Assert.assertEquals( 3000, id_res[0]); - Assert.assertEquals( msgNo3, msg_res[0]); - } - - // Fake release (wrong key) - bt2.alEventCallback0(null, context02); - Assert.assertEquals(true, bt2.isAlEventCallback0Mapped(myKey01)); - Assert.assertEquals(false, bt2.isAlEventCallback0Mapped(myKey02)); - - // Just release the callback and mapped context01 - bt2.alEventCallback0(null, context01); - Assert.assertEquals(false, bt2.isAlEventCallback0Mapped(myKey01)); - Assert.assertEquals(false, bt2.isAlEventCallback0Mapped(myKey02)); - Assert.assertEquals(0, bt2.getAlEventCallback0Keys().size()); - - { - final String msgNo4 = "Forth message"; - id_res[0] = -1; - msg_res[0] = null; - bt2.alEventCallback0Inject(context01, 0, 4, 0, msgNo4); - Assert.assertEquals( -1, id_res[0]); - Assert.assertEquals( null, msg_res[0]); - - bt2.alEventCallback0Inject(context02, 0, 4, 0, msgNo4); - Assert.assertEquals( -1, id_res[0]); - Assert.assertEquals( null, msg_res[0]); - } - } - - /** - * Test Bindingtest2 with ALEVENTPROCSOFT JavaCallback - * on alEventCallback0(..) having the 'Object userParam` and `int object` as keys. - */ - @Test - public void chapter05b() throws Exception { - final Bindingtest2 bt2 = new Bindingtest2Impl(); - - final int[] id_res = { -1 }; - final String[] msg_res = { null }; - final ALEVENTPROCSOFT myCallback01 = new ALEVENTPROCSOFT() { - @Override - public void callback(final int eventType, final int object, final int param, final int length, final String message, final ALCcontext context) { - id_res[0] = object; - msg_res[0] = message; - System.err.println("chapter05.myCallback01: type "+eventType+", obj "+object+", param "+param+", '"+message+"', userParam 0x"+ - Integer.toHexString(System.identityHashCode(context))); - } - }; - final ALEVENTPROCSOFT myCallback02 = new ALEVENTPROCSOFT() { - @Override - public void callback(final int eventType, final int object, final int param, final int length, final String message, final ALCcontext context) { - id_res[0] = 1000 * object; - msg_res[0] = message; - System.err.println("chapter05.myCallback02: type "+eventType+", obj "+object+", param "+param+", '"+message+"', userParam 0x"+ - Integer.toHexString(System.identityHashCode(context))); - } - }; - final ALCcontext context01 = new ALCcontext(); - final ALCcontext context02 = new ALCcontext(); - final AlEventCallback1Key myKey01 = new AlEventCallback1Key(1, context01); - final AlEventCallback1Key myKey02 = new AlEventCallback1Key(2, context02); - Assert.assertEquals(false, bt2.isAlEventCallback1Mapped(myKey01)); - Assert.assertEquals(false, bt2.isAlEventCallback1Mapped(myKey02)); - - bt2.alEventCallback1(1, myCallback01, context01); - Assert.assertEquals(true, bt2.isAlEventCallback1Mapped(myKey01)); - Assert.assertEquals(false, bt2.isAlEventCallback1Mapped(myKey02)); - { - final Set keys = bt2.getAlEventCallback1Keys(); - Assert.assertEquals(1, keys.size()); - Assert.assertEquals(true, keys.contains(myKey01)); - Assert.assertEquals(false, keys.contains(myKey02)); - } - Assert.assertEquals(myCallback01, bt2.getAlEventCallback1(myKey01)); - Assert.assertEquals(null, bt2.getAlEventCallback1(myKey02)); - Assert.assertEquals(-1, id_res[0]); - Assert.assertEquals(null, msg_res[0]); - - { - final String msgNo1 = "First message"; - id_res[0] = -1; - msg_res[0] = null; - bt2.alEventCallback1Inject(context01, 0, 1, 0, msgNo1); - Assert.assertEquals( 1, id_res[0]); - Assert.assertEquals(msgNo1, msg_res[0]); - } - { - final String msgNo2 = "Second message"; - id_res[0] = -1; - msg_res[0] = null; - bt2.alEventCallback1Inject(context02, 0, 2, 0, msgNo2); - Assert.assertEquals( -1, id_res[0]); - Assert.assertEquals( null, msg_res[0]); - - bt2.alEventCallback1Inject(context01, 0, 2, 0, msgNo2); - Assert.assertEquals( -1, id_res[0]); - Assert.assertEquals( null, msg_res[0]); - - bt2.alEventCallback1Inject(context01, 0, 1, 0, msgNo2); - Assert.assertEquals( 1, id_res[0]); - Assert.assertEquals( msgNo2, msg_res[0]); - } - - // Switch the callback function - // The previously mapped myCallback01 (1, context01) gets released - // and remapped to myCallback02 + ( 1, context01 )(key) - bt2.alEventCallback1(1, myCallback02, context01); - Assert.assertEquals(true, bt2.isAlEventCallback1Mapped(myKey01)); - Assert.assertEquals(false, bt2.isAlEventCallback1Mapped(myKey02)); - { - final Set keys = bt2.getAlEventCallback1Keys(); - Assert.assertEquals(1, keys.size()); - Assert.assertEquals(true, keys.contains(myKey01)); - Assert.assertEquals(false, keys.contains(myKey02)); - } - Assert.assertEquals(myCallback02, bt2.getAlEventCallback1(myKey01)); - Assert.assertEquals(null, bt2.getAlEventCallback1(myKey02)); - - { - final String msgNo3 = "Third message"; - id_res[0] = -1; - msg_res[0] = null; - bt2.alEventCallback1Inject(context02, 0, 2, 0, msgNo3); - Assert.assertEquals( -1, id_res[0]); - Assert.assertEquals( null, msg_res[0]); - - bt2.alEventCallback1Inject(context01, 0, 2, 0, msgNo3); - Assert.assertEquals( -1, id_res[0]); - Assert.assertEquals( null, msg_res[0]); - - bt2.alEventCallback1Inject(context01, 0, 1, 0, msgNo3); - Assert.assertEquals( 1000, id_res[0]); - Assert.assertEquals( msgNo3, msg_res[0]); - } - - // Fake release (wrong key) - bt2.alEventCallback1(2, null, context02); - Assert.assertEquals(true, bt2.isAlEventCallback1Mapped(myKey01)); - Assert.assertEquals(false, bt2.isAlEventCallback1Mapped(myKey02)); - - bt2.alEventCallback1(2, null, context01); - Assert.assertEquals(true, bt2.isAlEventCallback1Mapped(myKey01)); - Assert.assertEquals(false, bt2.isAlEventCallback1Mapped(myKey02)); - - // Just release the callback and mapped context01 - bt2.alEventCallback1(1, null, context01); - Assert.assertEquals(false, bt2.isAlEventCallback1Mapped(myKey01)); - Assert.assertEquals(false, bt2.isAlEventCallback1Mapped(myKey02)); - Assert.assertEquals(0, bt2.getAlEventCallback1Keys().size()); - - { - final String msgNo4 = "Forth message"; - id_res[0] = -1; - msg_res[0] = null; - bt2.alEventCallback1Inject(context01, 0, 4, 0, msgNo4); - Assert.assertEquals( -1, id_res[0]); - Assert.assertEquals( null, msg_res[0]); - - bt2.alEventCallback1Inject(context02, 0, 4, 0, msgNo4); - Assert.assertEquals( -1, id_res[0]); - Assert.assertEquals( null, msg_res[0]); - } - } - - /** - * Test Bindingtest2 with T2_CallbackFunc11 JavaCallback via MessageCallback11a() - * using the default MessageCallback11aKey class. - */ - @Test - public void chapter11a() throws Exception { - final Bindingtest2 bt2 = new Bindingtest2Impl(); - - final long userParam01Ptr = 0xAFFEBEAFC0FFEEL; - final long userParam02Ptr = 0xC0FFEEDEADBEAFL; - - final long[] id_res = { -1 }; - final T2_CallbackFunc11 myCallback01 = new T2_CallbackFunc11() { - @Override - public void callback(final long id, final T2_Callback11UserType usrParam, final long val) { - Assert.assertEquals(42, usrParam.getApiVersion()); // native toolkit should have set API version - if( 1 == id ) { - BaseClass.assertAPTR(userParam01Ptr, usrParam.getData()); - } else if( 2 == id ) { - BaseClass.assertAPTR(userParam02Ptr, usrParam.getData()); - } - final long res = val + usrParam.getI(); - id_res[0] = res; - usrParam.setR(res); - usrParam.setId(id); - System.err.println("chapter11a.myCallback01: id "+id+", val "+val); - } - }; - final T2_CallbackFunc11 myCallback02 = new T2_CallbackFunc11() { - @Override - public void callback(final long id, final T2_Callback11UserType usrParam, final long val) { - Assert.assertEquals(42, usrParam.getApiVersion()); // native toolkit should have set API version - if( 1 == id ) { - BaseClass.assertAPTR(userParam01Ptr, usrParam.getData()); - } else if( 2 == id ) { - BaseClass.assertAPTR(userParam02Ptr, usrParam.getData()); - } - final long res = val * usrParam.getI(); - id_res[0] = res; - usrParam.setR(res); - usrParam.setId(id); - System.err.println("chapter11a.myCallback02: id "+id+", val "+val); - } - }; - final int id1 = 1; - final int id2 = 2; - final int id3 = 3; - final MessageCallback11aKey id1Key = new MessageCallback11aKey(id1); - final MessageCallback11aKey id2Key = new MessageCallback11aKey(id2); - final MessageCallback11aKey id3Key = new MessageCallback11aKey(id3); - final T2_Callback11UserType userParam01 = T2_Callback11UserType.create(); // native toolkit should set API version - userParam01.setData(userParam01Ptr); - userParam01.setI(1); - final T2_Callback11UserType userParam02 = T2_Callback11UserType.create(); // native toolkit should set API version - userParam02.setData(userParam02Ptr); - userParam02.setI(2); - Assert.assertEquals( 1, userParam01.getI()); - Assert.assertEquals( 0, userParam01.getR()); - Assert.assertEquals( 0, userParam01.getId()); - Assert.assertEquals( 2, userParam02.getI()); - Assert.assertEquals( 0, userParam02.getR()); - Assert.assertEquals( 0, userParam02.getId()); - - Assert.assertEquals(false, bt2.isMessageCallback11aMapped(id1Key)); - Assert.assertEquals(false, bt2.isMessageCallback11aMapped(id2Key)); - Assert.assertEquals(false, bt2.isMessageCallback11aMapped(id3Key)); - Assert.assertEquals(0, bt2.getMessageCallback11aKeys().size()); - - // 1st mapping: buffer1 -> myCallback01, userParam01 - bt2.MessageCallback11a(id1, myCallback01, userParam01); - Assert.assertEquals(true, bt2.isMessageCallback11aMapped(id1Key)); - Assert.assertEquals(false, bt2.isMessageCallback11aMapped(id2Key)); - Assert.assertEquals(false, bt2.isMessageCallback11aMapped(id3Key)); - Assert.assertEquals(userParam01, bt2.getMessageCallback11aUserParam(id1Key)); - Assert.assertEquals(null, bt2.getMessageCallback11aUserParam(id2Key)); - Assert.assertEquals(null, bt2.getMessageCallback11aUserParam(id3Key)); - Assert.assertEquals(myCallback01, bt2.getMessageCallback11a(id1Key)); - Assert.assertEquals(null, bt2.getMessageCallback11a(id2Key)); - Assert.assertEquals(null, bt2.getMessageCallback11a(id3Key)); - Assert.assertEquals(1, bt2.getMessageCallback11aKeys().size()); - { - final Set keys = bt2.getMessageCallback11aKeys(); - Assert.assertEquals(true, keys.contains(id1Key)); - Assert.assertEquals(false, keys.contains(id2Key)); - Assert.assertEquals(false, keys.contains(id3Key)); - } - - // 2nd mapping: buffer2 -> myCallback02, userParam02 - bt2.MessageCallback11a(id2, myCallback02, userParam02); - Assert.assertEquals(true, bt2.isMessageCallback11aMapped(id1Key)); - Assert.assertEquals(true, bt2.isMessageCallback11aMapped(id2Key)); - Assert.assertEquals(false, bt2.isMessageCallback11aMapped(id3Key)); - Assert.assertEquals(userParam01, bt2.getMessageCallback11aUserParam(id1Key)); - Assert.assertEquals(userParam02, bt2.getMessageCallback11aUserParam(id2Key)); - Assert.assertEquals(null, bt2.getMessageCallback11aUserParam(id3Key)); - Assert.assertEquals(myCallback01, bt2.getMessageCallback11a(id1Key)); - Assert.assertEquals(myCallback02, bt2.getMessageCallback11a(id2Key)); - Assert.assertEquals(null, bt2.getMessageCallback11a(id3Key)); - Assert.assertEquals(2, bt2.getMessageCallback11aKeys().size()); - { - final Set keys = bt2.getMessageCallback11aKeys(); - Assert.assertEquals(true, keys.contains(id1Key)); - Assert.assertEquals(true, keys.contains(id2Key)); - Assert.assertEquals(false, keys.contains(id3Key)); - } - - { - final Thread thread = new Thread(new Runnable() { - @Override - public void run() { - bt2.MessageCallback11aInject(id1, 10); // buffer1 -> myCallback01, userParam01 - } - }); - thread.start(); - thread.join(); - Assert.assertEquals( 10+1, id_res[0]); - Assert.assertEquals( 1, userParam01.getI()); - Assert.assertEquals( 10+1, userParam01.getR()); - Assert.assertEquals( 1, userParam01.getId()); - Assert.assertEquals( 2, userParam02.getI()); - Assert.assertEquals( 0, userParam02.getR()); - Assert.assertEquals( 0, userParam02.getId()); - } - { - bt2.MessageCallback11aInject(id2, 10); // buffer2 -> myCallback02, userParam02 - Assert.assertEquals( 10*2, id_res[0]); - Assert.assertEquals( 1, userParam01.getI()); - Assert.assertEquals( 10+1, userParam01.getR()); - Assert.assertEquals( 1, userParam01.getId()); - Assert.assertEquals( 2, userParam02.getI()); - Assert.assertEquals( 10*2, userParam02.getR()); - Assert.assertEquals( 2, userParam02.getId()); - } - - // Switch the callback function for buffer2 -> myCallback01, userParam02 - bt2.MessageCallback11a(id2, myCallback01, userParam02); - Assert.assertEquals(true, bt2.isMessageCallback11aMapped(id1Key)); - Assert.assertEquals(true, bt2.isMessageCallback11aMapped(id2Key)); - Assert.assertEquals(false, bt2.isMessageCallback11aMapped(id3Key)); - Assert.assertEquals(userParam01, bt2.getMessageCallback11aUserParam(id1Key)); - Assert.assertEquals(userParam02, bt2.getMessageCallback11aUserParam(id2Key)); - Assert.assertEquals(null, bt2.getMessageCallback11aUserParam(id3Key)); - Assert.assertEquals(myCallback01, bt2.getMessageCallback11a(id1Key)); - Assert.assertEquals(myCallback01, bt2.getMessageCallback11a(id2Key)); - Assert.assertEquals(null, bt2.getMessageCallback11a(id3Key)); - - { - bt2.MessageCallback11aInject(id1, 11); // buffer1 -> myCallback01, userParam01 - Assert.assertEquals( 11+1, id_res[0]); - Assert.assertEquals( 1, userParam01.getI()); - Assert.assertEquals( 11+1, userParam01.getR()); - Assert.assertEquals( 1, userParam01.getId()); - Assert.assertEquals( 2, userParam02.getI()); - Assert.assertEquals( 10*2, userParam02.getR()); - Assert.assertEquals( 2, userParam02.getId()); - } - { - final Thread thread = new Thread(new Runnable() { - @Override - public void run() { - bt2.MessageCallback11aInject(id2, 22); // buffer2 -> myCallback01, userParam02 - } - }); - thread.start(); - thread.join(); - Assert.assertEquals( 22+2, id_res[0]); - Assert.assertEquals( 1, userParam01.getI()); - Assert.assertEquals( 11+1, userParam01.getR()); - Assert.assertEquals( 1, userParam01.getId()); - Assert.assertEquals( 2, userParam02.getI()); - Assert.assertEquals( 22+2, userParam02.getR()); - Assert.assertEquals( 2, userParam02.getId()); - } - - // Just release the buffer2 callback and mapped resources - bt2.MessageCallback11a(id2, null, userParam02); // usrptr is not key, only id is key! - Assert.assertEquals(true, bt2.isMessageCallback11aMapped(id1Key)); - Assert.assertEquals(false, bt2.isMessageCallback11aMapped(id2Key)); - Assert.assertEquals(false, bt2.isMessageCallback11aMapped(id3Key)); - Assert.assertEquals(userParam01, bt2.getMessageCallback11aUserParam(id1Key)); - Assert.assertEquals(null, bt2.getMessageCallback11aUserParam(id2Key)); - Assert.assertEquals(null, bt2.getMessageCallback11aUserParam(id3Key)); - Assert.assertEquals(myCallback01, bt2.getMessageCallback11a(id1Key)); - Assert.assertEquals(null, bt2.getMessageCallback11a(id2Key)); - Assert.assertEquals(null, bt2.getMessageCallback11a(id3Key)); - Assert.assertEquals(1, bt2.getMessageCallback11aKeys().size()); - { - final Set keys = bt2.getMessageCallback11aKeys(); - Assert.assertEquals(true, keys.contains(id1Key)); - Assert.assertEquals(false, keys.contains(id2Key)); - Assert.assertEquals(false, keys.contains(id3Key)); - } - - // Just release the buffer1 callback and mapped resources - bt2.MessageCallback11a(id1, null, null); // usrptr is not key, only id is key! - Assert.assertEquals(false, bt2.isMessageCallback11aMapped(id1Key)); - Assert.assertEquals(false, bt2.isMessageCallback11aMapped(id2Key)); - Assert.assertEquals(false, bt2.isMessageCallback11aMapped(id3Key)); - Assert.assertEquals(null, bt2.getMessageCallback11aUserParam(id1Key)); - Assert.assertEquals(null, bt2.getMessageCallback11aUserParam(id2Key)); - Assert.assertEquals(null, bt2.getMessageCallback11aUserParam(id3Key)); - Assert.assertEquals(null, bt2.getMessageCallback11a(id1Key)); - Assert.assertEquals(null, bt2.getMessageCallback11a(id2Key)); - Assert.assertEquals(null, bt2.getMessageCallback11a(id3Key)); - Assert.assertEquals(0, bt2.getMessageCallback11aKeys().size()); - { - final Set keys = bt2.getMessageCallback11aKeys(); - Assert.assertEquals(false, keys.contains(id1Key)); - Assert.assertEquals(false, keys.contains(id2Key)); - Assert.assertEquals(false, keys.contains(id3Key)); - } - - { - bt2.MessageCallback11aInject(id2, 5); // unmapped, no change in data - Assert.assertEquals( 22+2, id_res[0]); - Assert.assertEquals( 1, userParam01.getI()); - Assert.assertEquals( 11+1, userParam01.getR()); - Assert.assertEquals( 1, userParam01.getId()); - Assert.assertEquals( 2, userParam02.getI()); - Assert.assertEquals( 22+2, userParam02.getR()); - Assert.assertEquals( 2, userParam02.getId()); - } - } - - /** - * Test Bindingtest2 with T2_CallbackFunc11 JavaCallback via MessageCallback11b() - * using the default MessageCallback11bKey class. - */ - @Test - public void chapter11b() throws Exception { - final Bindingtest2 bt2 = new Bindingtest2Impl(); - - final long userParam01Ptr = 0xAFFEBEAFC0FFEEL; - final long userParam02Ptr = 0xC0FFEEDEADBEAFL; - - final long[] id_res = { -1 }; - final T2_CallbackFunc11 myCallback01 = new T2_CallbackFunc11() { - @Override - public void callback(final long id, final T2_Callback11UserType usrParam, final long val) { - Assert.assertEquals(42, usrParam.getApiVersion()); // native toolkit should have set API version - if( 1 == id ) { - BaseClass.assertAPTR(userParam01Ptr, usrParam.getData()); - } else if( 2 == id ) { - BaseClass.assertAPTR(userParam02Ptr, usrParam.getData()); - } - final long res = val + id; - id_res[0] = res; - usrParam.setR(res); - usrParam.setId(id); - System.err.println("chapter11b.myCallback01: id "+id+", val "+val); - } - }; - final T2_CallbackFunc11 myCallback02 = new T2_CallbackFunc11() { - @Override - public void callback(final long id, final T2_Callback11UserType usrParam, final long val) { - Assert.assertEquals(42, usrParam.getApiVersion()); // native toolkit should have set API version - if( 1 == id ) { - BaseClass.assertAPTR(userParam01Ptr, usrParam.getData()); - } else if( 2 == id ) { - BaseClass.assertAPTR(userParam02Ptr, usrParam.getData()); - } - final long res = val * id; - id_res[0] = res; - usrParam.setR(res); - usrParam.setId(id); - System.err.println("chapter11b.myCallback02: id "+id+", val "+val); - } - }; - final int id1 = 1; - final int id2 = 2; - final int id3 = 3; - final MessageCallback11bKey id1Key = new MessageCallback11bKey(id1); - final MessageCallback11bKey id2Key = new MessageCallback11bKey(id2); - final MessageCallback11bKey id3Key = new MessageCallback11bKey(id3); - - Assert.assertEquals(false, bt2.isMessageCallback11bMapped(id1Key)); - Assert.assertEquals(false, bt2.isMessageCallback11bMapped(id2Key)); - Assert.assertEquals(false, bt2.isMessageCallback11bMapped(id3Key)); - Assert.assertEquals(0, bt2.getMessageCallback11bKeys().size()); - - // 1st mapping: buffer1 -> myCallback01, userParam01Ptr - bt2.MessageCallback11b(id1, myCallback01, userParam01Ptr); - Assert.assertEquals(true, bt2.isMessageCallback11bMapped(id1Key)); - Assert.assertEquals(false, bt2.isMessageCallback11bMapped(id2Key)); - Assert.assertEquals(false, bt2.isMessageCallback11bMapped(id3Key)); - Assert.assertEquals(null, bt2.getMessageCallback11bUserParam(id2Key)); - Assert.assertEquals(null, bt2.getMessageCallback11bUserParam(id3Key)); - Assert.assertEquals(myCallback01, bt2.getMessageCallback11b(id1Key)); - Assert.assertEquals(null, bt2.getMessageCallback11b(id2Key)); - Assert.assertEquals(null, bt2.getMessageCallback11b(id3Key)); - Assert.assertEquals(1, bt2.getMessageCallback11bKeys().size()); - { - final Set keys = bt2.getMessageCallback11bKeys(); - Assert.assertEquals(true, keys.contains(id1Key)); - Assert.assertEquals(false, keys.contains(id2Key)); - Assert.assertEquals(false, keys.contains(id3Key)); - } - - // 2nd mapping: buffer2 -> myCallback02, userParam02Ptr - bt2.MessageCallback11b(id2, myCallback02, userParam02Ptr); - Assert.assertEquals(true, bt2.isMessageCallback11bMapped(id1Key)); - Assert.assertEquals(true, bt2.isMessageCallback11bMapped(id2Key)); - Assert.assertEquals(false, bt2.isMessageCallback11bMapped(id3Key)); - Assert.assertEquals(null, bt2.getMessageCallback11bUserParam(id3Key)); - Assert.assertEquals(null, bt2.getMessageCallback11b(id3Key)); - Assert.assertEquals(2, bt2.getMessageCallback11bKeys().size()); - { - final Set keys = bt2.getMessageCallback11bKeys(); - Assert.assertEquals(true, keys.contains(id1Key)); - Assert.assertEquals(true, keys.contains(id2Key)); - Assert.assertEquals(false, keys.contains(id3Key)); - } - - { - final Thread thread = new Thread(new Runnable() { - @Override - public void run() { - bt2.MessageCallback11bInject(id1, 10); // buffer1 -> myCallback01, userParam01 - } - }); - thread.start(); - thread.join(); - Assert.assertEquals( 10+1, id_res[0]); - } - { - bt2.MessageCallback11bInject(id2, 10); // buffer2 -> myCallback02, userParam02 - Assert.assertEquals( 10*2, id_res[0]); - } - - // Switch the callback function for buffer2 -> myCallback01, userParam02Ptr - bt2.MessageCallback11b(id2, myCallback01, userParam02Ptr); - Assert.assertEquals(true, bt2.isMessageCallback11bMapped(id1Key)); - Assert.assertEquals(true, bt2.isMessageCallback11bMapped(id2Key)); - Assert.assertEquals(false, bt2.isMessageCallback11bMapped(id3Key)); - Assert.assertEquals(null, bt2.getMessageCallback11bUserParam(id3Key)); - Assert.assertEquals(myCallback01, bt2.getMessageCallback11b(id1Key)); - Assert.assertEquals(myCallback01, bt2.getMessageCallback11b(id2Key)); - Assert.assertEquals(null, bt2.getMessageCallback11b(id3Key)); - - { - bt2.MessageCallback11bInject(id1, 11); // buffer1 -> myCallback01, userParam01 - Assert.assertEquals( 11+1, id_res[0]); - } - { - final Thread thread = new Thread(new Runnable() { - @Override - public void run() { - bt2.MessageCallback11bInject(id2, 22); // buffer2 -> myCallback01, userParam02 - } - }); - thread.start(); - thread.join(); - Assert.assertEquals( 22+2, id_res[0]); - } - - // Just release the buffer2 callback and mapped resources - bt2.MessageCallback11b(id2, null, userParam02Ptr); // usrptr is not key, only id is key! - Assert.assertEquals(true, bt2.isMessageCallback11bMapped(id1Key)); - Assert.assertEquals(false, bt2.isMessageCallback11bMapped(id2Key)); - Assert.assertEquals(false, bt2.isMessageCallback11bMapped(id3Key)); - Assert.assertEquals(null, bt2.getMessageCallback11bUserParam(id2Key)); - Assert.assertEquals(null, bt2.getMessageCallback11bUserParam(id3Key)); - Assert.assertEquals(myCallback01, bt2.getMessageCallback11b(id1Key)); - Assert.assertEquals(null, bt2.getMessageCallback11b(id2Key)); - Assert.assertEquals(null, bt2.getMessageCallback11b(id3Key)); - Assert.assertEquals(1, bt2.getMessageCallback11bKeys().size()); - { - final Set keys = bt2.getMessageCallback11bKeys(); - Assert.assertEquals(true, keys.contains(id1Key)); - Assert.assertEquals(false, keys.contains(id2Key)); - Assert.assertEquals(false, keys.contains(id3Key)); - } - - // Just release the buffer1 callback and mapped resources - bt2.MessageCallback11b(id1, null, 0); // usrptr is not key, only id is key! - Assert.assertEquals(false, bt2.isMessageCallback11bMapped(id1Key)); - Assert.assertEquals(false, bt2.isMessageCallback11bMapped(id2Key)); - Assert.assertEquals(false, bt2.isMessageCallback11bMapped(id3Key)); - Assert.assertEquals(null, bt2.getMessageCallback11bUserParam(id1Key)); - Assert.assertEquals(null, bt2.getMessageCallback11bUserParam(id2Key)); - Assert.assertEquals(null, bt2.getMessageCallback11bUserParam(id3Key)); - Assert.assertEquals(null, bt2.getMessageCallback11b(id1Key)); - Assert.assertEquals(null, bt2.getMessageCallback11b(id2Key)); - Assert.assertEquals(null, bt2.getMessageCallback11b(id3Key)); - Assert.assertEquals(0, bt2.getMessageCallback11bKeys().size()); - { - final Set keys = bt2.getMessageCallback11bKeys(); - Assert.assertEquals(false, keys.contains(id1Key)); - Assert.assertEquals(false, keys.contains(id2Key)); - Assert.assertEquals(false, keys.contains(id3Key)); - } - - { - bt2.MessageCallback11bInject(id2, 5); // unmapped, no change in data - Assert.assertEquals( 22+2, id_res[0]); - } - } - - public static class CustomMessageCallback11Key { - public CustomMessageCallback11Key() { - } - @Override - public boolean equals(final Object o) { - if( this == o ) { - return true; - } - if( !(o instanceof CustomAlBufferCallback1Key) ) { - return false; - } - final CustomAlBufferCallback1Key o2 = (CustomAlBufferCallback1Key)o; - return true; - } - @Override - public int hashCode() { - return 0; - } - @Override - public String toString() { - return "CustomMessageCallback11Key[this "+toHexString(System.identityHashCode(this))+"]"; - } - } - - /** - * Test Bindingtest2 with T2_CallbackFunc12a JavaCallback via SetLogCallBack12a() - */ - @Test - public void chapter12a() throws Exception { - final Bindingtest2 bt2 = new Bindingtest2Impl(); - - final AtomicReference messageExpected = new AtomicReference<>(null); - final AtomicReference messageReturned = new AtomicReference<>(null); - final T2_CallbackFunc12a logCallBack = new T2_CallbackFunc12a() { - @Override - public void callback(final T2_Callback12LogMessage usrParam) { - assertEquals(messageExpected.get(), usrParam); // compare value, not object hash value (reference) - messageReturned.set("Result-"+usrParam.getMessage()); - } - }; - - bt2.SetLogCallBack12a(logCallBack); - messageReturned.set(null); - { - final T2_Callback12LogMessage logMessage = T2_Callback12LogMessage.create(); - logMessage.setCategory("TEST"); - logMessage.setMessage("Example"); - logMessage.setLevel(Bindingtest2.LOG_Info); - messageExpected.set(logMessage); - - bt2.LogCallBack12aInject(logMessage); - Assert.assertEquals(messageReturned.get(), "Result-Example"); - } - - messageReturned.set(null); - { - final T2_Callback12LogMessage logMessage = T2_Callback12LogMessage.create(); - logMessage.setCategory("IDK"); - logMessage.setMessage("John Doe is absent."); - logMessage.setLevel(Bindingtest2.LOG_Warning); - messageExpected.set(logMessage); - - bt2.LogCallBack12aInject(logMessage); - Assert.assertEquals(messageReturned.get(), "Result-John Doe is absent."); - } - - bt2.SetLogCallBack12a(null); - messageReturned.set(null); - { - final T2_Callback12LogMessage logMessage = T2_Callback12LogMessage.create(); - logMessage.setCategory("SANITY"); - logMessage.setMessage("Callback is now unset"); - logMessage.setLevel(Bindingtest2.LOG_Fatal); - messageExpected.set(logMessage); - - bt2.LogCallBack12aInject(logMessage); - Assert.assertEquals(messageReturned.get(), null); - } - } - /** - * Test Bindingtest2 with T2_CallbackFunc12a JavaCallback via SetLogCallBack12a() - */ - @Test - public void chapter12b() throws Exception { - final Bindingtest2 bt2 = new Bindingtest2Impl(); - - final AtomicReference expMessage = new AtomicReference<>(null); - final AtomicReference hasReturnedMsg = new AtomicReference<>(null); - final T2_CallbackFunc12b logCallBack = new T2_CallbackFunc12b() { - int expParam0 = 1; - @Override - public void callback(final int param0, final T2_Callback12LogMessage usrParam) { - assertEquals(expMessage.get(), usrParam); // compare value, not object hash value (reference) - hasReturnedMsg.set("Result-"+usrParam.getMessage()); - Assert.assertEquals(expParam0++, param0); - } - }; - - bt2.SetLogCallBack12b(logCallBack); - hasReturnedMsg.set(null); - { - final T2_Callback12LogMessage logMessage = T2_Callback12LogMessage.create(); - logMessage.setCategory("TEST"); - logMessage.setMessage("Example"); - logMessage.setLevel(Bindingtest2.LOG_Info); - expMessage.set(logMessage); - - bt2.LogCallBack12bInject(logMessage, 1); - Assert.assertEquals("Result-Example", hasReturnedMsg.get()); - } - - hasReturnedMsg.set(null); - { - final T2_Callback12LogMessage logMessage = T2_Callback12LogMessage.create(); - logMessage.setCategory("IDK"); - logMessage.setMessage("John Doe is absent."); - logMessage.setLevel(Bindingtest2.LOG_Warning); - expMessage.set(logMessage); - - bt2.LogCallBack12bInject(logMessage, 2); - Assert.assertEquals("Result-John Doe is absent.", hasReturnedMsg.get()); - } - - bt2.SetLogCallBack12b(null); - hasReturnedMsg.set(null); - { - final T2_Callback12LogMessage logMessage = T2_Callback12LogMessage.create(); - logMessage.setCategory("SANITY"); - logMessage.setMessage("Callback is now unset"); - logMessage.setLevel(Bindingtest2.LOG_Fatal); - expMessage.set(logMessage); - - bt2.LogCallBack12bInject(logMessage, 3); - Assert.assertEquals(null, hasReturnedMsg.get()); - } - } - private static void assertEquals(final T2_Callback12LogMessage exp, final T2_Callback12LogMessage has) { - if( null == exp ) { - Assert.assertNull(has); - } else { - Assert.assertNotNull(has); - } - Assert.assertEquals(exp.getCategory(), has.getCategory()); - Assert.assertEquals(exp.getMessage(), has.getMessage()); - Assert.assertEquals(exp.getLevel(), has.getLevel()); - } - - /** - * Test Bindingtest2 with T2_CallbackFunc13 JavaCallback via MessageCallback13() - */ - @Test - public void chapter13() throws Exception { - final Bindingtest2 bt2 = new Bindingtest2Impl(); - - // - // Key 1 - // - final AtomicReference hasReturnedMsgKey1 = new AtomicReference<>(null); - final T2_CallbackFunc13 callbackKey1 = new T2_CallbackFunc13() { - int localCallCount = 1; - - @Override - public void callback(final String msg1, final T2_Callback13UserType info, final String msg2, final T2_Callback13UserKey1 usrParamKey1, final long usrKey2) { - Assert.assertEquals(localCallCount, info.getANumber()); - final String strKey1 = String.valueOf( usrParamKey1.getKeyValue1() ); - final String strKey2 = String.valueOf( usrKey2 ); - Assert.assertEquals(strKey1, msg1); - Assert.assertEquals(strKey2, msg2); - hasReturnedMsgKey1.set("Result1-"+localCallCount+"-"+strKey1+"."+strKey2); - System.err.println("Callback: "+hasReturnedMsgKey1.get()); - localCallCount++; - } - }; - - // key 1 + 1 = ibdex 2 - final T2_Callback13UserKey1 key1a = T2_Callback13UserKey1.create(); - key1a.setKeyValue1(1); - final long key1b = 1; - final MessageCallback13Key expKey1 = new MessageCallback13Key(key1a, key1b); - bt2.MessageCallback13("a debug message - key2", callbackKey1, key1a, key1b); - - // - // Key 2 - // - final AtomicReference hasReturnedMsgKey2 = new AtomicReference<>(null); - final T2_CallbackFunc13 callbackKey2 = new T2_CallbackFunc13() { - int localCallCount = 1; - - @Override - public void callback(final String msg1, final T2_Callback13UserType info, final String msg2, final T2_Callback13UserKey1 usrParamKey2, final long usrKey2) { - Assert.assertEquals(localCallCount, info.getANumber()); - final String strKey1 = String.valueOf( usrParamKey2.getKeyValue1() ); - final String strKey2 = String.valueOf( usrKey2 ); - Assert.assertEquals(strKey1, msg1); - Assert.assertEquals(strKey2, msg2); - hasReturnedMsgKey2.set("Result2-"+localCallCount+"-"+strKey1+"."+strKey2); - System.err.println("Callback: "+hasReturnedMsgKey2.get()); - localCallCount++; - } - }; - - // key 2 + 2 = ibdex 4 - final T2_Callback13UserKey1 key2a = T2_Callback13UserKey1.create(); - key2a.setKeyValue1(2); - final long key2b = 2; - final MessageCallback13Key expKey2 = new MessageCallback13Key(key2a, key2b); - bt2.MessageCallback13("a debug message - key2", callbackKey2, key2a, key2b); - - // Check keys - final Set keys = bt2.getMessageCallback13Keys(); - Assert.assertNotNull(keys); - Assert.assertEquals(2, keys.size()); - { - System.err.println("XXX expKey1[key1a 0x"+Long.toHexString(key1a.getDirectBufferAddress())+", hash 0x"+Integer.toHexString(System.identityHashCode(key1a))+"]"); - System.err.println("XXX expKey1 hash 0x"+Integer.toHexString(expKey1.hashCode())); - - System.err.println("XXX expKey2[key1a 0x"+Long.toHexString(key2a.getDirectBufferAddress())+", hash 0x"+Integer.toHexString(System.identityHashCode(key2a))+"]"); - System.err.println("XXX expKey2 hash 0x"+Integer.toHexString(expKey2.hashCode())); - - final MessageCallback13Key[] keys0 = keys.toArray(new MessageCallback13Key[2]); - Assert.assertEquals(2, keys0.length); - final MessageCallback13Key hasKey1, hasKey2; - if( 1 == keys0[0].usrKey2 ) { - // keys0[0] -> hasKey1, keys0[1] -> hasKey2 - hasKey1 = keys0[0]; - hasKey2 = keys0[1]; - } else { - // keys0[0] -> hasKey2, keys0[1] -> hasKey1 - hasKey1 = keys0[1]; - hasKey2 = keys0[0]; - } - System.err.println("XXX hasKey1 hash 0x"+Integer.toHexString(hasKey1.hashCode())); - System.err.println("XXX hasKey2 hash 0x"+Integer.toHexString(hasKey2.hashCode())); - - Assert.assertEquals(key1a.getDirectBufferAddress(), hasKey1.usrParamKey1.getDirectBufferAddress()); - Assert.assertEquals(key1b, hasKey1.usrKey2); - Assert.assertEquals(expKey1, hasKey1); - Assert.assertEquals(expKey1.hashCode(), hasKey1.hashCode()); - final T2_CallbackFunc13 cb1 = bt2.getMessageCallback13(hasKey1); - Assert.assertNotNull(cb1); - final T2_Callback13UserKey1 up1 = bt2.getMessageCallback13UserParam(hasKey1); - Assert.assertNotNull(up1); - - Assert.assertEquals(key2a.getDirectBufferAddress(), hasKey2.usrParamKey1.getDirectBufferAddress()); - Assert.assertEquals(key2b, hasKey2.usrKey2); - Assert.assertEquals(expKey2, hasKey2); - Assert.assertEquals(expKey2.hashCode(), hasKey2.hashCode()); - final T2_CallbackFunc13 cb2 = bt2.getMessageCallback13(hasKey2); - Assert.assertNotNull(cb2); - final T2_Callback13UserKey1 up2 = bt2.getMessageCallback13UserParam(hasKey2); - Assert.assertNotNull(up2); - } - - // Send -> Key1 - int key1CallCount = 1; - final T2_Callback13UserType info1 = T2_Callback13UserType.create(); - hasReturnedMsgKey1.set(null); - { - final String expReturnedMsg = "Result1-"+key1CallCount+"-"+String.valueOf(key1a.getKeyValue1())+"."+String.valueOf(key1b); - info1.setANumber(key1CallCount); - bt2.InjectMessageCallback13(String.valueOf(key1a.getKeyValue1()), info1, String.valueOf(key1b), key1a, key1b); - Assert.assertEquals(expReturnedMsg, hasReturnedMsgKey1.get()); - key1CallCount++; - } - // Send -> Key2 - int key2CallCount = 1; - final T2_Callback13UserType info2 = T2_Callback13UserType.create(); - hasReturnedMsgKey2.set(null); - { - final String expReturnedMsg = "Result2-"+key2CallCount+"-"+String.valueOf(key2a.getKeyValue1())+"."+String.valueOf(key2b); - info2.setANumber(key2CallCount); - bt2.InjectMessageCallback13(String.valueOf(key2a.getKeyValue1()), info2, String.valueOf(key2b), key2a, key2b); - Assert.assertEquals(expReturnedMsg, hasReturnedMsgKey2.get()); - key2CallCount++; - } - - // Send -> Key1 - hasReturnedMsgKey1.set(null); - { - final String expReturnedMsg = "Result1-"+key1CallCount+"-"+String.valueOf(key1a.getKeyValue1())+"."+String.valueOf(key1b); - info1.setANumber(key1CallCount); - bt2.InjectMessageCallback13(String.valueOf(key1a.getKeyValue1()), info1, String.valueOf(key1b), key1a, key1b); - Assert.assertEquals(expReturnedMsg, hasReturnedMsgKey1.get()); - key1CallCount++; - } - // Send -> Key2 - hasReturnedMsgKey2.set(null); - { - final String expReturnedMsg = "Result2-"+key2CallCount+"-"+String.valueOf(key2a.getKeyValue1())+"."+String.valueOf(key2b); - info2.setANumber(key2CallCount); - bt2.InjectMessageCallback13(String.valueOf(key2a.getKeyValue1()), info2, String.valueOf(key2b), key2a, key2b); - Assert.assertEquals(expReturnedMsg, hasReturnedMsgKey2.get()); - key2CallCount++; - } - - // Send -> Key1 -> nil - bt2.MessageCallback13("turned off - key1", null, key1a, key1b); - hasReturnedMsgKey1.set(null); - { - final String expReturnedMsg = null; - info1.setANumber(key1CallCount); - bt2.InjectMessageCallback13(String.valueOf(key1a.getKeyValue1()), info1, String.valueOf(key1b), key1a, key1b); - Assert.assertEquals(expReturnedMsg, hasReturnedMsgKey1.get()); - } - // Send -> Key2 -> nil - bt2.MessageCallback13("turned off - key1", null, key2a, key2b); - hasReturnedMsgKey2.set(null); - { - final String expReturnedMsg = null; - info2.setANumber(key2CallCount); - bt2.InjectMessageCallback13(String.valueOf(key2a.getKeyValue1()), info2, String.valueOf(key2b), key2a, key2b); - Assert.assertEquals(expReturnedMsg, hasReturnedMsgKey2.get()); - } - } - - static private String toHexString(final int v) { return "0x"+Integer.toHexString(v); } - - public static void main(final String args[]) throws IOException { - final String tstname = Test4JavaCallback.class.getName(); - org.junit.runner.JUnitCore.main(tstname); - } -} diff --git a/src/junit/com/jogamp/gluegen/test/junit/generation/Test4p1JavaCallback.java b/src/junit/com/jogamp/gluegen/test/junit/generation/Test4p1JavaCallback.java new file mode 100644 index 0000000..b07faa7 --- /dev/null +++ b/src/junit/com/jogamp/gluegen/test/junit/generation/Test4p1JavaCallback.java @@ -0,0 +1,125 @@ +/** + * Copyright 2023 JogAmp Community. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are + * permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are those of the + * authors and should not be interpreted as representing official policies, either expressed + * or implied, of JogAmp Community. + */ + +package com.jogamp.gluegen.test.junit.generation; + +import com.jogamp.common.os.NativeLibrary; +import com.jogamp.gluegen.test.junit.generation.impl.Bindingtest2p1Impl; +import org.junit.*; +import org.junit.runners.MethodSorters; + +import java.io.IOException; + +/** + * Test {@link Bindingtest2p1} with {@link T2_PointerStorage} instance and pointer pointer.. + */ +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +public class Test4p1JavaCallback extends BaseTest4JavaCallback { + + static NativeLibrary dynamicLookupHelper; + + /** + * Verifies loading of the new library. + */ + @BeforeClass + public static void chapter__TestLoadLibrary() throws Exception { + BindingJNILibLoader.loadBindingtest2p1(); + dynamicLookupHelper = NativeLibrary.open("test2", false, false, Test4p1JavaCallback.class.getClassLoader(), true); + Assert.assertNotNull("NativeLibrary.open(test2) failed", dynamicLookupHelper); + } + + /** + * Verifies unloading of the new library. + */ + @AfterClass + public static void chapter0XTestUnloadLibrary() throws Exception { + Assert.assertNotNull(dynamicLookupHelper); + dynamicLookupHelper.close(); + dynamicLookupHelper = null; + } + + @Test + public void chapter01() throws Exception { + chapter01(new Bindingtest2p1Impl()); + } + + @Test + public void chapter02() throws Exception { + chapter02(new Bindingtest2p1Impl()); + } + + @Test + public void chapter03() throws Exception { + chapter03(new Bindingtest2p1Impl()); + } + + @Test + public void chapter04() throws Exception { + chapter04(new Bindingtest2p1Impl()); + } + + @Test + public void chapter05a() throws Exception { + chapter05a(new Bindingtest2p1Impl()); + } + + @Test + public void chapter05b() throws Exception { + chapter05b(new Bindingtest2p1Impl()); + } + + @Test + public void chapter11a() throws Exception { + chapter11a(new Bindingtest2p1Impl()); + } + + @Test + public void chapter11b() throws Exception { + chapter11b(new Bindingtest2p1Impl()); + } + + @Test + public void chapter12a() throws Exception { + chapter12a(new Bindingtest2p1Impl()); + } + + @Test + public void chapter12b() throws Exception { + chapter12b(new Bindingtest2p1Impl()); + } + + @Test + public void chapter13() throws Exception { + chapter13(new Bindingtest2p1Impl()); + } + + public static void main(final String args[]) throws IOException { + final String tstname = Test4p1JavaCallback.class.getName(); + org.junit.runner.JUnitCore.main(tstname); + } +} diff --git a/src/junit/com/jogamp/gluegen/test/junit/generation/Test4p2JavaCallback.java b/src/junit/com/jogamp/gluegen/test/junit/generation/Test4p2JavaCallback.java new file mode 100644 index 0000000..3f94565 --- /dev/null +++ b/src/junit/com/jogamp/gluegen/test/junit/generation/Test4p2JavaCallback.java @@ -0,0 +1,127 @@ +/** + * Copyright 2023 JogAmp Community. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are + * permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are those of the + * authors and should not be interpreted as representing official policies, either expressed + * or implied, of JogAmp Community. + */ + +package com.jogamp.gluegen.test.junit.generation; + +import com.jogamp.common.os.NativeLibrary; +import com.jogamp.gluegen.test.junit.generation.impl.Bindingtest2p2Impl; +import org.junit.*; +import org.junit.runners.MethodSorters; + +import java.io.IOException; + +/** + * Test {@link Bindingtest2p2} with {@link T2_PointerStorage} instance and pointer pointer.. + */ +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +public class Test4p2JavaCallback extends BaseTest4JavaCallback { + + static NativeLibrary dynamicLookupHelper; + + /** + * Verifies loading of the new library. + */ + @BeforeClass + public static void chapter__TestLoadLibrary() throws Exception { + BindingJNILibLoader.loadBindingtest2p1(); + dynamicLookupHelper = NativeLibrary.open("test2", false, false, Test4p2JavaCallback.class.getClassLoader(), true); + Assert.assertNotNull("NativeLibrary.open(test2) failed", dynamicLookupHelper); + + Bindingtest2p2Impl.resetProcAddressTable(dynamicLookupHelper); + } + + /** + * Verifies unloading of the new library. + */ + @AfterClass + public static void chapter0XTestUnloadLibrary() throws Exception { + Assert.assertNotNull(dynamicLookupHelper); + dynamicLookupHelper.close(); + dynamicLookupHelper = null; + } + + @Test + public void chapter01() throws Exception { + chapter01(new Bindingtest2p2Impl()); + } + + @Test + public void chapter02() throws Exception { + chapter02(new Bindingtest2p2Impl()); + } + + @Test + public void chapter03() throws Exception { + chapter03(new Bindingtest2p2Impl()); + } + + @Test + public void chapter04() throws Exception { + chapter04(new Bindingtest2p2Impl()); + } + + @Test + public void chapter05a() throws Exception { + chapter05a(new Bindingtest2p2Impl()); + } + + @Test + public void chapter05b() throws Exception { + chapter05b(new Bindingtest2p2Impl()); + } + + @Test + public void chapter11a() throws Exception { + chapter11a(new Bindingtest2p2Impl()); + } + + @Test + public void chapter11b() throws Exception { + chapter11b(new Bindingtest2p2Impl()); + } + + @Test + public void chapter12a() throws Exception { + chapter12a(new Bindingtest2p2Impl()); + } + + @Test + public void chapter12b() throws Exception { + chapter12b(new Bindingtest2p2Impl()); + } + + @Test + public void chapter13() throws Exception { + chapter13(new Bindingtest2p2Impl()); + } + + public static void main(final String args[]) throws IOException { + final String tstname = Test4p2JavaCallback.class.getName(); + org.junit.runner.JUnitCore.main(tstname); + } +} diff --git a/src/junit/com/jogamp/gluegen/test/junit/generation/test2-common.cfg b/src/junit/com/jogamp/gluegen/test/junit/generation/test2-common.cfg new file mode 100644 index 0000000..d187577 --- /dev/null +++ b/src/junit/com/jogamp/gluegen/test/junit/generation/test2-common.cfg @@ -0,0 +1,210 @@ +# Opaque long void* + +# Undefined struct forward declaration, implementation secret: 'struct T2_UndefStruct;' +Opaque long T2_UndefStruct* +Ignore T2_UndefStruct + +EmitStruct T2_ThreadAffinity +StructPackage T2_ThreadAffinity com.jogamp.gluegen.test.junit.generation + +EmitStruct T2_UserData +StructPackage T2_UserData com.jogamp.gluegen.test.junit.generation +ReturnsStringOnly T2_UserData.name + +EmitStruct T2_PointerStorage +StructPackage T2_PointerStorage com.jogamp.gluegen.test.junit.generation + +EmitStruct T2_InitializeOptions +StructPackage T2_InitializeOptions com.jogamp.gluegen.test.junit.generation +ReturnsStringOnly T2_InitializeOptions.ProductName +ReturnsStringOnly T2_InitializeOptions.ProductVersion + +# ReturnedArrayLength T2_InitializeOptions.OverrideThreadAffinity 1 +MaxOneElement T2_InitializeOptions.OverrideThreadAffinity + +# Begin JavaCallback. +# +# JavaCallback requires `JNI_OnLoad*(..)` and `JVMUtil_GetJNIEnv(..)` +LibraryOnLoad Bindingtest2 + +# typedef void ( * T2_CallbackFunc01)(size_t id, const char* msg, void* usrParam); +# void MessageCallback01(T2_CallbackFunc01 cbFunc, void* usrParam); +# void InjectMessageCallback01(size_t id, const char* msg); +ArgumentIsString T2_CallbackFunc01 1 +ArgumentIsString InjectMessageCallback01 1 + +# Define a JavaCallback. +# Set JavaCallback via function `MessageCallback01` if `T2_CallbackFunc01` argument is non-null, otherwise removes the mapped callback and associated resources. +# +# It uses the function-pointer argument `T2_CallbackFunc01` as the callback function type +# and marks `T2_CallbackFunc01`s 3rd argument (index 2) as the mandatory user-param. +# +# This callback has no keys defines, rendering it of global scope! +# +# Explicit maintenance methods are generated, passing the keys as paramters +# - `boolean isMessageCallback01Mapped()` queries whether `MessageCallback0` is mapped globally +# - `T2_CallbackFunc01 getMessageCallback01()` returns the global T2_CallbackFunc01, null if not mapped +# - `Object getMessageCallback01UserParam()` returns the global `usrParam` object, null if not mapped +# - `void releaseMessageCallback01()` releases callback data skipping toolkit API. Favor passing `null` callback ref to `MessageCallback01(..)` +JavaCallbackDef MessageCallback01 1 T2_CallbackFunc01 2 +# +# End JavaCallback + +# Begin JavaCallback (OpanAL AL_SOFT_callback_buffer) +# +# // typedef void ( * ALBUFFERCALLBACKTYPESOFT)(int buffer /* key */, void *userptr, void *sampledata, int numbytes); +# typedef void ( * ALBUFFERCALLBACKTYPESOFT)(int buffer /* key */, void *userptr, int sampledata, int numbytes); +# +# void alBufferCallback0(int buffer /* key */, int format, int freq, ALBUFFERCALLBACKTYPESOFT callback, void *userptr); +# +# // void alBufferCallback0Inject(int buffer, void *sampledata, int numbytes); +# void alBufferCallback0Inject(int buffer, int sampledata, int numbytes); + +# Define a JavaCallback. +# Set JavaCallback via function `alBufferCallback0` if `ALBUFFERCALLBACKTYPESOFT` argument is non-null, otherwise removes the mapped callback and associated resources. +# +# It uses the function-pointer argument `ALBUFFERCALLBACKTYPESOFT` as the callback function type +# and marks `ALBUFFERCALLBACKTYPESOFT`s 2nd argument (index 1) as the mandatory user-param. +# +# This callback defines one key, `buffer`, index 0 of alBufferCallback0(..) parameter list, limiting it to buffer-name scope! +# The `buffer` key allows setting one callback per buffer-name, compatible with the `AL_SOFT_callback_buffer` spec. +# +# Explicit maintenance methods are generated, passing the keys as paramters +# - `Set getAlBufferCallback0Keys()` returns set of Key { int buffer } +# - `boolean isAlBufferCallback0Mapped(AlBufferCallback0Key)` queries whether `alBufferCallback0` is mapped to `buffer`. +# - `ALBUFFERCALLBACKTYPESOFT getAlBufferCallback0(AlBufferCallback0Key)` returns the `buffer` mapped ALEVENTPROCSOFT, null if not mapped +# - `ALCcontext getAlBufferCallback0UserParam(AlBufferCallback0Key)` returns the `buffer` mapped `userptr` object, null if not mapped +# - `void releaseAllAlBufferCallback0()` releases all callback data mapped via Key { int buffer } skipping toolkit API. Favor passing `null` callback ref to `alBufferCallback0(..)` +# - `void releaseAlBufferCallback0(AlBufferCallback0Key)` releases callback data mapped to Key { int buffer } skipping toolkit API. Favor passing `null` callback ref to `alBufferCallback0(..)` +JavaCallbackDef alBufferCallback0 4 ALBUFFERCALLBACKTYPESOFT 1 ALCcontext +JavaCallbackKey alBufferCallback0 0 ALBUFFERCALLBACKTYPESOFT 0 +# +# End JavaCallback + +# Begin JavaCallback (OpanAL AL_SOFT_callback_buffer, variant 2) +# +# Reuses: ALBUFFERCALLBACKTYPESOFT, see above. +# +# // typedef void ( * ALBUFFERCALLBACKTYPESOFT)(int buffer /* key */, void *userptr, void *sampledata, int numbytes); +# typedef void ( * ALBUFFERCALLBACKTYPESOFT)(int buffer /* key */, void *userptr, int sampledata, int numbytes); +# +# void alBufferCallback1(void* user_ptr, int buffer_key /* key */, int format, int freq, ALBUFFERCALLBACKTYPESOFT callback); +# +# // void alBufferCallback1Inject(int buffer, void *sampledata, int numbytes); +# void alBufferCallback1Inject(int buffer, int sampledata, int numbytes); + +# Define a JavaCallback. +# Set JavaCallback via function `alBufferCallback1` if `ALBUFFERCALLBACKTYPESOFT` argument is non-null, otherwise removes the mapped callback and associated resources. +# +# It uses the function-pointer argument `ALBUFFERCALLBACKTYPESOFT` as the callback function type +# and marks `ALBUFFERCALLBACKTYPESOFT`s 2nd argument (index 1) as the mandatory user-param. +# +# This callback defines one key, `buffer`, index 0 of alBufferCallback1(..) parameter list, limiting it to buffer-name scope! +# The `buffer` key allows setting one callback per buffer-name, compatible with the `AL_SOFT_callback_buffer` spec. +# +# Explicit maintenance methods are generated, passing the keys as paramters +# - `boolean isAlBufferCallback1Mapped(int buffer)` queries whether `alBufferCallback1` is mapped to `buffer`. +# - `ALBUFFERCALLBACKTYPESOFT getAlBufferCallback1(int buffer)` returns the `buffer` mapped ALEVENTPROCSOFT, null if not mapped +# - `ALCcontext getAlBufferCallback1UserParam(int buffer)` returns the `buffer` mapped `userptr` object, null if not mapped +JavaCallbackDef alBufferCallback1 0 ALBUFFERCALLBACKTYPESOFT 1 ALCcontext com.jogamp.gluegen.test.junit.generation.BaseTest4JavaCallback.CustomAlBufferCallback1Key +JavaCallbackKey alBufferCallback1 1 ALBUFFERCALLBACKTYPESOFT 0 +# +# End JavaCallback + +# Begin JavaCallback. +# +# typedef void ( * ALEVENTPROCSOFT)(int eventType, int object, int param, int length, const char *message, void *userParam /* key */); +# +# void alEventCallback0(ALEVENTPROCSOFT callback, void *userParam /* key */); +ArgumentIsPascalString ALEVENTPROCSOFT 3 4 + +# Define a JavaCallback (OpenAL AL_SOFT_events) +# Set JavaCallback via function `alEventCallback` if `ALEVENTPROCSOFT` argument is non-null, otherwise removes the mapped callback and associated resources. +# +# It uses the function-pointer argument `ALEVENTPROCSOFT` as the callback function type +# and marks `ALEVENTPROCSOFT`s 6th argument (index 5) as the mandatory user-param. +# +# This callback uses 'Object userParam' as its key (for a context), similar to `AL_SOFT_events` context binding. +JavaCallbackDef alEventCallback0 1 ALEVENTPROCSOFT 5 ALCcontext +JavaCallbackKey alEventCallback0 1 ALEVENTPROCSOFT 5 +# +# End JavaCallback + +# Begin JavaCallback. +# +# void alEventCallback1(int object /* key */, ALEVENTPROCSOFT callback, void *userParam /* key */); +JavaCallbackDef alEventCallback1 2 ALEVENTPROCSOFT 5 ALCcontext +JavaCallbackKey alEventCallback1 0 2 ALEVENTPROCSOFT 1 5 + +# +# End JavaCallback + +IncludeAs CustomJavaCode Bindingtest2 test2-CustomJavaIfCode.java.stub +IncludeAs CustomJavaCode Bindingtest2Impl test2-CustomJavaImplCode.java.stub + +# Begin JavaCallback +# +# typedef void ( * T2_CallbackFunc11)(size_t id, const T2_Callback11UserType* usrParam); +# void MessageCallback11a(size_t id /* key */, T2_CallbackFunc11 cbFunc, const T2_Callback11UserType* usrParam); +# void MessageCallback11aInject(size_t id); +#JavaCallbackDef MessageCallback11a T2_CallbackFunc11 1 Object com.jogamp.gluegen.test.junit.generation.BaseTest4JavaCallback.CustomMessageCallback11Key +JavaCallbackDef MessageCallback11a 2 T2_CallbackFunc11 1 +JavaCallbackKey MessageCallback11a 0 T2_CallbackFunc11 0 +# +# End JavaCallback + +# void MessageCallback11b(size_t id /* key */, T2_CallbackFunc11 cbFunc, void* Data); +# void MessageCallback11bInject(size_t id); +JavaCallbackDef MessageCallback11b 2 T2_CallbackFunc11 1 +JavaCallbackKey MessageCallback11b 0 T2_CallbackFunc11 0 + + +# T2_Callback12LogMessage +ReturnsStringOnly T2_Callback12LogMessage.Category +ReturnsStringOnly T2_Callback12LogMessage.Message + +# Begin JavaCallback +# +# typedef void ( * T2_CallbackFunc12a)(const T2_Callback12LogMessage* usrParam); +# void SetLogCallBack12a(T2_CallbackFunc12a cbFunc); +# void LogCallBack12aInject(const T2_Callback12LogMessage* message); +JavaCallbackDef SetLogCallBack12a -1 T2_CallbackFunc12a -1 +# +# End JavaCallback + +# Begin JavaCallback +# +# typedef void ( * T2_CallbackFunc12b)(int param0, const T2_Callback12LogMessage* usrParam1); +# void SetLogCallBack12b(T2_CallbackFunc12b cbFunc); +# void LogCallBack12bInject(const T2_Callback12LogMessage* message); +JavaCallbackDef SetLogCallBack12b -1 T2_CallbackFunc12b -1 +# +# End JavaCallback + +# Begin JavaCallback +# +# typedef void ( * T2_CallbackFunc13)(const char* msg1, const T2_Callback13UserType* info, const char* msg2, const T2_Callback13UserKey1* usrParamKey1 /* key */, size_t usrKey2 /* key */); +# void MessageCallback13(const char* debugMsg, T2_CallbackFunc13 cbFunc, const T2_Callback13UserKey1* usrParamKey1 /* key */, size_t usrKey2 /* key */); +# void InjectMessageCallback13(const char* msg1, const T2_Callback13UserType* info, const char* msg2, const T2_Callback13UserKey1* usrParamKey1 /* key */, size_t usrKey2 /* key */); + +ArgumentIsString T2_CallbackFunc13 0 2 +ArgumentIsString InjectMessageCallback13 0 2 +ArgumentIsString MessageCallback13 0 +JavaCallbackDef MessageCallback13 2 T2_CallbackFunc13 3 +JavaCallbackKey MessageCallback13 2 3 T2_CallbackFunc13 3 4 +# +# End JavaCallback + +CustomCCode #include "test2.h" + +Import com.jogamp.gluegen.test.junit.generation.Bindingtest2 +Import com.jogamp.gluegen.test.junit.generation.T2_PointerStorage +Import com.jogamp.gluegen.test.junit.generation.T2_InitializeOptions +Import com.jogamp.gluegen.test.junit.generation.T2_ThreadAffinity +Import com.jogamp.gluegen.test.junit.generation.T2_UserData +Import com.jogamp.gluegen.test.junit.generation.T2_Callback11UserType +Import com.jogamp.gluegen.test.junit.generation.T2_Callback12LogMessage +Import com.jogamp.gluegen.test.junit.generation.T2_Callback13UserType +Import com.jogamp.gluegen.test.junit.generation.T2_Callback13UserKey1 +Import com.jogamp.gluegen.test.junit.generation.BaseTest4JavaCallback.ALCcontext + diff --git a/src/junit/com/jogamp/gluegen/test/junit/generation/test2-gluegen.cfg b/src/junit/com/jogamp/gluegen/test/junit/generation/test2-gluegen.cfg new file mode 100644 index 0000000..6531a7b --- /dev/null +++ b/src/junit/com/jogamp/gluegen/test/junit/generation/test2-gluegen.cfg @@ -0,0 +1,11 @@ +Package com.jogamp.gluegen.test.junit.generation +Style AllStatic +JavaClass Bindingtest2 +Style InterfaceOnly +JavaOutputDir classes +NativeOutputDir native + +Include test2-common.cfg + + + diff --git a/src/junit/com/jogamp/gluegen/test/junit/generation/test2.cfg b/src/junit/com/jogamp/gluegen/test/junit/generation/test2.cfg deleted file mode 100644 index 44b6468..0000000 --- a/src/junit/com/jogamp/gluegen/test/junit/generation/test2.cfg +++ /dev/null @@ -1,236 +0,0 @@ -Package com.jogamp.gluegen.test.junit.generation -JavaClass Bindingtest2 -Style InterfaceAndImpl -JavaOutputDir classes -NativeOutputDir native - -# Use a ProcAddressTable so we dynamically look up the routines -EmitProcAddressTable true -ProcAddressTableClassName Bindingtest2ProcAddressTable -GetProcAddressTableExpr _table -ProcAddressNameExpr PFN $UPPERCASE({0}) PROC - -# Force all of the methods to be emitted using dynamic linking so we -# don't need to link against any emulation library on the desktop or -# depend on the presence of an import library for a particular device -ForceProcAddressGen __ALL__ - -# Also force the calling conventions of the locally generated function -# pointer typedefs for these routines to MYAPIENTRY -# LocalProcAddressCallingConvention __ALL__ MYAPIENTRY - -# Opaque long void* - -# Undefined struct forward declaration, implementation secret: 'struct T2_UndefStruct;' -Opaque long T2_UndefStruct* -Ignore T2_UndefStruct - -EmitStruct T2_ThreadAffinity -StructPackage T2_ThreadAffinity com.jogamp.gluegen.test.junit.generation - -EmitStruct T2_UserData -StructPackage T2_UserData com.jogamp.gluegen.test.junit.generation -ReturnsStringOnly T2_UserData.name - -EmitStruct T2_PointerStorage -StructPackage T2_PointerStorage com.jogamp.gluegen.test.junit.generation - -EmitStruct T2_InitializeOptions -StructPackage T2_InitializeOptions com.jogamp.gluegen.test.junit.generation -ReturnsStringOnly T2_InitializeOptions.ProductName -ReturnsStringOnly T2_InitializeOptions.ProductVersion - -# ReturnedArrayLength T2_InitializeOptions.OverrideThreadAffinity 1 -MaxOneElement T2_InitializeOptions.OverrideThreadAffinity - -# Begin JavaCallback. -# -# JavaCallback requires `JNI_OnLoad*(..)` and `JVMUtil_GetJNIEnv(..)` -LibraryOnLoad Bindingtest2 - -# typedef void ( * T2_CallbackFunc01)(size_t id, const char* msg, void* usrParam); -# void MessageCallback01(T2_CallbackFunc01 cbFunc, void* usrParam); -# void InjectMessageCallback01(size_t id, const char* msg); -ArgumentIsString T2_CallbackFunc01 1 -ArgumentIsString InjectMessageCallback01 1 - -# Define a JavaCallback. -# Set JavaCallback via function `MessageCallback01` if `T2_CallbackFunc01` argument is non-null, otherwise removes the mapped callback and associated resources. -# -# It uses the function-pointer argument `T2_CallbackFunc01` as the callback function type -# and marks `T2_CallbackFunc01`s 3rd argument (index 2) as the mandatory user-param. -# -# This callback has no keys defines, rendering it of global scope! -# -# Explicit maintenance methods are generated, passing the keys as paramters -# - `boolean isMessageCallback01Mapped()` queries whether `MessageCallback0` is mapped globally -# - `T2_CallbackFunc01 getMessageCallback01()` returns the global T2_CallbackFunc01, null if not mapped -# - `Object getMessageCallback01UserParam()` returns the global `usrParam` object, null if not mapped -# - `void releaseMessageCallback01()` releases callback data skipping toolkit API. Favor passing `null` callback ref to `MessageCallback01(..)` -JavaCallbackDef MessageCallback01 1 T2_CallbackFunc01 2 -# -# End JavaCallback - -# Begin JavaCallback (OpanAL AL_SOFT_callback_buffer) -# -# // typedef void ( * ALBUFFERCALLBACKTYPESOFT)(int buffer /* key */, void *userptr, void *sampledata, int numbytes); -# typedef void ( * ALBUFFERCALLBACKTYPESOFT)(int buffer /* key */, void *userptr, int sampledata, int numbytes); -# -# void alBufferCallback0(int buffer /* key */, int format, int freq, ALBUFFERCALLBACKTYPESOFT callback, void *userptr); -# -# // void alBufferCallback0Inject(int buffer, void *sampledata, int numbytes); -# void alBufferCallback0Inject(int buffer, int sampledata, int numbytes); - -# Define a JavaCallback. -# Set JavaCallback via function `alBufferCallback0` if `ALBUFFERCALLBACKTYPESOFT` argument is non-null, otherwise removes the mapped callback and associated resources. -# -# It uses the function-pointer argument `ALBUFFERCALLBACKTYPESOFT` as the callback function type -# and marks `ALBUFFERCALLBACKTYPESOFT`s 2nd argument (index 1) as the mandatory user-param. -# -# This callback defines one key, `buffer`, index 0 of alBufferCallback0(..) parameter list, limiting it to buffer-name scope! -# The `buffer` key allows setting one callback per buffer-name, compatible with the `AL_SOFT_callback_buffer` spec. -# -# Explicit maintenance methods are generated, passing the keys as paramters -# - `Set getAlBufferCallback0Keys()` returns set of Key { int buffer } -# - `boolean isAlBufferCallback0Mapped(AlBufferCallback0Key)` queries whether `alBufferCallback0` is mapped to `buffer`. -# - `ALBUFFERCALLBACKTYPESOFT getAlBufferCallback0(AlBufferCallback0Key)` returns the `buffer` mapped ALEVENTPROCSOFT, null if not mapped -# - `ALCcontext getAlBufferCallback0UserParam(AlBufferCallback0Key)` returns the `buffer` mapped `userptr` object, null if not mapped -# - `void releaseAllAlBufferCallback0()` releases all callback data mapped via Key { int buffer } skipping toolkit API. Favor passing `null` callback ref to `alBufferCallback0(..)` -# - `void releaseAlBufferCallback0(AlBufferCallback0Key)` releases callback data mapped to Key { int buffer } skipping toolkit API. Favor passing `null` callback ref to `alBufferCallback0(..)` -JavaCallbackDef alBufferCallback0 4 ALBUFFERCALLBACKTYPESOFT 1 ALCcontext -JavaCallbackKey alBufferCallback0 0 ALBUFFERCALLBACKTYPESOFT 0 -# -# End JavaCallback - -# Begin JavaCallback (OpanAL AL_SOFT_callback_buffer, variant 2) -# -# Reuses: ALBUFFERCALLBACKTYPESOFT, see above. -# -# // typedef void ( * ALBUFFERCALLBACKTYPESOFT)(int buffer /* key */, void *userptr, void *sampledata, int numbytes); -# typedef void ( * ALBUFFERCALLBACKTYPESOFT)(int buffer /* key */, void *userptr, int sampledata, int numbytes); -# -# void alBufferCallback1(void* user_ptr, int buffer_key /* key */, int format, int freq, ALBUFFERCALLBACKTYPESOFT callback); -# -# // void alBufferCallback1Inject(int buffer, void *sampledata, int numbytes); -# void alBufferCallback1Inject(int buffer, int sampledata, int numbytes); - -# Define a JavaCallback. -# Set JavaCallback via function `alBufferCallback1` if `ALBUFFERCALLBACKTYPESOFT` argument is non-null, otherwise removes the mapped callback and associated resources. -# -# It uses the function-pointer argument `ALBUFFERCALLBACKTYPESOFT` as the callback function type -# and marks `ALBUFFERCALLBACKTYPESOFT`s 2nd argument (index 1) as the mandatory user-param. -# -# This callback defines one key, `buffer`, index 0 of alBufferCallback1(..) parameter list, limiting it to buffer-name scope! -# The `buffer` key allows setting one callback per buffer-name, compatible with the `AL_SOFT_callback_buffer` spec. -# -# Explicit maintenance methods are generated, passing the keys as paramters -# - `boolean isAlBufferCallback1Mapped(int buffer)` queries whether `alBufferCallback1` is mapped to `buffer`. -# - `ALBUFFERCALLBACKTYPESOFT getAlBufferCallback1(int buffer)` returns the `buffer` mapped ALEVENTPROCSOFT, null if not mapped -# - `ALCcontext getAlBufferCallback1UserParam(int buffer)` returns the `buffer` mapped `userptr` object, null if not mapped -JavaCallbackDef alBufferCallback1 0 ALBUFFERCALLBACKTYPESOFT 1 ALCcontext com.jogamp.gluegen.test.junit.generation.Test4JavaCallback.CustomAlBufferCallback1Key -JavaCallbackKey alBufferCallback1 1 ALBUFFERCALLBACKTYPESOFT 0 -# -# End JavaCallback - -# Begin JavaCallback. -# -# typedef void ( * ALEVENTPROCSOFT)(int eventType, int object, int param, int length, const char *message, void *userParam /* key */); -# -# void alEventCallback0(ALEVENTPROCSOFT callback, void *userParam /* key */); -ArgumentIsPascalString ALEVENTPROCSOFT 3 4 - -# Define a JavaCallback (OpenAL AL_SOFT_events) -# Set JavaCallback via function `alEventCallback` if `ALEVENTPROCSOFT` argument is non-null, otherwise removes the mapped callback and associated resources. -# -# It uses the function-pointer argument `ALEVENTPROCSOFT` as the callback function type -# and marks `ALEVENTPROCSOFT`s 6th argument (index 5) as the mandatory user-param. -# -# This callback uses 'Object userParam' as its key (for a context), similar to `AL_SOFT_events` context binding. -JavaCallbackDef alEventCallback0 1 ALEVENTPROCSOFT 5 ALCcontext -JavaCallbackKey alEventCallback0 1 ALEVENTPROCSOFT 5 -# -# End JavaCallback - -# Begin JavaCallback. -# -# void alEventCallback1(int object /* key */, ALEVENTPROCSOFT callback, void *userParam /* key */); -JavaCallbackDef alEventCallback1 2 ALEVENTPROCSOFT 5 ALCcontext -JavaCallbackKey alEventCallback1 0 2 ALEVENTPROCSOFT 1 5 - -# -# End JavaCallback - -IncludeAs CustomJavaCode Bindingtest2 test2-CustomJavaIfCode.java.stub -IncludeAs CustomJavaCode Bindingtest2Impl test2-CustomJavaImplCode.java.stub - -# Begin JavaCallback -# -# typedef void ( * T2_CallbackFunc11)(size_t id, const T2_Callback11UserType* usrParam); -# void MessageCallback11a(size_t id /* key */, T2_CallbackFunc11 cbFunc, const T2_Callback11UserType* usrParam); -# void MessageCallback11aInject(size_t id); -#JavaCallbackDef MessageCallback11a T2_CallbackFunc11 1 Object com.jogamp.gluegen.test.junit.generation.Test4JavaCallback.CustomMessageCallback11Key -JavaCallbackDef MessageCallback11a 2 T2_CallbackFunc11 1 -JavaCallbackKey MessageCallback11a 0 T2_CallbackFunc11 0 -# -# End JavaCallback - -# void MessageCallback11b(size_t id /* key */, T2_CallbackFunc11 cbFunc, void* Data); -# void MessageCallback11bInject(size_t id); -JavaCallbackDef MessageCallback11b 2 T2_CallbackFunc11 1 -JavaCallbackKey MessageCallback11b 0 T2_CallbackFunc11 0 - - -# T2_Callback12LogMessage -ReturnsStringOnly T2_Callback12LogMessage.Category -ReturnsStringOnly T2_Callback12LogMessage.Message - -# Begin JavaCallback -# -# typedef void ( * T2_CallbackFunc12a)(const T2_Callback12LogMessage* usrParam); -# void SetLogCallBack12a(T2_CallbackFunc12a cbFunc); -# void LogCallBack12aInject(const T2_Callback12LogMessage* message); -JavaCallbackDef SetLogCallBack12a -1 T2_CallbackFunc12a -1 -# -# End JavaCallback - -# Begin JavaCallback -# -# typedef void ( * T2_CallbackFunc12b)(int param0, const T2_Callback12LogMessage* usrParam1); -# void SetLogCallBack12b(T2_CallbackFunc12b cbFunc); -# void LogCallBack12bInject(const T2_Callback12LogMessage* message); -JavaCallbackDef SetLogCallBack12b -1 T2_CallbackFunc12b -1 -# -# End JavaCallback - -# Begin JavaCallback -# -# typedef void ( * T2_CallbackFunc13)(const char* msg1, const T2_Callback13UserType* info, const char* msg2, const T2_Callback13UserKey1* usrParamKey1 /* key */, size_t usrKey2 /* key */); -# void MessageCallback13(const char* debugMsg, T2_CallbackFunc13 cbFunc, const T2_Callback13UserKey1* usrParamKey1 /* key */, size_t usrKey2 /* key */); -# void InjectMessageCallback13(const char* msg1, const T2_Callback13UserType* info, const char* msg2, const T2_Callback13UserKey1* usrParamKey1 /* key */, size_t usrKey2 /* key */); - -ArgumentIsString T2_CallbackFunc13 0 2 -ArgumentIsString InjectMessageCallback13 0 2 -ArgumentIsString MessageCallback13 0 -JavaCallbackDef MessageCallback13 2 T2_CallbackFunc13 3 -JavaCallbackKey MessageCallback13 2 3 T2_CallbackFunc13 3 4 -# -# End JavaCallback - -CustomCCode #include "test2.h" - -Import com.jogamp.gluegen.test.junit.generation.Bindingtest2 -Import com.jogamp.gluegen.test.junit.generation.T2_PointerStorage -Import com.jogamp.gluegen.test.junit.generation.T2_InitializeOptions -Import com.jogamp.gluegen.test.junit.generation.T2_ThreadAffinity -Import com.jogamp.gluegen.test.junit.generation.T2_UserData -Import com.jogamp.gluegen.test.junit.generation.T2_Callback11UserType -Import com.jogamp.gluegen.test.junit.generation.T2_Callback12LogMessage -Import com.jogamp.gluegen.test.junit.generation.T2_Callback13UserType -Import com.jogamp.gluegen.test.junit.generation.T2_Callback13UserKey1 -Import com.jogamp.gluegen.test.junit.generation.Test4JavaCallback.ALCcontext - -CustomJavaCode Bindingtest2Impl private static Bindingtest2ProcAddressTable _table = new Bindingtest2ProcAddressTable(); -CustomJavaCode Bindingtest2Impl public static void resetProcAddressTable(DynamicLookupHelper lookup) { -CustomJavaCode Bindingtest2Impl _table.reset(lookup); -CustomJavaCode Bindingtest2Impl } - diff --git a/src/junit/com/jogamp/gluegen/test/junit/generation/test2p1.cfg b/src/junit/com/jogamp/gluegen/test/junit/generation/test2p1.cfg new file mode 100644 index 0000000..58dc44e --- /dev/null +++ b/src/junit/com/jogamp/gluegen/test/junit/generation/test2p1.cfg @@ -0,0 +1,16 @@ +Package com.jogamp.gluegen.test.junit.generation +JavaClass Bindingtest2p1 +Style InterfaceAndImpl +JavaOutputDir classes +NativeOutputDir native + +Extends Bindingtest2p1 Bindingtest2 + +ExtendedInterfaceSymbolsIgnore ../build-temp/gensrc/classes/com/jogamp/gluegen/test/junit/generation/Bindingtest2.java + +Include test2-common.cfg + +Import com.jogamp.gluegen.test.junit.generation.Bindingtest2 +Import com.jogamp.gluegen.test.junit.generation.Bindingtest2p1 + + diff --git a/src/junit/com/jogamp/gluegen/test/junit/generation/test2p2.cfg b/src/junit/com/jogamp/gluegen/test/junit/generation/test2p2.cfg new file mode 100644 index 0000000..4aecb5f --- /dev/null +++ b/src/junit/com/jogamp/gluegen/test/junit/generation/test2p2.cfg @@ -0,0 +1,36 @@ +Package com.jogamp.gluegen.test.junit.generation +JavaClass Bindingtest2p2 +Style InterfaceAndImpl +JavaOutputDir classes +NativeOutputDir native + +Extends Bindingtest2p2 Bindingtest2 + +ExtendedInterfaceSymbolsIgnore ../build-temp/gensrc/classes/com/jogamp/gluegen/test/junit/generation/Bindingtest2.java + +# Use a ProcAddressTable so we dynamically look up the routines +EmitProcAddressTable true +ProcAddressTableClassName Bindingtest2p2ProcAddressTable +GetProcAddressTableExpr _table +ProcAddressNameExpr PFN $UPPERCASE({0}) PROC + +# Force all of the methods to be emitted using dynamic linking so we +# don't need to link against any emulation library on the desktop or +# depend on the presence of an import library for a particular device +ForceProcAddressGen __ALL__ + +# Also force the calling conventions of the locally generated function +# pointer typedefs for these routines to MYAPIENTRY +# LocalProcAddressCallingConvention __ALL__ MYAPIENTRY + +Include test2-common.cfg + +Import com.jogamp.gluegen.test.junit.generation.Bindingtest2 +Import com.jogamp.gluegen.test.junit.generation.Bindingtest2p2 + +CustomJavaCode Bindingtest2p2Impl private static Bindingtest2p2ProcAddressTable _table = new Bindingtest2p2ProcAddressTable(); +CustomJavaCode Bindingtest2p2Impl public static void resetProcAddressTable(DynamicLookupHelper lookup) { +CustomJavaCode Bindingtest2Ip2mpl _table.reset(lookup); +CustomJavaCode Bindingtest2p2Impl } + + -- cgit v1.2.3