-
Notifications
You must be signed in to change notification settings - Fork 14
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
Conversation
return self.init(ip_safely: rand) | ||
} | ||
} | ||
|
||
// TODO: evaluate whether we should move random to be a member of CountableClosedRange |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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) { | ||
|
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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() { |
There was a problem hiding this comment.
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() { |
There was a problem hiding this comment.
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() { |
There was a problem hiding this comment.
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)) |
There was a problem hiding this comment.
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)) |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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)
?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 == []) |
There was a problem hiding this comment.
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.
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) |
There was a problem hiding this comment.
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) | ||
} | ||
|
||
There was a problem hiding this comment.
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() { |
There was a problem hiding this comment.
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)) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice tests.
4f64195
to
9c1b1d3
Compare
There was a problem hiding this 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 |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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.
Fixed incorrect tests
Addressed PR comments 2 Addressed PR comments 3 Fixed incorrect function name
9c1b1d3
to
433aa79
Compare
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) { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.)
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 :-) )
Closing because I let this get really out-of-date. |
No description provided.