Project

General

Profile

root / branches / compiler / cSharp / ooasCompiler / src / libs / c5 / nunit / templates / Clone.cs @ 3

1
/*
2
 Copyright (c) 2003-2006 Niels Kokholm and Peter Sestoft
3
 Permission is hereby granted, free of charge, to any person obtaining a copy
4
 of this software and associated documentation files (the "Software"), to deal
5
 in the Software without restriction, including without limitation the rights
6
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
 copies of the Software, and to permit persons to whom the Software is
8
 furnished to do so, subject to the following conditions:
9
 
10
 The above copyright notice and this permission notice shall be included in
11
 all copies or substantial portions of the Software.
12
 
13
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
 SOFTWARE.
20
*/
21

    
22
using System;
23
using C5;
24
using NUnit.Framework;
25
using SCG = System.Collections.Generic;
26

    
27
namespace C5UnitTests.Templates.Extensible
28
{
29
  class Clone
30
  {
31
    public static void Tester<U>() where U : class, IExtensible<int>, new()
32
    {
33
      U extensible = new U();
34
      RealTester<U>(extensible);
35
      extensible.Add(12);
36
      extensible.Add(23);
37
      extensible.Add(56);
38
      RealTester<U>(extensible);
39
    }
40

    
41
    public static void ViewTester<U>() where U : class, IList<int>, new()
42
    {
43
      U baselist = new U();
44
      baselist.Add(12);
45
      baselist.Add(23);
46
      baselist.Add(56);
47
      baselist.Add(112);
48
      baselist.Add(123);
49
      baselist.Add(156);
50
      U view = (U)baselist.View(2, 2);
51
      RealTester<U>(view);
52
    }
53

    
54
    public static void RealTester<U>(U extensible) where U : class, IExtensible<int>, new()
55
    {
56
      object clone = extensible.Clone();
57
      Assert.IsNotNull(clone);
58
      Assert.AreEqual(typeof(U), clone.GetType(),
59
        String.Format("Wrong type '{0}' of clone of '{1}'", clone.GetType(), typeof(U)));
60
      U theClone = clone as U;
61
      Assert.IsTrue(theClone.Check(), "Clone does not pass Check()");
62
      if (typeof(ICollection<int>).IsAssignableFrom(typeof(U)))
63
        Assert.IsTrue(EqualityComparer<U>.Default.Equals(extensible, theClone), "Clone has wrong contents");
64
      else //merely extensible
65
        Assert.IsTrue(IC.eq(theClone, extensible.ToArray()), "Clone has wrong contents");
66
    }
67
  }
68

    
69
  class Serialization
70
  {
71
    public static void Tester<U>() where U : class, IExtensible<int>, new()
72
    {
73
      U extensible = new U();
74
      realtester<U>(extensible);
75
      extensible.Add(12);
76
      extensible.Add(23);
77
      extensible.Add(56);
78
      realtester<U>(extensible);
79
    }
80

    
81
    public static void ViewTester<U>() where U : class, IList<int>, new()
82
    {
83
      U baselist = new U();
84
      baselist.Add(12);
85
      baselist.Add(23);
86
      baselist.Add(56);
87
      baselist.Add(112);
88
      baselist.Add(123);
89
      baselist.Add(156);
90
      U view = (U)baselist.View(2, 2);
91
      realtester<U>(view);
92
    }
93

    
94
    private static void realtester<U>(U extensible) where U : class, IExtensible<int>, new()
95
    {
96
      object clone = serializeAndDeserialize(extensible);
97

    
98
      Assert.IsNotNull(clone);
99
      Assert.AreEqual(typeof(U), clone.GetType(),
100
        String.Format("Wrong type '{0}' of clone of '{1}'", clone.GetType(), typeof(U)));
101
      U theClone = clone as U;
102
      if (typeof(IExtensible<int>).IsAssignableFrom(typeof(U)))
103
        Assert.IsTrue(((IExtensible<int>)theClone).Check(), "Clone does not pass Check()");
104
      if (typeof(ICollection<int>).IsAssignableFrom(typeof(U)))
105
        Assert.IsTrue(EqualityComparer<U>.Default.Equals(extensible, theClone), "Clone has wrong contents");
106
      else //merely extensible
107
        Assert.IsTrue(IC.eq(theClone, extensible.ToArray()), "Clone has wrong contents");
108
    }
109

    
110
    private static object serializeAndDeserialize(object extensible) 
111
    {
112
      System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter =
113
        new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
114
      System.IO.Stream stream = new System.IO.MemoryStream();
115
      formatter.Serialize(stream, extensible);
116
      stream.Flush();
117
      stream.Seek(0L, System.IO.SeekOrigin.Begin);
118
      object clone = formatter.Deserialize(stream);
119
      return clone;
120
    }
121

    
122
    public static void DTester<U>() where U : class, IDictionary<int, int>, new()
123
    {
124
      U dict = new U();
125
      realDtester<U>(dict);
126
      dict.Add(12, 4);
127
      dict.Add(23, 6);
128
      dict.Add(56, 1);
129
      realDtester<U>(dict);
130
      Assert.IsTrue(IC.eq((ICollectionValue<int>)serializeAndDeserialize(dict.Keys), dict.Keys.ToArray()), "Keys clone has wrong contents");
131
      Assert.IsTrue(IC.eq((ICollectionValue<int>)serializeAndDeserialize(dict.Values), dict.Values.ToArray()), "Values Clone has wrong contents");
132
    }
133

    
134
    private static void realDtester<U>(U dict) where U : class, IDictionary<int, int>, new()
135
    {
136
      System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter =
137
        new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
138
      System.IO.Stream stream = new System.IO.MemoryStream();
139
      formatter.Serialize(stream, dict);
140
      stream.Flush();
141
      stream.Seek(0L, System.IO.SeekOrigin.Begin);
142
      object clone = formatter.Deserialize(stream);
143

    
144
      Assert.IsNotNull(clone);
145
      Assert.AreEqual(typeof(U), clone.GetType(),
146
        String.Format("Wrong type '{0}' of clone of '{1}'", clone.GetType(), typeof(U)));
147
      U theClone = clone as U;
148
      Assert.IsTrue(theClone.Check(), "Clone does not pass Check()");
149

    
150
      Assert.AreEqual(dict.Count, theClone.Count, "wrong size");
151
      foreach (int i in dict.Keys)
152
      {
153
        Assert.AreEqual(dict[i], theClone[i], "Wrong value");
154
      }
155
    }
156
  }
157

    
158
}
159