[tests] Avoid false pinning in weak-fields test
[mono-project.git] / mono / tests / weak-fields.cs
blobbfca78068f784a2f3c6848bdf0f7f12204d58114
1 using System;
2 using System.Threading;
3 using MonoTests.Helpers;
5 [AttributeUsage(AttributeTargets.Field)]
6 public sealed class Weak2Attribute : Attribute
10 public class Finalizable {
11 public int a;
13 ~Finalizable () {
14 Console.WriteLine ("Finalized. {0}", a);
18 public class OneField {
19 int x;
21 public class Tests
23 static Finalizable retain;
25 [Weak]
26 public object Obj;
27 [Weak2]
28 public object Obj3;
29 [Weak]
30 public object Obj2;
31 [Weak]
32 public Finalizable Obj4;
34 public static int Main (String[] args) {
35 var t = new Tests ();
36 FinalizerHelpers.PerformNoPinAction (delegate () {
37 FinalizerHelpers.PerformNoPinAction (delegate () {
38 t.Obj = new Finalizable ();
39 t.Obj2 = new Finalizable ();
40 t.Obj3 = new Finalizable ();
41 t.Obj4 = retain = new Finalizable ();
42 retain.a = 0x1029458;
43 });
44 GC.Collect (0);
45 GC.Collect ();
46 GC.WaitForPendingFinalizers ();
47 if (t.Obj != null)
48 Environment.Exit (1);
49 if (t.Obj2 != null)
50 Environment.Exit (2);
51 if (t.Obj3 == null)
52 Environment.Exit (3);
53 //overflow the nursery, make sure we fill it
54 for (int i = 0; i < 1000 * 1000 * 10; ++i)
55 new OneField ();
57 if (retain.a != 0x1029458)
58 Environment.Exit (4);
60 retain = null;
61 });
62 GC.Collect ();
63 GC.WaitForPendingFinalizers ();
64 if (t.Obj4 != null)
65 return 5;
67 return 0;