Skip to content

Commit 2a7ec0f

Browse files
committed
[Max] Fix Max on IStructEnumerable.
1 parent 4e9b90a commit 2a7ec0f

File tree

3 files changed

+101
-55
lines changed

3 files changed

+101
-55
lines changed

src/StructLinq.Tests/MaxTests.cs

Lines changed: 29 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -7,61 +7,47 @@ namespace StructLinq.Tests
77
{
88
public class MaxTests
99
{
10-
[Fact]
11-
public void MaxTest()
10+
[Theory]
11+
[InlineData(int.MaxValue, 10)]
12+
[InlineData(-10, 10)]
13+
[InlineData(0, 10)]
14+
[InlineData(10, 10)]
15+
public void MaxTest(int end, int count)
1216
{
13-
var count = 100;
14-
var mult = 2.0;
15-
var structMax = StructEnumerable
16-
.Range(0, count)
17-
.Select(x=> x * mult, x=> (IStructEnumerable<int, RangeEnumerator>) x)
18-
.Max();
19-
Assert.Equal((count-1) * mult, structMax);
17+
var array = Enumerable.Range(end - count, count).ToArray();
18+
19+
var max = array.ToStructEnumerable().Max();
20+
21+
var expected = array.Max();
22+
23+
Assert.Equal(expected, max);
2024
}
2125

22-
[Fact]
23-
public void MaxTest2()
26+
[Theory]
27+
[InlineData(int.MaxValue, 10)]
28+
[InlineData(-10, 10)]
29+
[InlineData(0, 10)]
30+
[InlineData(10, 10)]
31+
public void MaxTestOnWhere(int end, int count)
2432
{
25-
var count = 100;
26-
var mult = 2.0;
27-
var structMax = StructEnumerable
28-
.Range(0, count)
29-
.Select(x=> x * mult, x=> (IStructEnumerable<int, RangeEnumerator>)x)
30-
.Max(x=>x);
31-
Assert.Equal((count-1) * mult, structMax);
33+
var array = Enumerable.Range(end - count, count).ToArray();
34+
35+
var max = array.ToStructEnumerable().Where(x=> true).Max();
36+
37+
var expected = array.Max();
38+
39+
Assert.Equal(expected, max);
3240
}
33-
41+
42+
3443
[Fact]
3544
public void ErrorTest()
3645
{
3746
var structEnum = Enumerable.Empty<double>().ToStructEnumerable();
3847
Assert.Throws<ArgumentOutOfRangeException>(() => structEnum.Max());
3948
}
4049

41-
[Fact]
42-
public void MaxTestOnCollection()
43-
{
44-
var count = 100;
45-
var mult = 2.0;
46-
var structMax = StructEnumerable
47-
.Range(0, count)
48-
.Select(x=> x * mult, x=> x)
49-
.Max();
50-
Assert.Equal((count-1) * mult, structMax);
51-
}
52-
53-
[Fact]
54-
public void MaxTestOnCollection2()
55-
{
56-
var count = 100;
57-
var mult = 2.0;
58-
var structMax = StructEnumerable
59-
.Range(0, count)
60-
.Select(x=> x * mult, x=>x)
61-
.Max(x=>x);
62-
Assert.Equal((count-1) * mult, structMax);
63-
}
64-
50+
6551
[Fact]
6652
public void ErrorTestOnCollection()
6753
{

src/StructLinq/Max/MaxStructEnumerable.cs

Lines changed: 66 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@ public MaxInt16Visitor(bool hasMax, Int16 max)
2222
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2323
public bool Visit(Int16 input)
2424
{
25-
HasMax = true;
25+
if (!HasMax)
26+
{
27+
Max = input;
28+
HasMax = true;
29+
return true;
30+
}
2631
if (Max < input)
2732
Max = input;
2833
return true;
@@ -69,7 +74,12 @@ public MaxInt32Visitor(bool hasMax, Int32 max)
6974
[MethodImpl(MethodImplOptions.AggressiveInlining)]
7075
public bool Visit(Int32 input)
7176
{
72-
HasMax = true;
77+
if (!HasMax)
78+
{
79+
Max = input;
80+
HasMax = true;
81+
return true;
82+
}
7383
if (Max < input)
7484
Max = input;
7585
return true;
@@ -116,7 +126,12 @@ public MaxInt64Visitor(bool hasMax, Int64 max)
116126
[MethodImpl(MethodImplOptions.AggressiveInlining)]
117127
public bool Visit(Int64 input)
118128
{
119-
HasMax = true;
129+
if (!HasMax)
130+
{
131+
Max = input;
132+
HasMax = true;
133+
return true;
134+
}
120135
if (Max < input)
121136
Max = input;
122137
return true;
@@ -163,7 +178,12 @@ public MaxUInt16Visitor(bool hasMax, UInt16 max)
163178
[MethodImpl(MethodImplOptions.AggressiveInlining)]
164179
public bool Visit(UInt16 input)
165180
{
166-
HasMax = true;
181+
if (!HasMax)
182+
{
183+
Max = input;
184+
HasMax = true;
185+
return true;
186+
}
167187
if (Max < input)
168188
Max = input;
169189
return true;
@@ -210,7 +230,12 @@ public MaxUInt32Visitor(bool hasMax, UInt32 max)
210230
[MethodImpl(MethodImplOptions.AggressiveInlining)]
211231
public bool Visit(UInt32 input)
212232
{
213-
HasMax = true;
233+
if (!HasMax)
234+
{
235+
Max = input;
236+
HasMax = true;
237+
return true;
238+
}
214239
if (Max < input)
215240
Max = input;
216241
return true;
@@ -257,7 +282,12 @@ public MaxUInt64Visitor(bool hasMax, UInt64 max)
257282
[MethodImpl(MethodImplOptions.AggressiveInlining)]
258283
public bool Visit(UInt64 input)
259284
{
260-
HasMax = true;
285+
if (!HasMax)
286+
{
287+
Max = input;
288+
HasMax = true;
289+
return true;
290+
}
261291
if (Max < input)
262292
Max = input;
263293
return true;
@@ -304,7 +334,12 @@ public MaxSingleVisitor(bool hasMax, Single max)
304334
[MethodImpl(MethodImplOptions.AggressiveInlining)]
305335
public bool Visit(Single input)
306336
{
307-
HasMax = true;
337+
if (!HasMax)
338+
{
339+
Max = input;
340+
HasMax = true;
341+
return true;
342+
}
308343
if (Max < input)
309344
Max = input;
310345
return true;
@@ -351,7 +386,12 @@ public MaxDoubleVisitor(bool hasMax, Double max)
351386
[MethodImpl(MethodImplOptions.AggressiveInlining)]
352387
public bool Visit(Double input)
353388
{
354-
HasMax = true;
389+
if (!HasMax)
390+
{
391+
Max = input;
392+
HasMax = true;
393+
return true;
394+
}
355395
if (Max < input)
356396
Max = input;
357397
return true;
@@ -398,7 +438,12 @@ public MaxByteVisitor(bool hasMax, Byte max)
398438
[MethodImpl(MethodImplOptions.AggressiveInlining)]
399439
public bool Visit(Byte input)
400440
{
401-
HasMax = true;
441+
if (!HasMax)
442+
{
443+
Max = input;
444+
HasMax = true;
445+
return true;
446+
}
402447
if (Max < input)
403448
Max = input;
404449
return true;
@@ -445,7 +490,12 @@ public MaxSByteVisitor(bool hasMax, SByte max)
445490
[MethodImpl(MethodImplOptions.AggressiveInlining)]
446491
public bool Visit(SByte input)
447492
{
448-
HasMax = true;
493+
if (!HasMax)
494+
{
495+
Max = input;
496+
HasMax = true;
497+
return true;
498+
}
449499
if (Max < input)
450500
Max = input;
451501
return true;
@@ -492,7 +542,12 @@ public MaxDateTimeVisitor(bool hasMax, DateTime max)
492542
[MethodImpl(MethodImplOptions.AggressiveInlining)]
493543
public bool Visit(DateTime input)
494544
{
495-
HasMax = true;
545+
if (!HasMax)
546+
{
547+
Max = input;
548+
HasMax = true;
549+
return true;
550+
}
496551
if (Max < input)
497552
Max = input;
498553
return true;

src/StructLinq/Max/MaxStructEnumerable.tt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,12 @@ namespace StructLinq
4444
[MethodImpl(MethodImplOptions.AggressiveInlining)]
4545
public bool Visit(<#= t.Name #> input)
4646
{
47-
HasMax = true;
47+
if (!HasMax)
48+
{
49+
Max = input;
50+
HasMax = true;
51+
return true;
52+
}
4853
if (Max < input)
4954
Max = input;
5055
return true;

0 commit comments

Comments
 (0)