PopsicleListTest.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. AssertNotSupported(() => list.Add(new[] {"", ""}));
  18. }
  19. [Test]
  20. public void NonMutatingOperationsOnFrozenList() {
  21. PopsicleList<string> list = new PopsicleList<string>();
  22. list.MakeReadOnly();
  23. Assert.IsFalse(list.Contains(""));
  24. Assert.AreEqual(0, list.Count);
  25. list.CopyTo(new string[5], 0);
  26. list.GetEnumerator();
  27. Assert.AreEqual(-1, list.IndexOf(""));
  28. Assert.IsTrue(list.IsReadOnly);
  29. }
  30. [Test]
  31. public void MutatingOperationsOnFluidList() {
  32. PopsicleList<string> list = new PopsicleList<string>();
  33. list.Add("");
  34. list.Clear();
  35. list.Insert(0, "");
  36. list.Remove("");
  37. list.Add("x"); // Just to make the next call valid
  38. list.RemoveAt(0);
  39. }
  40. [Test]
  41. public void NonMutatingOperationsOnFluidList() {
  42. PopsicleList<string> list = new PopsicleList<string>();
  43. Assert.IsFalse(list.Contains(""));
  44. Assert.AreEqual(0, list.Count);
  45. list.CopyTo(new string[5], 0);
  46. list.GetEnumerator();
  47. Assert.AreEqual(-1, list.IndexOf(""));
  48. Assert.IsFalse(list.IsReadOnly);
  49. }
  50. private static void AssertNotSupported(Action action) {
  51. try {
  52. action();
  53. Assert.Fail("Expected NotSupportedException");
  54. } catch (NotSupportedException) {
  55. // Expected
  56. }
  57. }
  58. }
  59. }