Skip to content

Commit

Permalink
Fix time control bug
Browse files Browse the repository at this point in the history
  • Loading branch information
dykstrom committed Aug 24, 2024
1 parent 4308fae commit b35a4d1
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public record IncrementalTimeControl(int minutes, int seconds, int increment) im
public IncrementalTimeControl {
ensure(minutes >= 0, "minutes must be >= 0");
ensure(seconds >= 0 && seconds < 60, "seconds must be in [0, 59]");
ensure(increment > 0, "increment must be > 0");
ensure(increment >= 0, "increment must be >= 0");
}

public IncrementalTimeControl(final int seconds, final int increment) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public static TimeControl parse(final String s) throws ParseException {
final var seconds = Integer.parseInt(classicMatcher.group(2));
return new ClassicTimeControl(moves, seconds);
} else if (incrementalMatcher.matches()) {
final var seconds = Integer.parseInt(classicMatcher.group(1));
final var increment = Integer.parseInt(classicMatcher.group(2));
final var seconds = Integer.parseInt(incrementalMatcher.group(1));
final var increment = Integer.parseInt(incrementalMatcher.group(2));
return new IncrementalTimeControl(seconds, increment);
} else {
throw new ParseException(s, 0);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2024 Johan Dykström
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package se.dykstrom.cet.engine.time;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

class TimeControlFormatTest {

@Test
void shouldParseClassicTimeControl() throws Exception {
assertEquals(new ClassicTimeControl(40,20), TimeControlFormat.parse("40/20"));
assertEquals(new ClassicTimeControl(100,1, 2), TimeControlFormat.parse("100/62"));
}

@Test
void shouldParseIncrementalTimeControl() throws Exception {
assertEquals(new IncrementalTimeControl(100,3), TimeControlFormat.parse("100+3"));
assertEquals(new IncrementalTimeControl(15,0), TimeControlFormat.parse("15+0"));
}
}

0 comments on commit b35a4d1

Please sign in to comment.