New test.
[mono.git] / mcs / class / Mono.C5 / Test / templates / Clone.cs
1 /*\r
2  Copyright (c) 2003-2006 Niels Kokholm and Peter Sestoft\r
3  Permission is hereby granted, free of charge, to any person obtaining a copy\r
4  of this software and associated documentation files (the "Software"), to deal\r
5  in the Software without restriction, including without limitation the rights\r
6  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r
7  copies of the Software, and to permit persons to whom the Software is\r
8  furnished to do so, subject to the following conditions:\r
9  \r
10  The above copyright notice and this permission notice shall be included in\r
11  all copies or substantial portions of the Software.\r
12  \r
13  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
14  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
15  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r
16  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r
17  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
18  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\r
19  SOFTWARE.\r
20 */\r
21 \r
22 using System;\r
23 using C5;\r
24 using NUnit.Framework;\r
25 using SCG = System.Collections.Generic;\r
26 \r
27 namespace C5UnitTests.Templates.Extensible\r
28 {\r
29   class Clone\r
30   {\r
31     public static void Tester<U>() where U : class, IExtensible<int>, new()\r
32     {\r
33       U extensible = new U();\r
34       RealTester<U>(extensible);\r
35       extensible.Add(12);\r
36       extensible.Add(23);\r
37       extensible.Add(56);\r
38       RealTester<U>(extensible);\r
39     }\r
40 \r
41     public static void ViewTester<U>() where U : class, IList<int>, new()\r
42     {\r
43       U baselist = new U();\r
44       baselist.Add(12);\r
45       baselist.Add(23);\r
46       baselist.Add(56);\r
47       baselist.Add(112);\r
48       baselist.Add(123);\r
49       baselist.Add(156);\r
50       U view = (U)baselist.View(2, 2);\r
51       RealTester<U>(view);\r
52     }\r
53 \r
54     public static void RealTester<U>(U extensible) where U : class, IExtensible<int>, new()\r
55     {\r
56       object clone = extensible.Clone();\r
57       Assert.IsNotNull(clone);\r
58       Assert.AreEqual(typeof(U), clone.GetType(),\r
59         String.Format("Wrong type '{0}' of clone of '{1}'", clone.GetType(), typeof(U)));\r
60       U theClone = clone as U;\r
61       Assert.IsTrue(theClone.Check(), "Clone does not pass Check()");\r
62       if (typeof(ICollection<int>).IsAssignableFrom(typeof(U)))\r
63         Assert.IsTrue(EqualityComparer<U>.Default.Equals(extensible, theClone), "Clone has wrong contents");\r
64       else //merely extensible\r
65         Assert.IsTrue(IC.eq(theClone, extensible.ToArray()), "Clone has wrong contents");\r
66     }\r
67   }\r
68 \r
69   class Serialization\r
70   {\r
71     public static void Tester<U>() where U : class, IExtensible<int>, new()\r
72     {\r
73       U extensible = new U();\r
74       realtester<U>(extensible);\r
75       extensible.Add(12);\r
76       extensible.Add(23);\r
77       extensible.Add(56);\r
78       realtester<U>(extensible);\r
79     }\r
80 \r
81     public static void ViewTester<U>() where U : class, IList<int>, new()\r
82     {\r
83       U baselist = new U();\r
84       baselist.Add(12);\r
85       baselist.Add(23);\r
86       baselist.Add(56);\r
87       baselist.Add(112);\r
88       baselist.Add(123);\r
89       baselist.Add(156);\r
90       U view = (U)baselist.View(2, 2);\r
91       realtester<U>(view);\r
92     }\r
93 \r
94     private static void realtester<U>(U extensible) where U : class, IExtensible<int>, new()\r
95     {\r
96       System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter =\r
97         new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();\r
98       System.IO.Stream stream = new System.IO.MemoryStream();\r
99       formatter.Serialize(stream, extensible);\r
100       stream.Flush();\r
101       stream.Seek(0L, System.IO.SeekOrigin.Begin);\r
102       object clone = formatter.Deserialize(stream);\r
103 \r
104       Assert.IsNotNull(clone);\r
105       Assert.AreEqual(typeof(U), clone.GetType(),\r
106         String.Format("Wrong type '{0}' of clone of '{1}'", clone.GetType(), typeof(U)));\r
107       U theClone = clone as U;\r
108       Assert.IsTrue(theClone.Check(), "Clone does not pass Check()");\r
109       if (typeof(ICollection<int>).IsAssignableFrom(typeof(U)))\r
110         Assert.IsTrue(EqualityComparer<U>.Default.Equals(extensible, theClone), "Clone has wrong contents");\r
111       else //merely extensible\r
112         Assert.IsTrue(IC.eq(theClone, extensible.ToArray()), "Clone has wrong contents");\r
113     }\r
114   }\r
115 \r
116 }\r
117 \r