Skip to content

Commit

Permalink
Added thorough tests to Unsigned Integer and refactored code
Browse files Browse the repository at this point in the history
Fixed incorrect tests
  • Loading branch information
Dennis-Cherchenko committed Nov 29, 2016
1 parent ce47ba7 commit ccc9a10
Show file tree
Hide file tree
Showing 2 changed files with 239 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,46 @@ public func random(inRange range: CountableClosedRange<Int>) -> Int {

// TODO: write tests for this extension
extension UnsignedInteger {
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 init(ip_data: Data) {
let hexInt = ip_data.ip_hexInt ?? 0
self.init(ip_safely: hexInt)
}

public init(ip_data: NSData) {
let hexInt = ip_data.ip_hexInt ?? 0
self.init(ip_safely: hexInt)
}

public init(ip_data: Data) {
let hexInt = ip_data.ip_hexInt ?? 0
self.init(ip_safely: hexInt)

public init<T : SignedInteger>(ip_safely value: T) {
if value < 0 {
self.init(ip_safely: UInt8(0))
} else {
let max = UIntMax(value.toIntMax())
self.init(ip_safely: max)
}
}


public init<T : UnsignedInteger>(ip_safely value: T) {
self = 0

let maxSelf = type(of: self).ip_maxValue
if maxSelf.toUIntMax() >= value.toUIntMax() {
self = .init(value.toUIntMax())
} else {
self = maxSelf
}
}

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 @@ -48,7 +72,9 @@ extension UnsignedInteger {
return NSData(bytes: &copy, length: MemoryLayout<Self>.size)
}

/// Converts a bit mask into its given indexes. For example, `0b101` will return `[0,2]`
/// 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
public var ip_maskedIndexes: [Self] {
var copy = self
var currentIndex: Self = 0
Expand All @@ -64,10 +90,6 @@ extension UnsignedInteger {
return selectedIndexes
}

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

public static var ip_maxValue: Self {
return ip_bitStackOfLength(ip_maximumNumberOfBits)
}
Expand All @@ -89,24 +111,4 @@ extension UnsignedInteger {
}
return stack
}

public init<T : SignedInteger>(ip_safely value: T) {
if value < 0 {
self.init(ip_safely: UInt8(0))
} else {
let max = UIntMax(value.toIntMax())
self.init(ip_safely: max)
}
}

public init<T : UnsignedInteger>(ip_safely value: T) {
self = 0

let maxSelf = type(of: self).ip_maxValue
if maxSelf.toUIntMax() >= value.toUIntMax() {
self = .init(value.toUIntMax())
} else {
self = maxSelf
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,207 @@ 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))

data = Data(byte: UInt8(1))
number = UInt8.init(ip_data: data)
XCTAssert(number == UInt8(1))
XCTAssertFalse(number == UInt8(0))

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

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

//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))

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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)

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

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

number = UInt8(INT8_MAX)
correctData = Data(byte: number)
XCTAssert(correctData == UInt8(INT8_MAX).ip_data)
XCTAssertFalse(correctData == UInt8(0).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)

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

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

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

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

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])

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])

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
}
}

0 comments on commit ccc9a10

Please sign in to comment.