1 | // Licensed to the .NET Foundation under one or more agreements. |
2 | // The .NET Foundation licenses this file to you under the MIT license. |
3 | // See the LICENSE file in the project root for more information. |
4 | |
5 | /*============================================================ |
6 | ** |
7 | ** Source: test.c |
8 | ** |
9 | ** Purpose: Test for InterlockedCompareExchangePointer() function |
10 | ** |
11 | ** |
12 | **=========================================================*/ |
13 | |
14 | /* This test is FINISHED. Note: The biggest feature of this function is that |
15 | it locks the value before it increments it -- in order to make it so only |
16 | one thread can access it. But, I really don't have a great test to |
17 | make sure it's thread safe. Any ideas? |
18 | */ |
19 | |
20 | #include <palsuite.h> |
21 | |
22 | int __cdecl main(int argc, char *argv[]) |
23 | { |
24 | |
25 | long StartValue = 5; |
26 | long NewValue = 10; |
27 | PVOID ReturnValue = NULL; |
28 | |
29 | /* |
30 | * Initialize the PAL and return FAILURE if this fails |
31 | */ |
32 | |
33 | if(0 != (PAL_Initialize(argc, argv))) |
34 | { |
35 | return FAIL; |
36 | } |
37 | |
38 | ReturnValue = InterlockedCompareExchangePointer((PVOID)&StartValue, |
39 | (PVOID)NewValue, |
40 | (PVOID)StartValue); |
41 | |
42 | /* StartValue and NewValue should be equal now */ |
43 | if(StartValue != NewValue) |
44 | { |
45 | Fail("ERROR: These values should be equal after the exchange. " |
46 | "They should both be %d, however the value that should have " |
47 | "been exchanged is %d.\n" ,NewValue,StartValue); |
48 | } |
49 | |
50 | /* Returnvalue should have been set to what 'StartValue' was |
51 | (5 in this case) |
52 | */ |
53 | |
54 | if((int)(size_t)ReturnValue != 5) |
55 | { |
56 | Fail("ERROR: The return value should be the value of the " |
57 | "variable before the exchange took place, which was 5. " |
58 | "But, the return value was %d.\n" ,ReturnValue); |
59 | } |
60 | |
61 | /* This is a mismatch, so no exchange should happen */ |
62 | InterlockedCompareExchangePointer((PVOID)&StartValue, |
63 | ReturnValue, |
64 | ReturnValue); |
65 | if(StartValue != NewValue) |
66 | { |
67 | Fail("ERROR: The compare should have failed and no exchange should " |
68 | "have been made, but it seems the exchange still happened.\n" ); |
69 | } |
70 | |
71 | |
72 | PAL_Terminate(); |
73 | return PASS; |
74 | } |
75 | |
76 | |
77 | |
78 | |
79 | |
80 | |
81 | |