summaryrefslogtreecommitdiff
path: root/tests/src/GC/API/GCHandle/Target_neg.cs
blob: 1434a8aa3334dd361df222057267cfbad8b9ff06 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

// Tests GCHandle.Target negative scenarios

using System;
using System.Runtime.InteropServices;

public class Test
{
    public static int Main()
    {
        bool passed = true;

        Object o = new Object();
        GCHandle gch = GCHandle.Alloc(o);
        gch.Free();

        try
        {
            gch.Target = o;
        }
        catch (InvalidOperationException)
        {
            Console.WriteLine("Caught expected InvalidOperationException");
        }
        catch (Exception)
        {
            Console.WriteLine("Caught unexpected exception!");

            Console.WriteLine("Test1 Failed!");
            passed = false;
        }

        try
        {
            Object o2 = gch.Target;
        }
        catch (InvalidOperationException)
        {
            Console.WriteLine("Caught expected InvalidOperationException");
        }
        catch (Exception)
        {
            Console.WriteLine("Caught unexpected exception!");
            Console.WriteLine("Test2 Failed!");
            passed = false;
        }

        try
        {
            Object o2 = gch.Target;
        }
        catch (InvalidOperationException)
        {
            Console.WriteLine("Caught expected InvalidOperationException");
        }
        catch (Exception)
        {
            Console.WriteLine("Caught unexpected exception!");
            Console.WriteLine("Test3 Failed!");
            passed = false;
        }

        if (!passed)
        {
            Console.WriteLine("Test Failed!");
            return 1;
        }

        Console.WriteLine("Test Passed!");
        return 100;
    }
}