StringCollectionTest.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /* System.Collections.Specialized.StringCollection.cs
  2. * Authors:
  3. * John Barnette ([email protected])
  4. *
  5. * Copyright (C) 2001 John Barnette
  6. */
  7. using NUnit.Framework;
  8. using System.Collections.Specialized;
  9. namespace MonoTests.System.Collections.Specialized {
  10. public class StringCollectionTest : TestCase {
  11. public static ITest Suite {
  12. get {
  13. return new TestSuite(typeof (StringCollectionTest));
  14. }
  15. }
  16. private StringCollection sc;
  17. string[] strings = {
  18. "foo",
  19. "bar",
  20. "baz",
  21. "john",
  22. "paul",
  23. "george",
  24. "ringo"
  25. };
  26. public StringCollectionTest() : base("MonoTests.System.Collections.Specialized.StringCollectionTest testsuite") {}
  27. public StringCollectionTest(string name) : base(name) {}
  28. protected override void SetUp() {
  29. sc = new StringCollection();
  30. sc.AddRange(strings);
  31. }
  32. // Simple Tests
  33. public void TestSimpleCount() {
  34. Assert(sc.Count == 7);
  35. }
  36. public void TestSimpleIsReadOnly() {
  37. Assert(!sc.IsReadOnly);
  38. }
  39. public void TestSimpleIsSynchronized() {
  40. Assert(!sc.IsSynchronized);
  41. }
  42. public void TestSimpleItemGet() {
  43. for(int i = 0; i < strings.Length; i++) {
  44. Assert(strings[i].Equals(sc[i]));
  45. }
  46. }
  47. public void TestSimpleItemSet() {
  48. sc[0] = "bob";
  49. Assert(sc[0].Equals("bob"));
  50. }
  51. public void TestSimpleSyncRoot() {
  52. Assert(sc.Equals(sc.SyncRoot));
  53. }
  54. public void TestSimpleAdd() {
  55. int index = sc.Add("chuck");
  56. Assert(index == strings.Length);
  57. Assert(sc[strings.Length].Equals("chuck"));
  58. }
  59. public void TestSimpleAddRange() {
  60. string[] newStrings = {
  61. "peter",
  62. "paul",
  63. "mary"
  64. };
  65. int index = sc.Count;
  66. sc.AddRange(newStrings);
  67. Assert(sc.Count == index + newStrings.Length);
  68. for (int i = 0; i+index <= sc.Count-1; i++) {
  69. Assert(newStrings[i].Equals(sc[i+index]));
  70. }
  71. }
  72. public void TestSimpleClear() {
  73. sc.Clear();
  74. Assert(sc.Count == 0);
  75. }
  76. public void TestSimpleContains() {
  77. Assert(sc.Contains(strings[0]));
  78. Assert(!sc.Contains("NOT CONTAINED"));
  79. }
  80. public void TestSimpleCopyTo() {
  81. string[] copyArray = new string[sc.Count];
  82. sc.CopyTo(copyArray, 0);
  83. for (int i = 0; i < copyArray.Length; i++) {
  84. Assert(copyArray[i] == sc[i]);
  85. }
  86. }
  87. public void TestSimpleGetEnumerator() {
  88. int index = 0;
  89. foreach(string s in sc) {
  90. Assert(s.Equals(strings[index]));
  91. index++;
  92. }
  93. }
  94. public void TestSimpleIndexOf() {
  95. Assert(sc.IndexOf(strings[0]) == 0);
  96. }
  97. public void TestSimpleInsert() {
  98. int index = 3;
  99. int oldCount = sc.Count;
  100. string before = sc[index - 1];
  101. string current = sc[index];
  102. string after = sc[index + 1];
  103. string newStr = "paco";
  104. sc.Insert(index, newStr);
  105. Assert(sc.Count == oldCount + 1);
  106. Assert(sc[index].Equals(newStr));
  107. Assert(sc[index-1].Equals(before));
  108. Assert(sc[index+1].Equals(current));
  109. Assert(sc[index+2].Equals(after));
  110. }
  111. public void TestSimpleRemove() {
  112. int oldCount = sc.Count;
  113. sc.Remove(strings[0]);
  114. Assert(oldCount == sc.Count + 1);
  115. Assert(!sc.Contains(strings[0]));
  116. }
  117. public void TestSimpleRemoveAt() {
  118. int index = 3;
  119. int oldCount = sc.Count;
  120. string after = sc[index+1];
  121. sc.RemoveAt(index);
  122. Assert(oldCount == sc.Count + 1);
  123. Assert(sc[index].Equals(after));
  124. }
  125. }
  126. }