StringCollectionTest.cs 3.2 KB

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