Merge pull request #900 from Blewzman/FixAggregateExceptionGetBaseException
[mono.git] / mcs / class / corlib / Test / System.Collections.Generic / ComparerTest.cs
1 //
2 // MonoTests.System.Collections.Generic.Test.ComparerTest
3 //
4 // Authors:
5 //      Gert Driesen (drieseng@users.sourceforge.net)
6 //
7 // Copyright (C) 2007 Gert Driesen
8 // Copyright 2011 Xamarin Inc (http://www.xamarin.com).
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 //
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 //
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using System.Collections;
31 using System.Collections.Generic;
32 using System.IO;
33 using System.Runtime.Serialization.Formatters.Binary;
34
35 using NUnit.Framework;
36 using System;
37
38 namespace MonoTests.System.Collections.Generic
39 {
40         [TestFixture]
41         public class ComparerTest
42         {
43                 class CustomComparer : IComparable, IComparable<object>
44                 {
45                         int IComparable<object>.CompareTo (object other)
46                         {
47                                 throw new NotImplementedException ();
48                         }
49
50                         int IComparable.CompareTo (object obj)
51                         {
52                                 return 9;
53                         }
54                 }
55
56 #if NET_4_5
57                 [Test]
58                 public void Create ()
59                 {
60                         var comparer = Comparer<int>.Create ((a, b) => a - b);
61                         Assert.AreEqual (-1, comparer.Compare (1, 2), "#1");
62                 }
63
64                 [Test]
65                 public void Create_Invalid ()
66                 {
67                         try {
68                                 Comparer<int>.Create (null);
69                                 Assert.Fail ("#1");
70                         } catch (ArgumentNullException) {
71                         }
72                 }
73 #endif
74
75                 [Test]
76                 public void DefaultComparer_UserComparable ()
77                 {
78                         IComparer c = Comparer<object>.Default;
79                         Assert.AreEqual (-9, c.Compare (new object (), new CustomComparer ()), "#1");
80                         Assert.AreEqual (9, c.Compare (new CustomComparer (), new object ()), "#2");
81                 }
82
83                 [Test]
84                 public void DefaultComparer_NotComparableArgument ()
85                 {
86                         IComparer c = Comparer<object>.Default;
87                         try {
88                                 c.Compare (new object (), new object ());
89                                 Assert.Fail ("#1");
90                         } catch (ArgumentException) {
91                         }
92
93                         var o = new object ();
94                         Assert.AreEqual (0, c.Compare (o, o), "#2");
95                 }
96
97
98 #if !NET_4_0 // FIXME: the blob contains the 2.0 mscorlib version
99
100                 [Test] // bug #80929
101                 public void SerializeDefault ()
102                 {
103                         Comparer<int> c = Comparer<int>.Default;
104
105                         BinaryFormatter bf = new BinaryFormatter ();
106                         MemoryStream ms = new MemoryStream ();
107                         bf.Serialize (ms, c);
108
109                         byte [] buffer = new byte [ms.Length];
110                         ms.Position = 0;
111                         ms.Read (buffer, 0, buffer.Length);
112
113                         Assert.AreEqual (_serializedDefault, buffer);
114                 }
115
116 #endif
117
118                 [Test]
119                 public void DeserializeDefault ()
120                 {
121                         MemoryStream ms = new MemoryStream ();
122                         ms.Write (_serializedDefault, 0, _serializedDefault.Length);
123                         ms.Position = 0;
124
125                         BinaryFormatter bf = new BinaryFormatter ();
126                         Comparer<int> c = (Comparer<int>) bf.Deserialize (ms);
127                         Assert.IsNotNull (c);
128                 }
129
130                 private static readonly byte [] _serializedDefault = new byte [] {
131                         0x00, 0x01, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00,
132                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x01, 0x00, 0x00, 0x00,
133                         0x89, 0x01, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x43, 0x6f,
134                         0x6c, 0x6c, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x2e, 0x47,
135                         0x65, 0x6e, 0x65, 0x72, 0x69, 0x63, 0x2e, 0x47, 0x65, 0x6e, 0x65,
136                         0x72, 0x69, 0x63, 0x43, 0x6f, 0x6d, 0x70, 0x61, 0x72, 0x65, 0x72,
137                         0x60, 0x31, 0x5b, 0x5b, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e,
138                         0x49, 0x6e, 0x74, 0x33, 0x32, 0x2c, 0x20, 0x6d, 0x73, 0x63, 0x6f,
139                         0x72, 0x6c, 0x69, 0x62, 0x2c, 0x20, 0x56, 0x65, 0x72, 0x73, 0x69,
140                         0x6f, 0x6e, 0x3d, 0x32, 0x2e, 0x30, 0x2e, 0x30, 0x2e, 0x30, 0x2c,
141                         0x20, 0x43, 0x75, 0x6c, 0x74, 0x75, 0x72, 0x65, 0x3d, 0x6e, 0x65,
142                         0x75, 0x74, 0x72, 0x61, 0x6c, 0x2c, 0x20, 0x50, 0x75, 0x62, 0x6c,
143                         0x69, 0x63, 0x4b, 0x65, 0x79, 0x54, 0x6f, 0x6b, 0x65, 0x6e, 0x3d,
144                         0x62, 0x37, 0x37, 0x61, 0x35, 0x63, 0x35, 0x36, 0x31, 0x39, 0x33,
145                         0x34, 0x65, 0x30, 0x38, 0x39, 0x5d, 0x5d, 0x00, 0x00, 0x00, 0x00,
146                         0x0b };
147         }
148 }