Skip to content

Commit 6b6dc12

Browse files
committed
Add supprt for System.MathF methods
1 parent 92de0b7 commit 6b6dc12

File tree

4 files changed

+177
-3
lines changed

4 files changed

+177
-3
lines changed
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
#if NETCOREAPP2_0
2+
using System;
3+
using System.Linq;
4+
using System.Linq.Expressions;
5+
using NHibernate.DomainModel.Northwind.Entities;
6+
using NUnit.Framework;
7+
8+
namespace NHibernate.Test.Linq
9+
{
10+
[TestFixture]
11+
public class MathFTests : LinqTestCase
12+
{
13+
private IQueryable<OrderLine> _orderLines;
14+
15+
protected override void OnSetUp()
16+
{
17+
base.OnSetUp();
18+
_orderLines = db.OrderLines
19+
.OrderBy(ol => ol.Id)
20+
.Take(10).ToList().AsQueryable();
21+
}
22+
23+
[Test]
24+
public void SignAllPositiveTest()
25+
{
26+
AssumeFunctionSupported("sign");
27+
var signs = (from o in db.OrderLines
28+
select MathF.Sign((float) o.UnitPrice)).ToList();
29+
30+
Assert.That(signs.All(x => x == 1), Is.True);
31+
}
32+
33+
[Test]
34+
public void SignAllNegativeTest()
35+
{
36+
AssumeFunctionSupported("sign");
37+
var signs = (from o in db.OrderLines
38+
select MathF.Sign(0f - (float) o.UnitPrice)).ToList();
39+
40+
Assert.That(signs.All(x => x == -1), Is.True);
41+
}
42+
43+
[Test]
44+
public void SinTest()
45+
{
46+
AssumeFunctionSupported("sin");
47+
Test(o => MathF.Round(MathF.Sin((float) o.UnitPrice), 5));
48+
}
49+
50+
[Test]
51+
public void CosTest()
52+
{
53+
AssumeFunctionSupported("cos");
54+
Test(o => MathF.Round(MathF.Cos((float)o.UnitPrice), 5));
55+
}
56+
57+
[Test]
58+
public void TanTest()
59+
{
60+
AssumeFunctionSupported("tan");
61+
Test(o => MathF.Round(MathF.Tan((float)o.Discount), 5));
62+
}
63+
64+
[Test]
65+
public void SinhTest()
66+
{
67+
AssumeFunctionSupported("sinh");
68+
Test(o => MathF.Round(MathF.Sinh((float)o.Discount), 5));
69+
}
70+
71+
[Test]
72+
public void CoshTest()
73+
{
74+
AssumeFunctionSupported("cosh");
75+
Test(o => MathF.Round(MathF.Cosh((float)o.Discount), 5));
76+
}
77+
78+
[Test]
79+
public void TanhTest()
80+
{
81+
AssumeFunctionSupported("tanh");
82+
Test(o => MathF.Round(MathF.Tanh((float)o.Discount), 5));
83+
}
84+
85+
[Test]
86+
public void AsinTest()
87+
{
88+
AssumeFunctionSupported("asin");
89+
Test(o => MathF.Round(MathF.Asin((float)o.Discount), 5));
90+
}
91+
92+
[Test]
93+
public void AcosTest()
94+
{
95+
AssumeFunctionSupported("acos");
96+
Test(o => MathF.Round(MathF.Acos((float)o.Discount), 5));
97+
}
98+
99+
[Test]
100+
public void AtanTest()
101+
{
102+
AssumeFunctionSupported("atan");
103+
Test(o => MathF.Round(MathF.Atan((float)o.UnitPrice), 5));
104+
}
105+
106+
[Test]
107+
public void Atan2Test()
108+
{
109+
AssumeFunctionSupported("atan2");
110+
Test(o => MathF.Round(MathF.Atan2((float)o.Discount, 0.5f), 5));
111+
}
112+
113+
[Test]
114+
public void PowTest()
115+
{
116+
AssumeFunctionSupported("power");
117+
Test(o => MathF.Round(MathF.Pow((float)o.Discount, 0.5f), 5));
118+
}
119+
120+
private void Test(Expression<Func<OrderLine, float>> selector)
121+
{
122+
var expected = _orderLines
123+
.Select(selector)
124+
.ToList();
125+
126+
var actual = db.OrderLines
127+
.OrderBy(ol => ol.Id)
128+
.Select(selector)
129+
.Take(10)
130+
.ToList();
131+
132+
Assert.AreEqual(expected.Count, actual.Count);
133+
for (var i = 0; i < expected.Count; i++)
134+
Assert.AreEqual(expected[i], actual[i], 0.000001);
135+
}
136+
}
137+
}
138+
#endif

src/NHibernate/Linq/Functions/MathGenerator.cs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,39 @@ public MathGenerator()
4848
ReflectHelper.GetMethodDefinition(() => Math.Floor(default(decimal))),
4949
ReflectHelper.GetMethodDefinition(() => Math.Floor(default(double))),
5050
ReflectHelper.GetMethodDefinition(() => decimal.Floor(default(decimal))),
51-
51+
5252
ReflectHelper.GetMethodDefinition(() => Math.Ceiling(default(decimal))),
5353
ReflectHelper.GetMethodDefinition(() => Math.Ceiling(default(double))),
5454
ReflectHelper.GetMethodDefinition(() => decimal.Ceiling(default(decimal))),
55-
55+
5656
ReflectHelper.GetMethodDefinition(() => Math.Pow(default(double), default(double))),
57+
58+
#if NETCOREAPP2_0
59+
ReflectHelper.GetMethodDefinition(() => MathF.Sin(default(float))),
60+
ReflectHelper.GetMethodDefinition(() => MathF.Cos(default(float))),
61+
ReflectHelper.GetMethodDefinition(() => MathF.Tan(default(float))),
62+
63+
ReflectHelper.GetMethodDefinition(() => MathF.Sinh(default(float))),
64+
ReflectHelper.GetMethodDefinition(() => MathF.Cosh(default(float))),
65+
ReflectHelper.GetMethodDefinition(() => MathF.Tanh(default(float))),
66+
67+
ReflectHelper.GetMethodDefinition(() => MathF.Asin(default(float))),
68+
ReflectHelper.GetMethodDefinition(() => MathF.Acos(default(float))),
69+
ReflectHelper.GetMethodDefinition(() => MathF.Atan(default(float))),
70+
ReflectHelper.GetMethodDefinition(() => MathF.Atan2(default(float), default(float))),
71+
72+
ReflectHelper.GetMethodDefinition(() => MathF.Sqrt(default(float))),
73+
74+
ReflectHelper.GetMethodDefinition(() => MathF.Abs(default(float))),
75+
76+
ReflectHelper.GetMethodDefinition(() => MathF.Sign(default(float))),
77+
78+
ReflectHelper.GetMethodDefinition(() => MathF.Floor(default(float))),
79+
80+
ReflectHelper.GetMethodDefinition(() => MathF.Ceiling(default(float))),
81+
82+
ReflectHelper.GetMethodDefinition(() => MathF.Pow(default(float), default(float))),
83+
#endif
5784
};
5885
}
5986

src/NHibernate/Linq/Functions/RoundGenerator.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ public RoundGenerator()
2020
ReflectHelper.GetMethodDefinition(() => Math.Round(default(decimal), default(int))),
2121
ReflectHelper.GetMethodDefinition(() => decimal.Round(default(decimal))),
2222
ReflectHelper.GetMethodDefinition(() => decimal.Round(default(decimal), default(int))),
23+
24+
#if NETCOREAPP2_0
25+
ReflectHelper.GetMethodDefinition(() => MathF.Round(default(float))),
26+
ReflectHelper.GetMethodDefinition(() => MathF.Round(default(float), default(int))),
27+
#endif
2328
};
2429
}
2530

src/NHibernate/Linq/Functions/TruncateGenerator.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ public TruncateGenerator()
1616
{
1717
ReflectHelper.GetMethodDefinition(() => Math.Truncate(default(decimal))),
1818
ReflectHelper.GetMethodDefinition(() => Math.Truncate(default(double))),
19-
ReflectHelper.GetMethodDefinition(() => decimal.Truncate(default(decimal)))
19+
ReflectHelper.GetMethodDefinition(() => decimal.Truncate(default(decimal))),
20+
21+
#if NETCOREAPP2_0
22+
ReflectHelper.GetMethodDefinition(() => MathF.Truncate(default(float))),
23+
#endif
2024
};
2125
}
2226

0 commit comments

Comments
 (0)