[bcl] Remove NET_4_0 defines from class libs
[mono.git] / mcs / class / corlib / Test / System.Diagnostics.Contracts / ContractCollectionMethodsTest.cs
1
2 #define CONTRACTS_FULL
3 #define DEBUG
4
5 using System;
6 using System.Collections.Generic;
7 using System.Linq;
8 using System.Text;
9 using NUnit.Framework;
10 using MonoTests.System.Diagnostics.Contracts.Helpers;
11 using System.Diagnostics.Contracts;
12
13 namespace MonoTests.System.Diagnostics.Contracts {
14
15         [TestFixture]
16         public class ContractCollectionMethodsTest {
17
18                 /// <summary>
19                 /// Contract.Exists() determines that at least one element in the collection satisfies the predicate.
20                 /// </summary>
21                 [Test, RunAgainstReference]
22                 public void TestExistsInt ()
23                 {
24                         try {
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");
29                         }
30
31                         try {
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");
36                         }
37
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");
42                 }
43
44                 /// <summary>
45                 /// Contract.Exists() determines that at least one element in the collection satisfies the predicate.
46                 /// </summary>
47                 [Test, RunAgainstReference]
48                 public void TestExistsEnumeration ()
49                 {
50                         try {
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");
55                         }
56
57                         try {
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");
62                         }
63
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");
69                 }
70
71                 /// <summary>
72                 /// Contract.ForAll() determines if all elements in the collection satisfy the predicate.
73                 /// </summary>
74                 [Test, RunAgainstReference]
75                 public void TestForAllInt ()
76                 {
77                         try {
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");
82                         }
83
84                         try {
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");
89                         }
90
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");
95                 }
96
97                 /// <summary>
98                 /// Contract.ForAll() determines if all elements in the collection satisfy the predicate.
99                 /// </summary>
100                 [Test, RunAgainstReference]
101                 public void TestForAllEnumeration ()
102                 {
103                         try {
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");
108                         }
109
110                         try {
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");
115                         }
116
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");
122                 }
123
124         }
125
126 }
127