Unit tests for COM callbacks

db4
Joe Groff 2008-03-21 20:36:24 -07:00
parent 11d28109cf
commit 5d6720f991
1 changed files with 69 additions and 93 deletions

View File

@ -1,93 +1,69 @@
USING: kernel windows.com windows.com.syntax windows.ole32 USING: kernel windows.com windows.com.syntax windows.ole32
alien alien.syntax tools.test libc alien.c-types arrays.lib alien alien.syntax tools.test libc alien.c-types arrays.lib
namespaces arrays continuations ; namespaces arrays continuations accessors ;
IN: windows.com.tests IN: windows.com.tests
! Create some test COM interfaces COM-INTERFACE: ISimple IUnknown {216fb341-0eb2-44b1-8edb-60b76e353abc}
HRESULT returnOK ( )
COM-INTERFACE: ISimple IUnknown {216fb341-0eb2-44b1-8edb-60b76e353abc} HRESULT returnError ( ) ;
HRESULT returnOK ( )
HRESULT returnError ( ) ; COM-INTERFACE: IInherited ISimple {9620ecec-8438-423b-bb14-86f835aa40dd}
int getX ( )
COM-INTERFACE: IInherited ISimple {9620ecec-8438-423b-bb14-86f835aa40dd} void setX ( int newX ) ;
int getX ( )
void setX ( int newX ) ; COM-INTERFACE: IUnrelated IUnknown {b06ac3f4-30e4-406b-a7cd-c29cead4552c}
int xPlus ( int y )
! Implement the IInherited interface in factor using alien-callbacks int xMulAdd ( int mul, int add ) ;
C-STRUCT: test-implementation "{216fb341-0eb2-44b1-8edb-60b76e353abc}" string>guid 1array [ ISimple-iid ] unit-test
{ "void*" "vtbl" } "{9620ecec-8438-423b-bb14-86f835aa40dd}" string>guid 1array [ IInherited-iid ] unit-test
{ "int" "x" } ; "{00000000-0000-0000-C000-000000000046}" string>guid 1array [ IUnknown-iid ] unit-test
: QueryInterface-callback SYMBOL: +test-vtbl+
"HRESULT" { "void*" "REFGUID" "void**" } "stdcall" [ nip 0 swap set-void*-nth S_OK ] SYMBOL: +guinea-pig-implementation+
alien-callback ;
: AddRef-callback TUPLE: test-implementation x ;
"ULONG" { "void*" } "stdcall" [ drop 2 ] C: test-implementation <test-implementation>
alien-callback ;
: Release-callback {
"ULONG" { "void*" } "stdcall" [ drop 1 ] { "IInherited" {
alien-callback ; [ drop S_OK ] ! ISimple::returnOK
: returnOK-callback [ drop E_FAIL ] ! ISimple::returnError
"HRESULT" { "void*" } "stdcall" [ drop S_OK ] [ x>> ] ! IInherited::getX
alien-callback ; [ >>x drop ] ! IInherited::setX
: returnError-callback } }
"HRESULT" { "void*" } "stdcall" [ drop E_FAIL ] { "IUnrelated" {
alien-callback ; [ swap x>> + ] ! IUnrelated::xPlus
: getX-callback [ spin x>> * + ] ! IUnrealted::xMulAdd
"int" { "void*" } "stdcall" [ test-implementation-x ] } }
alien-callback ; } <com-vtbl>
: setX-callback dup +test-vtbl+ set [
"void" { "void*" "int" } "stdcall" [ swap set-test-implementation-x ]
alien-callback ; 0 <test-implementation> +test-vtbl+ get com-wrap
dup +guinea-pig-implementation+ set [
SYMBOL: +test-implementation-vtbl+
SYMBOL: +guinea-pig-implementation+ S_OK 1array [ +guinea-pig-implementation+ get ISimple::returnOK ] unit-test
E_FAIL <long> *long 1array [ +guinea-pig-implementation+ get ISimple::returnError ] unit-test
: (make-test-implementation) ( x imp -- imp ) 20 1array [ +guinea-pig-implementation+ get dup 20 IInherited::setX IInherited::getX ] unit-test
[ set-test-implementation-x ] keep 420 1array [ +guinea-pig-implementation+ get 20 20 IUnrelated::xMulAdd ] unit-test
+test-implementation-vtbl+ get over set-test-implementation-vtbl ; 40 1array [ +guinea-pig-implementation+ get 20 IUnrelated::xPlus ] unit-test
: <test-implementation> ( x -- imp ) +guinea-pig-implementation+ get 1array [
"test-implementation" <c-object> (make-test-implementation) ; +guinea-pig-implementation+ get com-add-ref
] unit-test
: <malloced-test-implementation> ( x -- imp )
"test-implementation" heap-size malloc (make-test-implementation) ; { } [ +guinea-pig-implementation+ get com-release ] unit-test
QueryInterface-callback +guinea-pig-implementation+ get 1array [
AddRef-callback +guinea-pig-implementation+ get IUnknown-iid com-query-interface
Release-callback ] unit-test
returnOK-callback +guinea-pig-implementation+ get 1array [
returnError-callback +guinea-pig-implementation+ get ISimple-iid com-query-interface
getX-callback ] unit-test
setX-callback "void*" heap-size +guinea-pig-implementation+ get <displaced-alien> 1array [
7 narray >c-void*-array +guinea-pig-implementation+ get IUnrelated-iid com-query-interface
dup byte-length [ ] unit-test
[ byte-array>memory ] keep
+test-implementation-vtbl+ set ] with-com-interface
! Test that the words defined by COM-INTERFACE: do their magic ] [ free-com-vtbl ] [ ] cleanup
"{216fb341-0eb2-44b1-8edb-60b76e353abc}" string>guid 1array [ ISimple-iid ] unit-test
"{9620ecec-8438-423b-bb14-86f835aa40dd}" string>guid 1array [ IInherited-iid ] unit-test
"{00000000-0000-0000-C000-000000000046}" string>guid 1array [ IUnknown-iid ] unit-test
S_OK 1array [ 0 <test-implementation> ISimple::returnOK ] unit-test
E_FAIL <long> *long 1array [ 0 <test-implementation> ISimple::returnError ] unit-test
1984 1array [ 0 <test-implementation> dup 1984 IInherited::setX IInherited::getX ] unit-test
! Test that the helper functions for QueryInterface, AddRef, Release work
0 <malloced-test-implementation> +guinea-pig-implementation+ set
[
+guinea-pig-implementation+ get 1array [
+guinea-pig-implementation+ get com-add-ref
] unit-test
{ } [ +guinea-pig-implementation+ get com-release ] unit-test
+guinea-pig-implementation+ get 1array [
+guinea-pig-implementation+ get IUnknown-iid com-query-interface
] unit-test
] [ +guinea-pig-implementation+ get free ] [ ] cleanup
] with-malloc