Created
September 18, 2019 07:39
-
-
Save godrm/611dd3835e02fb79eb38d26e609a5375 to your computer and use it in GitHub Desktop.
This file contains 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
struct FizzBuzz { | |
func makeIf(with value: Int) -> String { | |
guard value > 0 else { return "" } | |
if value % 15 == 0 { | |
return "fizzbuzz" | |
} | |
else if value % 3 == 0 { | |
return "fizz" | |
} | |
else if value % 5 == 0 { | |
return "buzz" | |
} | |
else { | |
return String(value) | |
} | |
} | |
func make(with value: Int) -> String { | |
guard value > 0 else { return "" } | |
switch (value % 3, value % 5) { | |
case (0, 0): return "fizzbuzz" | |
case (0, _): return "fizz" | |
case (_, 0): return "buzz" | |
case (_, _): return String(value) } | |
} | |
func fizzbuzz(lines : Int)->Array<String> { | |
var result : [String] = [] | |
guard lines > 0 else { return [] } | |
for index in 1...lines { | |
result.append(make(with: index)) | |
} | |
return result | |
} | |
} |
This file contains 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
// | |
// FizzBuzzTest.swift | |
// FizzBuzzTest | |
// | |
// Created by JK on 2019/09/18. | |
// Copyright © 2019 codesquad. All rights reserved. | |
// | |
import XCTest | |
class FizzBuzzTest: XCTestCase { | |
var fb : FizzBuzz! = nil | |
override func setUp() { | |
fb = FizzBuzz() | |
} | |
override func tearDown() { | |
} | |
func test_인스턴스_만들기_성공() { | |
XCTAssertNotNil(fb) | |
} | |
func test_0일때_공백출력() { | |
let result = fb.make(with: 0) | |
XCTAssertEqual(result, "") | |
} | |
func test_1일때_숫자출력() { | |
let result = fb.make(with: 1) | |
XCTAssertEqual(result, "1") | |
} | |
func test_3일때_fizz출력() { | |
let result = fb.make(with: 3) | |
XCTAssertEqual(result, "fizz") | |
} | |
func test_5일때_buzz출력() { | |
let result = fb.make(with: 5) | |
XCTAssertEqual(result, "buzz") | |
} | |
func test_15일때_fizzbuzz출력() { | |
let result = fb.make(with: 15) | |
XCTAssertEqual(result, "fizzbuzz") | |
} | |
func test_3배수일때_fizz출력() { | |
let result6 = fb.make(with: 6) | |
XCTAssertEqual(result6, "fizz") | |
let result9 = fb.make(with: 9) | |
XCTAssertEqual(result9, "fizz") | |
} | |
func test_5배수일때_buzz출력() { | |
let result10 = fb.make(with: 10) | |
XCTAssertEqual(result10, "buzz") | |
let result25 = fb.make(with: 25) | |
XCTAssertEqual(result25, "buzz") | |
} | |
func test_15배수일때_buzz출력() { | |
let result30 = fb.make(with: 30) | |
XCTAssertEqual(result30, "fizzbuzz") | |
let result45 = fb.make(with: 45) | |
XCTAssertEqual(result45, "fizzbuzz") | |
} | |
func test_모든3배수일때_fizz출력() { | |
for index in stride(from: 3, to: 300, by: 3) | |
{ | |
guard (index % 5 != 0 && index % 15 != 0) else { continue } | |
let result = fb.make(with: index) | |
XCTAssertEqual(result, "fizz") | |
} | |
} | |
func test_모든5배수일때_buzz출력() { | |
for index in stride(from: 5, to: 300, by: 5) | |
{ | |
guard (index % 3 != 0 && index % 15 != 0) else { continue } | |
let result = fb.make(with: index) | |
XCTAssertEqual(result, "buzz") | |
} | |
} | |
func test_모든15배수일때_buzz출력() { | |
for index in stride(from: 15, to: 300, by: 15) | |
{ | |
guard (index % 3 != 0 && index % 5 != 0) else { continue } | |
let result = fb.make(with: index) | |
XCTAssertEqual(result, "fizzbuzz") | |
} | |
} | |
func test_모든예외숫자일때_숫자출력() { | |
for index in stride(from: 1, to: 300, by: 1) | |
{ | |
guard (index % 3 != 0 && index % 5 != 0 && index % 15 != 0) else { continue } | |
let result = fb.make(with: index) | |
XCTAssertEqual(result, String(index)) | |
} | |
} | |
//MARK:- fizzbuzz array | |
func test_0일때_빈배열() { | |
let result = fb.fizzbuzz(lines: 0) | |
XCTAssertEqual(result, []) | |
} | |
func test_1일때_정상배열() { | |
let result = fb.fizzbuzz(lines: 1) | |
XCTAssertEqual(result, ["1"]) | |
} | |
func test_2일때_정상배열() { | |
let result = fb.fizzbuzz(lines: 2) | |
XCTAssertEqual(result, ["1", "2"]) | |
} | |
func test_모든경우_정상배열() { | |
let result = fb.fizzbuzz(lines: 300) | |
let compare = FizzBuzz() | |
var otherResult : [String] = [] | |
for index in 1...300 { | |
otherResult.append(compare.makeIf(with: index)) | |
} | |
XCTAssertEqual(result, otherResult) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment