StringCollectionTest.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // System.Collections.Specialized.StringCollection.cs
  2. //
  3. // Authors:
  4. // John Barnette ([email protected])
  5. // Martin Willemoes Hansen ([email protected])
  6. //
  7. // (C) Copyright 2001 John Barnette
  8. // (C) Copyright 2003 Martin Willemoes Hansen
  9. //
  10. using NUnit.Framework;
  11. using System.Collections.Specialized;
  12. namespace MonoTests.System.Collections.Specialized {
  13. [TestFixture]
  14. public class StringCollectionTest {
  15. private StringCollection sc;
  16. string[] strings = {
  17. "foo",
  18. "bar",
  19. "baz",
  20. "john",
  21. "paul",
  22. "george",
  23. "ringo"
  24. };
  25. [SetUp]
  26. public void GetReady()
  27. {
  28. sc = new StringCollection();
  29. sc.AddRange(strings);
  30. }
  31. // Simple Tests
  32. [Test]
  33. public void SimpleCount()
  34. {
  35. Assertion.Assert(sc.Count == 7);
  36. }
  37. [Test]
  38. public void SimpleIsReadOnly()
  39. {
  40. Assertion.Assert(!sc.IsReadOnly);
  41. }
  42. [Test]
  43. public void SimpleIsSynchronized()
  44. {
  45. Assertion.Assert(!sc.IsSynchronized);
  46. }
  47. [Test]
  48. public void SimpleItemGet()
  49. {
  50. for(int i = 0; i < strings.Length; i++) {
  51. Assertion.Assert(strings[i].Equals(sc[i]));
  52. }
  53. }
  54. [Test]
  55. public void SimpleItemSet()
  56. {
  57. sc[0] = "bob";
  58. Assertion.Assert(sc[0].Equals("bob"));
  59. }
  60. [Test]
  61. public void SimpleSyncRoot()
  62. {
  63. Assertion.Assert(sc.Equals(sc.SyncRoot));
  64. }
  65. [Test]
  66. public void SimpleAdd()
  67. {
  68. int index = sc.Add("chuck");
  69. Assertion.Assert(index == strings.Length);
  70. Assertion.Assert(sc[strings.Length].Equals("chuck"));
  71. }
  72. [Test]
  73. public void SimpleAddRange()
  74. {
  75. string[] newStrings = {
  76. "peter",
  77. "paul",
  78. "mary"
  79. };
  80. int index = sc.Count;
  81. sc.AddRange(newStrings);
  82. Assertion.Assert(sc.Count == index + newStrings.Length);
  83. for (int i = 0; i+index <= sc.Count-1; i++) {
  84. Assertion.Assert(newStrings[i].Equals(sc[i+index]));
  85. }
  86. }
  87. [Test]
  88. public void SimpleClear()
  89. {
  90. sc.Clear();
  91. Assertion.Assert(sc.Count == 0);
  92. }
  93. [Test]
  94. public void SimpleContains()
  95. {
  96. Assertion.Assert(sc.Contains(strings[0]));
  97. Assertion.Assert(!sc.Contains("NOT CONTAINED"));
  98. }
  99. [Test]
  100. public void SimpleCopyTo()
  101. {
  102. string[] copyArray = new string[sc.Count];
  103. sc.CopyTo(copyArray, 0);
  104. for (int i = 0; i < copyArray.Length; i++) {
  105. Assertion.Assert(copyArray[i] == sc[i]);
  106. }
  107. }
  108. [Test]
  109. public void SimpleGetEnumerator()
  110. {
  111. int index = 0;
  112. foreach(string s in sc) {
  113. Assertion.Assert(s.Equals(strings[index]));
  114. index++;
  115. }
  116. }
  117. [Test]
  118. public void SimpleIndexOf()
  119. {
  120. Assertion.Assert(sc.IndexOf(strings[0]) == 0);
  121. }
  122. [Test]
  123. public void SimpleInsert()
  124. {
  125. int index = 3;
  126. int oldCount = sc.Count;
  127. string before = sc[index - 1];
  128. string current = sc[index];
  129. string after = sc[index + 1];
  130. string newStr = "paco";
  131. sc.Insert(index, newStr);
  132. Assertion.Assert(sc.Count == oldCount + 1);
  133. Assertion.Assert(sc[index].Equals(newStr));
  134. Assertion.Assert(sc[index-1].Equals(before));
  135. Assertion.Assert(sc[index+1].Equals(current));
  136. Assertion.Assert(sc[index+2].Equals(after));
  137. }
  138. [Test]
  139. public void SimpleRemove()
  140. {
  141. int oldCount = sc.Count;
  142. sc.Remove(strings[0]);
  143. Assertion.Assert(oldCount == sc.Count + 1);
  144. Assertion.Assert(!sc.Contains(strings[0]));
  145. }
  146. [Test]
  147. public void SimpleRemoveAt()
  148. {
  149. int index = 3;
  150. int oldCount = sc.Count;
  151. string after = sc[index+1];
  152. sc.RemoveAt(index);
  153. Assertion.Assert(oldCount == sc.Count + 1);
  154. Assertion.Assert(sc[index].Equals(after));
  155. }
  156. }
  157. }