1 /* ****************************************************************************
3 * Copyright (c) Microsoft Corporation.
5 * This source code is subject to terms and conditions of the Apache License, Version 2.0. A
6 * copy of the license can be found in the License.html file at the root of this distribution. If
7 * you cannot locate the Apache License, Version 2.0, please send an email to
8 * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
9 * by the terms of the Apache License, Version 2.0.
11 * You must not remove this notice, or any other, from this software.
14 * ***************************************************************************/
17 using System.Linq.Expressions;
19 using Microsoft.Scripting.Ast;
23 using System.Collections;
24 using System.Collections.Generic;
25 using System.Diagnostics;
27 namespace Microsoft.Scripting.Utils {
28 public static class ContractUtils {
29 [Conditional("DEBUG")]
30 public static void Assert(bool precondition) {
31 Debug.Assert(precondition);
34 public static void Requires(bool precondition) {
36 throw new ArgumentException(Strings.MethodPreconditionViolated);
40 public static void Requires(bool precondition, string paramName) {
41 Utils.Assert.NotEmpty(paramName);
44 throw new ArgumentException(Strings.InvalidArgumentValue, paramName);
48 public static void Requires(bool precondition, string paramName, string message) {
49 Utils.Assert.NotEmpty(paramName);
52 throw new ArgumentException(message, paramName);
56 public static void RequiresNotNull(object value, string paramName) {
57 Utils.Assert.NotEmpty(paramName);
60 throw new ArgumentNullException(paramName);
64 public static void RequiresNotEmpty(string str, string paramName) {
65 RequiresNotNull(str, paramName);
66 if (str.Length == 0) {
67 throw new ArgumentException(Strings.NonEmptyStringRequired, paramName);
71 public static void RequiresNotEmpty<T>(ICollection<T> collection, string paramName) {
72 RequiresNotNull(collection, paramName);
73 if (collection.Count == 0) {
74 throw new ArgumentException(Strings.NonEmptyCollectionRequired, paramName);
79 /// Requires the specified index to point inside the array.
81 /// <exception cref="ArgumentNullException">Array is <c>null</c>.</exception>
82 /// <exception cref="ArgumentOutOfRangeException">Index is outside the array.</exception>
83 public static void RequiresArrayIndex<T>(IList<T> array, int index, string indexName) {
84 RequiresArrayIndex(array.Count, index, indexName);
88 /// Requires the specified index to point inside the array.
90 /// <exception cref="ArgumentOutOfRangeException">Index is outside the array.</exception>
91 public static void RequiresArrayIndex(int arraySize, int index, string indexName) {
92 Utils.Assert.NotEmpty(indexName);
93 Debug.Assert(arraySize >= 0);
95 if (index < 0 || index >= arraySize) throw new ArgumentOutOfRangeException(indexName);
99 /// Requires the specified index to point inside the array or at the end
101 /// <exception cref="ArgumentNullException">Array is <c>null</c>.</exception>
102 /// <exception cref="ArgumentOutOfRangeException">Index is outside the array.</exception>
103 public static void RequiresArrayInsertIndex<T>(IList<T> array, int index, string indexName) {
104 RequiresArrayInsertIndex(array.Count, index, indexName);
108 /// Requires the specified index to point inside the array or at the end
110 /// <exception cref="ArgumentNullException">Array is <c>null</c>.</exception>
111 /// <exception cref="ArgumentOutOfRangeException">Index is outside the array.</exception>
112 public static void RequiresArrayInsertIndex(int arraySize, int index, string indexName) {
113 Utils.Assert.NotEmpty(indexName);
114 Debug.Assert(arraySize >= 0);
116 if (index < 0 || index > arraySize) throw new ArgumentOutOfRangeException(indexName);
120 /// Requires the range [offset, offset + count] to be a subset of [0, array.Count].
122 /// <exception cref="ArgumentOutOfRangeException">Offset or count are out of range.</exception>
123 public static void RequiresArrayRange<T>(IList<T> array, int offset, int count, string offsetName, string countName) {
124 Utils.Assert.NotNull(array);
125 RequiresArrayRange(array.Count, offset, count, offsetName, countName);
129 /// Requires the range [offset, offset + count] to be a subset of [0, array.Count].
131 /// <exception cref="ArgumentOutOfRangeException">Offset or count are out of range.</exception>
132 public static void RequiresArrayRange(int arraySize, int offset, int count, string offsetName, string countName) {
133 Utils.Assert.NotEmpty(offsetName);
134 Utils.Assert.NotEmpty(countName);
135 Debug.Assert(arraySize >= 0);
137 if (count < 0) throw new ArgumentOutOfRangeException(countName);
138 if (offset < 0 || arraySize - offset < count) throw new ArgumentOutOfRangeException(offsetName);
143 /// Requires the range [offset, offset + count] to be a subset of [0, array.Count].
145 /// <exception cref="ArgumentNullException">Array is <c>null</c>.</exception>
146 /// <exception cref="ArgumentOutOfRangeException">Offset or count are out of range.</exception>
147 public static void RequiresListRange(IList array, int offset, int count, string offsetName, string countName) {
148 Utils.Assert.NotEmpty(offsetName);
149 Utils.Assert.NotEmpty(countName);
150 Utils.Assert.NotNull(array);
152 if (count < 0) throw new ArgumentOutOfRangeException(countName);
153 if (offset < 0 || array.Count - offset < count) throw new ArgumentOutOfRangeException(offsetName);
157 /// Requires the range [offset, offset + count] to be a subset of [0, array.Count].
159 /// <exception cref="ArgumentNullException">String is <c>null</c>.</exception>
160 /// <exception cref="ArgumentOutOfRangeException">Offset or count are out of range.</exception>
161 public static void RequiresArrayRange(string str, int offset, int count, string offsetName, string countName) {
162 Utils.Assert.NotEmpty(offsetName);
163 Utils.Assert.NotEmpty(countName);
164 Utils.Assert.NotNull(str);
166 if (count < 0) throw new ArgumentOutOfRangeException(countName);
167 if (offset < 0 || str.Length - offset < count) throw new ArgumentOutOfRangeException(offsetName);
171 /// Requires the array and all its items to be non-null.
173 public static void RequiresNotNullItems<T>(IList<T> array, string arrayName) {
174 Utils.Assert.NotNull(arrayName);
175 RequiresNotNull(array, arrayName);
177 for (int i = 0; i < array.Count; i++) {
178 if (array[i] == null) {
179 throw ExceptionUtils.MakeArgumentItemNullException(i, arrayName);
185 /// Requires the enumerable collection and all its items to be non-null.
187 public static void RequiresNotNullItems<T>(IEnumerable<T> collection, string collectionName) {
188 Utils.Assert.NotNull(collectionName);
189 RequiresNotNull(collection, collectionName);
192 foreach (var item in collection) {
194 throw ExceptionUtils.MakeArgumentItemNullException(i, collectionName);
200 [Conditional("FALSE")]
201 public static void Invariant(bool condition) {
202 Debug.Assert(condition);
205 [Conditional("FALSE")]
206 public static void Invariant(bool condition, string message) {
207 Debug.Assert(condition, message);
210 [Conditional("FALSE")]
211 public static void Ensures(bool condition) {
215 [Conditional("FALSE")]
216 public static void Ensures(bool condition, string message) {
220 public static T Result<T>() {
224 public static T Parameter<T>(out T value) {
229 public static T Old<T>(T value) {