File tree Expand file tree Collapse file tree 3 files changed +47
-0
lines changed
src/Whathecode.System/Algorithm
test/Whathecode.System.Tests/Algorithm Expand file tree Collapse file tree 3 files changed +47
-0
lines changed Original file line number Diff line number Diff line change 5
5
/// </summary>
6
6
public abstract class AbstractGate
7
7
{
8
+ /// <summary>
9
+ /// Automatically reset the gate after entering.
10
+ /// </summary>
8
11
protected bool AutoReset { get ; private set ; }
9
12
10
13
Original file line number Diff line number Diff line change @@ -9,6 +9,11 @@ public class SkipGate : AbstractGate
9
9
int _curCount ;
10
10
11
11
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>
12
17
public SkipGate ( int skipCount , bool autoReset = false )
13
18
: base ( autoReset )
14
19
{
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments