Created
May 26, 2021 09:13
-
-
Save denisenepraunig/91af99355d83b348b7c28a1fd6d0f142 to your computer and use it in GitHub Desktop.
Example UnitTests for a Stack in Swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
public struct Stack<Element> { | |
private var storage = [Element]() | |
public init() {} | |
public init(_ elements: [Element]) { | |
storage = elements | |
} | |
public mutating func push(_ element: Element) { | |
storage.append(element) | |
} | |
@discardableResult | |
public mutating func pop() -> Element? { | |
storage.popLast() | |
} | |
public func peek() -> Element? { | |
storage.last | |
} | |
public var isEmpty: Bool { | |
peek() == nil | |
} | |
} | |
extension Stack: CustomStringConvertible { | |
public var description: String { | |
""" | |
--- top --- | |
\(storage.map { "\($0)" }.reversed().joined(separator: "\n")) | |
----------- | |
""" | |
} | |
} | |
extension Stack: ExpressibleByArrayLiteral { | |
public init(arrayLiteral elements: Element...) { | |
storage = elements | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import XCTest | |
@testable import DataStructuresAndAlgorithms | |
class StackTests: XCTestCase { | |
func test_init_stack_is_empty() { | |
var sut = Stack<Int>() | |
XCTAssertNil(sut.pop()) | |
} | |
func test_push_LIFO() { | |
var sut = Stack<Int>() | |
sut.push(1) | |
sut.push(2) | |
sut.push(3) | |
XCTAssertEqual(3, sut.pop()) | |
XCTAssertEqual(2, sut.pop()) | |
XCTAssertEqual(1, sut.pop()) | |
XCTAssertNil(sut.pop()) | |
} | |
func test_description() { | |
var sut = Stack<Int>() | |
sut.push(1) | |
sut.push(2) | |
let stackDescription = | |
""" | |
--- top --- | |
2 | |
1 | |
----------- | |
""" | |
XCTAssertEqual(sut.description, stackDescription) | |
} | |
func test_peek_isEmpty_empty_stack() { | |
let sut = Stack<Int>() | |
XCTAssertNil(sut.peek()) | |
XCTAssertTrue(sut.isEmpty) | |
} | |
func test_peek_isEmpty_not_empty_stack() { | |
var sut = Stack<Int>() | |
sut.push(1) | |
sut.push(2) | |
XCTAssertEqual(sut.peek(), 2) | |
XCTAssertFalse(sut.isEmpty) | |
} | |
func test_init_from_array() { | |
let elements = [1, 2, 3] | |
var sut = Stack(elements) | |
XCTAssertFalse(sut.isEmpty) | |
XCTAssertEqual(sut.peek(), 3) | |
sut.push(4) | |
XCTAssertEqual(sut.peek(), 4) | |
} | |
func test_init_array_literal() { | |
var sut: Stack = [1, 2, 3] | |
XCTAssertFalse(sut.isEmpty) | |
XCTAssertEqual(sut.peek(), 3) | |
sut.push(4) | |
XCTAssertEqual(sut.peek(), 4) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment