|
1 | 1 | using System; |
2 | | -using System.Collections.Generic; |
3 | 2 | using DesktopClock.Utilities; |
4 | 3 |
|
5 | 4 | namespace DesktopClock.Tests; |
6 | 5 |
|
7 | 6 | public class PixelShifterTests |
8 | 7 | { |
9 | 8 | [Theory] |
10 | | - [InlineData(5, 10)] // Evenly divisible. |
11 | | - [InlineData(3, 10)] // Not evenly divisible. |
12 | | - [InlineData(10, 5)] // Amount is larger than total. |
13 | | - public void ShiftX_ShouldNotExceedMaxTotalShift(int shiftAmount, int maxTotalShift) |
| 9 | + [InlineData(50, 0.1, 100, 5)] |
| 10 | + [InlineData(20, 0.5, 4, 4)] |
| 11 | + public void GetEffectiveMaxOffset_ShouldUseWindowSizeRatio(double windowSize, double ratio, int maxOffset, double expected) |
14 | 12 | { |
15 | 13 | var shifter = new PixelShifter |
16 | 14 | { |
17 | | - PixelsPerShift = shiftAmount, |
18 | | - MaxPixelOffset = maxTotalShift, |
| 15 | + MaxPixelOffsetRatio = ratio, |
| 16 | + MaxPixelOffset = maxOffset, |
19 | 17 | }; |
20 | 18 |
|
21 | | - double totalShiftX = 0; |
22 | | - |
23 | | - // Test 100 times because it's random. |
24 | | - for (var i = 0; i < 100; i++) |
25 | | - { |
26 | | - var shift = shifter.ShiftX(); |
27 | | - totalShiftX += shift; |
| 19 | + var effective = shifter.GetEffectiveMaxOffset(windowSize); |
28 | 20 |
|
29 | | - Assert.InRange(Math.Abs(totalShiftX), 0, maxTotalShift); |
30 | | - } |
| 21 | + Assert.Equal(expected, effective); |
31 | 22 | } |
32 | 23 |
|
33 | | - [Theory] |
34 | | - [InlineData(5, 10)] // Evenly divisible. |
35 | | - [InlineData(3, 10)] // Not evenly divisible. |
36 | | - [InlineData(10, 5)] // Amount is larger than total. |
37 | | - public void ShiftY_ShouldNotExceedMaxTotalShift(int shiftAmount, int maxTotalShift) |
| 24 | + [Fact] |
| 25 | + public void ShiftX_ShouldBounceDeterministicallyWithinBounds() |
38 | 26 | { |
39 | 27 | var shifter = new PixelShifter |
40 | 28 | { |
41 | | - PixelsPerShift = shiftAmount, |
42 | | - MaxPixelOffset = maxTotalShift, |
| 29 | + PixelsPerShift = 2, |
| 30 | + MaxPixelOffset = 100, |
| 31 | + MaxPixelOffsetRatio = 0.1, |
43 | 32 | }; |
44 | 33 |
|
45 | | - double totalShiftY = 0; |
| 34 | + const double windowSize = 50; |
| 35 | + Assert.Equal(5d, shifter.GetEffectiveMaxOffset(windowSize)); |
46 | 36 |
|
47 | | - // Test 100 times because it's random. |
48 | | - for (var i = 0; i < 100; i++) |
| 37 | + var expectedShifts = new double[] { 2, 2, 1, -2, -2, -2, -2, -2, 2, 2 }; |
| 38 | + foreach (var expected in expectedShifts) |
49 | 39 | { |
50 | | - var shift = shifter.ShiftY(); |
51 | | - totalShiftY += shift; |
52 | | - |
53 | | - Assert.InRange(Math.Abs(totalShiftY), 0, maxTotalShift); |
| 40 | + var shift = shifter.ShiftX(windowSize); |
| 41 | + Assert.Equal(expected, shift); |
| 42 | + Assert.InRange(Math.Abs(shifter.TotalShiftX), 0, 5); |
54 | 43 | } |
55 | 44 | } |
56 | 45 |
|
57 | 46 | [Fact] |
58 | | - public void ShiftX_WithZeroPixelsPerShift_ShouldReturnZero() |
| 47 | + public void ShiftY_ShouldBounceDeterministicallyWithinBounds() |
59 | 48 | { |
60 | | - // Arrange |
61 | 49 | var shifter = new PixelShifter |
62 | 50 | { |
63 | | - PixelsPerShift = 0, |
64 | | - MaxPixelOffset = 10, |
| 51 | + PixelsPerShift = 2, |
| 52 | + MaxPixelOffset = 100, |
| 53 | + MaxPixelOffsetRatio = 0.1, |
65 | 54 | }; |
66 | 55 |
|
67 | | - // Act |
68 | | - var shift = shifter.ShiftX(); |
69 | | - |
70 | | - // Assert |
71 | | - Assert.Equal(0, shift); |
72 | | - } |
| 56 | + const double windowSize = 50; |
| 57 | + Assert.Equal(5d, shifter.GetEffectiveMaxOffset(windowSize)); |
73 | 58 |
|
74 | | - [Fact] |
75 | | - public void ShiftY_WithZeroPixelsPerShift_ShouldReturnZero() |
76 | | - { |
77 | | - // Arrange |
78 | | - var shifter = new PixelShifter |
79 | | - { |
80 | | - PixelsPerShift = 0, |
81 | | - MaxPixelOffset = 10, |
82 | | - }; |
83 | | - |
84 | | - // Act |
85 | | - var shift = shifter.ShiftY(); |
86 | | - |
87 | | - // Assert |
88 | | - Assert.Equal(0, shift); |
89 | | - } |
90 | | - |
91 | | - [Fact] |
92 | | - public void ShiftX_WithZeroMaxOffset_ShouldReturnZero() |
93 | | - { |
94 | | - // Arrange |
95 | | - var shifter = new PixelShifter |
| 59 | + var expectedShifts = new double[] { 2, 2, 1, -2, -2, -2, -2, -2, 2, 2 }; |
| 60 | + foreach (var expected in expectedShifts) |
96 | 61 | { |
97 | | - PixelsPerShift = 5, |
98 | | - MaxPixelOffset = 0, |
99 | | - }; |
100 | | - |
101 | | - // Act |
102 | | - var shift = shifter.ShiftX(); |
103 | | - |
104 | | - // Assert |
105 | | - Assert.Equal(0, shift); |
| 62 | + var shift = shifter.ShiftY(windowSize); |
| 63 | + Assert.Equal(expected, shift); |
| 64 | + Assert.InRange(Math.Abs(shifter.TotalShiftY), 0, 5); |
| 65 | + } |
106 | 66 | } |
107 | 67 |
|
108 | | - [Fact] |
109 | | - public void ShiftY_WithZeroMaxOffset_ShouldReturnZero() |
| 68 | + [Theory] |
| 69 | + [InlineData(0, 50)] |
| 70 | + [InlineData(2, 0)] |
| 71 | + public void ShiftX_WhenDisabled_ReturnsZero(int pixelsPerShift, double windowSize) |
110 | 72 | { |
111 | | - // Arrange |
112 | 73 | var shifter = new PixelShifter |
113 | 74 | { |
114 | | - PixelsPerShift = 5, |
115 | | - MaxPixelOffset = 0, |
| 75 | + PixelsPerShift = pixelsPerShift, |
| 76 | + MaxPixelOffset = 10, |
| 77 | + MaxPixelOffsetRatio = 0.1, |
116 | 78 | }; |
117 | 79 |
|
118 | | - // Act |
119 | | - var shift = shifter.ShiftY(); |
| 80 | + var shift = shifter.ShiftX(windowSize); |
120 | 81 |
|
121 | | - // Assert |
122 | 82 | Assert.Equal(0, shift); |
| 83 | + Assert.Equal(0, shifter.TotalShiftX); |
123 | 84 | } |
124 | 85 |
|
125 | 86 | [Fact] |
126 | 87 | public void DefaultValues_ShouldBeExpected() |
127 | 88 | { |
128 | | - // Arrange |
129 | 89 | var shifter = new PixelShifter(); |
130 | 90 |
|
131 | | - // Assert |
132 | 91 | Assert.Equal(1, shifter.PixelsPerShift); |
133 | 92 | Assert.Equal(4, shifter.MaxPixelOffset); |
134 | | - } |
135 | | - |
136 | | - [Fact] |
137 | | - public void ShiftX_ShouldReverseDirectionAtBoundary() |
138 | | - { |
139 | | - // Arrange - set up to hit boundary quickly |
140 | | - var shifter = new PixelShifter |
141 | | - { |
142 | | - PixelsPerShift = 10, |
143 | | - MaxPixelOffset = 10, |
144 | | - }; |
145 | | - |
146 | | - // Act - call multiple times to force direction reversal |
147 | | - double total = 0; |
148 | | - var shifts = new List<double>(); |
149 | | - for (int i = 0; i < 10; i++) |
150 | | - { |
151 | | - var shift = shifter.ShiftX(); |
152 | | - shifts.Add(shift); |
153 | | - total += shift; |
154 | | - } |
155 | | - |
156 | | - // Assert - total should stay within bounds |
157 | | - Assert.InRange(Math.Abs(total), 0, 10); |
158 | | - |
159 | | - // There should be both positive and negative shifts (direction reversal) |
160 | | - // OR the total stayed within bounds |
161 | | - Assert.True(Math.Abs(total) <= 10); |
162 | | - } |
163 | | - |
164 | | - [Fact] |
165 | | - public void ShiftY_ShouldReverseDirectionAtBoundary() |
166 | | - { |
167 | | - // Arrange - set up to hit boundary quickly |
168 | | - var shifter = new PixelShifter |
169 | | - { |
170 | | - PixelsPerShift = 10, |
171 | | - MaxPixelOffset = 10, |
172 | | - }; |
173 | | - |
174 | | - // Act - call multiple times to force direction reversal |
175 | | - double total = 0; |
176 | | - var shifts = new List<double>(); |
177 | | - for (int i = 0; i < 10; i++) |
178 | | - { |
179 | | - var shift = shifter.ShiftY(); |
180 | | - shifts.Add(shift); |
181 | | - total += shift; |
182 | | - } |
183 | | - |
184 | | - // Assert - total should stay within bounds |
185 | | - Assert.InRange(Math.Abs(total), 0, 10); |
186 | | - } |
187 | | - |
188 | | - [Fact] |
189 | | - public void ShiftX_And_ShiftY_ShouldBeIndependent() |
190 | | - { |
191 | | - // Arrange |
192 | | - var shifter = new PixelShifter |
193 | | - { |
194 | | - PixelsPerShift = 5, |
195 | | - MaxPixelOffset = 20, |
196 | | - }; |
197 | | - |
198 | | - // Act |
199 | | - double totalX = 0, totalY = 0; |
200 | | - for (int i = 0; i < 50; i++) |
201 | | - { |
202 | | - totalX += shifter.ShiftX(); |
203 | | - totalY += shifter.ShiftY(); |
204 | | - } |
205 | | - |
206 | | - // Assert - both should be within bounds independently |
207 | | - Assert.InRange(Math.Abs(totalX), 0, 20); |
208 | | - Assert.InRange(Math.Abs(totalY), 0, 20); |
209 | | - } |
210 | | - |
211 | | - [Fact] |
212 | | - public void ShiftX_MultipleShiftersWithSameConfig_ShouldBeIndependent() |
213 | | - { |
214 | | - // Arrange |
215 | | - var shifter1 = new PixelShifter { PixelsPerShift = 5, MaxPixelOffset = 10 }; |
216 | | - var shifter2 = new PixelShifter { PixelsPerShift = 5, MaxPixelOffset = 10 }; |
217 | | - |
218 | | - // Act |
219 | | - double total1 = 0, total2 = 0; |
220 | | - for (int i = 0; i < 20; i++) |
221 | | - { |
222 | | - total1 += shifter1.ShiftX(); |
223 | | - total2 += shifter2.ShiftX(); |
224 | | - } |
225 | | - |
226 | | - // Assert - both should be within their own bounds |
227 | | - Assert.InRange(Math.Abs(total1), 0, 10); |
228 | | - Assert.InRange(Math.Abs(total2), 0, 10); |
229 | | - } |
230 | | - |
231 | | - [Theory] |
232 | | - [InlineData(1, 5)] |
233 | | - [InlineData(2, 8)] |
234 | | - [InlineData(3, 15)] |
235 | | - public void Shift_ShouldReturnValueWithinPixelsPerShiftRange(int pixelsPerShift, int maxOffset) |
236 | | - { |
237 | | - // Arrange |
238 | | - var shifter = new PixelShifter |
239 | | - { |
240 | | - PixelsPerShift = pixelsPerShift, |
241 | | - MaxPixelOffset = maxOffset, |
242 | | - }; |
243 | | - |
244 | | - // Act & Assert |
245 | | - for (int i = 0; i < 50; i++) |
246 | | - { |
247 | | - var shiftX = shifter.ShiftX(); |
248 | | - var shiftY = shifter.ShiftY(); |
249 | | - |
250 | | - // Shift should be within the range [-pixelsPerShift, +pixelsPerShift] |
251 | | - Assert.InRange(shiftX, -pixelsPerShift, pixelsPerShift); |
252 | | - Assert.InRange(shiftY, -pixelsPerShift, pixelsPerShift); |
253 | | - } |
| 93 | + Assert.Equal(0.1, shifter.MaxPixelOffsetRatio, 5); |
254 | 94 | } |
255 | 95 | } |
0 commit comments