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 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
Original file line number Diff line number Diff 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 {
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