fixed tests
[mono.git] / mcs / class / System.Data / Test / System.Data.SqlTypes / SqlBinaryTest.cs
1 //
2 // SqlBinaryTest.cs - NUnit Test Cases for System.Data.SqlTypes.SqlBinary
3 //
4 // Authors:
5 //   Ville Palo (vi64pa@koti.soon.fi)
6 //   Martin Willemoes Hansen (mwh@sysrq.dk)
7 //
8 // (C) 2002 Ville Palo
9 // (C) 2003 Martin Willemoes Hansen
10 // 
11
12 //
13 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
14 //
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
22 // 
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
25 // 
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 //
34
35 using NUnit.Framework;
36 using System;
37 using System.Data.SqlTypes;
38
39 namespace MonoTests.System.Data.SqlTypes
40 {
41         [TestFixture]
42         public class SqlBinaryTest : Assertion {
43         
44                 SqlBinary Test1;
45                 SqlBinary Test2;
46                 SqlBinary Test3;
47
48                 [SetUp]
49                 public void GetReady() 
50                 {
51                         byte [] b1 = new Byte [2];
52                         byte [] b2 = new Byte [3];
53                         byte [] b3 = new Byte [2];
54
55                         b1 [0] = 240;
56                         b1 [1] = 15;
57                         b2 [0] = 10;
58                         b2 [1] = 10;
59                         b2 [2] = 10;
60                         b3 [0] = 240;
61                         b3 [1] = 15;
62
63                         Test1 = new SqlBinary(b1);
64                         Test2 = new SqlBinary(b2);
65                         Test3 = new SqlBinary(b3);
66                 }
67
68                 // Test constructor
69                 [Test]
70                 public void Create()
71                 {
72                         byte [] b = new byte [3];                        
73                         SqlBinary Test = new SqlBinary (b);
74                         Assert ("#A01", !(Test.IsNull)); 
75                 }
76
77                 // Test public fields
78                 [Test]
79                 public void PublicFields()
80                 {
81                         Assert ("#B01", SqlBinary.Null.IsNull);
82                 }
83
84                 // Test properties
85                 [Test]
86                 public void Properties()
87                 {
88                         byte [] b = new byte [2];
89                         b [0] = 64;
90                         b [1] = 128;
91
92                         SqlBinary TestBinary = new SqlBinary (b);
93
94                         // IsNull
95                         Assert ("#C01", SqlBinary.Null.IsNull);
96
97                         // Item
98                         AssertEquals ("#C02", (byte)128, TestBinary [1]);
99                         AssertEquals ("#C03", (byte)64, TestBinary [0]);
100
101                         // FIXME: MSDN says that should throw SqlNullValueException
102                         // but throws IndexOutOfRangeException
103                         try {
104                                 byte test = TestBinary [TestBinary.Length];
105                                 Fail ("#C04");
106                         } catch (Exception e) {
107                                 AssertEquals ("#C05", typeof (IndexOutOfRangeException),
108                                             e.GetType ());
109                         }
110                  
111                         try {
112                                 byte test = SqlBinary.Null [2];
113                                 Fail ("#C06");
114                         } catch (Exception e) {
115                                 AssertEquals ("#C07", typeof (SqlNullValueException),
116                                             e.GetType ());
117                         }
118
119                         // Length
120                         AssertEquals ("#C08", 2, TestBinary.Length);    
121
122                         try {
123                                 int test = SqlBinary.Null.Length;
124                                 Fail ("#C09");
125                         } catch (Exception e) {
126                                 AssertEquals ("#C10", typeof (SqlNullValueException),
127                                             e.GetType ());
128                         }
129
130                         // Value
131                         AssertEquals ("#C11", (byte)128, TestBinary [1]);
132                         AssertEquals ("#C12", (byte)64, TestBinary [0]);               
133
134                         try {
135                                 Byte [] test = SqlBinary.Null.Value;
136                                 Fail ("#C13");
137                         } catch (Exception e) {
138                                 AssertEquals ("#C14", typeof (SqlNullValueException),
139                                             e.GetType ());
140                         }
141                 }
142
143                 // Methods 
144                 [Test]
145                 public void ComparisonMethods()
146                 {
147                         // GreaterThan
148                         Assert ("#D01", SqlBinary.GreaterThan (Test1, Test2).Value);
149                         Assert ("#D02", SqlBinary.GreaterThan (Test3, Test2).Value);
150                         Assert ("#D03", !SqlBinary.GreaterThan (Test2, Test1).Value);
151                         
152                         // GreaterThanOrEqual
153                         Assert ("#D04", SqlBinary.GreaterThanOrEqual (Test1, Test2).Value);
154                         Assert ("#D05", SqlBinary.GreaterThanOrEqual (Test1, Test2).Value);
155                         Assert ("#D06", !SqlBinary.GreaterThanOrEqual (Test2, Test1).Value);
156
157                         // LessThan
158                         Assert ("#D07", !SqlBinary.LessThan (Test1, Test2).Value);
159                         Assert ("#D08", !SqlBinary.LessThan (Test3, Test2).Value);
160                         Assert ("#D09", SqlBinary.LessThan (Test2, Test1).Value);
161
162                         // LessThanOrEqual
163                         Assert ("#D10", !SqlBinary.LessThanOrEqual (Test1, Test2).Value);
164                         Assert ("#D11", SqlBinary.LessThanOrEqual (Test3, Test1).Value);
165                         Assert ("#D12", SqlBinary.LessThanOrEqual (Test2, Test1).Value);
166
167                         // Equals
168                         Assert ("#D13", !Test1.Equals (Test2));
169                         Assert ("#D14", !Test3.Equals (Test2));
170                         Assert ("#D15", Test3.Equals (Test1));
171
172                         // NotEquals
173                         Assert ("#D16", SqlBinary.NotEquals (Test1, Test2).Value);
174                         Assert ("#D17", !SqlBinary.NotEquals (Test3, Test1).Value);
175                         Assert ("#D18", SqlBinary.NotEquals (Test2, Test1).Value);
176                 }
177
178                 [Test]
179                 public void CompareTo()
180                 {
181                         SqlString TestString = new SqlString ("This is a test");
182                         
183                         Assert ("#E01", Test1.CompareTo(Test2) > 0);
184                         Assert ("#E02", Test2.CompareTo(Test1) < 0);
185                         Assert ("#E03", Test1.CompareTo(Test3) == 0);
186                         
187                         try {
188                                 Test1.CompareTo (TestString);
189                                 Fail ("#E04");
190                         } catch(Exception e) {
191                                 AssertEquals ("#E05", typeof (ArgumentException), e.GetType ());
192                         }                       
193                 }
194
195                 [Test]
196                 public void GetHashCodeTest()
197                 {
198                         AssertEquals ("#F01", Test1.GetHashCode (), Test1.GetHashCode ());
199                         Assert ("#F02", Test2.GetHashCode () !=  Test1.GetHashCode ());
200                 }
201
202                 [Test]
203                 public void GetTypeTest()
204                 {
205                         AssertEquals("#G01", "System.Data.SqlTypes.SqlBinary", 
206                                      Test1.GetType().ToString());                       
207                 }
208
209                 [Test]
210                 public void Concat()
211                 {                       
212                         SqlBinary TestBinary;
213
214                         TestBinary = SqlBinary.Concat (Test2, Test3);
215                         AssertEquals ("H01", (byte)15, TestBinary [4]);
216
217                         TestBinary = SqlBinary.Concat (Test1, Test2);
218                         AssertEquals ("#H02", (byte)240, TestBinary [0]);
219                         AssertEquals ("#H03", (byte)15, TestBinary [1]);
220                 }
221
222                 [Test]
223                 public void ToSqlGuid()
224                 {
225                         SqlBinary TestBinary = new SqlBinary (new byte [16]);
226                         SqlGuid TestGuid = TestBinary.ToSqlGuid ();
227                         Assert ("#I01", !TestGuid.IsNull);
228                 }
229
230                 [Test]
231                 public void ToStringTest()
232                 {
233                         AssertEquals ("#J01", "SqlBinary(3)", Test2.ToString ());
234                         AssertEquals ("#J02", "SqlBinary(2)", Test1.ToString ());              
235                 }
236
237                 // OPERATORS
238                 [Test]
239                 public void AdditionOperator()
240                 {
241                         SqlBinary TestBinary = Test1 + Test2;
242                         AssertEquals ("#K01", (byte)240, TestBinary [0]);
243                         AssertEquals ("#K02", (byte)15, TestBinary [1]);
244                 }
245
246                 [Test]
247                 public void ComparisonOperators()
248                 {
249                         // Equality
250                         Assert ("#L01", !(Test1 == Test2).Value);
251                         Assert ("#L02", (Test3 == Test1).Value);
252
253                         // Greater than
254                         Assert ("#L03", (Test1 > Test2).Value);
255                         Assert ("#L04", !(Test3 > Test1).Value);
256
257                         // Greater than or equal
258                         Assert ("#L05", (Test1 >= Test2).Value);
259                         Assert ("#L06", (Test3 >= Test2).Value);
260
261                         // Inequality
262                         Assert ("#L07", (Test1 != Test2).Value);
263                         Assert ("#L08", !(Test3 != Test1).Value);
264
265                         // Less than
266                         Assert ("#L09", !(Test1 < Test2).Value);
267                         Assert ("#L10", !(Test3 < Test2).Value);
268
269                         // Less than or equal
270                         Assert ("#L11", !(Test1 <= Test2).Value);
271                         Assert ("#L12", (Test3 <= Test1).Value);
272                 }
273
274                 [Test]
275                 public void SqlBinaryToByteArray() 
276                 {
277                         byte [] TestByteArray = (Byte[])Test1;
278                         AssertEquals ("#M01", (byte)240, TestByteArray[0]);                     
279                 }
280
281                 [Test]
282                 public void SqlGuidToSqlBinary()
283                 {
284                         byte [] TestByteArray = new Byte [16];
285                         TestByteArray [0] = 15;
286                         TestByteArray [1] = 200;
287                         SqlGuid TestGuid = new SqlGuid (TestByteArray);
288                         
289                         SqlBinary TestBinary = (SqlBinary)TestGuid;
290                         AssertEquals ("#N01", (byte)15, TestBinary [0]);
291                 }
292
293                 [Test]
294                 public void ByteArrayToSqlBinary()
295                 {
296                         byte [] TestByteArray = new Byte [2];
297                         TestByteArray [0] = 15;
298                         TestByteArray [1] = 200;
299                         SqlBinary TestBinary = (SqlBinary)TestByteArray;
300                         AssertEquals ("#O1", (byte)15, TestBinary [0]);
301                 }
302         }
303 }
304