Skip to content

Commit d3150db

Browse files
committed
overflow handling and new tests
1 parent f42b543 commit d3150db

3 files changed

Lines changed: 62 additions & 5 deletions

File tree

Clocks.Tests/ClockIn24HTests.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,5 +149,39 @@ public void AddSecondAddsSecond(int seconds, int expectedHour, int expectedMinut
149149
clock.Minute.Should().Be(expectedMinute);
150150
clock.Second.Should().Be(expectedSecond);
151151
}
152+
153+
[Fact]
154+
public void AddMinuteRollsHour()
155+
{
156+
var clock = new ClockIn24H(1,30, 0);
157+
clock.Hour.Should().Be(1);
158+
clock.Minute.Should().Be(30);
159+
clock.Second.Should().Be(0);
160+
clock.Invariant.Should().BeTrue();
161+
162+
clock.AddMinutes(30);
163+
164+
clock.Invariant.Should().BeTrue();
165+
clock.Hour.Should().Be(2);
166+
clock.Minute.Should().Be(0);
167+
clock.Second.Should().Be(0);
168+
}
169+
170+
[Fact]
171+
public void AddSecondsRollsMinuteAndHour()
172+
{
173+
var clock = new ClockIn24H(0,59, 59);
174+
clock.Hour.Should().Be(0);
175+
clock.Minute.Should().Be(59);
176+
clock.Second.Should().Be(59);
177+
clock.Invariant.Should().BeTrue();
178+
179+
clock.AddSeconds(61);
180+
181+
clock.Invariant.Should().BeTrue();
182+
clock.Hour.Should().Be(1);
183+
clock.Minute.Should().Be(1);
184+
clock.Second.Should().Be(0);
185+
}
152186
}
153187
}

Clocks.Tests/StopwatchTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,24 @@ public void AddSecondAddsSecond(int seconds, int expectedHour, int expectedMinut
154154
stopwatch.Second.Should().Be(expectedSecond);
155155
}
156156

157+
[Fact]
158+
public void AddSecondAddsSecond2()
159+
{
160+
var stopwatch = new Stopwatch(23, 59, 59);
161+
162+
stopwatch.Hour.Should().Be(23);
163+
stopwatch.Minute.Should().Be(59);
164+
stopwatch.Second.Should().Be(59);
165+
stopwatch.Invariant.Should().BeTrue();
166+
167+
stopwatch.AddSeconds(1);
168+
169+
stopwatch.Invariant.Should().BeTrue();
170+
stopwatch.Hour.Should().Be(24);
171+
stopwatch.Minute.Should().Be(0);
172+
stopwatch.Second.Should().Be(0);
173+
}
174+
157175
[Fact]
158176
public void AddHoursBreaksInvariantWithMaxValueOverflow()
159177
{

Clocks/BaseClock.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,20 @@ public void AddHours(int hour)
2828

2929
public void AddMinutes(int minute)
3030
{
31-
Hour += minute / 60;
32-
Minute += minute % 60;
31+
var newMinute = Minute + minute;
32+
33+
Hour += newMinute / 60;
34+
Minute = newMinute % 60;
3335
}
3436

3537
public void AddSeconds(int second)
3638
{
37-
Hour += second / 3600;
38-
Minute += second % 3600 / 60;
39-
Second += second % 3600 % 60;
39+
var newSecond = Second + second;
40+
var newMinute = Minute + newSecond % 3600 / 60;
41+
42+
Hour += newMinute / 60 + newSecond / 3600 % 60;
43+
Minute = newMinute % 60;
44+
Second = newSecond % 3600 % 60;
4045
}
4146
}
4247
}

0 commit comments

Comments
 (0)