[System] UriKind.RelativeOrAbsolute workaround.
[mono.git] / mcs / class / corlib / Test / System.Diagnostics.Contracts / ContractCollectionMethodsTest.cs
1 #if NET_4_0
2
3 #define CONTRACTS_FULL
4 #define DEBUG
5
6 using System;
7 using System.Collections.Generic;
8 using System.Linq;
9 using System.Text;
10 using NUnit.Framework;
11 using MonoTests.System.Diagnostics.Contracts.Helpers;
12 using System.Diagnostics.Contracts;
13
14 namespace MonoTests.System.Diagnostics.Contracts {
15
16         [TestFixture]
17         public class ContractCollectionMethodsTest {
18
19                 /// <summary>
20                 /// Contract.Exists() determines that at least one element in the collection satisfies the predicate.
21                 /// </summary>
22                 [Test, RunAgainstReference]
23                 public void TestExistsInt ()
24                 {
25                         try {
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");
30                         }
31
32                         try {
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");
37                         }
38
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");
43                 }
44
45                 /// <summary>
46                 /// Contract.Exists() determines that at least one element in the collection satisfies the predicate.
47                 /// </summary>
48                 [Test, RunAgainstReference]
49                 public void TestExistsEnumeration ()
50                 {
51                         try {
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");
56                         }
57
58                         try {
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");
63                         }
64
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");
70                 }
71
72                 /// <summary>
73                 /// Contract.ForAll() determines if all elements in the collection satisfy the predicate.
74                 /// </summary>
75                 [Test, RunAgainstReference]
76                 public void TestForAllInt ()
77                 {
78                         try {
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");
83                         }
84
85                         try {
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");
90                         }
91
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");
96                 }
97
98                 /// <summary>
99                 /// Contract.ForAll() determines if all elements in the collection satisfy the predicate.
100                 /// </summary>
101                 [Test, RunAgainstReference]
102                 public void TestForAllEnumeration ()
103                 {
104                         try {
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");
109                         }
110
111                         try {
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");
116                         }
117
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");
123                 }
124
125         }
126
127 }
128
129 #endif