PopsicleListTest.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using NUnit.Framework;
  5. namespace Google.ProtocolBuffers.Collections {
  6. [TestFixture]
  7. public class PopsicleListTest {
  8. [Test]
  9. public void MutatingOperationsOnFrozenList() {
  10. PopsicleList<string> list = new PopsicleList<string>();
  11. list.MakeReadOnly();
  12. AssertNotSupported(() => list.Add(""));
  13. AssertNotSupported(() => list.Clear());
  14. AssertNotSupported(() => list.Insert(0, ""));
  15. AssertNotSupported(() => list.Remove(""));
  16. AssertNotSupported(() => list.RemoveAt(0));
  17. }
  18. [Test]
  19. public void NonMutatingOperationsOnFrozenList() {
  20. PopsicleList<string> list = new PopsicleList<string>();
  21. list.MakeReadOnly();
  22. Assert.IsFalse(list.Contains(""));
  23. Assert.AreEqual(0, list.Count);
  24. list.CopyTo(new string[5], 0);
  25. list.GetEnumerator();
  26. Assert.AreEqual(-1, list.IndexOf(""));
  27. Assert.IsTrue(list.IsReadOnly);
  28. }
  29. [Test]
  30. public void MutatingOperationsOnFluidList() {
  31. PopsicleList<string> list = new PopsicleList<string>();
  32. list.Add("");
  33. list.Clear();
  34. list.Insert(0, "");
  35. list.Remove("");
  36. list.Add("x"); // Just to make the next call valid
  37. list.RemoveAt(0);
  38. }
  39. [Test]
  40. public void NonMutatingOperationsOnFluidList() {
  41. PopsicleList<string> list = new PopsicleList<string>();
  42. Assert.IsFalse(list.Contains(""));
  43. Assert.AreEqual(0, list.Count);
  44. list.CopyTo(new string[5], 0);
  45. list.GetEnumerator();
  46. Assert.AreEqual(-1, list.IndexOf(""));
  47. Assert.IsFalse(list.IsReadOnly);
  48. }
  49. private static void AssertNotSupported(Action action) {
  50. try {
  51. action();
  52. Assert.Fail("Expected NotSupportedException");
  53. } catch (NotSupportedException) {
  54. // Expected
  55. }
  56. }
  57. }
  58. }