UniqueIdTest.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. //
  2. // UniqueIdTest.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. // Jonathan Pryor <[email protected]>
  7. //
  8. // Copyright (C) 2006, 2009 Novell, Inc. http://www.novell.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. using System;
  30. using System.Linq;
  31. using System.Text;
  32. using System.Xml;
  33. using NUnit.Framework;
  34. namespace MonoTests.System.Xml
  35. {
  36. [TestFixture]
  37. public class UniqueIdTest
  38. {
  39. [Test]
  40. public void TestDefault ()
  41. {
  42. UniqueId id = new UniqueId ();
  43. Assert.IsTrue (id.IsGuid, "#1");
  44. Guid g = Guid.NewGuid ();
  45. UniqueId a = new UniqueId (g);
  46. UniqueId b = new UniqueId (g.ToByteArray ());
  47. Assert.AreEqual (a, b, "#2");
  48. Assert.AreEqual ("urn:uuid:", a.ToString ().Substring (0, 9), "#3");
  49. a = new UniqueId ("foo");
  50. Assert.AreEqual ("foo", a.ToString (), "#4");
  51. }
  52. [Test]
  53. [ExpectedException (typeof (FormatException))]
  54. public void ZeroLengthCtor ()
  55. {
  56. new UniqueId ("");
  57. }
  58. [Test]
  59. [ExpectedException (typeof (ArgumentNullException))]
  60. public void CtorNull1 ()
  61. {
  62. new UniqueId ((string) null);
  63. }
  64. [Test, ExpectedException (typeof (ArgumentNullException))]
  65. public void Ctor_IdNull ()
  66. {
  67. new UniqueId (null, 0);
  68. }
  69. [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
  70. public void Ctor_OffsetNegative ()
  71. {
  72. new UniqueId (new byte[0], -1);
  73. }
  74. [Test, ExpectedException (typeof (ArgumentException))]
  75. public void Ctor_OffsetTooLarge ()
  76. {
  77. new UniqueId (new byte[16], 16);
  78. }
  79. [Test, ExpectedException (typeof (ArgumentException))]
  80. public void Ctor_BufferTooSmall ()
  81. {
  82. new UniqueId (new byte[15], 0);
  83. }
  84. [Test]
  85. public void Ctor_Id ()
  86. {
  87. byte[] buf = Encoding.UTF8.GetBytes ("Hello!")
  88. .Concat (new Guid ().ToByteArray ()).ToArray ();
  89. var g = new UniqueId (buf, "Hello!".Length);
  90. Assert.AreEqual (new UniqueId (new Guid ()), g);
  91. }
  92. [Test, ExpectedException (typeof (ArgumentNullException))]
  93. public void Ctor_CharsNull ()
  94. {
  95. new UniqueId (null, 0, 1);
  96. }
  97. [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
  98. public void Ctor_Chars_OffsetNegative ()
  99. {
  100. new UniqueId (new char[2], -1, 1);
  101. }
  102. [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
  103. public void Ctor_Chars_OffsetTooLarge ()
  104. {
  105. new UniqueId (new char[2], 2, 1);
  106. }
  107. [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
  108. public void Ctor_Chars_CountNegative ()
  109. {
  110. new UniqueId (new char[2], 0, -1);
  111. }
  112. [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
  113. public void Ctor_Chars_CountTooLarge ()
  114. {
  115. new UniqueId (new char[2], 1, 2);
  116. }
  117. [Test, ExpectedException (typeof (FormatException))]
  118. public void Ctor_Chars_CountZero ()
  119. {
  120. new UniqueId (new char[2], 0, 0);
  121. }
  122. [Test]
  123. public void Ctor_Chars ()
  124. {
  125. var a = new UniqueId ("Hello!".ToCharArray (), 0, 5);
  126. Assert.IsFalse (a.IsGuid);
  127. Assert.AreEqual ("Hello", a.ToString ());
  128. a = new UniqueId ();
  129. var b = new UniqueId (a.ToString ().ToCharArray (), 0, 45);
  130. Assert.IsTrue (b.IsGuid);
  131. Assert.AreEqual (a, b);
  132. string s = "foo" + a.ToString () + "bar";
  133. b = new UniqueId (s.ToCharArray (), 3, s.Length-6);
  134. Assert.IsTrue (b.IsGuid);
  135. Assert.AreEqual (a, b);
  136. a = new UniqueId (new Guid ());
  137. b = new UniqueId (a.ToString ().ToCharArray (), 0, 45);
  138. Assert.IsFalse (b.IsGuid);
  139. Assert.AreEqual (a, b);
  140. }
  141. [Test]
  142. public void CharArrayLength ()
  143. {
  144. var u = new UniqueId ("string");
  145. Assert.AreEqual (6, u.CharArrayLength);
  146. Assert.AreEqual (u.ToString().Length, u.CharArrayLength);
  147. u = new UniqueId (new Guid());
  148. Assert.AreEqual (45, u.CharArrayLength);
  149. Assert.AreEqual (u.ToString().Length, u.CharArrayLength);
  150. }
  151. [Test]
  152. public void Equals ()
  153. {
  154. var a = new UniqueId ("a");
  155. var b = new UniqueId (new Guid ());
  156. Assert.IsFalse (a.Equals (null));
  157. Assert.IsFalse (b.Equals (null));
  158. Assert.IsFalse (a.Equals (b));
  159. Assert.IsFalse (a == b);
  160. Assert.IsTrue (a != b);
  161. var c = new UniqueId ("a");
  162. Assert.IsTrue (a.Equals (c));
  163. Assert.IsTrue (a == c);
  164. }
  165. [Test]
  166. public new void GetHashCode ()
  167. {
  168. Assert.AreEqual ("a".GetHashCode (), new UniqueId ("a").GetHashCode ());
  169. // TODO: What does .NET do for UniqueId.GetHashCode() when UniqueId.IsGuid==true?
  170. // Assert.AreEqual (new Guid ().GetHashCode (), new UniqueId (new Guid ()).GetHashCode ());
  171. }
  172. [Test]
  173. public void IsGuid ()
  174. {
  175. var a = new UniqueId ("string");
  176. Assert.IsFalse (a.IsGuid);
  177. a = new UniqueId ();
  178. Assert.IsTrue (a.IsGuid);
  179. a = new UniqueId (new Guid ());
  180. Assert.IsFalse (a.IsGuid);
  181. }
  182. [Test, ExpectedException (typeof (ArgumentNullException))]
  183. public void ToCharArray_CharsNull ()
  184. {
  185. new UniqueId ("s").ToCharArray (null, 0);
  186. }
  187. [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
  188. public void ToCharArray_CharsTooSmall ()
  189. {
  190. new UniqueId ().ToCharArray (new char[15], 0);
  191. }
  192. [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
  193. public void ToCharArray_OffsetNegative ()
  194. {
  195. new UniqueId ("s").ToCharArray (new char[1], -1);
  196. }
  197. [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
  198. public void ToCharArray_OffsetOutOfRange ()
  199. {
  200. new UniqueId ("s").ToCharArray (new char[1], 2);
  201. }
  202. [Test]
  203. public void ToCharArray ()
  204. {
  205. char[] chars = new char[4];
  206. Assert.AreEqual (4, new UniqueId ("data").ToCharArray (chars, 0));
  207. Assert.AreEqual ("data".ToCharArray (), chars);
  208. chars = new char[45];
  209. Assert.AreEqual (45, new UniqueId (new Guid ()).ToCharArray (chars, 0));
  210. Assert.AreEqual (("urn:uuid:" + new Guid ().ToString ()).ToCharArray (), chars);
  211. }
  212. [Test]
  213. public new void ToString ()
  214. {
  215. Assert.AreEqual ("string", new UniqueId ("string").ToString ());
  216. Assert.AreEqual (
  217. "urn:uuid:00000000-0000-0000-0000-000000000000",
  218. new UniqueId (new Guid ()).ToString ());
  219. Guid g = Guid.NewGuid ();
  220. Assert.AreEqual ("urn:uuid:" + g.ToString (), new UniqueId (g).ToString ());
  221. }
  222. [Test, ExpectedException (typeof (ArgumentNullException))]
  223. public void TryGetGuid_BufferNull ()
  224. {
  225. new UniqueId ().TryGetGuid (null, 0);
  226. }
  227. [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
  228. public void TryGetGuid_BufferTooSmall ()
  229. {
  230. new UniqueId ().TryGetGuid (new byte[16], 1);
  231. }
  232. [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
  233. public void TryGetGuid_OffsetNegative ()
  234. {
  235. new UniqueId ().TryGetGuid (new byte[16], -1);
  236. }
  237. [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
  238. public void TryGetGuid_OffsetTooLarge ()
  239. {
  240. new UniqueId ().TryGetGuid (new byte[16], 16);
  241. }
  242. [Test]
  243. public void TryGetGuid ()
  244. {
  245. Guid g;
  246. Assert.IsFalse (new UniqueId ("string").TryGetGuid (out g));
  247. Assert.IsFalse (new UniqueId ("string").TryGetGuid (new byte [16], 0));
  248. Assert.IsFalse (new UniqueId (new Guid ()).TryGetGuid (out g));
  249. Assert.IsFalse (new UniqueId (new Guid ()).TryGetGuid (new byte [16], 0));
  250. g = Guid.NewGuid ();
  251. Guid g2;
  252. byte[] bg;
  253. Assert.IsTrue (new UniqueId (g).TryGetGuid (out g2));
  254. Assert.AreEqual (g, g2);
  255. Assert.IsTrue (new UniqueId (g).TryGetGuid (bg = new byte [17], 1));
  256. Assert.AreEqual (g, new Guid (bg.Skip (1).ToArray ()));
  257. }
  258. }
  259. }