-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathExpressionExtensionsTests.cs
More file actions
81 lines (64 loc) · 2.33 KB
/
ExpressionExtensionsTests.cs
File metadata and controls
81 lines (64 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
using System.Linq.Expressions;
using MathCore.Extensions.Expressions;
namespace MathCore.Tests;
[TestClass]
public class ExpressionExtensionsTests
{
public TestContext TestContext { get; set; }
#region Additional test attributes
//
//You can use the following additional attributes as you write your tests:
//
//Use ClassInitialize to run code before running the first test in the class
//[ClassInitialize()]
//public static void MyClassInitialize(TestContext testContext)
//{
//}
//
//Use ClassCleanup to run code after all tests in a class have run
//[ClassCleanup()]
//public static void MyClassCleanup()
//{
//}
//
//Use TestInitialize to run code before running each test
//[TestInitialize()]
//public void MyTestInitialize()
//{
//}
//
//Use TestCleanup to run code after each test has run
//[TestCleanup()]
//public void MyTestCleanup()
//{
//}
//
#endregion
[TestMethod]
public void TestSimpleFunction()
{
Expression<Func<double, double>> original_expression = t => t * t * t + 20;
Expression<Func<double, double>> substitute_expression = t => t * t;
var modified_expression = original_expression.Substitute("t", substitute_expression);
var modified_function = (Func<double, double>)modified_expression.Compile();
Assert.AreEqual(21, (int)modified_function(1));
Assert.AreEqual(2 * 2 * 2 * 2 * 2 * 2 + 20, (int)modified_function(2));
}
[TestMethod]
public void TestComplexPolynomial()
{
Expression<Func<double, double>> original_expression =
t => (1 - t) * (1 - t) * (1 - t) * -50 +
3 * (1 - t) * (1 - t) * t * -25 +
3 * (1 - t) * t * t * 25 +
t * t * t * 50;
Expression<Func<double, double, double, double>> substitute_expression = (t, factor, shift) => t * factor + shift;
var modified_expression = original_expression.Substitute("t", substitute_expression);
var modified_function = (Func<double, double, double, double>)modified_expression.Compile();
var result = modified_function(1, 10, -9);
Assert.AreEqual(50, result);
Console.WriteLine(original_expression);
Console.WriteLine();
Console.WriteLine(modified_expression);
}
}