PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/mcs/class/corlib/Test/System.Collections.ObjectModel/CollectionTest.cs

https://bitbucket.org/danipen/mono
C# | 190 lines | 128 code | 35 blank | 27 comment | 0 complexity | 2c268343fda831c352880e98a5cc2baf MD5 | raw file
Possible License(s): Unlicense, Apache-2.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0
  1. //
  2. // MonoTests.System.Collections.Generic.Test.CollectionTest
  3. //
  4. // Authors:
  5. // David Waite (mass@akuma.org)
  6. //
  7. // Copyright (C) 2005 David Waite
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. #if NET_2_0
  29. using System;
  30. using System.Collections;
  31. using System.Collections.Generic;
  32. using System.Collections.ObjectModel;
  33. using System.Text;
  34. using NUnit.Framework;
  35. namespace MonoTests.System.Collections.ObjectModel
  36. {
  37. [TestFixture]
  38. public class CollectionTest
  39. {
  40. [Test]
  41. public void UsableSyncLockTest ()
  42. {
  43. List <int> list = new List <int> ();
  44. Collection <int> c = new Collection <int> (list);
  45. object listLock = ((ICollection) list).SyncRoot;
  46. object cLock = ((ICollection) c).SyncRoot;
  47. Assert.AreSame (listLock, cLock);
  48. }
  49. [Test]
  50. public void UnusableSyncLockTest ()
  51. {
  52. UnimplementedList <int> list = new UnimplementedList <int> ();
  53. Collection <int> c = new Collection <int> (list);
  54. object cLock = ((ICollection) c).SyncRoot;
  55. Assert.IsNotNull (cLock);
  56. Assert.IsTrue (cLock.GetType ().Equals (typeof (object)));
  57. }
  58. [Test]
  59. public void ICollection_CopyTo ()
  60. {
  61. Collection <int> c = new Collection <int> ();
  62. c.Add (10);
  63. c.Add (7);
  64. Array array = Array.CreateInstance (typeof (int), 2);
  65. ((ICollection) c).CopyTo (array, 0);
  66. Assert.AreEqual (10, array.GetValue (0), "#A1");
  67. Assert.AreEqual (7, array.GetValue (1), "#A2");
  68. array = Array.CreateInstance (typeof (int), 5);
  69. ((ICollection) c).CopyTo (array, 2);
  70. Assert.AreEqual (0, array.GetValue (0), "#B1");
  71. Assert.AreEqual (0, array.GetValue (1), "#B2");
  72. Assert.AreEqual (10, array.GetValue (2), "#B3");
  73. Assert.AreEqual (7, array.GetValue (3), "#B4");
  74. Assert.AreEqual (0, array.GetValue (4), "#B5");
  75. array = Array.CreateInstance (typeof (object), 5);
  76. ((ICollection) c).CopyTo (array, 2);
  77. Assert.IsNull (array.GetValue (0), "#C1");
  78. Assert.IsNull (array.GetValue (1), "#C2");
  79. Assert.AreEqual (10, array.GetValue (2), "#C3");
  80. Assert.AreEqual (7, array.GetValue (3), "#C4");
  81. Assert.IsNull (array.GetValue (4), "#C2");
  82. }
  83. class UnimplementedList <T> : IList <T>
  84. {
  85. #region IList <T> Members
  86. int IList <T>.IndexOf (T item)
  87. {
  88. throw new Exception ("The method or operation is not implemented.");
  89. }
  90. void IList <T>.Insert (int index, T item)
  91. {
  92. throw new Exception ("The method or operation is not implemented.");
  93. }
  94. void IList <T>.RemoveAt (int index)
  95. {
  96. throw new Exception ("The method or operation is not implemented.");
  97. }
  98. T IList <T>.this [int index]
  99. {
  100. get
  101. {
  102. throw new Exception ("The method or operation is not implemented.");
  103. }
  104. set
  105. {
  106. throw new Exception ("The method or operation is not implemented.");
  107. }
  108. }
  109. #endregion
  110. #region ICollection <T> Members
  111. void ICollection <T>.Add (T item)
  112. {
  113. throw new Exception ("The method or operation is not implemented.");
  114. }
  115. void ICollection <T>.Clear ()
  116. {
  117. throw new Exception ("The method or operation is not implemented.");
  118. }
  119. bool ICollection <T>.Contains (T item)
  120. {
  121. throw new Exception ("The method or operation is not implemented.");
  122. }
  123. void ICollection <T>.CopyTo (T [] array, int arrayIndex)
  124. {
  125. throw new Exception ("The method or operation is not implemented.");
  126. }
  127. int ICollection <T>.Count
  128. {
  129. get { throw new Exception ("The method or operation is not implemented."); }
  130. }
  131. bool ICollection <T>.IsReadOnly
  132. {
  133. get { throw new Exception ("The method or operation is not implemented."); }
  134. }
  135. bool ICollection <T>.Remove (T item)
  136. {
  137. throw new Exception ("The method or operation is not implemented.");
  138. }
  139. #endregion
  140. #region IEnumerable <T> Members
  141. IEnumerator <T> IEnumerable <T>.GetEnumerator ()
  142. {
  143. throw new Exception ("The method or operation is not implemented.");
  144. }
  145. #endregion
  146. #region IEnumerable Members
  147. IEnumerator IEnumerable.GetEnumerator ()
  148. {
  149. throw new Exception ("The method or operation is not implemented.");
  150. }
  151. #endregion
  152. }
  153. }
  154. }
  155. #endif