|
| 1 | +using System.Numerics; |
| 2 | + |
| 3 | +namespace RoyalCode.SmartValidations.Tests; |
| 4 | + |
| 5 | +public partial class BuildInPredicatesTests |
| 6 | +{ |
| 7 | + [Theory] |
| 8 | + [MemberData(nameof(Comparable_Data))] |
| 9 | + public void Comparable_Min<T>(T value, T min, int compare) |
| 10 | + where T : IComparable<T> |
| 11 | + { |
| 12 | + // Arrange |
| 13 | + bool expected = compare >= 0; |
| 14 | + |
| 15 | + // Act |
| 16 | + var result = BuildInPredicates.Min(value, min); |
| 17 | + |
| 18 | + // Assert |
| 19 | + Assert.Equal(expected, result); |
| 20 | + } |
| 21 | + |
| 22 | + [Theory] |
| 23 | + [MemberData(nameof(Nullable_Comparable_Data))] |
| 24 | + public void Nullable_Comparable_Min<T>(T? value, T min, int compare) |
| 25 | + where T : struct, IComparable<T> |
| 26 | + { |
| 27 | + // Arrange |
| 28 | + bool expected = compare >= 0; |
| 29 | + |
| 30 | + // Act |
| 31 | + var result = BuildInPredicates.Min(value, min); |
| 32 | + |
| 33 | + // Assert |
| 34 | + Assert.Equal(expected, result); |
| 35 | + } |
| 36 | + |
| 37 | + [Theory] |
| 38 | + [InlineData(null, 0, false)] |
| 39 | + [InlineData(null, 1, false)] |
| 40 | + [InlineData(0, 0, true)] |
| 41 | + [InlineData(2, 1, true)] |
| 42 | + [InlineData(1, 2, false)] |
| 43 | + public void Comparable_Nulls_Min(int? value, int min, bool expected) |
| 44 | + { |
| 45 | + // Arrange |
| 46 | + // Act |
| 47 | + var result = BuildInPredicates.Min(value, min); |
| 48 | + |
| 49 | + // Assert |
| 50 | + Assert.Equal(expected, result); |
| 51 | + } |
| 52 | + |
| 53 | + [Theory] |
| 54 | + [MemberData(nameof(Comparable_Data))] |
| 55 | + public void Comparable_Max<T>(T value, T max, int compare) |
| 56 | + where T : IComparable<T> |
| 57 | + { |
| 58 | + // Arrange |
| 59 | + bool expected = compare <= 0; |
| 60 | + |
| 61 | + // Act |
| 62 | + var result = BuildInPredicates.Max(value, max); |
| 63 | + |
| 64 | + // Assert |
| 65 | + Assert.Equal(expected, result); |
| 66 | + } |
| 67 | + |
| 68 | + [Theory] |
| 69 | + [MemberData(nameof(Nullable_Comparable_Data))] |
| 70 | + public void Nullable_Comparable_Max<T>(T? value, T max, int compare) |
| 71 | + where T : struct, IComparable<T> |
| 72 | + { |
| 73 | + // Arrange |
| 74 | + bool expected = compare <= 0; |
| 75 | + |
| 76 | + // Act |
| 77 | + var result = BuildInPredicates.Max(value, max); |
| 78 | + |
| 79 | + // Assert |
| 80 | + Assert.Equal(expected, result); |
| 81 | + } |
| 82 | + |
| 83 | + [Theory] |
| 84 | + [InlineData(null, 0, false)] |
| 85 | + [InlineData(null, 1, false)] |
| 86 | + [InlineData(0, 0, true)] |
| 87 | + [InlineData(2, 1, false)] |
| 88 | + [InlineData(1, 2, true)] |
| 89 | + public void Comparable_Nulls_Max(int? value, int max, bool expected) |
| 90 | + { |
| 91 | + // Arrange |
| 92 | + // Act |
| 93 | + var result = BuildInPredicates.Max(value, max); |
| 94 | + |
| 95 | + // Assert |
| 96 | + Assert.Equal(expected, result); |
| 97 | + } |
| 98 | + |
| 99 | + |
| 100 | + public static IEnumerable<object[]> Nullable_Comparable_Data() |
| 101 | + { |
| 102 | + foreach(var data in Comparable_Data()) |
| 103 | + yield return data; |
| 104 | + |
| 105 | + yield return [(byte?)0, (byte)1, -1]; |
| 106 | + yield return [(byte?)1, (byte)1, 0]; |
| 107 | + yield return [(byte?)2, (byte)1, 1]; |
| 108 | + |
| 109 | + yield return [(short?)0, (short)1, -1]; |
| 110 | + yield return [(short?)1, (short)1, 0]; |
| 111 | + yield return [(short?)2, (short)1, 1]; |
| 112 | + |
| 113 | + yield return [(int?)0, 1, -1]; |
| 114 | + yield return [(int?)1, 1, 0]; |
| 115 | + yield return [(int?)2, 1, 2]; |
| 116 | + |
| 117 | + yield return [(long?)0, 1L, -1]; |
| 118 | + yield return [(long?)1, 1L, 0]; |
| 119 | + yield return [(long?)2, 1L, 1]; |
| 120 | + |
| 121 | + yield return [(float?)0, 1f, -1]; |
| 122 | + yield return [(float?)1, 1f, 0]; |
| 123 | + yield return [(float?)2, 1f, 1]; |
| 124 | + |
| 125 | + yield return [(double?)0, 1d, -1]; |
| 126 | + yield return [(double?)1, 1d, 0]; |
| 127 | + yield return [(double?)2, 1d, 1]; |
| 128 | + |
| 129 | + yield return [(decimal?)0, 1M, -1]; |
| 130 | + yield return [(decimal?)1, 1M, 0]; |
| 131 | + yield return [(decimal?)2, 1M, 1]; |
| 132 | + } |
| 133 | + |
| 134 | + public static IEnumerable<object[]> Comparable_Data() |
| 135 | + { |
| 136 | + yield return [(byte)0, (byte)1, -1]; |
| 137 | + yield return [(byte)1, (byte)1, 0]; |
| 138 | + yield return [(byte)2, (byte)1, 1]; |
| 139 | + |
| 140 | + yield return [(short)0, (short)1, -1]; |
| 141 | + yield return [(short)1, (short)1, 0]; |
| 142 | + yield return [(short)2, (short)1, 1]; |
| 143 | + |
| 144 | + yield return [0, 1, -1]; |
| 145 | + yield return [1, 1, 0]; |
| 146 | + yield return [2, 1, 1]; |
| 147 | + |
| 148 | + yield return [0L, 1L, -1]; |
| 149 | + yield return [1L, 1L, 0]; |
| 150 | + yield return [2L, 1L, 1]; |
| 151 | + |
| 152 | + yield return [0f, 1f, -1]; |
| 153 | + yield return [1f, 1f, 0]; |
| 154 | + yield return [2f, 1f, 1]; |
| 155 | + |
| 156 | + yield return [0d, 1d, -1]; |
| 157 | + yield return [1d, 1d, 0]; |
| 158 | + yield return [2d, 1d, 1]; |
| 159 | + |
| 160 | + yield return [0M, 1M, -1]; |
| 161 | + yield return [1M, 1M, 0]; |
| 162 | + yield return [2M, 1M, 1]; |
| 163 | + |
| 164 | + yield return [new BigInteger(0), BigInteger.One, -1]; |
| 165 | + yield return [BigInteger.One, BigInteger.One, 0]; |
| 166 | + yield return [new BigInteger(2), BigInteger.One, 1]; |
| 167 | + } |
| 168 | +} |
0 commit comments