public static void VirtualMethods() { VirtualTestReadOnlyCollection collectionBase = new VirtualTestReadOnlyCollection(); Assert.Equal(int.MinValue, collectionBase.Count); Assert.Null(collectionBase.GetEnumerator()); }
public void TesReadOnlyCollectionBasBasic() { MyReadOnlyCollectionBase mycol1; MyReadOnlyCollectionBase mycol2; Foo f1; Foo[] arrF1; Foo[] arrF2; IEnumerator enu1; int iCount; object obj1; ReadOnlyCollectionBase collectionBase; //[]ReadOnlyCollectionBase implements IList (which means both ICollection and IEnumerator as well :-() //To test this class, we will implement our own strongly typed ReadOnlyCollectionBase and call its methods //[] SyncRoot property arrF1 = new Foo[100]; for (int i = 0; i < 100; i++) arrF1[i] = new Foo(); mycol1 = new MyReadOnlyCollectionBase(arrF1); Assert.False(mycol1.SyncRoot is ArrayList, "Error SyncRoot returned ArrayList"); //[] Count property arrF1 = new Foo[100]; for (int i = 0; i < 100; i++) arrF1[i] = new Foo(); mycol1 = new MyReadOnlyCollectionBase(arrF1); Assert.Equal(100, mycol1.Count); //[]CopyTo arrF2 = new Foo[100]; for (int i = 0; i < 100; i++) arrF2[i] = new Foo(i, i.ToString()); mycol1 = new MyReadOnlyCollectionBase(arrF2); arrF1 = new Foo[100]; mycol1.CopyTo(arrF1, 0); for (int i = 0; i < 100; i++) { Assert.Equal(i, arrF1[i].IValue); Assert.Equal(i.ToString(), arrF1[i].SValue); } //Argument checking arrF2 = new Foo[100]; for (int i = 0; i < 100; i++) arrF2[i] = new Foo(i, i.ToString()); mycol1 = new MyReadOnlyCollectionBase(arrF2); arrF1 = new Foo[100]; Assert.Throws<ArgumentException>(() => { mycol1.CopyTo(arrF1, 50); } ); Assert.Throws<ArgumentOutOfRangeException>(() => { mycol1.CopyTo(arrF1, -1); } ); arrF1 = new Foo[200]; mycol1.CopyTo(arrF1, 100); for (int i = 0; i < 100; i++) { Assert.Equal(i, arrF1[100 + i].IValue); Assert.Equal(i.ToString(), arrF1[100 + i].SValue); } //[]GetEnumerator arrF2 = new Foo[100]; for (int i = 0; i < 100; i++) arrF2[i] = new Foo(i, i.ToString()); mycol1 = new MyReadOnlyCollectionBase(arrF2); enu1 = mycol1.GetEnumerator(); //Calling current should throw here Assert.Throws<InvalidOperationException>(() => { f1 = (Foo)enu1.Current; } ); iCount = 0; while (enu1.MoveNext()) { f1 = (Foo)enu1.Current; Assert.False((f1.IValue != iCount) || (f1.SValue != iCount.ToString()), "Error, does not match, " + f1.IValue); iCount++; } Assert.Equal(100, iCount); //Calling current should throw here Assert.Throws<InvalidOperationException>(() => { f1 = (Foo)enu1.Current; } ); //[]IsSynchronized arrF2 = new Foo[100]; for (int i = 0; i < 100; i++) arrF2[i] = new Foo(i, i.ToString()); Assert.False(((ICollection)mycol1).IsSynchronized); //[]SyncRoot arrF2 = new Foo[100]; for (int i = 0; i < 100; i++) arrF2[i] = new Foo(i, i.ToString()); obj1 = mycol1.SyncRoot; mycol2 = mycol1; Assert.Equal(obj1, mycol2.SyncRoot); //End of ICollection and IEnumerator methods //Now to IList methods //[]this, Contains, IndexOf arrF2 = new Foo[100]; for (int i = 0; i < 100; i++) arrF2[i] = new Foo(i, i.ToString()); mycol1 = new MyReadOnlyCollectionBase(arrF2); for (int i = 0; i < 100; i++) { Assert.False((mycol1[i].IValue != i) || (mycol1[i].SValue != i.ToString())); Assert.False((mycol1.IndexOf(new Foo(i, i.ToString())) != i)); Assert.False((!mycol1.Contains(new Foo(i, i.ToString())))); } //[]Rest of the IList methods: IsFixedSize, IsReadOnly arrF2 = new Foo[100]; for (int i = 0; i < 100; i++) arrF2[i] = new Foo(i, i.ToString()); mycol1 = new MyReadOnlyCollectionBase(arrF2); Assert.True(mycol1.IsFixedSize); Assert.True(mycol1.IsReadOnly); //The following operations are not allowed by the compiler. Hence, commented out arrF2 = new Foo[100]; for (int i = 0; i < 100; i++) arrF2[i] = new Foo(i, i.ToString()); mycol1 = new MyReadOnlyCollectionBase(arrF2); //[] Verify Count is virtual collectionBase = new VirtualTestReadOnlyCollection(); Assert.Equal(collectionBase.Count, int.MinValue); //[] Verify Count is virtual collectionBase = new VirtualTestReadOnlyCollection(); Assert.Null(collectionBase.GetEnumerator()); }
public bool runTest() { //////////// Global Variables used for all tests int iCountErrors = 0; int iCountTestcases = 0; MyReadOnlyCollectionBase mycol1; MyReadOnlyCollectionBase mycol2; Foo f1; Foo[] arrF1; Foo[] arrF2; IEnumerator enu1; Int32 iCount; Object obj1; ReadOnlyCollectionBase collectionBase; try { do { ///////////////////////// START TESTS //////////////////////////// /////////////////////////////////////////////////////////////////// //[]ReadOnlyCollectionBase implements IList (which means both ICollection and IEnumerator as well :-() //To test this class, we will implement our own strongly typed ReadOnlyCollectionBase and call its methods iCountTestcases++; //[] SyncRoot property iCountTestcases++; arrF1 = new Foo[100]; for (int i = 0; i < 100; i++) { arrF1[i] = new Foo(); } mycol1 = new MyReadOnlyCollectionBase(arrF1); if (mycol1.SyncRoot is ArrayList) { iCountErrors++; Console.WriteLine("Error SyncRoot returned ArrayList"); } //[] Count property iCountTestcases++; arrF1 = new Foo[100]; for (int i = 0; i < 100; i++) { arrF1[i] = new Foo(); } mycol1 = new MyReadOnlyCollectionBase(arrF1); if (mycol1.Count != 100) { iCountErrors++; Console.WriteLine("Error Expected value not returned, " + mycol1.Count); } //[]CopyTo iCountTestcases++; arrF2 = new Foo[100]; for (int i = 0; i < 100; i++) { arrF2[i] = new Foo(i, i.ToString()); } mycol1 = new MyReadOnlyCollectionBase(arrF2); arrF1 = new Foo[100]; mycol1.CopyTo(arrF1, 0); for (int i = 0; i < 100; i++) { if ((arrF1[i].IValue != i) || (arrF1[i].SValue != i.ToString())) { iCountErrors++; Console.WriteLine("Error #" + i + "! Expected value not returned"); } } //Argument checking arrF2 = new Foo[100]; for (int i = 0; i < 100; i++) { arrF2[i] = new Foo(i, i.ToString()); } mycol1 = new MyReadOnlyCollectionBase(arrF2); arrF1 = new Foo[100]; try { mycol1.CopyTo(arrF1, 50); iCountErrors++; Console.WriteLine("Err_2075dfgv! Exception not thrown"); } catch (ArgumentException) { } catch (Exception ex) { iCountErrors++; Console.WriteLine("Err_854732f! Unexception not thrown, " + ex.GetType().Name); } try { mycol1.CopyTo(arrF1, -1); iCountErrors++; Console.WriteLine("Err_2075dfgv! Exception not thrown"); } catch (ArgumentException) { } catch (Exception ex) { iCountErrors++; Console.WriteLine("Err_854732f! Unexception not thrown, " + ex.GetType().Name); } arrF1 = new Foo[200]; mycol1.CopyTo(arrF1, 100); for (int i = 0; i < 100; i++) { if ((arrF1[100 + i].IValue != i) || (arrF1[100 + i].SValue != i.ToString())) { iCountErrors++; Console.WriteLine("Err_2874sf_" + i + "! Expected value not returned"); } } //[]GetEnumerator iCountTestcases++; arrF2 = new Foo[100]; for (int i = 0; i < 100; i++) { arrF2[i] = new Foo(i, i.ToString()); } mycol1 = new MyReadOnlyCollectionBase(arrF2); enu1 = mycol1.GetEnumerator(); //Calling current should throw here try { //Calling current should throw here f1 = (Foo)enu1.Current; iCountErrors++; Console.WriteLine("Err_87543! Exception not thrown"); } catch (InvalidOperationException) { } catch (Exception ex) { Console.WriteLine("failure should throw InvalidOperationException, thrown, " + ex.GetType().Name); } iCount = 0; while (enu1.MoveNext()) { f1 = (Foo)enu1.Current; if ((f1.IValue != iCount) || (f1.SValue != iCount.ToString())) { iCountErrors++; Console.WriteLine("Err_87543! does not match, " + f1.IValue); } iCount++; } if (iCount != 100) { iCountErrors++; Console.WriteLine("Err_87543! doesnot match"); } //Calling current should throw here try { //Calling current should throw here f1 = (Foo)enu1.Current; iCountErrors++; Console.WriteLine("Err_438fsfd! Exception not thrown"); } catch (InvalidOperationException) { } catch (Exception ex) { Console.WriteLine("fail, should throw InvalidOperationException, thrown, " + ex.GetType().Name); } //[]IsSynchronized iCountTestcases++; arrF2 = new Foo[100]; for (int i = 0; i < 100; i++) { arrF2[i] = new Foo(i, i.ToString()); } if (((ICollection)mycol1).IsSynchronized) { iCountErrors++; Console.WriteLine("Err_275eg! Expected value not returned, " + ((ICollection)mycol1).IsSynchronized); } //[]SyncRoot iCountTestcases++; arrF2 = new Foo[100]; for (int i = 0; i < 100; i++) { arrF2[i] = new Foo(i, i.ToString()); } obj1 = mycol1.SyncRoot; mycol2 = mycol1; if (obj1 != mycol2.SyncRoot) { iCountErrors++; Console.WriteLine("Err_9745sg! Expected value not returned"); } //End of ICollection and IEnumerator methods //Now to IList methods //[]this, Contains, IndexOf iCountTestcases++; arrF2 = new Foo[100]; for (int i = 0; i < 100; i++) { arrF2[i] = new Foo(i, i.ToString()); } mycol1 = new MyReadOnlyCollectionBase(arrF2); for (int i = 0; i < 100; i++) { if ((mycol1[i].IValue != i) || (mycol1[i].SValue != i.ToString())) { iCountErrors++; Console.WriteLine("Err_2974swsg_" + i + "! Expected value not returned"); } if ((mycol1.IndexOf(new Foo(i, i.ToString())) != i)) { iCountErrors++; Console.WriteLine("Err_205sg_" + i + "! Expected value not returned"); } if ((!mycol1.Contains(new Foo(i, i.ToString())))) { iCountErrors++; Console.WriteLine("Err_975wg_" + i + "! Expected value not returned"); } } //[]Rest of the IList methods: IsFixedSize, IsReadOnly iCountTestcases++; arrF2 = new Foo[100]; for (int i = 0; i < 100; i++) { arrF2[i] = new Foo(i, i.ToString()); } mycol1 = new MyReadOnlyCollectionBase(arrF2); if (!mycol1.IsFixedSize) { iCountErrors++; Console.WriteLine("Err_9753sfg! Expected value not returned, " + mycol1.IsFixedSize); } if (!mycol1.IsReadOnly) { iCountErrors++; Console.WriteLine("Err_834sg! Expected value not returned, " + mycol1.IsReadOnly); } //The following operations are not allowed by the compiler. Hence, commented out arrF2 = new Foo[100]; for (int i = 0; i < 100; i++) { arrF2[i] = new Foo(i, i.ToString()); } mycol1 = new MyReadOnlyCollectionBase(arrF2); //[] Verify Count is virtual iCountTestcases++; collectionBase = new VirtualTestReadOnlyCollection(); if (collectionBase.Count != Int32.MinValue) { Console.WriteLine("Err_707272hapba Expected Count={0} actual={1}", Int32.MinValue, collectionBase.Count); } //[] Verify Count is virtual iCountTestcases++; collectionBase = new VirtualTestReadOnlyCollection(); if (collectionBase.GetEnumerator() != null) { Console.WriteLine("Err_456548ahpba Expected GetEnumerator()=null actual={1}", null, collectionBase.GetEnumerator()); } /////////////////////////////////////////////////////////////////// /////////////////////////// END TESTS ///////////////////////////// } while (false); } catch (Exception exc_general) { ++iCountErrors; Console.WriteLine("Error Err_8888yyy! , exc_general==\n" + exc_general.ToString()); } Assert.Equal(0, iCountErrors); //// Finish Diagnostics if (iCountErrors == 0) { Console.WriteLine("Passed: iCountTestcases==" + iCountTestcases); return(true); } else { Console.WriteLine("Failure! iCountErrors==" + iCountErrors); return(false); } }
public static void TestVirtualMethods() { VirtualTestReadOnlyCollection collectionBase = new VirtualTestReadOnlyCollection(); Assert.Equal(collectionBase.Count, int.MinValue); Assert.Null(collectionBase.GetEnumerator()); }
public void TesReadOnlyCollectionBasBasic() { MyReadOnlyCollectionBase mycol1; MyReadOnlyCollectionBase mycol2; Foo f1; Foo[] arrF1; Foo[] arrF2; IEnumerator enu1; int iCount; object obj1; ReadOnlyCollectionBase collectionBase; //[]ReadOnlyCollectionBase implements IList (which means both ICollection and IEnumerator as well :-() //To test this class, we will implement our own strongly typed ReadOnlyCollectionBase and call its methods //[] SyncRoot property arrF1 = new Foo[100]; for (int i = 0; i < 100; i++) { arrF1[i] = new Foo(); } mycol1 = new MyReadOnlyCollectionBase(arrF1); Assert.False(mycol1.SyncRoot is ArrayList, "Error SyncRoot returned ArrayList"); //[] Count property arrF1 = new Foo[100]; for (int i = 0; i < 100; i++) { arrF1[i] = new Foo(); } mycol1 = new MyReadOnlyCollectionBase(arrF1); Assert.Equal(100, mycol1.Count); //[]CopyTo arrF2 = new Foo[100]; for (int i = 0; i < 100; i++) { arrF2[i] = new Foo(i, i.ToString()); } mycol1 = new MyReadOnlyCollectionBase(arrF2); arrF1 = new Foo[100]; mycol1.CopyTo(arrF1, 0); for (int i = 0; i < 100; i++) { Assert.Equal(i, arrF1[i].IValue); Assert.Equal(i.ToString(), arrF1[i].SValue); } //Argument checking arrF2 = new Foo[100]; for (int i = 0; i < 100; i++) { arrF2[i] = new Foo(i, i.ToString()); } mycol1 = new MyReadOnlyCollectionBase(arrF2); arrF1 = new Foo[100]; Assert.Throws <ArgumentException>(() => { mycol1.CopyTo(arrF1, 50); } ); Assert.Throws <ArgumentOutOfRangeException>(() => { mycol1.CopyTo(arrF1, -1); } ); arrF1 = new Foo[200]; mycol1.CopyTo(arrF1, 100); for (int i = 0; i < 100; i++) { Assert.Equal(i, arrF1[100 + i].IValue); Assert.Equal(i.ToString(), arrF1[100 + i].SValue); } //[]GetEnumerator arrF2 = new Foo[100]; for (int i = 0; i < 100; i++) { arrF2[i] = new Foo(i, i.ToString()); } mycol1 = new MyReadOnlyCollectionBase(arrF2); enu1 = mycol1.GetEnumerator(); //Calling current should throw here Assert.Throws <InvalidOperationException>(() => { f1 = (Foo)enu1.Current; } ); iCount = 0; while (enu1.MoveNext()) { f1 = (Foo)enu1.Current; Assert.False((f1.IValue != iCount) || (f1.SValue != iCount.ToString()), "Error, does not match, " + f1.IValue); iCount++; } Assert.Equal(100, iCount); //Calling current should throw here Assert.Throws <InvalidOperationException>(() => { f1 = (Foo)enu1.Current; } ); //[]IsSynchronized arrF2 = new Foo[100]; for (int i = 0; i < 100; i++) { arrF2[i] = new Foo(i, i.ToString()); } Assert.False(((ICollection)mycol1).IsSynchronized); //[]SyncRoot arrF2 = new Foo[100]; for (int i = 0; i < 100; i++) { arrF2[i] = new Foo(i, i.ToString()); } obj1 = mycol1.SyncRoot; mycol2 = mycol1; Assert.Equal(obj1, mycol2.SyncRoot); //End of ICollection and IEnumerator methods //Now to IList methods //[]this, Contains, IndexOf arrF2 = new Foo[100]; for (int i = 0; i < 100; i++) { arrF2[i] = new Foo(i, i.ToString()); } mycol1 = new MyReadOnlyCollectionBase(arrF2); for (int i = 0; i < 100; i++) { Assert.False((mycol1[i].IValue != i) || (mycol1[i].SValue != i.ToString())); Assert.False((mycol1.IndexOf(new Foo(i, i.ToString())) != i)); Assert.False((!mycol1.Contains(new Foo(i, i.ToString())))); } //[]Rest of the IList methods: IsFixedSize, IsReadOnly arrF2 = new Foo[100]; for (int i = 0; i < 100; i++) { arrF2[i] = new Foo(i, i.ToString()); } mycol1 = new MyReadOnlyCollectionBase(arrF2); Assert.True(mycol1.IsFixedSize); Assert.True(mycol1.IsReadOnly); //The following operations are not allowed by the compiler. Hence, commented out arrF2 = new Foo[100]; for (int i = 0; i < 100; i++) { arrF2[i] = new Foo(i, i.ToString()); } mycol1 = new MyReadOnlyCollectionBase(arrF2); //[] Verify Count is virtual collectionBase = new VirtualTestReadOnlyCollection(); Assert.Equal(collectionBase.Count, int.MinValue); //[] Verify Count is virtual collectionBase = new VirtualTestReadOnlyCollection(); Assert.Null(collectionBase.GetEnumerator()); }