Skip to content

Commit 4e9b90a

Browse files
committed
[Min] Fix Min on IStructEnumerable.
1 parent 2c1aa44 commit 4e9b90a

File tree

3 files changed

+92
-53
lines changed

3 files changed

+92
-53
lines changed

src/StructLinq.Tests/MinTests.cs

Lines changed: 20 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,53 +7,32 @@ namespace StructLinq.Tests
77
{
88
public class MinTests
99
{
10-
[Fact]
11-
public void MinTest()
10+
[Theory]
11+
[InlineData(int.MinValue, 10)]
12+
[InlineData(-10, 10)]
13+
[InlineData(0, 10)]
14+
[InlineData(10, 10)]
15+
public void MinTest(int start, int count)
1216
{
13-
var count = 100;
14-
var mult = -2.0;
15-
var structMin = StructEnumerable
16-
.Range(0, count)
17-
.Select(x => x * mult, x=> (IStructEnumerable<int, RangeEnumerator>) x)
18-
.Min();
19-
Assert.Equal((count - 1) * mult, structMin);
20-
}
17+
var array = Enumerable.Range(start, count).ToArray();
2118

22-
[Fact]
23-
public void MinTest2()
24-
{
25-
var count = 100;
26-
var mult = -2.0;
27-
var structMin = StructEnumerable
28-
.Range(0, count)
29-
.Select(x => x * mult, x => (IStructEnumerable<int, RangeEnumerator>)x)
30-
.Min(x=>x);
31-
Assert.Equal((count - 1) * mult, structMin);
19+
var structMin = array.ToStructEnumerable().Min();
20+
var expected = array.Min();
21+
Assert.Equal(expected, structMin);
3222
}
3323

34-
35-
[Fact]
36-
public void MinTestOnCollection()
24+
[Theory]
25+
[InlineData(int.MinValue, 10)]
26+
[InlineData(-10, 10)]
27+
[InlineData(0, 10)]
28+
[InlineData(10, 10)]
29+
public void MinTestOnWhre(int start, int count)
3730
{
38-
var count = 100;
39-
var mult = -2.0;
40-
var structMin = StructEnumerable
41-
.Range(0, count)
42-
.Select(x => x * mult, x=>x)
43-
.Min();
44-
Assert.Equal((count - 1) * mult, structMin);
45-
}
31+
var array = Enumerable.Range(start, count).ToArray();
4632

47-
[Fact]
48-
public void MinTestOnCollection2()
49-
{
50-
var count = 100;
51-
var mult = -2.0;
52-
var structMin = StructEnumerable
53-
.Range(0, count)
54-
.Select(x => x * mult, x => x)
55-
.Min(x=>x);
56-
Assert.Equal((count - 1) * mult, structMin);
33+
var structMin = array.ToStructEnumerable().Where(x=> true).Min();
34+
var expected = array.Min();
35+
Assert.Equal(expected, structMin);
5736
}
5837

5938

src/StructLinq/Min/MinStructEnumerable.cs

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

src/StructLinq/Min/MinStructEnumerable.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-
HasMin = true;
47+
if (!HasMin)
48+
{
49+
Min = input;
50+
HasMin = true;
51+
return true;
52+
}
4853
if (Min > input)
4954
Min = input;
5055
return true;

0 commit comments

Comments
 (0)