|
| 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 | + |
| 4 | +using System.Globalization; |
| 5 | +using System.Runtime.InteropServices; |
| 6 | +using System.Threading; |
| 7 | + |
| 8 | +using Microsoft.Build.Evaluation; |
| 9 | + |
| 10 | +using Shouldly; |
| 11 | + |
| 12 | +using Xunit; |
| 13 | +using Xunit.Abstractions; |
| 14 | + |
| 15 | +namespace Microsoft.Build.Engine.UnitTests.Evaluation |
| 16 | +{ |
| 17 | + public class ExpanderFunction_Tests |
| 18 | + { |
| 19 | + private readonly ITestOutputHelper _output; |
| 20 | + |
| 21 | + public ExpanderFunction_Tests(ITestOutputHelper output) => _output = output; |
| 22 | + |
| 23 | + /* Tests for TryConvertToInt */ |
| 24 | + |
| 25 | + [Fact] |
| 26 | + public void TryConvertToIntGivenNull() |
| 27 | + { |
| 28 | + Expander<IProperty, IItem>.Function<IProperty>.TryConvertToInt(null, out int actual).ShouldBeFalse(); |
| 29 | + actual.ShouldBe(0); |
| 30 | + } |
| 31 | + |
| 32 | + [Fact] |
| 33 | + public void TryConvertToIntGivenDouble() |
| 34 | + { |
| 35 | + const double value = 10.0; |
| 36 | + Expander<IProperty, IItem>.Function<IProperty>.TryConvertToInt(value, out int actual).ShouldBeTrue(); |
| 37 | + actual.ShouldBe(10); |
| 38 | + } |
| 39 | + |
| 40 | + [Fact] |
| 41 | + public void TryConvertToIntGivenLong() |
| 42 | + { |
| 43 | + const long value = 10; |
| 44 | + Expander<IProperty, IItem>.Function<IProperty>.TryConvertToInt(value, out int actual).ShouldBeTrue(); |
| 45 | + actual.ShouldBe(10); |
| 46 | + } |
| 47 | + |
| 48 | + [Fact] |
| 49 | + public void TryConvertToIntGivenInt() |
| 50 | + { |
| 51 | + const int value = 10; |
| 52 | + Expander<IProperty, IItem>.Function<IProperty>.TryConvertToInt(value, out int actual).ShouldBeTrue(); |
| 53 | + actual.ShouldBe(10); |
| 54 | + } |
| 55 | + |
| 56 | + [Fact] |
| 57 | + public void TryConvertToIntGivenString() |
| 58 | + { |
| 59 | + const string value = "10"; |
| 60 | + Expander<IProperty, IItem>.Function<IProperty>.TryConvertToInt(value, out int actual).ShouldBeTrue(); |
| 61 | + actual.ShouldBe(10); |
| 62 | + } |
| 63 | + |
| 64 | + [Fact] |
| 65 | + public void TryConvertToIntGivenDoubleWithIntMinValue() |
| 66 | + { |
| 67 | + const int expected = int.MinValue; |
| 68 | + const double value = expected; |
| 69 | + Expander<IProperty, IItem>.Function<IProperty>.TryConvertToInt(value, out int actual).ShouldBeTrue(); |
| 70 | + actual.ShouldBe(expected); |
| 71 | + } |
| 72 | + |
| 73 | + [Fact] |
| 74 | + public void TryConvertToIntGivenDoubleWithIntMaxValue() |
| 75 | + { |
| 76 | + const int expected = int.MaxValue; |
| 77 | + const double value = expected; |
| 78 | + Expander<IProperty, IItem>.Function<IProperty>.TryConvertToInt(value, out int actual).ShouldBeTrue(); |
| 79 | + actual.ShouldBe(expected); |
| 80 | + } |
| 81 | + |
| 82 | + [Fact] |
| 83 | + public void TryConvertToIntGivenDoubleWithLessThanIntMinValue() |
| 84 | + { |
| 85 | + const double value = int.MinValue - 1.0; |
| 86 | + Expander<IProperty, IItem>.Function<IProperty>.TryConvertToInt(value, out int actual).ShouldBeFalse(); |
| 87 | + actual.ShouldBe(0); |
| 88 | + } |
| 89 | + |
| 90 | + [Fact] |
| 91 | + public void TryConvertToIntGivenDoubleWithGreaterThanIntMaxValue() |
| 92 | + { |
| 93 | + const double value = int.MaxValue + 1.0; |
| 94 | + Expander<IProperty, IItem>.Function<IProperty>.TryConvertToInt(value, out int actual).ShouldBeFalse(); |
| 95 | + actual.ShouldBe(0); |
| 96 | + } |
| 97 | + |
| 98 | + [Fact] |
| 99 | + public void TryConvertToIntGivenLongWithGreaterThanIntMaxValue() |
| 100 | + { |
| 101 | + const long value = int.MaxValue + 1L; |
| 102 | + Expander<IProperty, IItem>.Function<IProperty>.TryConvertToInt(value, out int actual).ShouldBeFalse(); |
| 103 | + actual.ShouldBe(0); |
| 104 | + } |
| 105 | + |
| 106 | + /* Tests for TryConvertToLong */ |
| 107 | + |
| 108 | + [Fact] |
| 109 | + public void TryConvertToLongGivenNull() |
| 110 | + { |
| 111 | + Expander<IProperty, IItem>.Function<IProperty>.TryConvertToLong(null, out long actual).ShouldBeFalse(); |
| 112 | + actual.ShouldBe(0); |
| 113 | + } |
| 114 | + |
| 115 | + [Fact] |
| 116 | + public void TryConvertToLongGivenDouble() |
| 117 | + { |
| 118 | + const double value = 10.0; |
| 119 | + Expander<IProperty, IItem>.Function<IProperty>.TryConvertToLong(value, out long actual).ShouldBeTrue(); |
| 120 | + actual.ShouldBe(10); |
| 121 | + } |
| 122 | + |
| 123 | + [Fact] |
| 124 | + public void TryConvertToLongGivenLong() |
| 125 | + { |
| 126 | + const long value = 10; |
| 127 | + Expander<IProperty, IItem>.Function<IProperty>.TryConvertToLong(value, out long actual).ShouldBeTrue(); |
| 128 | + actual.ShouldBe(10); |
| 129 | + } |
| 130 | + |
| 131 | + [Fact] |
| 132 | + public void TryConvertToLongGivenInt() |
| 133 | + { |
| 134 | + const int value = 10; |
| 135 | + Expander<IProperty, IItem>.Function<IProperty>.TryConvertToLong(value, out long actual).ShouldBeTrue(); |
| 136 | + actual.ShouldBe(10); |
| 137 | + } |
| 138 | + |
| 139 | + [Fact] |
| 140 | + public void TryConvertToLongGivenString() |
| 141 | + { |
| 142 | + const string value = "10"; |
| 143 | + Expander<IProperty, IItem>.Function<IProperty>.TryConvertToLong(value, out long actual).ShouldBeTrue(); |
| 144 | + actual.ShouldBe(10); |
| 145 | + } |
| 146 | + |
| 147 | + [Fact] |
| 148 | + public void TryConvertToLongGivenDoubleWithLongMinValue() |
| 149 | + { |
| 150 | + const long expected = long.MinValue; |
| 151 | + const double value = expected; |
| 152 | + Expander<IProperty, IItem>.Function<IProperty>.TryConvertToLong(value, out long actual).ShouldBeTrue(); |
| 153 | + actual.ShouldBe(expected); |
| 154 | + } |
| 155 | + |
| 156 | + [Fact] |
| 157 | + public void TryConvertToLongGivenDoubleWithLongMaxValueShouldNotThrow() |
| 158 | + { |
| 159 | + // An OverflowException should not be thrown from TryConvertToLong(). |
| 160 | + // Convert.ToInt64(double) has a defect and will throw an OverflowException |
| 161 | + // for values >= (long.MaxValue - 511) and <= long.MaxValue. |
| 162 | + _ = Should.NotThrow(() => Expander<IProperty, IItem>.Function<IProperty>.TryConvertToLong((double)long.MaxValue, out _)); |
| 163 | + } |
| 164 | + |
| 165 | + [Fact] |
| 166 | + public void TryConvertToLongGivenDoubleWithLongMaxValue() |
| 167 | + { |
| 168 | + const long longMaxValue = long.MaxValue; |
| 169 | + bool result = Expander<IProperty, IItem>.Function<IProperty>.TryConvertToLong((double)longMaxValue, out long actual); |
| 170 | + if (RuntimeInformation.OSArchitecture != Architecture.Arm64) |
| 171 | + { |
| 172 | + // Because of loss of precision, long.MaxValue will not 'round trip' from long to double to long. |
| 173 | + result.ShouldBeFalse(); |
| 174 | + actual.ShouldBe(0); |
| 175 | + } |
| 176 | + else |
| 177 | + { |
| 178 | + // Testing on macOS 12 on Apple Silicon M1 Pro produces different result. |
| 179 | + result.ShouldBeTrue(); |
| 180 | + actual.ShouldBe(longMaxValue); |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + [Fact] |
| 185 | + public void TryConvertToLongGivenDoubleWithVeryLargeLongValue() |
| 186 | + { |
| 187 | + // Because of loss of precision, veryLargeLong will not 'round trip' but within TryConvertToLong |
| 188 | + // the double to long conversion will pass the tolerance test. Return will be true and veryLargeLong != expected. |
| 189 | + const long veryLargeLong = long.MaxValue - 512; |
| 190 | + const double value = veryLargeLong; |
| 191 | + const long expected = 9223372036854774784L; |
| 192 | + Expander<IProperty, IItem>.Function<IProperty>.TryConvertToLong(value, out long actual).ShouldBeTrue(); |
| 193 | + actual.ShouldBe(expected); |
| 194 | + } |
| 195 | + |
| 196 | + [Fact] |
| 197 | + public void TryConvertToLongGivenDoubleWithLessThanLongMinValue() |
| 198 | + { |
| 199 | + const double value = -92233720368547758081D; |
| 200 | + Expander<IProperty, IItem>.Function<IProperty>.TryConvertToLong(value, out long actual).ShouldBeFalse(); |
| 201 | + actual.ShouldBe(0); |
| 202 | + } |
| 203 | + |
| 204 | + [Fact] |
| 205 | + public void TryConvertToLongGivenDoubleWithGreaterThanLongMaxValue() |
| 206 | + { |
| 207 | + const double value = (double)long.MaxValue + long.MaxValue; |
| 208 | + Expander<IProperty, IItem>.Function<IProperty>.TryConvertToLong(value, out long actual).ShouldBeFalse(); |
| 209 | + actual.ShouldBe(0); |
| 210 | + } |
| 211 | + |
| 212 | + /* Tests for TryConvertToDouble */ |
| 213 | + |
| 214 | + [Fact] |
| 215 | + public void TryConvertToDoubleGivenNull() |
| 216 | + { |
| 217 | + Expander<IProperty, IItem>.Function<IProperty>.TryConvertToDouble(null, out double actual).ShouldBeFalse(); |
| 218 | + actual.ShouldBe(0); |
| 219 | + } |
| 220 | + |
| 221 | + [Fact] |
| 222 | + public void TryConvertToDoubleGivenDouble() |
| 223 | + { |
| 224 | + const double value = 10.0; |
| 225 | + Expander<IProperty, IItem>.Function<IProperty>.TryConvertToDouble(value, out double actual).ShouldBeTrue(); |
| 226 | + actual.ShouldBe(10.0); |
| 227 | + } |
| 228 | + |
| 229 | + [Fact] |
| 230 | + public void TryConvertToDoubleGivenLong() |
| 231 | + { |
| 232 | + const long value = 10; |
| 233 | + Expander<IProperty, IItem>.Function<IProperty>.TryConvertToDouble(value, out double actual).ShouldBeTrue(); |
| 234 | + actual.ShouldBe(10.0); |
| 235 | + } |
| 236 | + |
| 237 | + [Fact] |
| 238 | + public void TryConvertToDoubleGivenInt() |
| 239 | + { |
| 240 | + const int value = 10; |
| 241 | + Expander<IProperty, IItem>.Function<IProperty>.TryConvertToDouble(value, out double actual).ShouldBeTrue(); |
| 242 | + actual.ShouldBe(10.0); |
| 243 | + } |
| 244 | + |
| 245 | + [Fact] |
| 246 | + public void TryConvertToDoubleGivenString() |
| 247 | + { |
| 248 | + const string value = "10"; |
| 249 | + Expander<IProperty, IItem>.Function<IProperty>.TryConvertToDouble(value, out double actual).ShouldBeTrue(); |
| 250 | + actual.ShouldBe(10.0); |
| 251 | + } |
| 252 | + |
| 253 | + [Fact] |
| 254 | + public void TryConvertToDoubleGivenStringAndLocale() |
| 255 | + { |
| 256 | + const string value = "1,2"; |
| 257 | + |
| 258 | + Thread currentThread = Thread.CurrentThread; |
| 259 | + CultureInfo originalCulture = currentThread.CurrentCulture; |
| 260 | + |
| 261 | + try |
| 262 | + { |
| 263 | + // English South Africa locale uses ',' as decimal separator. |
| 264 | + // The invariant culture should be used and "1,2" should be 12.0 not 1.2. |
| 265 | + var cultureEnglishSouthAfrica = CultureInfo.CreateSpecificCulture("en-ZA"); |
| 266 | + currentThread.CurrentCulture = cultureEnglishSouthAfrica; |
| 267 | + Expander<IProperty, IItem>.Function<IProperty>.TryConvertToDouble(value, out double actual).ShouldBeTrue(); |
| 268 | + actual.ShouldBe(12.0); |
| 269 | + } |
| 270 | + finally |
| 271 | + { |
| 272 | + // Restore CultureInfo. |
| 273 | + currentThread.CurrentCulture = originalCulture; |
| 274 | + CultureInfo.CurrentCulture = originalCulture; |
| 275 | + CultureInfo.DefaultThreadCurrentCulture = originalCulture; |
| 276 | + } |
| 277 | + } |
| 278 | + } |
| 279 | +} |
0 commit comments