2003-06-01 Ville Palo <vi64pa@kolumbus.fi>
[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 using NUnit.Framework;
13 using System;
14 using System.Data.SqlTypes;
15
16 namespace MonoTests.System.Data.SqlTypes
17 {
18         [TestFixture]
19         public class SqlBinaryTest : Assertion {
20         
21                 SqlBinary Test1;
22                 SqlBinary Test2;
23                 SqlBinary Test3;
24
25                 [SetUp]
26                 public void GetReady() 
27                 {
28                         byte [] b1 = new Byte [2];
29                         byte [] b2 = new Byte [3];
30                         byte [] b3 = new Byte [2];
31
32                         b1 [0] = 240;
33                         b1 [1] = 15;
34                         b2 [0] = 10;
35                         b2 [1] = 10;
36                         b2 [2] = 10;
37                         b3 [0] = 240;
38                         b3 [1] = 15;
39
40                         Test1 = new SqlBinary(b1);
41                         Test2 = new SqlBinary(b2);
42                         Test3 = new SqlBinary(b3);
43                 }
44
45                 // Test constructor
46                 [Test]
47                 public void Create()
48                 {
49                         byte [] b = new byte [3];                        
50                         SqlBinary Test = new SqlBinary (b);
51                         Assert ("#A01", !(Test.IsNull)); 
52                 }
53
54                 // Test public fields
55                 [Test]
56                 public void PublicFields()
57                 {
58                         Assert ("#B01", SqlBinary.Null.IsNull);
59                 }
60
61                 // Test properties
62                 [Test]
63                 public void Properties()
64                 {
65                         byte [] b = new byte [2];
66                         b [0] = 64;
67                         b [1] = 128;
68
69                         SqlBinary TestBinary = new SqlBinary (b);
70
71                         // IsNull
72                         Assert ("#C01", SqlBinary.Null.IsNull);
73
74                         // Item
75                         AssertEquals ("#C02", (byte)128, TestBinary [1]);
76                         AssertEquals ("#C03", (byte)64, TestBinary [0]);
77
78                         // FIXME: MSDN says that should throw SqlNullValueException
79                         // but throws IndexOutOfRangeException
80                         try {
81                                 byte test = TestBinary [TestBinary.Length];
82                                 Fail ("#C04");
83                         } catch (Exception e) {
84                                 AssertEquals ("#C05", typeof (SqlNullValueException),
85                                             e.GetType ());
86                         }
87                  
88                         try {
89                                 byte test = SqlBinary.Null [2];
90                                 Fail ("#C06");
91                         } catch (Exception e) {
92                                 AssertEquals ("#C07", typeof (SqlNullValueException),
93                                             e.GetType ());
94                         }
95
96                         // Length
97                         AssertEquals ("#C08", 2, TestBinary.Length);    
98
99                         try {
100                                 int test = SqlBinary.Null.Length;
101                                 Fail ("#C09");
102                         } catch (Exception e) {
103                                 AssertEquals ("#C10", typeof (SqlNullValueException),
104                                             e.GetType ());
105                         }
106
107                         // Value
108                         AssertEquals ("#C11", (byte)128, TestBinary [1]);
109                         AssertEquals ("#C12", (byte)64, TestBinary [0]);               
110
111                         try {
112                                 Byte [] test = SqlBinary.Null.Value;
113                                 Fail ("#C13");
114                         } catch (Exception e) {
115                                 AssertEquals ("#C14", typeof (SqlNullValueException),
116                                             e.GetType ());
117                         }
118                 }
119
120                 // Methods 
121                 [Test]
122                 public void ComparisonMethods()
123                 {
124                         // GreaterThan
125                         Assert ("#D01", SqlBinary.GreaterThan (Test1, Test2).Value);
126                         Assert ("#D02", SqlBinary.GreaterThan (Test3, Test2).Value);
127                         Assert ("#D03", !SqlBinary.GreaterThan (Test2, Test1).Value);
128                         
129                         // GreaterThanOrEqual
130                         Assert ("#D04", SqlBinary.GreaterThanOrEqual (Test1, Test2).Value);
131                         Assert ("#D05", SqlBinary.GreaterThanOrEqual (Test1, Test2).Value);
132                         Assert ("#D06", !SqlBinary.GreaterThanOrEqual (Test2, Test1).Value);
133
134                         // LessThan
135                         Assert ("#D07", !SqlBinary.LessThan (Test1, Test2).Value);
136                         Assert ("#D08", !SqlBinary.LessThan (Test3, Test2).Value);
137                         Assert ("#D09", SqlBinary.LessThan (Test2, Test1).Value);
138
139                         // LessThanOrEqual
140                         Assert ("#D10", !SqlBinary.LessThanOrEqual (Test1, Test2).Value);
141                         Assert ("#D11", SqlBinary.LessThanOrEqual (Test3, Test1).Value);
142                         Assert ("#D12", SqlBinary.LessThanOrEqual (Test2, Test1).Value);
143
144                         // Equals
145                         Assert ("#D13", !Test1.Equals (Test2));
146                         Assert ("#D14", !Test3.Equals (Test2));
147                         Assert ("#D15", Test3.Equals (Test1));
148
149                         // NotEquals
150                         Assert ("#D16", SqlBinary.NotEquals (Test1, Test2).Value);
151                         Assert ("#D17", !SqlBinary.NotEquals (Test3, Test1).Value);
152                         Assert ("#D18", SqlBinary.NotEquals (Test2, Test1).Value);
153                 }
154
155                 [Test]
156                 public void CompareTo()
157                 {
158                         SqlString TestString = new SqlString ("This is a test");
159                         
160                         Assert ("#E01", Test1.CompareTo(Test2) > 0);
161                         Assert ("#E02", Test2.CompareTo(Test1) < 0);
162                         Assert ("#E03", Test1.CompareTo(Test3) == 0);
163                         
164                         try {
165                                 Test1.CompareTo (TestString);
166                                 Fail ("#E04");
167                         } catch(Exception e) {
168                                 AssertEquals ("#E05", typeof (ArgumentException), e.GetType ());
169                         }                       
170                 }
171
172                 [Test]
173                 public void GetHashCodeTest()
174                 {
175                         AssertEquals ("#F01", Test1.GetHashCode (), Test1.GetHashCode ());
176                         Assert ("#F02", Test2.GetHashCode () !=  Test1.GetHashCode ());
177                 }
178
179                 [Test]
180                 public void GetTypeTest()
181                 {
182                         AssertEquals("#G01", "System.Data.SqlTypes.SqlBinary", 
183                                      Test1.GetType().ToString());                       
184                 }
185
186                 [Test]
187                 public void Concat()
188                 {                       
189                         SqlBinary TestBinary;
190
191                         TestBinary = SqlBinary.Concat (Test2, Test3);
192                         AssertEquals ("H01", (byte)15, TestBinary [4]);
193
194                         TestBinary = SqlBinary.Concat (Test1, Test2);
195                         AssertEquals ("#H02", (byte)240, TestBinary [0]);
196                         AssertEquals ("#H03", (byte)15, TestBinary [1]);
197                 }
198
199                 [Test]
200                 public void ToSqlGuid()
201                 {
202                         SqlBinary TestBinary = new SqlBinary (new byte [16]);
203                         SqlGuid TestGuid = TestBinary.ToSqlGuid ();
204                         Assert ("#I01", !TestGuid.IsNull);
205                 }
206
207                 [Test]
208                 public void ToStringTest()
209                 {
210                         AssertEquals ("#J01", "SqlBinary(3)", Test2.ToString ());
211                         AssertEquals ("#J02", "SqlBinary(2)", Test1.ToString ());              
212                 }
213
214                 // OPERATORS
215                 [Test]
216                 public void AdditionOperator()
217                 {
218                         SqlBinary TestBinary = Test1 + Test2;
219                         AssertEquals ("#K01", (byte)240, TestBinary [0]);
220                         AssertEquals ("#K02", (byte)15, TestBinary [1]);
221                 }
222
223                 [Test]
224                 public void ComparisonOperators()
225                 {
226                         // Equality
227                         Assert ("#L01", !(Test1 == Test2).Value);
228                         Assert ("#L02", (Test3 == Test1).Value);
229
230                         // Greater than
231                         Assert ("#L03", (Test1 > Test2).Value);
232                         Assert ("#L04", !(Test3 > Test1).Value);
233
234                         // Greater than or equal
235                         Assert ("#L05", (Test1 >= Test2).Value);
236                         Assert ("#L06", (Test3 >= Test2).Value);
237
238                         // Inequality
239                         Assert ("#L07", (Test1 != Test2).Value);
240                         Assert ("#L08", !(Test3 != Test1).Value);
241
242                         // Less than
243                         Assert ("#L09", !(Test1 < Test2).Value);
244                         Assert ("#L10", !(Test3 < Test2).Value);
245
246                         // Less than or equal
247                         Assert ("#L11", !(Test1 <= Test2).Value);
248                         Assert ("#L12", (Test3 <= Test1).Value);
249                 }
250
251                 [Test]
252                 public void SqlBinaryToByteArray() 
253                 {
254                         byte [] TestByteArray = (Byte[])Test1;
255                         AssertEquals ("#M01", (byte)240, TestByteArray[0]);                     
256                 }
257
258                 [Test]
259                 public void SqlGuidToSqlBinary()
260                 {
261                         byte [] TestByteArray = new Byte [16];
262                         TestByteArray [0] = 15;
263                         TestByteArray [1] = 200;
264                         SqlGuid TestGuid = new SqlGuid (TestByteArray);
265                         
266                         SqlBinary TestBinary = (SqlBinary)TestGuid;
267                         AssertEquals ("#N01", (byte)15, TestBinary [0]);
268                 }
269
270                 [Test]
271                 public void ByteArrayToSqlBinary()
272                 {
273                         byte [] TestByteArray = new Byte [2];
274                         TestByteArray [0] = 15;
275                         TestByteArray [1] = 200;
276                         SqlBinary TestBinary = (SqlBinary)TestByteArray;
277                         AssertEquals ("#O1", (byte)15, TestBinary [0]);
278                 }
279         }
280 }
281