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

UnsignedInteger+Extension + tests #98

Closed
wants to merge 6 commits into from

Conversation

Dennis-Cherchenko
Copy link
Contributor

No description provided.

return self.init(ip_safely: rand)
}
}

// TODO: evaluate whether we should move random to be a member of CountableClosedRange
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this still TODO, or is it now resolved one way or another?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe @andrewdolce is addressing this in #97

@@ -26,16 +18,46 @@ public func random(inRange range: CountableClosedRange<Int>) -> Int {

// TODO: write tests for this extension
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this still TODO?

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

public init(ip_data: Data) {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unneeded whitespace

/// 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typos: "mast" -> "mask", "it's" -> "its", and "indexs" -> "indexes" (or "indices," but we use "indexes" on line 75.

//XCTAssertFalse()
}

//Finished
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary comment.

XCTAssertFalse(number == UInt8(0))
}

//Finished
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary comment.

XCTAssertFalse(number == UInt8(0))
}

//Finished
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary comment.

XCTAssertFalse(number == UInt(0))
}

//Finished
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary comment.

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

//Finished
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary comment.

XCTAssertFalse(correctData == UInt8(0).ip_data)
}

//Finished
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary comment.

XCTAssertFalse(correctNSData == UInt8(0).ip_nsdata)
}

//Finished
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary comment.

@@ -19,4 +19,207 @@ class UnsignedIntegerExtensionTests: XCTestCase {
let mixed = -100...100
XCTAssert(mixed.contains(random(inRange: mixed)))
}

func testRandom() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove, or implement, or add a TODO?

XCTAssert(number == UInt(UINT8_MAX))
}

func test_ip_random() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove, or implement, or add a TODO?

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

func test_ip_maxValue() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove, or implement, or add a TODO for this and the next two, similar methods?

func test_ip_initData() {
var data = Data(byte: UInt8(0))
var number = UInt8.init(ip_data: data)
XCTAssert(number == UInt8(0))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This, and all similar tests throughout the PR, should be calls to XCTAssertEqual

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly, this and all similar tests throughout the PR, should be calls to XCTAssertNotEqual


public init(ip_data: Data) {
public init(ip_data: NSData) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You just flipped where these initializers were in the file. Just pointing that out, seems like an accident?

return self.init(ip_safely: rand)
}

public func ip_containsMask(_ mask: Self) -> Bool {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is confusing. What's the difference between this and public func ip_containsBitMask(_ bitMask: Self)?

Copy link
Contributor

@paulrolfe paulrolfe Nov 23, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I see you just moved it up from below, but still I have to question why we need both. I believe [client] is the only group making use of these and we prefer ip_containsBitMask

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what the difference is. I received it like this and it seems like both are used in a project somewhere

Copy link
Contributor

@paulrolfe paulrolfe Nov 23, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand that both were in there. I just don't like having two functions that do literally the exact same thing, so we should pick one. I'm casting my vote for ip_containsBitMask to stay: it's used heavily in [client] and BitMask is a more descriptive term than Mask.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. Should I make the change?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Go for it

}

func test_ip_maskedIndexes() {
XCTAssert(UInt(0).ip_maskedIndexes == [])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The calls in this method should also get the XCTAssertEqual / XCTAssertNotEqual treatment.

@bobgilmore
Copy link
Contributor

Not ready for merge yet, but when it is, might want to squash this down into only a few commits.

// TODO: write tests for this function
public static func ip_random() -> Self {
let intMax = Int(ip_maxValue.toIntMax())
let rand = random(inRange: 0...intMax)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add another TODO here, which is to use @andrewdolce's new collection ip_random method when it comes out.

public static var ip_maxValue: Self {
return ip_bitStackOfLength(ip_maximumNumberOfBits)
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

additional spaces, should clean out

@@ -19,4 +19,188 @@ class UnsignedIntegerExtensionTests: XCTestCase {
let mixed = -100...100
XCTAssert(mixed.contains(random(inRange: mixed)))
}

// TODO: Write test
func testRandom() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

empty method should be remoed

var data = Data(byte: UInt8(0))
var number = UInt8.init(ip_data: data)
XCTAssertEqual(number, UInt8(0))
XCTAssertNotEqual(number, UInt8(1))
Copy link
Contributor

@brightredchilli brightredchilli Nov 23, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This not equal case is here, and in the rest of the files, are redundant. This is because XCTAssertNotEqual is simply an alias to XCTAssertEqual with the assertion flipped from true to false. IF XCTAssertEqual is true, then number can't be anything else. I'd like some other people to weigh in on this though.

Copy link
Contributor

@bobgilmore bobgilmore Nov 23, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch @brightredchilli , the Equal check is sufficient.

}

func test_ip_containsMask() {
XCTAssert(UInt(0).ip_containsMask(0)) // 0b0, 0b0 -> true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice tests.

@Dennis-Cherchenko Dennis-Cherchenko force-pushed the evaluate-api-for-randomInRange branch 2 times, most recently from 4f64195 to 9c1b1d3 Compare November 29, 2016 14:16
Copy link
Contributor

@brightredchilli brightredchilli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really great job doing excellent testing here - some more aesthetic things I really think we need to close out before merging but this looks great.

}

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this self = 0 is not necessary because if and else block seems to cover it


func test_ip_initData() {
var data = Data(byte: UInt8(0))
var number = UInt8.init(ip_data: data)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's remove .init from this test and the rest of the tests in this diff - UInt8(...) should do.

@andrewdolce andrewdolce self-assigned this Feb 14, 2017
Addressed PR comments 2

Addressed PR comments 3

Fixed incorrect function name
@brightredchilli brightredchilli force-pushed the evaluate-api-for-randomInRange branch from 9c1b1d3 to 433aa79 Compare February 17, 2017 19:46
extension UnsignedInteger {

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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does anyone have context as to why these two intializers would need to be introduced? do we need them?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that ip_safely appears now and then in files in the project, but doesn't seem to be explained anywhere. Can someone explain the convention?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are used to avoid overflow/underflow when converting between types. I'm potentially using one as part of the ip_random() function for my other PR (in progress.)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we not encourage this? Isn't that glossing over a potential problem?

Copy link
Contributor

@brightredchilli brightredchilli Feb 17, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For example, here is a test that i wrote that exercises this code:

number = UInt(ip_safely: -1)
XCTAssertEqual(number, UInt(0))

this seems rather.....surprising? I feel like this should be a faillable initializer

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I agree with @brightredchilli (unless that 's the point of the whole thing :-) )

@andrewdolce
Copy link

Closing because I let this get really out-of-date.

@andrewdolce andrewdolce closed this May 5, 2017
@tzm41 tzm41 deleted the evaluate-api-for-randomInRange branch June 20, 2017 19:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants