|
| 1 | +using static Microsoft.VisualStudio.TestTools.UnitTesting.Assert; |
| 2 | +using static SqlServerSimulator.TestHelpers; |
| 3 | + |
| 4 | +namespace SqlServerSimulator; |
| 5 | + |
| 6 | +[TestClass] |
| 7 | +public sealed class MathScalarTrigTests |
| 8 | +{ |
| 9 | + [TestMethod] |
| 10 | + public void Sin_OfZero_ReturnsZero() => AreEqual(0.0, ExecuteScalar("select sin(cast(0 as float))")); |
| 11 | + |
| 12 | + [TestMethod] |
| 13 | + public void Cos_OfZero_ReturnsOne() => AreEqual(1.0, ExecuteScalar("select cos(cast(0 as float))")); |
| 14 | + |
| 15 | + [TestMethod] |
| 16 | + public void Tan_OfZero_ReturnsZero() => AreEqual(0.0, ExecuteScalar("select tan(cast(0 as float))")); |
| 17 | + |
| 18 | + [TestMethod] |
| 19 | + public void Asin_OfOne_ReturnsHalfPi() |
| 20 | + => AreEqual(Math.PI / 2, ExecuteScalar("select asin(cast(1 as float))")); |
| 21 | + |
| 22 | + [TestMethod] |
| 23 | + public void Asin_OfNegativeOne_ReturnsNegativeHalfPi() |
| 24 | + => AreEqual(-Math.PI / 2, ExecuteScalar("select asin(cast(-1 as float))")); |
| 25 | + |
| 26 | + [TestMethod] |
| 27 | + public void Acos_OfOne_ReturnsZero() => AreEqual(0.0, ExecuteScalar("select acos(cast(1 as float))")); |
| 28 | + |
| 29 | + [TestMethod] |
| 30 | + public void Acos_OfNegativeOne_ReturnsPi() |
| 31 | + => AreEqual(Math.PI, ExecuteScalar("select acos(cast(-1 as float))")); |
| 32 | + |
| 33 | + [TestMethod] |
| 34 | + public void Atan_OfOne_ReturnsQuarterPi() |
| 35 | + => AreEqual(Math.PI / 4, ExecuteScalar("select atan(cast(1 as float))")); |
| 36 | + |
| 37 | + [TestMethod] |
| 38 | + public void Atn2_OfOneOne_ReturnsQuarterPi() |
| 39 | + => AreEqual(Math.PI / 4, ExecuteScalar("select atn2(cast(1 as float), cast(1 as float))")); |
| 40 | + |
| 41 | + [TestMethod] |
| 42 | + public void Atn2_OfZeroZero_RaisesMsg3623() |
| 43 | + => AssertSqlError("select atn2(cast(0 as float), cast(0 as float))", 3623, "An invalid floating point operation occurred."); |
| 44 | + |
| 45 | + [TestMethod] |
| 46 | + public void Cot_OfOne_ReturnsCotangentOne() |
| 47 | + { |
| 48 | + var v = (double)ExecuteScalar("select cot(cast(1 as float))")!; |
| 49 | + IsLessThan(1e-12, Math.Abs(v - (1.0 / Math.Tan(1.0)))); |
| 50 | + } |
| 51 | + |
| 52 | + [TestMethod] |
| 53 | + public void Cot_OfZero_RaisesMsg3623() |
| 54 | + => AssertSqlError("select cot(cast(0 as float))", 3623, "An invalid floating point operation occurred."); |
| 55 | + |
| 56 | + [TestMethod] |
| 57 | + public void Pi_ReturnsPiAsFloat() => AreEqual(Math.PI, ExecuteScalar("select pi()")); |
| 58 | + |
| 59 | + [TestMethod] |
| 60 | + public void Pi_WithArgument_RaisesMsg174() |
| 61 | + => AssertSqlError("select pi(1)", 174, "The pi function requires 0 argument(s)."); |
| 62 | + |
| 63 | + [TestMethod] |
| 64 | + public void Sin_TooFewArgs_RaisesMsg174() |
| 65 | + => AssertSqlError("select sin()", 174, "The sin function requires 1 argument(s)."); |
| 66 | + |
| 67 | + [TestMethod] |
| 68 | + public void Sin_TooManyArgs_RaisesMsg174() |
| 69 | + => AssertSqlError("select sin(1, 2)", 174, "The sin function requires 1 argument(s)."); |
| 70 | + |
| 71 | + [TestMethod] |
| 72 | + public void Atn2_OneArg_RaisesMsg174() |
| 73 | + => AssertSqlError("select atn2(1)", 174, "The atn2 function requires 2 argument(s)."); |
| 74 | + |
| 75 | + [TestMethod] |
| 76 | + public void Atn2_ThreeArgs_RaisesMsg174() |
| 77 | + => AssertSqlError("select atn2(1, 2, 3)", 174, "The atn2 function requires 2 argument(s)."); |
| 78 | + |
| 79 | + [TestMethod] |
| 80 | + public void Asin_OutOfDomain_RaisesMsg3623() |
| 81 | + { |
| 82 | + AssertSqlError("select asin(cast(2 as float))", 3623, "An invalid floating point operation occurred."); |
| 83 | + AssertSqlError("select asin(cast(-2 as float))", 3623, "An invalid floating point operation occurred."); |
| 84 | + } |
| 85 | + |
| 86 | + [TestMethod] |
| 87 | + public void Acos_OutOfDomain_RaisesMsg3623() |
| 88 | + { |
| 89 | + AssertSqlError("select acos(cast(2 as float))", 3623, "An invalid floating point operation occurred."); |
| 90 | + AssertSqlError("select acos(cast(-2 as float))", 3623, "An invalid floating point operation occurred."); |
| 91 | + } |
| 92 | + |
| 93 | + [TestMethod] |
| 94 | + public void Sin_NullInput_PropagatesNull() |
| 95 | + => AreEqual(DBNull.Value, ExecuteScalar("select sin(cast(null as float))")); |
| 96 | + |
| 97 | + [TestMethod] |
| 98 | + public void Atn2_NullFirst_PropagatesNull() |
| 99 | + => AreEqual(DBNull.Value, ExecuteScalar("select atn2(cast(null as float), cast(1 as float))")); |
| 100 | + |
| 101 | + [TestMethod] |
| 102 | + public void Atn2_NullSecond_PropagatesNull() |
| 103 | + => AreEqual(DBNull.Value, ExecuteScalar("select atn2(cast(1 as float), cast(null as float))")); |
| 104 | + |
| 105 | + [TestMethod] |
| 106 | + public void Sin_AcceptsAnyNumericInput() |
| 107 | + { |
| 108 | + AreEqual(Math.Sin(1), ExecuteScalar("select sin(cast(1 as int))")); |
| 109 | + AreEqual(Math.Sin(1), ExecuteScalar("select sin(cast(1 as bigint))")); |
| 110 | + AreEqual(Math.Sin(1), ExecuteScalar("select sin(cast(1 as decimal(10,2)))")); |
| 111 | + AreEqual(Math.Sin(1), ExecuteScalar("select sin(cast(1 as money))")); |
| 112 | + } |
| 113 | + |
| 114 | + [TestMethod] |
| 115 | + public void Square_OfTwoFloat_ReturnsFour() |
| 116 | + => AreEqual(4.0, ExecuteScalar("select square(cast(2 as float))")); |
| 117 | + |
| 118 | + [TestMethod] |
| 119 | + public void Square_OfDecimal_WidensToFloat() |
| 120 | + => AreEqual(12.25, ExecuteScalar("select square(cast(3.5 as decimal(10,2)))")); |
| 121 | + |
| 122 | + [TestMethod] |
| 123 | + public void Square_OfInt_WidensToFloat() |
| 124 | + => AreEqual(2500000000.0, ExecuteScalar("select square(cast(50000 as int))")); |
| 125 | + |
| 126 | + [TestMethod] |
| 127 | + public void Square_Overflow_RaisesMsg8115() |
| 128 | + => AssertSqlError("select square(cast(1e200 as float))", 8115, "Arithmetic overflow error converting expression to data type float."); |
| 129 | + |
| 130 | + [TestMethod] |
| 131 | + public void Degrees_OfPi_ReturnsOneHundredEighty() |
| 132 | + => AreEqual(180.0, ExecuteScalar("select degrees(pi())")); |
| 133 | + |
| 134 | + [TestMethod] |
| 135 | + public void Radians_OfOneEighty_ReturnsPi() |
| 136 | + => AreEqual(Math.PI, ExecuteScalar("select radians(cast(180 as float))")); |
| 137 | + |
| 138 | + [TestMethod] |
| 139 | + public void Degrees_OfInt_PreservesIntTypeAndTruncates() |
| 140 | + { |
| 141 | + AreEqual(57, ExecuteScalar<int>("select degrees(cast(1 as int))")); |
| 142 | + AreEqual(20626, ExecuteScalar<int>("select degrees(cast(360 as int))")); |
| 143 | + AreEqual(-57, ExecuteScalar<int>("select degrees(cast(-1 as int))")); |
| 144 | + } |
| 145 | + |
| 146 | + [TestMethod] |
| 147 | + public void Degrees_OfBigint_PreservesBigint() |
| 148 | + => AreEqual(57L, ExecuteScalar<long>("select degrees(cast(1 as bigint))")); |
| 149 | + |
| 150 | + [TestMethod] |
| 151 | + public void Degrees_IntOverflow_RaisesMsg8115Int() |
| 152 | + => AssertSqlError("select degrees(cast(2147483646 as int))", 8115, "Arithmetic overflow error converting expression to data type int."); |
| 153 | + |
| 154 | + [TestMethod] |
| 155 | + public void Degrees_OfDecimal_WidensToDecimal38_18() |
| 156 | + { |
| 157 | + // SQL Server's decimal arithmetic uses higher-precision intermediate |
| 158 | + // accumulators than .NET's 96-bit decimal, so the trailing digits |
| 159 | + // diverge past the 14th decimal place. Real value: 85.94366926962348429...; |
| 160 | + // simulator value: 85.94366926962348131... (matches at 13 digits). |
| 161 | + var v = (decimal)ExecuteScalar("select degrees(cast(1.5 as decimal(10,2)))")!; |
| 162 | + IsLessThan(1e-12m, Math.Abs(85.94366926962348m - v)); |
| 163 | + } |
| 164 | + |
| 165 | + [TestMethod] |
| 166 | + public void Radians_OfDecimal_WidensToDecimal38_18() |
| 167 | + { |
| 168 | + var v = (decimal)ExecuteScalar("select radians(cast(180 as decimal(10,2)))")!; |
| 169 | + // 180 * pi / 180 = pi rendered as decimal(38, 18). Tolerance to absorb |
| 170 | + // .NET-vs-SQL-Server decimal-arithmetic precision differences. |
| 171 | + IsLessThan(1e-12m, Math.Abs(3.141592653589793m - v)); |
| 172 | + } |
| 173 | + |
| 174 | + [TestMethod] |
| 175 | + public void Degrees_PreservesScaleAboveEighteen() |
| 176 | + { |
| 177 | + // Input scale 22 > 18 → result decimal(38, 22). The .NET decimal storage |
| 178 | + // accommodates scale 22 for small values; the encoder's Pow10(22) ≈ 1e22 |
| 179 | + // fits inside decimal.MaxValue (7.92e28). Larger scales (e.g. > 28) |
| 180 | + // exceed .NET decimal's range entirely — that's the pre-existing |
| 181 | + // 28-digit-max quirk noted in CLAUDE.md, not specific to DEGREES. |
| 182 | + var v = (decimal)ExecuteScalar("select degrees(cast(0.001 as decimal(25,22)))")!; |
| 183 | + var scale = (decimal.GetBits(v)[3] >> 16) & 0xFF; |
| 184 | + IsLessThanOrEqualTo(22, scale); |
| 185 | + } |
| 186 | + |
| 187 | + [TestMethod] |
| 188 | + public void Degrees_OfMoney_StaysMoney() |
| 189 | + { |
| 190 | + var v = (decimal)ExecuteScalar("select degrees(cast(1 as money))")!; |
| 191 | + // money has scale 4; 1 radian in degrees ≈ 57.2958. |
| 192 | + AreEqual(57.2958m, v); |
| 193 | + } |
| 194 | + |
| 195 | + [TestMethod] |
| 196 | + public void Degrees_TooFewArgs_RaisesMsg174() |
| 197 | + => AssertSqlError("select degrees()", 174, "The degrees function requires 1 argument(s)."); |
| 198 | + |
| 199 | + [TestMethod] |
| 200 | + public void Radians_TooManyArgs_RaisesMsg174() |
| 201 | + => AssertSqlError("select radians(1, 2)", 174, "The radians function requires 1 argument(s)."); |
| 202 | + |
| 203 | + [TestMethod] |
| 204 | + public void Cos_OfPi_ReturnsNegativeOne() |
| 205 | + => AreEqual(-1.0, ExecuteScalar("select cos(pi())")); |
| 206 | + |
| 207 | + [TestMethod] |
| 208 | + public void Square_OfNullFloat_PropagatesNull() |
| 209 | + => AreEqual(DBNull.Value, ExecuteScalar("select square(cast(null as float))")); |
| 210 | +} |
0 commit comments