winemac: Consolidate mouse move handling into -[WineApplicationController handleMouse...
[wine.git] / dlls / kernel32 / tests / fiber.c
blob0e685351b461563413cbb66015682d600c0a4298
1 /*
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;
38 static WORD cbCount;
40 static VOID init_funcs(void)
42 HMODULE hKernel32 = GetModuleHandle("kernel32");
44 #define X(f) p##f = (void*)GetProcAddress(hKernel32, #f);
45 X(CreateFiber);
46 X(ConvertThreadToFiber);
47 X(ConvertFiberToThread);
48 X(SwitchToFiber);
49 X(DeleteFiber);
50 X(ConvertThreadToFiberEx);
51 X(CreateFiberEx);
52 X(IsThreadAFiber);
53 X(FlsAlloc);
54 X(FlsFree);
55 X(FlsGetValue);
56 X(FlsSetValue);
57 #undef X
60 static VOID WINAPI FiberLocalStorageProc(PVOID lpFlsData)
62 cbCount++;
63 ok(lpFlsData == (PVOID) 1587, "FlsData expected not to be changed\n");
66 static VOID WINAPI FiberMainProc(LPVOID lpFiberParameter)
68 BYTE *tparam = (BYTE *)lpFiberParameter;
69 cbCount++;
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());
81 else
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());
94 else
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());
107 else
109 win_skip( "ConvertFiberToThread not present\n" );
113 static void test_FiberHandling(void)
115 cbCount = 0;
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();
124 else
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]);
135 if (!pCreateFiberEx)
137 win_skip( "CreateFiberEx not present\n" );
138 return;
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" );
152 return;
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)
162 DWORD fls;
163 BOOL ret;
164 PVOID val = (PVOID) 1587;
166 if (!pFlsAlloc)
168 win_skip( "Fiber Local Storage not supported\n" );
169 return;
171 cbCount = 0;
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");
180 ret = pFlsFree(fls);
181 ok(ret, "FlsFree failed\n");
182 if (cbfunc)
183 todo_wine ok(cbCount == 1, "Wrong callback count: %d\n", cbCount);
186 START_TEST(fiber)
188 init_funcs();
190 if (!pCreateFiber)
192 win_skip( "Fibers not supported by win95\n" );
193 return;
196 test_FiberHandling();
197 test_FiberLocalStorage(NULL);
198 test_FiberLocalStorage(FiberLocalStorageProc);