Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gtest-056.cs « tests « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3def77b61f5ee55e19b15150322edb380c95d996 (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
//-- ex-gen-logger
//-- ex-gen-struct-pair
//-- ex-gen-logging-pairs
// 1.2 alpha

using System;

public class Log<T> {
  private const int SIZE = 5;
  private static int instanceCount = 0;
  private int count = 0;
  private T[] log = new T[SIZE];
  public Log() { instanceCount++; }
  public static int InstanceCount { get { return instanceCount; } }
  public void Add(T msg) { log[count++ % SIZE] = msg; }
  public int Count { get { return count; } }
  public T Last {
    get { // Return the last log entry, or null if nothing logged yet
      return count==0 ? default(T) : log[(count-1)%SIZE];
    }
    set { // Update the last log entry, or create one if nothing logged yet 
      if (count==0)
        log[count++] = value;
      else
        log[(count-1)%SIZE] = value;
    }
  }    
  public T[] All {
    get {
      int size = Math.Min(count, SIZE);
      T[] res = new T[size];
      for (int i=0; i<size; i++)
        res[i] = log[(count-size+i) % SIZE];
      return res;
    }
  }
}

class TestLog {
    public static void Main(String[] args) {
      Log<String> log1 = new Log<String>();
      log1.Add("Reboot");
      log1.Add("Coffee");
      Log<DateTime> log2 = new Log<DateTime>();
      log2.Add(DateTime.Now);
      log2.Add(DateTime.Now.AddHours(1));
      DateTime[] dts = log2.All;
      // Printing both logs:
      foreach (String s in log1.All) 
	Console.Write("{0}   ", s);
      Console.WriteLine();
      foreach (DateTime dt in dts) 
	Console.Write("{0}   ", dt);
      Console.WriteLine();
  }
}