Instantly share code, notes, and snippets.

Embed
What would you like to do?
Test Triangle Type (exercise from Kent Beck's "Test-Driven Development by Example")
//
// TriangleTest.swift
// TriangleTest
//
// Created by Stanislaw Pankevich on 24/01/16.
// Copyright © 2016 Stanislaw Pankevich. All rights reserved.
//
import XCTest
enum TriangleType {
case Equilateral
case Isosceles
case Scalene
case NotWellFormed
}
func triangleType(a: Int, _ b: Int, _ c: Int) -> TriangleType {
if (a <= 0 || b <= 0 || c <= 0) {
return .NotWellFormed
}
if (a == b && b == c) {
return .Equilateral
}
if (a == b || b == c || a == c) {
return .Isosceles
}
return .Scalene
}
class TriangleTest: XCTestCase {
func testEquilateral() {
let type = triangleType(1, 1, 1)
XCTAssert(type == .Equilateral)
}
func testScalene() {
let type = triangleType(1, 2, 3)
XCTAssert(type == .Scalene)
}
// Isosceles: 3 cases
func testIsoscelesAandB() {
let type = triangleType(2, 2, 1)
XCTAssert(type == .Isosceles)
}
func testIsoscelesBandC() {
let type = triangleType(2, 1, 2)
XCTAssert(type == .Isosceles)
}
func testIsoscelesAandC() {
let type = triangleType(2, 1, 2)
XCTAssert(type == .Isosceles)
}
// First parameter validation
func testMalformedIfFirstParameterIsZero() {
let type = triangleType(0, 1, 1)
XCTAssert(type == .NotWellFormed)
}
func testMalformedIfFirstParameterIsNegative() {
let type = triangleType(-1, 1, 1)
XCTAssert(type == .NotWellFormed)
}
// Second parameter validation
func testMalformedIfSecondParameterIsZero() {
let type = triangleType(1, 0, 1)
XCTAssert(type == .NotWellFormed)
}
func testMalformedIfSecondParameterIsNegative() {
let type = triangleType(1, -2, 1)
XCTAssert(type == .NotWellFormed)
}
// Third parameter validation
func testMalformedIfThirdParameterIsZero() {
let type = triangleType(1, 1, 0)
XCTAssert(type == .NotWellFormed)
}
func testMalformedIfThirdParameterIsNegative() {
let type = triangleType(1, 1, -3)
XCTAssert(type == .NotWellFormed)
}
}
@sjaustirni

This comment has been minimized.

Show comment
Hide comment
@sjaustirni

sjaustirni Jul 28, 2017

You forgot to check if sum of any two sides is bigger than the third one. For instance, a 5:1:2 triangle is according to your program scalene, whilst in fact it is malformed.

sjaustirni commented Jul 28, 2017

You forgot to check if sum of any two sides is bigger than the third one. For instance, a 5:1:2 triangle is according to your program scalene, whilst in fact it is malformed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment