-
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathFixedCurveTests.cs
More file actions
167 lines (139 loc) · 6.22 KB
/
FixedCurveTests.cs
File metadata and controls
167 lines (139 loc) · 6.22 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
using MemoryPack;
using System.Text.Json;
using System.Text.Json.Serialization;
using Xunit;
namespace FixedMathSharp.Tests
{
public class FixedCurveTests
{
[Fact]
public void Evaluate_LinearInterpolation_ShouldInterpolateCorrectly()
{
FixedCurve curve = new FixedCurve(FixedCurveMode.Linear,
new FixedCurveKey(0, 0),
new FixedCurveKey(10, 100));
Assert.Equal(Fixed64.Zero, curve.Evaluate(Fixed64.Zero));
Assert.Equal((Fixed64)50, curve.Evaluate((Fixed64)5)); // Midpoint
Assert.Equal((Fixed64)100, curve.Evaluate((Fixed64)10));
}
[Fact]
public void Evaluate_StepInterpolation_ShouldJumpToNearestKeyframe()
{
FixedCurve curve = new FixedCurve(FixedCurveMode.Step,
new FixedCurveKey(0, 10),
new FixedCurveKey(5, 50),
new FixedCurveKey(10, 100));
Assert.Equal((Fixed64)10, curve.Evaluate(Fixed64.Zero));
Assert.Equal((Fixed64)10, curve.Evaluate((Fixed64)4.99)); // Should not interpolate
Assert.Equal((Fixed64)50, curve.Evaluate((Fixed64)5));
Assert.Equal((Fixed64)50, curve.Evaluate((Fixed64)9.99));
Assert.Equal((Fixed64)100, curve.Evaluate((Fixed64)10));
}
[Fact]
public void Evaluate_SmoothInterpolation_ShouldSmoothlyTransition()
{
FixedCurve curve = new FixedCurve(FixedCurveMode.Smooth,
new FixedCurveKey(0, 0),
new FixedCurveKey(10, 100));
Fixed64 result = curve.Evaluate((Fixed64)5);
Assert.True(result > (Fixed64)45 && result < (Fixed64)55, $"Unexpected smooth interpolation result: {result}");
}
[Fact]
public void Evaluate_CubicInterpolation_ShouldUseTangentsCorrectly()
{
FixedCurve curve = new FixedCurve(FixedCurveMode.Cubic,
new FixedCurveKey(0, 0, 10, 10),
new FixedCurveKey(10, 100, -10, -10));
Fixed64 result = curve.Evaluate((Fixed64)5);
Assert.True(result > (Fixed64)45 && result < (Fixed64)55, $"Unexpected cubic interpolation result: {result}");
}
[Fact]
public void Evaluate_TimeBeforeFirstKeyframe_ShouldReturnFirstValue()
{
FixedCurve curve = new FixedCurve(FixedCurveMode.Linear,
new FixedCurveKey(5, 50),
new FixedCurveKey(10, 100));
Assert.Equal((Fixed64)50, curve.Evaluate(Fixed64.Zero));
}
[Fact]
public void Evaluate_TimeAfterLastKeyframe_ShouldReturnLastValue()
{
FixedCurve curve = new FixedCurve(FixedCurveMode.Linear,
new FixedCurveKey(5, 50),
new FixedCurveKey(10, 100));
Assert.Equal((Fixed64)100, curve.Evaluate((Fixed64)15));
}
[Fact]
public void Evaluate_SingleKeyframe_ShouldAlwaysReturnSameValue()
{
FixedCurve curve = new FixedCurve(FixedCurveMode.Linear,
new FixedCurveKey(5, 50));
Assert.Equal((Fixed64)50, curve.Evaluate(Fixed64.Zero));
Assert.Equal((Fixed64)50, curve.Evaluate((Fixed64)10));
}
[Fact]
public void Evaluate_DuplicateKeyframes_ShouldHandleGracefully()
{
FixedCurve curve = new FixedCurve(FixedCurveMode.Linear,
new FixedCurveKey(5, 50),
new FixedCurveKey(5, 50), // Duplicate
new FixedCurveKey(10, 100));
Assert.Equal((Fixed64)50, curve.Evaluate((Fixed64)5));
Assert.Equal((Fixed64)100, curve.Evaluate((Fixed64)10));
}
[Fact]
public void Evaluate_NegativeValues_ShouldInterpolateCorrectly()
{
FixedCurve curve = new FixedCurve(FixedCurveMode.Linear,
new FixedCurveKey(-10, -100),
new FixedCurveKey(0, 0),
new FixedCurveKey(10, 100));
Assert.Equal((Fixed64)(-100), curve.Evaluate(-(Fixed64)10));
Assert.Equal((Fixed64)(0), curve.Evaluate(Fixed64.Zero));
Assert.Equal((Fixed64)(100), curve.Evaluate((Fixed64)10));
}
[Fact]
public void Evaluate_ExtremeValues_ShouldHandleCorrectly()
{
FixedCurve curve = new FixedCurve(FixedCurveMode.Linear,
new FixedCurveKey(Fixed64.MIN_VALUE, -(Fixed64)10000),
new FixedCurveKey(Fixed64.MAX_VALUE, (Fixed64)10000));
Assert.Equal((Fixed64)(-10000), curve.Evaluate(Fixed64.MIN_VALUE));
Assert.Equal((Fixed64)(10000), curve.Evaluate(Fixed64.MAX_VALUE));
}
#region Test: Serialization
[Fact]
public void FixedCurve_NetSerialization_RoundTripMaintainsData()
{
var originalCurve = new FixedCurve(
new FixedCurveKey(-10, -100),
new FixedCurveKey(0, 0),
new FixedCurveKey(10, 100));
var jsonOptions = new JsonSerializerOptions
{
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
ReferenceHandler = ReferenceHandler.IgnoreCycles,
IncludeFields = true,
IgnoreReadOnlyProperties = true
};
var json = JsonSerializer.SerializeToUtf8Bytes(originalCurve, jsonOptions);
var deserializedCurve = JsonSerializer.Deserialize<FixedCurve>(json, jsonOptions);
Assert.NotNull(deserializedCurve);
// Check that deserialized values match the original
Assert.Equal(originalCurve, deserializedCurve);
}
[Fact]
public void FixedCurve_MemoryPackSerialization_RoundTripMaintainsData()
{
FixedCurve originalValue = new FixedCurve(
new FixedCurveKey(-10, -100),
new FixedCurveKey(0, 0),
new FixedCurveKey(10, 100));
byte[] bytes = MemoryPackSerializer.Serialize(originalValue);
FixedCurve deserializedValue = MemoryPackSerializer.Deserialize<FixedCurve>(bytes)!;
// Check that deserialized values match the original
Assert.Equal(originalValue, deserializedValue);
}
#endregion
}
}