Skip to content

Commit 7f96b8e

Browse files
committed
Added test for SkipGate and improved documentation.
1 parent 6fa7ef8 commit 7f96b8e

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

src/Whathecode.System/Algorithm/AbstractGate.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
/// </summary>
66
public abstract class AbstractGate
77
{
8+
/// <summary>
9+
/// Automatically reset the gate after entering.
10+
/// </summary>
811
protected bool AutoReset { get; private set; }
912

1013

src/Whathecode.System/Algorithm/SkipGate.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ public class SkipGate : AbstractGate
99
int _curCount;
1010

1111

12+
/// <summary>
13+
/// Create a new gate which skips a specified amount of entries before opening.
14+
/// </summary>
15+
/// <param name = "skipCount">The amount of times entry is denied before opening.</param>
16+
/// <param name = "autoReset">Whether the gate is reset after entry is successful.</param>
1217
public SkipGate( int skipCount, bool autoReset = false )
1318
: base( autoReset )
1419
{
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using Whathecode.System.Algorithm;
2+
using Xunit;
3+
4+
5+
namespace Whathecode.Tests.System.Algorithm
6+
{
7+
public class SkipGateTest
8+
{
9+
[Fact]
10+
public void OpenOnThird()
11+
{
12+
SkipGate openOnThird = new SkipGate( 2 );
13+
for ( int i = 1; i < 5; ++i )
14+
{
15+
if ( openOnThird.TryEnter() )
16+
{
17+
Assert.True( i >= 3 );
18+
}
19+
}
20+
}
21+
22+
[Fact]
23+
public void OpenEveryFive()
24+
{
25+
SkipGate openOnPluralFive = new SkipGate( 4, true );
26+
int timesOpened = 0;
27+
for ( int i = 1; i <= 20; ++i )
28+
{
29+
if ( openOnPluralFive.TryEnter() )
30+
{
31+
Assert.True( i % 5 == 0 );
32+
++timesOpened;
33+
}
34+
}
35+
36+
Assert.Equal( 4, timesOpened );
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)