7 using System.Collections.Generic;
10 using NUnit.Framework;
11 using MonoTests.System.Diagnostics.Contracts.Helpers;
12 using System.Diagnostics.Contracts;
14 namespace MonoTests.System.Diagnostics.Contracts {
17 public class ContractCollectionMethodsTest {
20 /// Contract.Exists() determines that at least one element in the collection satisfies the predicate.
22 [Test, RunAgainstReference]
23 public void TestExistsInt ()
26 Contract.Exists (0, 10, null);
27 Assert.Fail ("TestExistsInt() no exception #1");
28 } catch (Exception ex) {
29 Assert.IsInstanceOfType (typeof (ArgumentNullException), ex, "TestExistsInt() wrong exception #1");
33 Contract.Exists (10, 0, i => false);
34 Assert.Fail ("TestExistsInt() no exception #2");
35 } catch (Exception ex) {
36 Assert.IsInstanceOfType (typeof (ArgumentException), ex, "TestExistsInt() wrong exception #2");
39 Assert.IsTrue (Contract.Exists (0, 10, i => i <= 0), "TestExistsInt() #1");
40 Assert.IsTrue (Contract.Exists (0, 10, i => i >= 9), "TestExistsInt() #2");
41 Assert.IsFalse (Contract.Exists (0, 10, i => i < 0), "TestExistsInt() #3");
42 Assert.IsFalse (Contract.Exists (0, 10, i => i > 9), "TestExistsInt() #4");
46 /// Contract.Exists() determines that at least one element in the collection satisfies the predicate.
48 [Test, RunAgainstReference]
49 public void TestExistsEnumeration ()
52 Contract.Exists (Enumerable.Range (0, 10), null);
53 Assert.Fail ("TestExistsEnumeration() no exception #1");
54 } catch (Exception ex) {
55 Assert.IsInstanceOfType (typeof (ArgumentNullException), ex, "TestExistsEnumeration() wrong exception #1");
59 Contract.Exists<int> (null, x => false);
60 Assert.Fail ("TestExistsEnumeration() no exception #2");
61 } catch (Exception ex) {
62 Assert.IsInstanceOfType (typeof (ArgumentNullException), ex, "TestExistsEnumeration() wrong exception #2");
65 var en = Enumerable.Range (0, 10);
66 Assert.IsTrue (Contract.Exists (en, i => i <= 0), "TestExistsEnumeration() #1");
67 Assert.IsTrue (Contract.Exists (en, i => i >= 9), "TestExistsEnumeration() #2");
68 Assert.IsFalse (Contract.Exists (en, i => i < 0), "TestExistsEnumeration() #3");
69 Assert.IsFalse (Contract.Exists (en, i => i > 9), "TestExistsEnumeration() #4");
73 /// Contract.ForAll() determines if all elements in the collection satisfy the predicate.
75 [Test, RunAgainstReference]
76 public void TestForAllInt ()
79 Contract.ForAll (0, 10, null);
80 Assert.Fail ("TestForAllInt() no exception #1");
81 } catch (Exception ex) {
82 Assert.IsInstanceOfType (typeof (ArgumentNullException), ex, "TestForAllInt() wrong exception #1");
86 Contract.ForAll (10, 0, i => false);
87 Assert.Fail ("TestForAllInt() no exception #2");
88 } catch (Exception ex) {
89 Assert.IsInstanceOfType (typeof (ArgumentException), ex, "TestForAllInt() wrong exception #2");
92 Assert.IsTrue (Contract.ForAll (0, 10, i => i <= 9), "TestForAllInt() #1");
93 Assert.IsTrue (Contract.ForAll (0, 10, i => i >= 0), "TestForAllInt() #2");
94 Assert.IsFalse (Contract.ForAll (0, 10, i => i < 9), "TestForAllInt() #3");
95 Assert.IsFalse (Contract.ForAll (0, 10, i => i > 0), "TestForAllInt() #4");
99 /// Contract.ForAll() determines if all elements in the collection satisfy the predicate.
101 [Test, RunAgainstReference]
102 public void TestForAllEnumeration ()
105 Contract.ForAll (Enumerable.Range (0, 10), null);
106 Assert.Fail ("TestForAllEnumeration() no exception #1");
107 } catch (Exception ex) {
108 Assert.IsInstanceOfType (typeof (ArgumentNullException), ex, "TestForAllEnumeration() wrong exception #1");
112 Contract.ForAll<int> (null, x => false);
113 Assert.Fail ("TestForAllEnumeration() no exception #2");
114 } catch (Exception ex) {
115 Assert.IsInstanceOfType (typeof (ArgumentNullException), ex, "TestForAllEnumeration() wrong exception #2");
118 var en = Enumerable.Range (0, 10);
119 Assert.IsTrue (Contract.ForAll (en, i => i <= 9), "TestForAllEnumeration() #1");
120 Assert.IsTrue (Contract.ForAll (en, i => i >= 0), "TestForAllEnumeration() #2");
121 Assert.IsFalse (Contract.ForAll (en, i => i < 9), "TestForAllEnumeration() #3");
122 Assert.IsFalse (Contract.ForAll (en, i => i > 0), "TestForAllEnumeration() #4");