File: SqlGuidTest.cs

package info (click to toggle)
mono 4.6.2.7+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 778,148 kB
  • ctags: 914,052
  • sloc: cs: 5,779,509; xml: 2,773,713; ansic: 432,645; sh: 14,749; makefile: 12,361; perl: 2,488; python: 1,434; cpp: 849; asm: 531; sql: 95; sed: 16; php: 1
file content (348 lines) | stat: -rw-r--r-- 14,223 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
//
// SqlGuidTest.cs - NUnit Test Cases for System.Data.SqlTypes.SqlGuid
//
// Authors:
//   Ville Palo (vi64pa@koti.soon.fi)
//   Martin Willemoes Hansen (mwh@sysrq.dk)
//
// (C) 2002 Ville Palo
// (C) 2003 Martin Willemoes Hansen
// 

//
// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

using NUnit.Framework;
using System;
using System.Xml;
using System.Data.SqlTypes;

namespace MonoTests.System.Data.SqlTypes
{
	[TestFixture]
        public class SqlGuidTest {

		// 00000a01-0000-0000-0000-000000000000
		private SqlGuid Test1;

        	// 00000f64-0000-0000-0000-000000000000
        	private SqlGuid Test2;         	
        	private SqlGuid Test3;

		// 0000fafa-0000-0000-0000-000000000000
		private SqlGuid Test4;
        	
		[SetUp]
                public void GetReady() 
                {
                	byte [] b1 = new byte [16];
                	byte [] b2 = new byte [16];
                	byte [] b3 = new byte [16];
                	byte [] b4 = new byte [16];

                	b1 [0] = 1;
                	b1 [1] = 10;
                	b2 [0] = 100;
                	b2 [1] = 15;
                	b3 [0] = 100;
                	b3 [1] = 15;
                	b4 [0] = 250;
                	b4 [1] = 250;

                   	Test1 = new SqlGuid (b1);
			Test2 = new SqlGuid (b2);
                	Test3 = new SqlGuid (b3);
                	Test4 = new SqlGuid (b4);
                }

                // Test constructor
		[Test]
                public void Create()
                {
			// SqlGuid (Byte[])
			byte [] b = new byte [16];
                	b [0] = 100;
                	b [1] = 200;

                	try {
                		SqlGuid Test = new SqlGuid (b);

				// SqlGuid (Guid)
				Guid TestGuid = new Guid (b);
                		Test = new SqlGuid (TestGuid);

				// SqlGuid (string)
				Test = new SqlGuid ("12345678-1234-1234-1234-123456789012");

				// SqlGuid (int, short, short, byte, byte, byte, byte, byte, byte, byte, byte)
                		Test = new SqlGuid (10, 1, 2, 13, 14, 15, 16, 17, 19, 20 ,21);

                	} catch (Exception e) {
                		Assert.Fail ("#A01 " + e);
                	}
                }

                // Test public fields
		[Test]
                public void PublicFields()
                {
                        Assert.IsTrue (SqlGuid.Null.IsNull, "#B01");
                }

                // Test properties
		[Test]
                public void Properties()
                {
                	Guid ResultGuid = new Guid ("00000f64-0000-0000-0000-000000000000");		       
			Assert.IsTrue (!Test1.IsNull, "#C01");
                	Assert.IsTrue (SqlGuid.Null.IsNull, "#C02");
                	Assert.AreEqual (ResultGuid, Test2.Value, "#C03");
                }

                // PUBLIC METHODS
		[Test]
                public void CompareTo()
                {
			String TestString = "This is a test string";
			SqlGuid test1 = new SqlGuid("1AAAAAAA-BBBB-CCCC-DDDD-3EEEEEEEEEEE");
			SqlGuid test2 = new SqlGuid("1AAAAAAA-BBBB-CCCC-DDDD-2EEEEEEEEEEE");
			SqlGuid test3 = new SqlGuid("1AAAAAAA-BBBB-CCCC-DDDD-1EEEEEEEEEEE");
                        Assert.IsTrue (Test1.CompareTo (Test3) <  0, "#D01");
                        Assert.IsTrue (Test4.CompareTo (Test1) > 0, "#D02");
                        Assert.IsTrue (Test3.CompareTo (Test2) == 0, "#D03");
                        Assert.IsTrue (Test4.CompareTo (SqlGuid.Null) > 0, "#D04");
			Assert.IsTrue (test1.CompareTo (test2) >  0, "#D05");
			Assert.IsTrue (test3.CompareTo (test2) <  0, "#D06");
			
                        try {
                                Test1.CompareTo (TestString);
                                Assert.Fail("#D05");
                        } catch(Exception e) {
                                Assert.AreEqual (typeof (ArgumentException), e.GetType (), "#D06");
                        }
                }

		[Test]
                public void EqualsMethods()
                {
                        Assert.IsTrue (!Test1.Equals (Test2), "#E01");
                        Assert.IsTrue (!Test2.Equals (Test4), "#E02");
                        Assert.IsTrue (!Test2.Equals (new SqlString ("TEST")), "#E03");
                        Assert.IsTrue (Test2.Equals (Test3), "#E04");

                        // Static Equals()-method
                        Assert.IsTrue (SqlGuid.Equals (Test2, Test3).Value, "#E05");
                        Assert.IsTrue (!SqlGuid.Equals (Test1, Test2).Value, "#E06");
                }

		[Test]
                public void GetHashCodeTest()
                {
                        Assert.AreEqual (Test1.GetHashCode (), Test1.GetHashCode (), "#F01");
                	Assert.IsTrue (Test1.GetHashCode () != Test2.GetHashCode (), "#F02");
                        Assert.AreEqual (Test3.GetHashCode (), Test2.GetHashCode (), "#F02");
                }

		[Test]
                public void GetTypeTest()
                {
                        Assert.AreEqual ("System.Data.SqlTypes.SqlGuid", Test1.GetType ().ToString (), "#G01");
                        Assert.AreEqual ("System.Guid", Test3.Value.GetType ().ToString (), "#G02");
                }

		[Test]
                public void Greaters()
                {
                        // GreateThan ()
                        Assert.IsTrue (!SqlGuid.GreaterThan (Test1, Test2).Value, "#H01");
                        Assert.IsTrue (SqlGuid.GreaterThan (Test2, Test1).Value, "#H02");
                        Assert.IsTrue (!SqlGuid.GreaterThan (Test2, Test3).Value, "#H03");
                        // GreaterTharOrEqual ()
                        Assert.IsTrue (!SqlGuid.GreaterThanOrEqual (Test1, Test2).Value, "#H04");
                        Assert.IsTrue (SqlGuid.GreaterThanOrEqual (Test2, Test1).Value, "#H05");
                        Assert.IsTrue (SqlGuid.GreaterThanOrEqual (Test2, Test3).Value, "#H06");
                }

		[Test]
                public void Lessers()
                {
                        // LessThan()
                        Assert.IsTrue (!SqlGuid.LessThan (Test2, Test3).Value, "#I01");
                        Assert.IsTrue (!SqlGuid.LessThan (Test2, Test1).Value, "#I02");
                        Assert.IsTrue (SqlGuid.LessThan (Test1, Test2).Value, "#I03");

                        // LessThanOrEqual ()
                        Assert.IsTrue (SqlGuid.LessThanOrEqual (Test1, Test2).Value, "#I04");
                        Assert.IsTrue (!SqlGuid.LessThanOrEqual (Test2, Test1).Value, "#I05");
                        Assert.IsTrue (SqlGuid.LessThanOrEqual (Test2, Test3).Value, "#I06");
                        Assert.IsTrue (SqlGuid.LessThanOrEqual (Test4, SqlGuid.Null).IsNull, "#I07");
                }

		[Test]
                public void NotEquals()
                {
                        Assert.IsTrue (SqlGuid.NotEquals (Test1, Test2).Value, "#J01");
                        Assert.IsTrue (SqlGuid.NotEquals (Test2, Test1).Value, "#J02");
                        Assert.IsTrue (SqlGuid.NotEquals (Test3, Test1).Value, "#J03");
                        Assert.IsTrue (!SqlGuid.NotEquals (Test3, Test2).Value, "#J04");
                        Assert.IsTrue (SqlGuid.NotEquals (SqlGuid.Null, Test2).IsNull, "#J05");
                }

		[Test]
                public void Parse()
                {
                        try {
                                SqlGuid.Parse (null);
                                Assert.Fail ("#K01");
                        } catch (Exception e) {
                                Assert.AreEqual (typeof (ArgumentNullException), e.GetType (), "#K02");
                        }

                        try {
                                SqlGuid.Parse ("not-a-number");
                                Assert.Fail ("#K03");
                        } catch (Exception e) {
                                Assert.AreEqual (typeof (FormatException), e.GetType (), "#K04");
                        }

                         try {
                                SqlGuid.Parse ("9e400");
                                Assert.Fail ("#K05");
                        } catch (Exception e) {
                                Assert.AreEqual (typeof (FormatException), e.GetType (), "#K06");
                        }

                        Assert.AreEqual(new Guid("87654321-0000-0000-0000-000000000000"), SqlGuid.Parse ("87654321-0000-0000-0000-000000000000").Value, "#K07");
                }

		[Test]
                public void Conversions()
                {
			// ToByteArray ()
			Assert.AreEqual ((byte)1, Test1.ToByteArray () [0], "#L01");
			Assert.AreEqual ((byte)15, Test2.ToByteArray () [1], "#L02");

			// ToSqlBinary ()
			byte [] b = new byte [2]; 
                	b [0] = 100;
                	b [1] = 15;
		       
                        Assert.AreEqual (new SqlBinary (b), Test3.ToSqlBinary (), "#L03");

                        // ToSqlString ()
                        Assert.AreEqual ("00000a01-0000-0000-0000-000000000000",  Test1.ToSqlString ().Value, "#L04");
                        Assert.AreEqual ("0000fafa-0000-0000-0000-000000000000", Test4.ToSqlString ().Value, "#L05");

                        // ToString ()
                        Assert.AreEqual ("00000a01-0000-0000-0000-000000000000", Test1.ToString (), "#L06");
                        Assert.AreEqual ("0000fafa-0000-0000-0000-000000000000", Test4.ToString (), "#L07");
                }

                // OPERATORS

		[Test]
                public void ThanOrEqualOperators()
                {
                        // == -operator
                        Assert.IsTrue ((Test3 == Test2).Value, "#M01");
                        Assert.IsTrue (!(Test1 == Test2).Value, "#M02");
                        Assert.IsTrue ((Test1 == SqlGuid.Null).IsNull, "#M03");
                        
                        // != -operator
                        Assert.IsTrue (!(Test2 != Test3).Value, "#M04");
                        Assert.IsTrue ((Test1 != Test3).Value, "#M05");
                        Assert.IsTrue ((Test1 != SqlGuid.Null).IsNull, "#M06");

                        // > -operator
                        Assert.IsTrue ((Test2 > Test1).Value, "#M07");
                        Assert.IsTrue (!(Test1 > Test3).Value, "#M08");
                        Assert.IsTrue (!(Test3 > Test2).Value, "#M09");
                        Assert.IsTrue ((Test1 > SqlGuid.Null).IsNull, "#M10");

                        // >=  -operator
                        Assert.IsTrue (!(Test1 >= Test3).Value, "#M12");
                        Assert.IsTrue ((Test3 >= Test1).Value, "#M13");
                        Assert.IsTrue ((Test3 >= Test2).Value, "#M14");
                        Assert.IsTrue ((Test1 >= SqlGuid.Null).IsNull, "#M15");

                        // < -operator
                        Assert.IsTrue (!(Test2 < Test1).Value, "#M16");
                        Assert.IsTrue ((Test1 < Test3).Value, "#M17");
                        Assert.IsTrue (!(Test2 < Test3).Value, "#M18");
                        Assert.IsTrue ((Test1 < SqlGuid.Null).IsNull, "#M19");

                        // <= -operator
                        Assert.IsTrue ((Test1 <= Test3).Value, "#M20");
                        Assert.IsTrue (!(Test3 <= Test1).Value, "#M21");
                        Assert.IsTrue ((Test2 <= Test3).Value, "#M22");
                        Assert.IsTrue ((Test1 <= SqlGuid.Null).IsNull, "#M23");
                }

		[Test]
		public void SqlBinaryToSqlGuid()
		{
			byte [] b = new byte [16];
			b [0] = 100;
			b [1] = 200;
			SqlBinary TestBinary = new SqlBinary (b);
			
			Assert.AreEqual (new Guid("0000c864-0000-0000-0000-000000000000"), ((SqlGuid)TestBinary).Value, "#N01");
		}

		[Test]
		public void SqlGuidToGuid()
		{
			Assert.AreEqual (new Guid("00000a01-0000-0000-0000-000000000000"), (Guid)Test1, "#O01");
			Assert.AreEqual (new Guid("00000f64-0000-0000-0000-000000000000"), (Guid)Test2, "#O02");
		}		

		[Test]
                public void SqlStringToSqlGuid()
                {
                        SqlString TestString = new SqlString ("Test string");
                        SqlString TestString100 = new SqlString ("0000c864-0000-0000-0000-000000000000");

                        Assert.AreEqual (new Guid("0000c864-0000-0000-0000-000000000000"), ((SqlGuid)TestString100).Value, "#P01");

                        try {
                                SqlGuid test = (SqlGuid)TestString;
                                Assert.Fail ("#P02");
                        } catch(Exception e) {
                                Assert.AreEqual (typeof (FormatException), e.GetType (), "#P03");
                        }
                }
		
		[Test]
		public void GuidToSqlGuid()
		{
			Guid TestGuid = new Guid("0000c864-0000-0000-0000-000007650000");
			Assert.AreEqual (new SqlGuid("0000c864-0000-0000-0000-000007650000"), (SqlGuid)TestGuid, "#Q01");
		}
		[Test]
		public void GetXsdTypeTest ()
		{
			XmlQualifiedName qualifiedName = SqlGuid.GetXsdType (null);
			Assert.AreEqual ("string", qualifiedName.Name, "#A01");
		}
        }
}