2009-03-11 Zoltan Varga <vargaz@gmail.com>
[mcs.git] / tests / gtest-056.cs
blob8080c0ab75f0b0f22ecc6fc9153756dde9772acc
1 //-- ex-gen-logger
2 //-- ex-gen-struct-pair
3 //-- ex-gen-logging-pairs
4 // 1.2 alpha
6 using System;
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];
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;
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;
39 class TestLog {
40 class MyTest {
41 public static void Main(String[] args) {
42 Log<String> log1 = new Log<String>();
43 log1.Add("Reboot");
44 log1.Add("Coffee");
45 Log<DateTime> log2 = new Log<DateTime>();
46 log2.Add(DateTime.Now);
47 log2.Add(DateTime.Now.AddHours(1));
48 DateTime[] dts = log2.All;
49 // Printing both logs:
50 foreach (String s in log1.All)
51 Console.Write("{0} ", s);
52 Console.WriteLine();
53 foreach (DateTime dt in dts)
54 Console.Write("{0} ", dt);
55 Console.WriteLine();