Merge pull request #4419 from BrzVlad/fix-oom-nre
[mono.git] / mcs / tests / gtest-072.cs
1 //-- ex-gen-logger
2 //-- ex-gen-struct-pair
3 //-- ex-gen-logging-pairs
4 // 1.2 alpha
5
6 using System;
7
8 public class Log<T> {
9   private const int SIZE = 5;
10   private static int instanceCount = 0;
11   private int count = 0;
12   private T[] log = new T[SIZE];
13   public Log() { instanceCount++; }
14   public static int InstanceCount { get { return instanceCount; } }
15   public void Add(T msg) { log[count++ % SIZE] = msg; }
16   public int Count { get { return count; } }
17   public T Last {
18     get { // Return the last log entry, or null if nothing logged yet
19       return count==0 ? default(T) : log[(count-1)%SIZE];
20     }
21     set { // Update the last log entry, or create one if nothing logged yet 
22       if (count==0)
23         log[count++] = value;
24       else
25         log[(count-1)%SIZE] = value;
26     }
27   }    
28   public T[] All {
29     get {
30       int size = Math.Min(count, SIZE);
31       T[] res = new T[size];
32       for (int i=0; i<size; i++)
33         res[i] = log[(count-size+i) % SIZE];
34       return res;
35     }
36   }
37 }
38
39 class TestLog {
40     public static void Main(String[] args) {
41       Log<String> log1 = new Log<String>();
42       log1.Add("Reboot");
43       log1.Add("Coffee");
44       Log<DateTime> log2 = new Log<DateTime>();
45       log2.Add(DateTime.Now);
46       log2.Add(DateTime.Now.AddHours(1));
47       DateTime[] dts = log2.All;
48       // Printing both logs:
49       foreach (String s in log1.All) 
50         Console.Write("{0}   ", s);
51       Console.WriteLine();
52       foreach (DateTime dt in dts) 
53         Console.Write("{0}   ", dt);
54       Console.WriteLine();
55       TestPairLog();
56     }
57     
58     public static void TestPairLog() {
59       Log<Pair<DateTime,String>> log = new Log<Pair<DateTime,String>>();
60       log.Add(new Pair<DateTime,String>(DateTime.Now, "Tea leaves"));
61       log.Add(new Pair<DateTime,String>(DateTime.Now.AddMinutes(2), "Hot water"));
62       log.Add(new Pair<DateTime,String>(DateTime.Now.AddMinutes(7), "Ready"));
63       Pair<DateTime,String>[] allMsgs = log.All;
64       foreach (Pair<DateTime,String> p in allMsgs) 
65         Console.WriteLine("At {0}: {1}", p.Fst, p.Snd);
66     }
67 }
68
69 public struct Pair<T,U> {
70   public readonly T Fst;
71   public readonly U Snd;
72   public Pair(T fst, U snd) {
73     this.Fst = fst; 
74     this.Snd = snd;
75   }
76 }