예제 #1
0
        public void Test()
        {
            List <string>         strings = new List <string>(new string[] { "a", "b", "c" });
            ReadOnlyList <String> read    = new ReadOnlyList <string>(strings);

            strings.Add("d");
            Assert.AreEqual(3, read.Count);
            Assert.IsTrue(read.Contains("a"));
            Assert.AreEqual(0, read.IndexOf("a"));
            Assert.IsTrue(read.Contains("b"));
            Assert.AreEqual(1, read.IndexOf("b"));
            Assert.IsTrue(read.Contains("c"));
            Assert.AreEqual(2, read.IndexOf("c"));
            Assert.IsFalse(read.Contains("d"));
            Assert.AreEqual(-1, read.IndexOf("d"));
            Assert.AreEqual("a,b,c", String.Join(",", read.ToArray()));
            Assert.AreEqual("a,b,c", String.Join(",", new List <String>(read).ToArray()));

            string[] arcopy = new string[3];
            read.CopyTo(arcopy, 0);
            Assert.AreEqual("a,b,c", String.Join(",", arcopy));

            System.Collections.IEnumerator en = ((System.Collections.IEnumerable)read).GetEnumerator();
            Assert.IsTrue(en.MoveNext());
            Assert.AreEqual("a", en.Current);
            Assert.IsTrue(en.MoveNext());
            Assert.AreEqual("b", en.Current);
            Assert.IsTrue(en.MoveNext());
            Assert.AreEqual("c", en.Current);
            Assert.IsFalse(en.MoveNext());
        }
예제 #2
0
		public void Test()
		{
			List<string> strings = new List<string>(new string[] { "a", "b", "c" });
			ReadOnlyList<String> read = new ReadOnlyList<string>(strings);
			strings.Add("d");
			Assert.AreEqual(3, read.Count);
			Assert.IsTrue(read.Contains("a"));
			Assert.AreEqual(0, read.IndexOf("a"));
			Assert.IsTrue(read.Contains("b"));
			Assert.AreEqual(1, read.IndexOf("b"));
			Assert.IsTrue(read.Contains("c"));
			Assert.AreEqual(2, read.IndexOf("c"));
			Assert.IsFalse(read.Contains("d"));
			Assert.AreEqual(-1, read.IndexOf("d"));
			Assert.AreEqual("a,b,c", String.Join(",", read.ToArray()));
			Assert.AreEqual("a,b,c", String.Join(",", new List<String>(read).ToArray()));
			
			string[] arcopy = new string[3];
			read.CopyTo(arcopy, 0);
			Assert.AreEqual("a,b,c", String.Join(",", arcopy));

			System.Collections.IEnumerator en = ((System.Collections.IEnumerable)read).GetEnumerator();
			Assert.IsTrue(en.MoveNext());
			Assert.AreEqual("a", en.Current);
			Assert.IsTrue(en.MoveNext());
			Assert.AreEqual("b", en.Current);
			Assert.IsTrue(en.MoveNext());
			Assert.AreEqual("c", en.Current);
			Assert.IsFalse(en.MoveNext());
		}
예제 #3
0
    public void TestContains() {
      int[] integers = new int[] { 1234, 6789 };
      ReadOnlyList<int> testList = new ReadOnlyList<int>(integers);

      Assert.IsTrue(testList.Contains(1234));
      Assert.IsFalse(testList.Contains(4321));
    }
예제 #4
0
        public static void Contains()
        {
            var collection = new ReadOnlyList <int>(s_intArray);

            for (int i = 0; i < s_intArray.Length; i++)
            {
                Assert.True(collection.Contains(s_intArray[i]));
            }

            for (int i = 0; i < s_excludedFromIntArray.Length; i++)
            {
                Assert.False(collection.Contains(s_excludedFromIntArray[i]));
            }
        }
예제 #5
0
        public static void MembersForwardedToUnderlyingIList()
        {
            var expectedApiCalls =
                IListApi.Count |
                IListApi.IndexerGet |
                IListApi.Contains |
                IListApi.CopyTo |
                IListApi.GetEnumeratorGeneric |
                IListApi.IndexOf |
                IListApi.GetEnumerator;

            var list       = new CallTrackingIList <int>(expectedApiCalls);
            var collection = new ReadOnlyList <int>(list);

            int  count    = collection.Count;
            bool readOnly = ((IList)collection).IsReadOnly;
            int  x        = collection[0];

            collection.Contains(x);
            collection.CopyTo(s_intArray, 0);
            collection.GetEnumerator();
            collection.IndexOf(x);
            ((IEnumerable)collection).GetEnumerator();

            list.AssertAllMembersCalled();
        }
예제 #6
0
 public void AddDatabaseObject(DatabaseObject databaseObject, bool checkThatGuidsAreUnique)
 {
     if (databaseObject.m_database != null)
     {
         throw new InvalidOperationException("public error: database object is already in a database.");
     }
     if (!m_databaseObjectTypes.Contains(databaseObject.Type))
     {
         throw new InvalidOperationException($"Database object type \"{databaseObject.Type.Name}\" is not supported by the database.");
     }
     if (checkThatGuidsAreUnique)
     {
         if (databaseObject.Guid != Guid.Empty && m_databaseObjectsByGuid.ContainsKey(databaseObject.Guid))
         {
             throw new InvalidOperationException($"Database object {databaseObject.Guid} is already present in the database.");
         }
         foreach (DatabaseObject explicitNestingChild in databaseObject.GetExplicitNestingChildren(null, directChildrenOnly: false))
         {
             if (explicitNestingChild.Guid != Guid.Empty && m_databaseObjectsByGuid.ContainsKey(explicitNestingChild.Guid))
             {
                 throw new InvalidOperationException($"Database object {explicitNestingChild.Guid} is already present in the database.");
             }
         }
     }
     databaseObject.m_database = this;
     if (databaseObject.Guid != Guid.Empty)
     {
         m_databaseObjectsByGuid.Add(databaseObject.Guid, databaseObject);
     }
     foreach (DatabaseObject explicitNestingChild2 in databaseObject.GetExplicitNestingChildren(null, directChildrenOnly: true))
     {
         AddDatabaseObject(explicitNestingChild2, checkThatGuidsAreUnique: false);
     }
 }
        public void Contains_With_Comparer_And_Contains_Must_ReturnTrue(int[] source)
        {
            // Arrange
            var value   = System.Linq.Enumerable.Last(source);
            var wrapped = Wrap.AsValueReadOnlyList(source);

            // Act
            var result = ReadOnlyList
                         .Contains <Wrap.ValueReadOnlyList <int>, int>(wrapped, value, EqualityComparer <int> .Default);

            // Assert
            _ = result.Must()
                .BeTrue();
        }
        public void Contains_With_Comparer_And_NotContains_Must_ReturnFalse(int[] source)
        {
            // Arrange
            var value   = int.MaxValue;
            var wrapped = Wrap.AsValueReadOnlyList(source);

            // Act
            var result = ReadOnlyList
                         .Contains <Wrap.ValueReadOnlyList <int>, int>(wrapped, value, EqualityComparer <int> .Default);

            // Assert
            _ = result.Must()
                .BeFalse();
        }
        public void SerializationTest()
        {
            BinaryFormatter serizalizer = new BinaryFormatter();
            MemoryStream    stream      = new MemoryStream();

            serizalizer.Serialize(stream, readOnlyList);
            stream.Position = 0;
            ReadOnlyList <string> deserialized = (ReadOnlyList <string>)serizalizer.Deserialize(stream);

            Assert.AreEqual(deserialized.Count, readOnlyList.Count);
            foreach (string s in deserialized)
            {
                Assert.IsTrue(readOnlyList.Contains(s));
            }
            foreach (string s in readOnlyList)
            {
                Assert.IsTrue(deserialized.Contains(s));
            }
        }