🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of KiligWY

KiligWY's solution

to Clock in the Objective-C Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

Implement a clock that handles times without dates.

You should be able to add and subtract minutes to it.

Two clocks that represent the same time should be equal to each other.

Setup

There are two different methods of getting set up to run the tests with Objective-C:

  • Create an Xcode project with a test target which will run the tests.
  • Use the ruby gem objc as a test runner utility.

Both are described in more detail here: http://exercism.io/languages/objective-c

Submitting Exercises

When submitting an exercise, make sure your solution file is in the same directory as the test code.

The submit command will look something like:

exercism submit <path-to-exercism-workspace>/objective-c/clock/Clock.m

You can find the Exercism workspace by running exercism debug and looking for the line beginning with Workspace.

Source

Pairing session with Erin Drummond https://twitter.com/ebdrummond

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

ClockTest.m

#import <XCTest/XCTest.h>

#if __has_include("ClockExample.h")
# import "ClockExample.h"
# else
# import "Clock.h"
#endif

@interface ClockTest : XCTestCase

@end

@implementation ClockTest //!OCLint

#pragma mark - Create: Test creating a new clock with an initial time.

- (void)testOnTheHour {
    XCTAssertEqualObjects(@"08:00", [[Clock clockWithHours:8] description]);
}

- (void)testPastTheHour {
    XCTAssertEqualObjects(@"11:09", [[Clock clockWithHours:11 minutes:9] description]);
}

- (void)testMidnightIsZeroHours {
    XCTAssertEqualObjects(@"00:00", [[Clock clockWithHours:24] description]);
}

- (void)testHourRollsOver {
    XCTAssertEqualObjects(@"01:00", [[Clock clockWithHours:25] description]);
}

- (void)testHourRollsOverContinuously {
    XCTAssertEqualObjects(@"04:00", [[Clock clockWithHours:100] description]);
}

- (void)testSixtyMinutesIsNextHour {
    XCTAssertEqualObjects(@"02:00", [[Clock clockWithHours:1 minutes:60] description]);
}

- (void)testMinutesRollOver {
    XCTAssertEqualObjects(@"02:40", [[Clock clockWithHours:0 minutes:160] description]);
}

- (void)testMinutesRollOverContinuously {
    XCTAssertEqualObjects(@"04:43", [[Clock clockWithHours:0 minutes:1723] description]);
}

- (void)testHoursAndMinutesRollOver {
    XCTAssertEqualObjects(@"11:01", [[Clock clockWithHours:201 minutes:3001] description]);
}

- (void)testHoursAndMinutesRollOverToExactlyMidnight {
    XCTAssertEqualObjects(@"00:00", [[Clock clockWithHours:72 minutes:8640] description]);
}

- (void)testNegativeHour {
    XCTAssertEqualObjects(@"23:15", [[Clock clockWithHours:-1 minutes:15] description]);
}

- (void)testNegativeHourRollsOver {
    XCTAssertEqualObjects(@"23:00", [[Clock clockWithHours:-25] description]);
}

- (void)testNegativeHourRollsOverContinuously {
    XCTAssertEqualObjects(@"05:00", [[Clock clockWithHours:-91] description]);
}

- (void)testNegativeMinutes {
    XCTAssertEqualObjects(@"00:20", [[Clock clockWithHours:1 minutes:-40] description]);
}

- (void)testNegativeMinutesRollOver {
    XCTAssertEqualObjects(@"22:20", [[Clock clockWithHours:1 minutes:-160] description]);
}

- (void)testNegativeMinutesRollOverContinuously {
    XCTAssertEqualObjects(@"16:40", [[Clock clockWithHours:1 minutes:-4820] description]);
}

- (void)testNegativeHoursAndMinutesBothRollOverContinuously {
    XCTAssertEqualObjects(@"22:10", [[Clock clockWithHours:-121 minutes:-5810] description]);
}

//#pragma mark - Add: Test adding and subtracting minutes.

- (void)testAddMinutes {
    Clock *clock = [[Clock clockWithHours:10] addMinutes:3];
    XCTAssertEqualObjects(@"10:03", clock.description);
}

- (void)testAddNoMinutes {
    Clock *clock = [[Clock clockWithHours:6 minutes:41] addMinutes:0];
    XCTAssertEqualObjects(@"06:41", clock.description);
}

- (void)testAddToNextHour {
    Clock *clock = [[Clock clockWithHours:0 minutes:45] addMinutes:40];
    XCTAssertEqualObjects(@"01:25", clock.description);
}

- (void)testAddMoreThanOneHour {
    Clock *clock = [[Clock clockWithHours:10] addMinutes:61];
    XCTAssertEqualObjects(@"11:01", clock.description);
}

- (void)testAddMoreThanTwoHoursWithCarry {
    Clock *clock = [[Clock clockWithHours:0 minutes:45] addMinutes:160];
    XCTAssertEqualObjects(@"03:25", clock.description);
}

- (void)testAddAcrossMidnight {
    Clock *clock = [[Clock clockWithHours:23 minutes:59] addMinutes:2];
    XCTAssertEqualObjects(@"00:01", clock.description);
}

- (void)testAddMoreThanOneDay {
    // (1500 min = 25 hrs)
    Clock *clock = [[Clock clockWithHours:5 minutes:32] addMinutes:1500];
    XCTAssertEqualObjects(@"06:32", clock.description);
}

- (void)testAddMoreThanTwoDays {
    Clock *clock = [[Clock clockWithHours:1 minutes:1] addMinutes:3500];
    XCTAssertEqualObjects(@"11:21", clock.description);
}

- (void)testSubtractMinutes {
    Clock *clock = [[Clock clockWithHours:10 minutes:3] subtractMinutes:3];
    XCTAssertEqualObjects(@"10:00", clock.description);
}

- (void)testSubtractToPreviousHour {
    Clock *clock = [[Clock clockWithHours:10 minutes:3] subtractMinutes:30];
    XCTAssertEqualObjects(@"09:33", clock.description);
}

- (void)testSubtractMoreThanAnHour {
    Clock *clock = [[Clock clockWithHours:10 minutes:3] subtractMinutes:70];
    XCTAssertEqualObjects(@"08:53", clock.description);
}

- (void)testSubtractAcrossMidnight {
    Clock *clock = [[Clock clockWithHours:0 minutes:3] subtractMinutes:4];
    XCTAssertEqualObjects(@"23:59", clock.description);
}

- (void)testSubtractMoreThanTwoHours {
    Clock *clock = [[Clock clockWithHours:0 minutes:0] subtractMinutes:160];
    XCTAssertEqualObjects(@"21:20", clock.description);
}

- (void)testSubtractMoreTHanTwoHoursWithBorrow {
    Clock *clock = [[Clock clockWithHours:6 minutes:15] subtractMinutes:160];
    XCTAssertEqualObjects(@"03:35", clock.description);
}

- (void)testSubtractMoreThanOneDay {
    // (1500 min = 25 hrs)
    Clock *clock = [[Clock clockWithHours:5 minutes:32] subtractMinutes:1500];
    XCTAssertEqualObjects(@"04:32", clock.description);
}

- (void)testSubtractMoreThanTwoDays {
    Clock *clock = [[Clock clockWithHours:2 minutes:20] subtractMinutes:3000];
    XCTAssertEqualObjects(@"00:20", clock.description);
}

//#pragma mark - Equal: Construct two separate clocks, set times, test if they are equal.

- (void)testClocksWithSameTime {
    Clock *clock1 = [Clock clockWithHours:15 minutes:37];
    Clock *clock2 = [Clock clockWithHours:15 minutes:37];
    XCTAssertEqualObjects(clock1, clock2);
}

- (void)testClocksAMinuteApart {
    Clock *clock1 = [Clock clockWithHours:15 minutes:36];
    Clock *clock2 = [Clock clockWithHours:15 minutes:37];
    XCTAssertNotEqual(clock1, clock2);
}

- (void)testClocksAnHourApart {
    Clock *clock1 = [Clock clockWithHours:14 minutes:37];
    Clock *clock2 = [Clock clockWithHours:15 minutes:37];
    XCTAssertNotEqual(clock1, clock2);
}

- (void)testClocksWithHourOverflow {
    Clock *clock1 = [Clock clockWithHours:10 minutes:37];
    Clock *clock2 = [Clock clockWithHours:34 minutes:37];
    XCTAssertEqualObjects(clock1, clock2);
}

- (void)testClocksWithHourOverflowBySeveralDays {
    Clock *clock1 = [Clock clockWithHours:3 minutes:11];
    Clock *clock2 = [Clock clockWithHours:99 minutes:11];
    XCTAssertEqualObjects(clock1, clock2);
}

- (void)testClocksWithNegativeHour {
    Clock *clock1 = [Clock clockWithHours:22 minutes:40];
    Clock *clock2 = [Clock clockWithHours:-2 minutes:40];
    XCTAssertEqualObjects(clock1, clock2);
}

- (void)testClocksWithNegativeHourThatWraps {
    Clock *clock1 = [Clock clockWithHours:17 minutes:3];
    Clock *clock2 = [Clock clockWithHours:-31 minutes:3];
    XCTAssertEqualObjects(clock1, clock2);
}

- (void)testClocksWithNegativeHourThatWrapsMultipleTimes {
    Clock *clock1 = [Clock clockWithHours:13 minutes:49];
    Clock *clock2 = [Clock clockWithHours:-83 minutes:49];
    XCTAssertEqualObjects(clock1, clock2);
}

- (void)testClocksWithMinuteOverflow {
    Clock *clock1 = [Clock clockWithHours:0 minutes:1];
    Clock *clock2 = [Clock clockWithHours:0 minutes:1441];
    XCTAssertEqualObjects(clock1, clock2);
}

- (void)testClocksWithMinuteOverflowBySeveralDays {
    Clock *clock1 = [Clock clockWithHours:2 minutes:2];
    Clock *clock2 = [Clock clockWithHours:2 minutes:4322];
    XCTAssertEqualObjects(clock1, clock2);
}

- (void)testClocksWithNegativeMinute {
    Clock *clock1 = [Clock clockWithHours:2 minutes:40];
    Clock *clock2 = [Clock clockWithHours:3 minutes:-20];
    XCTAssertEqualObjects(clock1, clock2);
}

- (void)testClocksWithNegativeMinuteThatWraps {
    Clock *clock1 = [Clock clockWithHours:4 minutes:10];
    Clock *clock2 = [Clock clockWithHours:5 minutes:-1490];
    XCTAssertEqualObjects(clock1, clock2);
}

- (void)testClocksWithNegativeMinuteThatWrapsMultipleTimes {
    Clock *clock1 = [Clock clockWithHours:6 minutes:15];
    Clock *clock2 = [Clock clockWithHours:6 minutes:-4305];
    XCTAssertEqualObjects(clock1, clock2);
}

- (void)testClocksWithNegativeHoursAndMinutes {
    Clock *clock1 = [Clock clockWithHours:7 minutes:32];
    Clock *clock2 = [Clock clockWithHours:-12 minutes:-268];
    XCTAssertEqualObjects(clock1, clock2);
}

- (void)testClocksWithNegativeHoursAndMinutesThatWrap {
    Clock *clock1 = [Clock clockWithHours:18 minutes:7];
    Clock *clock2 = [Clock clockWithHours:-54 minutes:-11513];
    XCTAssertEqualObjects(clock1, clock2);
}

@end

Clock.h

#import <Foundation/Foundation.h>

@interface Clock : NSObject

+ (instancetype)clockWithHours:(NSInteger)hours;
+ (instancetype)clockWithHours:(NSInteger)hours minutes:(NSInteger)minutes;
- (instancetype)addMinutes:(NSInteger)addMinutes;
- (instancetype)subtractMinutes:(NSInteger)subtractMinutes;

@end

Clock.m

#import "Clock.h"

@interface Clock ()

@property (nonatomic, assign) NSInteger minutes;

@end

@implementation Clock

+ (instancetype)clockWithHours:(NSInteger)hours {
    Clock *clock = [[self alloc] init];
    clock.minutes = hours * 60;
    return clock;
}

+ (instancetype)clockWithHours:(NSInteger)hours minutes:(NSInteger)minutes {
    Clock *clock = [[self alloc] init];
    clock.minutes = hours * 60 + minutes;
    return clock;
}

- (instancetype)addMinutes:(NSInteger)addMinutes {
    self.minutes += addMinutes;
    return self;
}

- (instancetype)subtractMinutes:(NSInteger)subtractMinutes {
    self.minutes -= subtractMinutes;
    return self;
}

- (NSString *)description {
    NSInteger hours = labs(self.minutes) / 60 % 24;
    NSInteger minutes = labs(self.minutes) % 60;
    if (self.minutes < 0) {
        hours = 24 - hours - (minutes > 0 ? 1 : 0);
        if (minutes > 0) {
            minutes = 60 - minutes;
        }
    }
    return [NSString stringWithFormat:@"%02ld:%02ld", (long)hours, (long)minutes];
}

- (BOOL)isEqual:(Clock *)object {
    return [[self description] isEqualToString:[object description]];
}

@end

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?