6 using System.Collections.Generic;
10 using MonoTests.System.Diagnostics.Contracts.Helpers;
11 using System.Diagnostics.Contracts;
13 namespace MonoTests.System.Diagnostics.Contracts {
16 public class ContractCollectionMethodsTest {
19 /// Contract.Exists() determines that at least one element in the collection satisfies the predicate.
21 [Test, RunAgainstReference]
22 public void TestExistsInt ()
25 Contract.Exists (0, 10, null);
26 Assert.Fail ("TestExistsInt() no exception #1");
27 } catch (Exception ex) {
28 Assert.IsInstanceOfType (typeof (ArgumentNullException), ex, "TestExistsInt() wrong exception #1");
32 Contract.Exists (10, 0, i => false);
33 Assert.Fail ("TestExistsInt() no exception #2");
34 } catch (Exception ex) {
35 Assert.IsInstanceOfType (typeof (ArgumentException), ex, "TestExistsInt() wrong exception #2");
38 Assert.IsTrue (Contract.Exists (0, 10, i => i <= 0), "TestExistsInt() #1");
39 Assert.IsTrue (Contract.Exists (0, 10, i => i >= 9), "TestExistsInt() #2");
40 Assert.IsFalse (Contract.Exists (0, 10, i => i < 0), "TestExistsInt() #3");
41 Assert.IsFalse (Contract.Exists (0, 10, i => i > 9), "TestExistsInt() #4");
45 /// Contract.Exists() determines that at least one element in the collection satisfies the predicate.
47 [Test, RunAgainstReference]
48 public void TestExistsEnumeration ()
51 Contract.Exists (Enumerable.Range (0, 10), null);
52 Assert.Fail ("TestExistsEnumeration() no exception #1");
53 } catch (Exception ex) {
54 Assert.IsInstanceOfType (typeof (ArgumentNullException), ex, "TestExistsEnumeration() wrong exception #1");
58 Contract.Exists<int> (null, x => false);
59 Assert.Fail ("TestExistsEnumeration() no exception #2");
60 } catch (Exception ex) {
61 Assert.IsInstanceOfType (typeof (ArgumentNullException), ex, "TestExistsEnumeration() wrong exception #2");
64 var en = Enumerable.Range (0, 10);
65 Assert.IsTrue (Contract.Exists (en, i => i <= 0), "TestExistsEnumeration() #1");
66 Assert.IsTrue (Contract.Exists (en, i => i >= 9), "TestExistsEnumeration() #2");
67 Assert.IsFalse (Contract.Exists (en, i => i < 0), "TestExistsEnumeration() #3");
68 Assert.IsFalse (Contract.Exists (en, i => i > 9), "TestExistsEnumeration() #4");
72 /// Contract.ForAll() determines if all elements in the collection satisfy the predicate.
74 [Test, RunAgainstReference]
75 public void TestForAllInt ()
78 Contract.ForAll (0, 10, null);
79 Assert.Fail ("TestForAllInt() no exception #1");
80 } catch (Exception ex) {
81 Assert.IsInstanceOfType (typeof (ArgumentNullException), ex, "TestForAllInt() wrong exception #1");
85 Contract.ForAll (10, 0, i => false);
86 Assert.Fail ("TestForAllInt() no exception #2");
87 } catch (Exception ex) {
88 Assert.IsInstanceOfType (typeof (ArgumentException), ex, "TestForAllInt() wrong exception #2");
91 Assert.IsTrue (Contract.ForAll (0, 10, i => i <= 9), "TestForAllInt() #1");
92 Assert.IsTrue (Contract.ForAll (0, 10, i => i >= 0), "TestForAllInt() #2");
93 Assert.IsFalse (Contract.ForAll (0, 10, i => i < 9), "TestForAllInt() #3");
94 Assert.IsFalse (Contract.ForAll (0, 10, i => i > 0), "TestForAllInt() #4");
98 /// Contract.ForAll() determines if all elements in the collection satisfy the predicate.
100 [Test, RunAgainstReference]
101 public void TestForAllEnumeration ()
104 Contract.ForAll (Enumerable.Range (0, 10), null);
105 Assert.Fail ("TestForAllEnumeration() no exception #1");
106 } catch (Exception ex) {
107 Assert.IsInstanceOfType (typeof (ArgumentNullException), ex, "TestForAllEnumeration() wrong exception #1");
111 Contract.ForAll<int> (null, x => false);
112 Assert.Fail ("TestForAllEnumeration() no exception #2");
113 } catch (Exception ex) {
114 Assert.IsInstanceOfType (typeof (ArgumentNullException), ex, "TestForAllEnumeration() wrong exception #2");
117 var en = Enumerable.Range (0, 10);
118 Assert.IsTrue (Contract.ForAll (en, i => i <= 9), "TestForAllEnumeration() #1");
119 Assert.IsTrue (Contract.ForAll (en, i => i >= 0), "TestForAllEnumeration() #2");
120 Assert.IsFalse (Contract.ForAll (en, i => i < 9), "TestForAllEnumeration() #3");
121 Assert.IsFalse (Contract.ForAll (en, i => i > 0), "TestForAllEnumeration() #4");