1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4 
5 using System.Linq;
6 using Xunit;
7 
8 namespace System.Diagnostics.Contracts.Tests
9 {
10     public class ForAllTests
11     {
12         [Fact]
ArgumentExceptions()13         public static void ArgumentExceptions()
14         {
15             Assert.Throws<ArgumentNullException>(() => Contract.ForAll(0, 1, null));
16             Assert.Throws<ArgumentNullException>(() => Contract.ForAll<int>(null, i => true));
17             Assert.Throws<ArgumentNullException>(() => Contract.ForAll<int>(Enumerable.Empty<int>(), null));
18             AssertExtensions.Throws<ArgumentException>(null, () => Contract.ForAll(1, 0, i => true)); // fromInclusive > toExclusive
19         }
20 
21         [Fact]
EmptyInputReturnsTrue()22         public static void EmptyInputReturnsTrue()
23         {
24             Assert.True(Contract.ForAll(Enumerable.Empty<int>(), i => { throw new ShouldNotBeInvokedException(); }));
25             Assert.True(Contract.ForAll(-2, -2, i => { throw new ShouldNotBeInvokedException(); }));
26             Assert.True(Contract.ForAll(1, 1, i => { throw new ShouldNotBeInvokedException(); }));
27         }
28 
29         [Fact]
AnyFailsPredicateReturnsFalseImmediately()30         public static void AnyFailsPredicateReturnsFalseImmediately()
31         {
32             int count;
33 
34             count = 0;
35             Assert.False(Contract.ForAll(Enumerable.Range(0, 10), i => {
36                 count++;
37                 return i != 3;
38             }));
39             Assert.Equal(4, count);
40 
41             count = 0;
42             Assert.False(Contract.ForAll(-10, 0, i => {
43                 count++;
44                 return i != -8;
45             }));
46             Assert.Equal(3, count);
47         }
48 
49         [Fact]
AllPassPredicateReturnsTrue()50         public static void AllPassPredicateReturnsTrue()
51         {
52             Assert.True(Contract.ForAll(Enumerable.Range(0, 10), i => true));
53             Assert.True(Contract.ForAll(0, 10, i => true));
54         }
55 
56         [Fact]
ExceptionsPropagatedImmediately()57         public static void ExceptionsPropagatedImmediately()
58         {
59             int count;
60 
61             count = 0;
62             Assert.Throws<FormatException>(() => Contract.ForAll(Enumerable.Range(0, 10), i => {
63                 count++;
64                 if (i == 3) throw new FormatException();
65                 return true;
66             }));
67             Assert.Equal(4, count);
68 
69             count = 0;
70             Assert.Throws<FormatException>(() => Contract.ForAll(100, 110, i => {
71                 count++;
72                 if (i == 105) throw new FormatException();
73                 return true;
74             }));
75             Assert.Equal(6, count);
76         }
77 
78     }
79 }
80