Skip to content

Commit

Permalink
Addressed PR comments
Browse files Browse the repository at this point in the history
Addressed PR comments 2

Addressed PR comments 3

Fixed incorrect function name
  • Loading branch information
Dennis-Cherchenko authored and brightredchilli committed Feb 17, 2017
1 parent d7840c3 commit 433aa79
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 110 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ public func random(inRange range: CountableClosedRange<Int>) -> Int {
return random
}

// TODO: write tests for this extension
extension UnsignedInteger {

public init(ip_data: Data) {
Expand All @@ -42,16 +41,13 @@ extension UnsignedInteger {
}
}

// TODO: write tests for this function and use @andrewdolce's new collection ip_random method when it comes out
public static func ip_random() -> Self {
let intMax = Int(ip_maxValue.toIntMax())
let rand = random(inRange: 0...intMax)
return self.init(ip_safely: rand)
}

public func ip_containsMask(_ mask: Self) -> Bool {
return (self & mask) == mask
}

public func ip_containsBitMask(_ bitMask: Self) -> Bool {
return (self & bitMask) == bitMask
}
Expand All @@ -61,9 +57,7 @@ extension UnsignedInteger {
return Data(bytes: &copy, count: MemoryLayout<Self>.size)
}

/// Converts a bit mask into its given indexes. For example, `0b10011` will return `[0, 1, 4]`

//Converts a bit mast into it's given indexs
// Converts a bit mask into its given indices. For example, `0b10011` will return `[0, 1, 4]`
public var ip_maskedIndexes: [Self] {
var copy = self
var currentIndex: Self = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,11 @@ class UnsignedIntegerExtensionTests: XCTestCase {
let mixed = -100...100
XCTAssert(mixed.contains(random(inRange: mixed)))
}

func testRandom() {
//XCTAssert()
//XCTAssertFalse()
}

//Finished
func test_ip_initData() {
var data = Data(byte: UInt8(0))
var number = UInt8.init(ip_data: data)
XCTAssert(number == UInt8(0))
XCTAssertFalse(number == UInt8(1))
XCTAssertEqual(number, UInt8(0))

data = Data(byte: UInt8(1))
number = UInt8.init(ip_data: data)
Expand All @@ -39,94 +32,59 @@ class UnsignedIntegerExtensionTests: XCTestCase {

data = Data(byte: UInt8(10))
number = UInt8.init(ip_data: data)
XCTAssert(number == UInt8(10))
XCTAssertFalse(number == UInt8(100))
XCTAssertEqual(number, UInt8(10))

data = Data(byte: UInt8(UINT8_MAX))
number = UInt8.init(ip_data: data)
XCTAssert(number == UInt8(UINT8_MAX))
XCTAssertFalse(number == UInt8(0))
XCTAssertEqual(number, UInt8(UINT8_MAX))
}

//Finished
func test_ip_initNSData() {
var nsdata = Data(byte: UInt8(0))
var number = UInt8.init(ip_data: nsdata)
XCTAssert(number == UInt8(0))
XCTAssertFalse(number == UInt8(1))
XCTAssertEqual(number, UInt8(0))

nsdata = Data(byte: UInt8(1))
number = UInt8.init(ip_data: nsdata)
XCTAssert(number == UInt8(1))
XCTAssertFalse(number == UInt8(0))
XCTAssertEqual(number, UInt8(1))

nsdata = Data(byte: UInt8(10))
number = UInt8.init(ip_data: nsdata)
XCTAssert(number == UInt8(10))
XCTAssertFalse(number == UInt8(100))
XCTAssertEqual(number, UInt8(10))

nsdata = Data(byte: UInt8(UINT8_MAX))
number = UInt8.init(ip_data: nsdata)
XCTAssert(number == UInt8(UINT8_MAX))
XCTAssertFalse(number == UInt8(0))
XCTAssertEqual(number, UInt8(UINT8_MAX))
}

//Finished
func test_ip_safety_signed() {
var number = UInt.init(ip_safely: 0)
XCTAssert(number == UInt(0))
XCTAssertFalse(number == UInt(1))
XCTAssertEqual(number, UInt(0))

number = UInt.init(ip_safely: 1)
XCTAssert(number == UInt(1))
XCTAssertFalse(number == UInt(0))
XCTAssertEqual(number, UInt(1))

number = UInt.init(ip_safely: 10)
XCTAssert(number == UInt(10))
XCTAssertFalse(number == UInt(100))
XCTAssertEqual(number, UInt(10))

number = UInt.init(ip_safely: Int.max)
XCTAssert(number == UInt(Int.max))
XCTAssertFalse(number == UInt(0))
XCTAssertEqual(number, UInt(Int.max))
}

//Finished
func test_ip_safety_unsigned() {
var number = UInt.init(ip_safely: 0)
XCTAssert(number == UInt(0))
XCTAssertEqual(number, UInt(0))

number = UInt.init(ip_safely: 1)
XCTAssert(number == UInt(1))
XCTAssertEqual(number, UInt(1))

number = UInt.init(ip_safely: 10)
XCTAssert(number == UInt(10))
XCTAssertEqual(number, UInt(10))

number = UInt.init(ip_safely: UInt8(UINT8_MAX))
XCTAssert(number == UInt(UINT8_MAX))
}

func test_ip_random() {
//XCTAssert()
//XCTAssertFalse()
XCTAssertEqual(number, UInt(UINT8_MAX))
}

func test_ip_containsMask() {
XCTAssert(UInt(0).ip_containsMask(0)) // 0b0, 0b0 -> true
XCTAssertFalse(UInt(0).ip_containsMask(1)) // 0b0, 0b1 -> false

XCTAssert(UInt(1).ip_containsMask(1)) // 0b1, 0b1 -> true
XCTAssertFalse(UInt(1).ip_containsMask(10)) // 0b1, 0b0 -> false

XCTAssert(UInt(135).ip_containsMask(7)) // 0b10000111, 0b111 -> true
XCTAssert(UInt(135).ip_containsMask(4)) // 0b10000111, 0b111 -> true
XCTAssertFalse(UInt(131).ip_containsMask(5)) // 0b10000011, 0b101 -> false

XCTAssert(UInt(UINT8_MAX).ip_containsMask(UInt(UINT8_MAX)))
XCTAssert(UInt(UINT8_MAX).ip_containsMask(3))
XCTAssert(UInt(UINT8_MAX).ip_containsMask(0))

}

func test_ip_containsBitMask() {
XCTAssert(UInt(0).ip_containsBitMask(0)) // 0b0, 0b0 -> true
XCTAssertFalse(UInt(0).ip_containsBitMask(1)) // 0b0, 0b1 -> false
Expand All @@ -136,90 +94,59 @@ class UnsignedIntegerExtensionTests: XCTestCase {

XCTAssert(UInt(135).ip_containsBitMask(7)) // 0b10000111, 0b111 -> true
XCTAssert(UInt(135).ip_containsBitMask(4)) // 0b10000111, 0b111 -> true
XCTAssertFalse(UInt(131).ip_containsMask(5)) // 0b10000011, 0b101 -> false
XCTAssertFalse(UInt(131).ip_containsBitMask(5)) // 0b10000011, 0b101 -> false

XCTAssert(UInt(UINT8_MAX).ip_containsBitMask(UInt(UINT8_MAX)))
XCTAssert(UInt(UINT8_MAX).ip_containsBitMask(3))
XCTAssert(UInt(UINT8_MAX).ip_containsBitMask(0))
}

//Finished
func test_ip_data() {
var number = UInt8(0)
var correctData = Data(byte: number)
XCTAssert(correctData == UInt8(0).ip_data)
XCTAssertFalse(correctData == UInt8(1).ip_data)
XCTAssertEqual(correctData, UInt8(0).ip_data)

number = UInt8(1)
correctData = Data(byte: number)
XCTAssert(correctData == UInt8(1).ip_data)
XCTAssertFalse(correctData == UInt8(0).ip_data)
XCTAssertEqual(correctData, UInt8(1).ip_data)

number = UInt8(10)
correctData = Data(byte: number)
XCTAssert(correctData == UInt8(10).ip_data)
XCTAssertFalse(correctData == UInt8(20).ip_data)
XCTAssertEqual(correctData, UInt8(10).ip_data)

number = UInt8(INT8_MAX)
correctData = Data(byte: number)
XCTAssert(correctData == UInt8(INT8_MAX).ip_data)
XCTAssertFalse(correctData == UInt8(0).ip_data)
XCTAssertEqual(correctData, UInt8(INT8_MAX).ip_data)
}

//Finished
func test_ip_nsdata() {
var number = UInt8(0)
var correctNSData = NSData(byte: number)
XCTAssert(correctNSData == UInt8(0).ip_nsdata)
XCTAssertFalse(correctNSData == UInt8(1).ip_nsdata)

number = UInt8(1)
correctNSData = NSData(byte: number)
XCTAssert(correctNSData == UInt8(1).ip_nsdata)
XCTAssertFalse(correctNSData == UInt8(0).ip_nsdata)
XCTAssertEqual(correctNSData, UInt8(1).ip_nsdata)

number = UInt8(10)
correctNSData = NSData(byte: number)
XCTAssert(correctNSData == UInt8(10).ip_nsdata)
XCTAssertFalse(correctNSData == UInt8(20).ip_nsdata)
XCTAssertEqual(correctNSData, UInt8(10).ip_nsdata)

number = UInt8(INT8_MAX)
correctNSData = NSData(byte: number)
XCTAssert(correctNSData == UInt8(INT8_MAX).ip_nsdata)
XCTAssertFalse(correctNSData == UInt8(0).ip_nsdata)
XCTAssertEqual(correctNSData, UInt8(INT8_MAX).ip_nsdata)
}

//Finished
func test_ip_maskedIndexes() {
XCTAssert(UInt(0).ip_maskedIndexes == [])
XCTAssertFalse(UInt(0).ip_maskedIndexes == [1])
XCTAssertEqual(UInt(0).ip_maskedIndexes, [])

XCTAssert(UInt(1).ip_maskedIndexes == [0])
XCTAssertFalse(UInt(1).ip_maskedIndexes == [2])
XCTAssertEqual(UInt(1).ip_maskedIndexes, [0])

XCTAssert(UInt(127).ip_maskedIndexes == [0,1,2,3,4,5,6])
XCTAssertFalse(UInt(127).ip_maskedIndexes == [1,2,3,4,5,6])
XCTAssertFalse(UInt(127).ip_maskedIndexes == [0,1,2,3,4,5])
XCTAssertEqual(UInt(127).ip_maskedIndexes, [0,1,2,3,4,5,6])

XCTAssert(UInt(135).ip_maskedIndexes == [0,1,2,7])
XCTAssertFalse(UInt(135).ip_maskedIndexes == [0,5,6,7])
XCTAssertFalse(UInt(135).ip_maskedIndexes == [0,3,6,7])
XCTAssertEqual(UInt(135).ip_maskedIndexes, [0,1,2,7])

XCTAssert(UInt(INT8_MAX).ip_maskedIndexes[0] == 0)
XCTAssert(UInt(INT8_MAX).ip_maskedIndexes[1] == 1)
XCTAssertFalse(UInt(INT8_MAX).ip_maskedIndexes[0] == 1)
XCTAssertFalse(UInt(INT8_MAX).ip_maskedIndexes[1] == 2)
}

func test_ip_maxValue() {
//Static function
}

func test_ip_maximumNumberOfBits() {
//Static function
}

func test_ip_bitStackOfLength() {
//Static function
XCTAssertEqual(UInt(INT8_MAX).ip_maskedIndexes[0], 0)
XCTAssertEqual(UInt(INT8_MAX).ip_maskedIndexes[1], 1)
}
}

0 comments on commit 433aa79

Please sign in to comment.