2 * Unit tests for fiber functions
4 * Copyright (c) 2010 André Hentschel
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "wine/test.h"
23 static LPVOID (WINAPI
*pCreateFiber
)(SIZE_T
,LPFIBER_START_ROUTINE
,LPVOID
);
24 static LPVOID (WINAPI
*pConvertThreadToFiber
)(LPVOID
);
25 static BOOL (WINAPI
*pConvertFiberToThread
)(void);
26 static void (WINAPI
*pSwitchToFiber
)(LPVOID
);
27 static void (WINAPI
*pDeleteFiber
)(LPVOID
);
28 static LPVOID (WINAPI
*pConvertThreadToFiberEx
)(LPVOID
,DWORD
);
29 static LPVOID (WINAPI
*pCreateFiberEx
)(SIZE_T
,SIZE_T
,DWORD
,LPFIBER_START_ROUTINE
,LPVOID
);
30 static BOOL (WINAPI
*pIsThreadAFiber
)(void);
31 static DWORD (WINAPI
*pFlsAlloc
)(PFLS_CALLBACK_FUNCTION
);
32 static BOOL (WINAPI
*pFlsFree
)(DWORD
);
33 static PVOID (WINAPI
*pFlsGetValue
)(DWORD
);
34 static BOOL (WINAPI
*pFlsSetValue
)(DWORD
,PVOID
);
36 static LPVOID fibers
[2];
37 static BYTE testparam
= 185;
40 static VOID
init_funcs(void)
42 HMODULE hKernel32
= GetModuleHandleA("kernel32.dll");
44 #define X(f) p##f = (void*)GetProcAddress(hKernel32, #f);
46 X(ConvertThreadToFiber
);
47 X(ConvertFiberToThread
);
50 X(ConvertThreadToFiberEx
);
60 static VOID WINAPI
FiberLocalStorageProc(PVOID lpFlsData
)
63 ok(lpFlsData
== (PVOID
) 1587, "FlsData expected not to be changed\n");
66 static VOID WINAPI
FiberMainProc(LPVOID lpFiberParameter
)
68 BYTE
*tparam
= (BYTE
*)lpFiberParameter
;
70 ok(*tparam
== 185, "Parameterdata expected not to be changed\n");
71 pSwitchToFiber(fibers
[0]);
74 static void test_ConvertThreadToFiber(void)
76 if (pConvertThreadToFiber
)
78 fibers
[0] = pConvertThreadToFiber(&testparam
);
79 ok(fibers
[0] != 0, "ConvertThreadToFiber failed with error %d\n", GetLastError());
83 win_skip( "ConvertThreadToFiber not present\n" );
87 static void test_ConvertThreadToFiberEx(void)
89 if (pConvertThreadToFiberEx
)
91 fibers
[0] = pConvertThreadToFiberEx(&testparam
, 0);
92 ok(fibers
[0] != 0, "ConvertThreadToFiberEx failed with error %d\n", GetLastError());
96 win_skip( "ConvertThreadToFiberEx not present\n" );
100 static void test_ConvertFiberToThread(void)
102 if (pConvertFiberToThread
)
104 BOOL ret
= pConvertFiberToThread();
105 ok(ret
, "ConvertFiberToThread failed with error %d\n", GetLastError());
109 win_skip( "ConvertFiberToThread not present\n" );
113 static void test_FiberHandling(void)
116 fibers
[0] = pCreateFiber(0,FiberMainProc
,&testparam
);
117 ok(fibers
[0] != 0, "CreateFiber failed with error %d\n", GetLastError());
118 pDeleteFiber(fibers
[0]);
120 test_ConvertThreadToFiber();
121 test_ConvertFiberToThread();
122 if (pConvertThreadToFiberEx
)
123 test_ConvertThreadToFiberEx();
125 test_ConvertThreadToFiber();
128 fibers
[1] = pCreateFiber(0,FiberMainProc
,&testparam
);
129 ok(fibers
[1] != 0, "CreateFiber failed with error %d\n", GetLastError());
131 pSwitchToFiber(fibers
[1]);
132 ok(cbCount
== 1, "Wrong callback count: %d\n", cbCount
);
133 pDeleteFiber(fibers
[1]);
137 win_skip( "CreateFiberEx not present\n" );
141 SetLastError(0xdeadbeef);
142 fibers
[1] = pCreateFiberEx(0,0,0,FiberMainProc
,&testparam
);
143 ok(fibers
[1] != 0, "CreateFiberEx failed with error %d\n", GetLastError());
145 pSwitchToFiber(fibers
[1]);
146 ok(cbCount
== 2, "Wrong callback count: %d\n", cbCount
);
147 pDeleteFiber(fibers
[1]);
149 if (!pIsThreadAFiber
)
151 win_skip( "IsThreadAFiber not present\n" );
155 ok(pIsThreadAFiber(), "IsThreadAFiber reported FALSE\n");
156 test_ConvertFiberToThread();
157 ok(!pIsThreadAFiber(), "IsThreadAFiber reported TRUE\n");
160 static void test_FiberLocalStorage(PFLS_CALLBACK_FUNCTION cbfunc
)
164 PVOID val
= (PVOID
) 1587;
168 win_skip( "Fiber Local Storage not supported\n" );
173 fls
= pFlsAlloc(cbfunc
);
174 ok(fls
!= FLS_OUT_OF_INDEXES
, "FlsAlloc failed with error %d\n", GetLastError());
176 ret
= pFlsSetValue(fls
, val
);
177 ok(ret
, "FlsSetValue failed\n");
178 ok(val
== pFlsGetValue(fls
), "FlsGetValue failed\n");
181 ok(ret
, "FlsFree failed\n");
183 todo_wine
ok(cbCount
== 1, "Wrong callback count: %d\n", cbCount
);
186 SetLastError( 0xdeadbeef );
187 val
= pFlsGetValue( 0 );
188 ok( !val
, "fls index 0 set to %p\n", val
);
189 ok( GetLastError() == ERROR_INVALID_PARAMETER
, "setting fls index wrong error %u\n", GetLastError() );
190 SetLastError( 0xdeadbeef );
191 ret
= pFlsSetValue( 0, (void *)0xdeadbeef );
192 ok( !ret
, "setting fls index 0 succeeded\n" );
193 ok( GetLastError() == ERROR_INVALID_PARAMETER
, "setting fls index wrong error %u\n", GetLastError() );
194 SetLastError( 0xdeadbeef );
195 val
= pFlsGetValue( 0 );
196 ok( !val
, "fls index 0 wrong value %p\n", val
);
197 ok( GetLastError() == ERROR_INVALID_PARAMETER
, "setting fls index wrong error %u\n", GetLastError() );
199 fls
= pFlsAlloc( NULL
);
200 ok( fls
!= FLS_OUT_OF_INDEXES
, "FlsAlloc failed\n" );
201 ok( fls
!= 0, "fls index 0 allocated\n" );
202 val
= pFlsGetValue( fls
);
203 ok( !val
, "fls index %u wrong value %p\n", fls
, val
);
204 ret
= pFlsSetValue( fls
, (void *)0xdeadbeef );
205 ok( ret
, "setting fls index %u failed\n", fls
);
206 val
= pFlsGetValue( fls
);
207 ok( val
== (void *)0xdeadbeef, "fls index %u wrong value %p\n", fls
, val
);
209 ret
= pFlsSetValue( fls
, (void *)0xdeadbabe );
210 ok( ret
, "setting fls index %u failed\n", fls
);
211 val
= pFlsGetValue( fls
);
212 ok( val
== (void *)0xdeadbabe, "fls index %u wrong value %p\n", fls
, val
);
221 win_skip( "Fibers not supported by win95\n" );
225 test_FiberHandling();
226 test_FiberLocalStorage(NULL
);
227 test_FiberLocalStorage(FiberLocalStorageProc
);