Merge pull request #347 from JamesB7/master
[mono.git] / mcs / class / corlib / System.Diagnostics.Contracts / Contract.cs
1 //
2 // System.Diagnostics.Contracts.Contract.cs
3 //
4 // Authors:
5 //    Miguel de Icaza (miguel@gnome.org)
6 //    Chris Bacon (chrisbacon76@gmail.com)
7 //    Marek Safar (marek.safar@gmail.com)
8 //
9 // Copyright 2009, 2010 Novell (http://www.novell.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 #if NET_4_0 || NET_2_1
32
33 using System;
34 using System.Collections.Generic;
35 using System.Diagnostics.Contracts.Internal;
36 using System.Runtime.ConstrainedExecution;
37
38 namespace System.Diagnostics.Contracts
39 {
40         public static class Contract
41         {
42 #if NET_4_0
43                 public
44 #else
45                 internal
46 #endif
47                 static event EventHandler<ContractFailedEventArgs> ContractFailed;
48
49                 // Used in test
50                 internal static EventHandler<ContractFailedEventArgs> InternalContractFailedEvent {
51                         get { return ContractFailed; }
52                 }
53
54                 // Used in test
55                 internal static Type GetContractExceptionType ()
56                 {
57                         return typeof (ContractException);
58                 }
59
60                 // Used in test
61                 internal static Type GetContractShouldAssertExceptionType ()
62                 {
63                         return typeof (ContractShouldAssertException);
64                 }
65
66                 static void ReportFailure (ContractFailureKind kind, string userMessage, string conditionText, Exception innerException)
67                 {
68                         string msg = ContractHelper.RaiseContractFailedEvent (kind, userMessage, conditionText, innerException);
69                         // if msg is null, then it has been handled already, so don't do anything here
70                         if (msg != null)
71                                 ContractHelper.TriggerFailure (kind, msg, userMessage, conditionText, innerException);
72                 }
73
74                 static void AssertMustUseRewriter (ContractFailureKind kind, string message)
75                 {
76                         ContractHelper.TriggerFailure (kind, "Assembly must be re-written by the code contract rewritter", null, message, null);
77                 }
78
79                 [ConditionalAttribute("CONTRACTS_FULL")]
80                 [ConditionalAttribute("DEBUG")]
81                 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
82                 public static void Assert (bool condition)
83                 {
84                         if (condition)
85                                 return;
86
87                         ReportFailure (ContractFailureKind.Assert, null, null, null);
88                 }
89
90                 [ConditionalAttribute("DEBUG")]
91                 [ConditionalAttribute("CONTRACTS_FULL")]
92                 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
93                 public static void Assert (bool condition, string userMessage)
94                 {
95                         if (condition)
96                                 return;
97
98                         ReportFailure (ContractFailureKind.Assert, userMessage, null, null);
99                 }
100
101                 [ConditionalAttribute("DEBUG")]
102                 [ConditionalAttribute("CONTRACTS_FULL")]
103                 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
104                 public static void Assume(bool condition)
105                 {
106                         // At runtime, this behaves like assert
107                         if (condition)
108                                 return;
109
110                         ReportFailure (ContractFailureKind.Assume, null, null, null);
111                 }
112
113                 [ConditionalAttribute("CONTRACTS_FULL")]
114                 [ConditionalAttribute("DEBUG")]
115                 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
116                 public static void Assume (bool condition, string userMessage)
117                 {
118                         // At runtime, this behaves like assert
119                         if (condition)
120                                 return;
121
122                         ReportFailure (ContractFailureKind.Assume, userMessage, null, null);
123                 }
124
125                 [ConditionalAttribute("CONTRACTS_FULL")]
126                 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
127                 public static void EndContractBlock ()
128                 {
129                         // Marker method, no code required.
130                 }
131
132                 [ConditionalAttribute("CONTRACTS_FULL")]
133                 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
134                 public static void Ensures (bool condition)
135                 {
136                         AssertMustUseRewriter (ContractFailureKind.Postcondition, "Contract.Ensures");
137                 }
138
139                 [ConditionalAttribute("CONTRACTS_FULL")]
140                 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
141                 public static void Ensures (bool condition, string userMessage)
142                 {
143                         AssertMustUseRewriter (ContractFailureKind.Postcondition, "Contract.Ensures");
144                 }
145
146                 [ConditionalAttribute("CONTRACTS_FULL")]
147                 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
148                 public static void EnsuresOnThrow<TException> (bool condition) where TException : Exception
149                 {
150                         AssertMustUseRewriter (ContractFailureKind.Postcondition, "Contract.EnsuresOnThrow");
151                 }
152
153                 [ConditionalAttribute("CONTRACTS_FULL")]
154                 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
155                 public static void EnsuresOnThrow<TException> (bool condition, string userMessage) where TException : Exception
156                 {
157                         AssertMustUseRewriter (ContractFailureKind.Postcondition, "Contract.EnsuresOnThrow");
158                 }
159
160                 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
161                 public static bool Exists<T> (IEnumerable<T> collection, Predicate<T> predicate)
162                 {
163                         if (predicate == null)
164                                 throw new ArgumentNullException ("predicate");
165                         if (collection == null)
166                                 throw new ArgumentNullException ("collection");
167                         
168                         foreach (var t in collection)
169                                 if (predicate (t))
170                                         return true;
171                         return false;
172                 }
173
174                 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
175                 public static bool Exists (int fromInclusive, int toExclusive, Predicate<int> predicate)
176                 {
177                         if (predicate == null)
178                                 throw new ArgumentNullException ("predicate");
179                         if (toExclusive < fromInclusive)
180                                 throw new ArgumentException ("toExclusitve < fromInclusive");
181                         
182                         for (int i = fromInclusive; i < toExclusive; i++)
183                                 if (predicate (i))
184                                         return true;
185
186                         return false;
187                 }
188
189                 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
190                 public static bool ForAll<T> (IEnumerable<T> collection, Predicate<T> predicate)
191                 {
192                         if (predicate == null)
193                                 throw new ArgumentNullException ("predicate");
194                         if (collection == null)
195                                 throw new ArgumentNullException ("collection");
196                         
197                         foreach (var t in collection)
198                                 if (!predicate (t))
199                                         return false;
200
201                         return true;
202                 }
203
204                 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
205                 public static bool ForAll (int fromInclusive, int toExclusive, Predicate<int> predicate)
206                 {
207                         if (predicate == null)
208                                 throw new ArgumentNullException ("predicate");
209                         if (toExclusive < fromInclusive)
210                                 throw new ArgumentException ("toExclusitve < fromInclusive");
211                         
212                         for (int i = fromInclusive; i < toExclusive; i++)
213                                 if (!predicate (i))
214                                         return false;
215
216                         return true;
217                 }
218
219                 [ConditionalAttribute("CONTRACTS_FULL")]
220                 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
221                 public static void Invariant (bool condition)
222                 {
223                         AssertMustUseRewriter (ContractFailureKind.Invariant, "Contract.Invariant");
224                 }
225
226                 [ConditionalAttribute("CONTRACTS_FULL")]
227                 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
228                 public static void Invariant (bool condition, string userMessage)
229                 {
230                         AssertMustUseRewriter (ContractFailureKind.Invariant, "Contract.Invariant");
231                 }
232
233                 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
234                 public static T OldValue<T> (T value)
235                 {
236                         // Marker method, no code required.
237                         return default (T);
238                 }
239
240                 [ConditionalAttribute("CONTRACTS_FULL")]
241                 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
242                 public static void Requires (bool condition)
243                 {
244                         AssertMustUseRewriter (ContractFailureKind.Precondition, "Contract.Requires");
245                 }
246
247                 [ConditionalAttribute("CONTRACTS_FULL")]
248                 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
249                 public static void Requires (bool condition, string userMessage)
250                 {
251                         AssertMustUseRewriter (ContractFailureKind.Precondition, "Contract.Requires");
252                 }
253
254                 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
255                 public static void Requires<TException> (bool condition) where TException : Exception
256                 {
257                         AssertMustUseRewriter (ContractFailureKind.Precondition, "Contract.Requires<TException>");
258                 }
259
260                 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
261                 public static void Requires<TException> (bool condition, string userMessage) where TException : Exception
262                 {
263                         AssertMustUseRewriter (ContractFailureKind.Precondition, "Contract.Requires<TException>");
264                 }
265
266                 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
267                 public static T Result<T> ()
268                 {
269                         // Marker method, no code required.
270                         return default (T);
271                 }
272
273                 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
274                 public static T ValueAtReturn<T> (out T value)
275                 {
276                         // Marker method, no code required.
277                         return value = default (T);
278                 }
279         }
280 }
281
282 #endif