summaryrefslogtreecommitdiff
path: root/tests/src/JIT/Directed/lifetime/lifetime1.cs
blob: 46d428098d8f3d711f82008172b7833705a551ca (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//

// testing the JIT handling and GC reporting of "liveness" of GC variable

using System;

internal class Test
{
    private class A
    {
        public A()
        {
            Console.WriteLine("A");
            _iMember = 123;
            Test.aExists = true;
        }
        ~A()
        {
            Console.WriteLine("~A");
            Test.aExists = false;
        }
        public bool F()
        {
            Console.WriteLine("A.F(): iMember = {0}", _iMember);
            return true;
        }
        private volatile int _iMember;
    }

    public static volatile bool aExists = false;

    public static int f1()
    {
        A a = new A();
        a.F();

        // Testcase 1
        Console.WriteLine();
        Console.WriteLine("Testcase 1");
        if (!Test.aExists)
        {
            Console.WriteLine("Testcase 1 FAILED");
            return -1;
        }
        a.F();
        a = null;
        return 100;
    }

    public static int f2()
    {
        A a = new A();
        a.F();
        a = null;

        // Testcase 3
        Console.WriteLine();
        Console.WriteLine("Testcase 3");
        if (!Test.aExists)
        {
            Console.WriteLine("Testcase 3 FAILED");
            return -1;
        }
        return 100;
    }


    public static int f3()
    {
        A a = new A();
        a.F();
        a = null;
        GC.Collect();
        GC.WaitForPendingFinalizers();

        A b = new A();
        a = b;
        GC.Collect();
        GC.WaitForPendingFinalizers();
        Console.WriteLine();
        Console.WriteLine("Testcase 5");
        if (!Test.aExists)
        {
            Console.WriteLine("Testcase 5 FAILED");
            return -1;
        }
        GC.KeepAlive(b);
        // Testcase 6
        Console.WriteLine();
        Console.WriteLine("Testcase 6");
        if (b == null)
        {
            Console.WriteLine("Testcase 6 FAILED");
            return -1;
        }

        b = null;

        return 100;
    }


    private static int Main()
    {
        if (f1() != 100) return -1;
        CleanGC();

        // Testcase 2
        Console.WriteLine();
        Console.WriteLine("Testcase 2");
        // here JIT should know a is not live anymore
        if (Test.aExists)
        {
            Console.WriteLine("Testcase 2 FAILED");
            return -1;
        }

        if (f2() != 100) return -1;
        CleanGC();

        // here JIT should know object a is not live anymore        
        // Testcase 4
        Console.WriteLine();
        Console.WriteLine("Testcase 4");
        if (Test.aExists)
        {
            Console.WriteLine("Testcase 4 FAILED");
            return -1;
        }

        if (f3() != 100) return -1;
        CleanGC();

        // here JIT should know object a is not live anymore        
        // Testcase 7
        Console.WriteLine();
        Console.WriteLine("Testcase 7");
        if (Test.aExists)
        {
            Console.WriteLine("Testcase 7 FAILED");
            return -1;
        }

        CleanGC();



        Console.WriteLine("Test SUCCESS");
        return 100;
    }

    private static void CleanGC()
    {
        GC.Collect();
        GC.WaitForPendingFinalizers();
    }
}