TestUtilities.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // -----------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. // -----------------------------------------------------------------------
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using System.Collections.ObjectModel;
  8. using System.ComponentModel.Composition;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Reflection;
  12. using Microsoft.VisualStudio.TestTools.UnitTesting;
  13. namespace System.UnitTesting
  14. {
  15. public static class TestUtilities
  16. {
  17. public static void CheckICollectionOfTConformance<T>(ICollection<T> list, T a, T b, T c, T d)
  18. {
  19. list.Clear();
  20. EnumerableAssert.AreEqual(list);
  21. Assert.IsFalse(list.IsReadOnly, "The list should not report being read-only for these tests to work");
  22. Assert.IsFalse(list.Contains(a), "Contains should fail for anything when the collection is empty");
  23. Assert.IsFalse(list.Remove(a), "Remove should fail on anything when the collection is empty");
  24. list.Add(a);
  25. EnumerableAssert.AreEqual(list, a);
  26. list.Add(b);
  27. EnumerableAssert.AreEqual(list, a, b);
  28. list.Add(c);
  29. EnumerableAssert.AreEqual(list, a, b, c);
  30. list.Remove(b);
  31. EnumerableAssert.AreEqual(list, a, c);
  32. list.Remove(c);
  33. EnumerableAssert.AreEqual(list, a);
  34. list.Remove(a);
  35. EnumerableAssert.AreEqual(list);
  36. list.Add(a); list.Add(b); list.Add(c);
  37. list.Clear();
  38. EnumerableAssert.AreEqual(list);
  39. list.Clear();
  40. EnumerableAssert.AreEqual(list);
  41. list.Add(d); list.Add(c); list.Add(b); list.Add(a);
  42. T[] destination = new T[5];
  43. list.CopyTo(destination, 0);
  44. EnumerableAssert.AreEqual(destination, d, c, b, a, default(T));
  45. }
  46. public static void CheckIListOfTConformance<T>(IList<T> list, T a, T b, T c, T d)
  47. {
  48. CheckICollectionOfTConformance(list, a, b, c, d);
  49. list.Clear();
  50. list.Insert(0, d);
  51. list.Insert(0, a);
  52. list.Insert(1, c);
  53. list.Insert(1, b);
  54. CompareListContents(list, a, b, c, d);
  55. list[1] = a;
  56. CompareListContents(list, a, a, c, d);
  57. list.RemoveAt(2);
  58. CompareListContents(list, a, a, d);
  59. Assert.AreEqual(2, list.IndexOf(d), "Expected indexof to return the correct location of {0}", d);
  60. Assert.AreEqual(-1, list.IndexOf(b), "{0} should not be found in the collection", b);
  61. Assert.AreEqual(-1, list.IndexOf(c), "{0} should not be found in the collection", c);
  62. }
  63. public static void CompareListContents<T>(IList<T> list, params object[] values)
  64. {
  65. EnumerableAssert.AreEqual(list, values);
  66. for (var index = 0; index < values.Length; index++)
  67. {
  68. Assert.AreEqual(values[index], list[index],
  69. "List should return true for Contains on every element, index {0}, length {1}", index, values[index]);
  70. }
  71. }
  72. }
  73. }