Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add NSData sequencetype extension, and test case #104

Merged
merged 2 commits into from
Mar 2, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions Sources/Vapor/Utilities/NSData+SequenceType.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// NSData+SequenceType.swift
// Vapor
//
// Created by Robert Thompson on 03/02/2016
//

import Foundation

extension NSData: SequenceType {
public func generate() -> UnsafeBufferPointer<UInt8>.Generator {
return UnsafeBufferPointer<UInt8>(start: UnsafePointer<UInt8>(self.bytes), count: self.length).generate()
}
}
42 changes: 42 additions & 0 deletions Tests/Vapor/UtilityTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//
// UtilityTests.swift
// Vapor
//
// Created by Robert Thompson on 03/02/2016
//

import XCTest
@testable import Vapor

#if os(Linux)
import Glibc
import Foundation
extension UtilityTests: XCTestCaseProvider {
var allTests : [(String, () throws -> Void)] {
return [
("testNSDataSequenceType", testNSDataSequenceType),
]
}
}
#endif

class UtilityTests: XCTestCase {
func testNSDataSequenceType() {
let string = "Hello world!"
let bytes = string.withCString {
(str: UnsafePointer<CChar>) -> UnsafeMutablePointer<UInt8> in
let result = UnsafeMutablePointer<UInt8>.alloc(string.utf8.count + 1)
memcpy(result, str, string.utf8.count)
return result
}

defer { free(bytes) }

let data = NSData(bytes: bytes, length: string.utf8.count + 1)
var i = 0
for byte in data {
XCTAssert(byte == bytes[i])
i += 1
}
}
}