Skip to content

Commit 8565ccd

Browse files
committed
Update comments, swiftlint autocorrect and rename some tests
1 parent 5ae2b74 commit 8565ccd

4 files changed

Lines changed: 23 additions & 21 deletions

File tree

Sources/LsbBitWriter.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,15 @@ public final class LsbBitWriter: BitWriter {
5454
mask <<= 1
5555
}
5656
}
57-
57+
5858
/**
5959
Writes `number`, using and advancing by `bitsCount` BIT positions.
6060

6161
- Note: If `bitsCount` is smaller than the actual amount of `number`'s bits than the `number` will be truncated to
6262
fit into `bitsCount` amount of bits.
6363
- Note: Bits of `number` are processed using the same bit-numbering scheme as of the writer (i.e. "LSB 0").
64-
- Note: This method is specifically useful when needing to write a UInt64 which can overflow and crash if converting to an Int when using the regular `write` method
64+
- Note: This method is especially useful when it is necessary to write an unsigned number which overflows and,
65+
thus, crashes when converting to an `Int` if `write(number:bitsCount:)` method is used.
6566
*/
6667
public func write(unsignedNumber: UInt, bitsCount: Int) {
6768
var mask: UInt = 1
@@ -70,7 +71,7 @@ public final class LsbBitWriter: BitWriter {
7071
mask <<= 1
7172
}
7273
}
73-
74+
7475
/**
7576
Writes `byte`, advancing by one BYTE position.
7677

Sources/MsbBitWriter.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,15 @@ public final class MsbBitWriter: BitWriter {
5454
mask >>= 1
5555
}
5656
}
57-
57+
5858
/**
5959
Writes `number`, using and advancing by `bitsCount` BIT positions.
6060

6161
- Note: If `bitsCount` is smaller than the actual amount of `number`'s bits than the `number` will be truncated to
6262
fit into `bitsCount` amount of bits.
6363
- Note: Bits of `number` are processed using the same bit-numbering scheme as of the writer (i.e. "MSB 0").
64-
- Note: This method is specifically useful when needing to write a UInt64 which can overflow and crash if converting to an Int when using the regular `write` method
64+
- Note: This method is especially useful when it is necessary to write an unsigned number which overflows and,
65+
thus, crashes when converting to an `Int` if `write(number:bitsCount:)` method is used.
6566
*/
6667
public func write(unsignedNumber: UInt, bitsCount: Int) {
6768
var mask: UInt = 1 << (UInt(truncatingIfNeeded: bitsCount) - 1)

Tests/BitByteDataTests/LsbBitWriterTests.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ class LsbBitWriterTests: XCTestCase {
4141
XCTAssertEqual(bitWriter.data, Data(bytes: [255, 62, 3]))
4242
}
4343

44+
func testWriteUnsignedNumber() {
45+
let bitWriter = LsbBitWriter()
46+
bitWriter.write(unsignedNumber: UInt(UInt64.max), bitsCount: UInt64.bitWidth)
47+
48+
let byteReader = ByteReader(data: bitWriter.data)
49+
XCTAssertEqual(byteReader.uint64(), UInt64.max)
50+
}
51+
4452
func testAppendByte() {
4553
let bitWriter = LsbBitWriter()
4654

@@ -78,13 +86,5 @@ class LsbBitWriterTests: XCTestCase {
7886
let bitReader = LsbBitReader(data: bitWriter.data)
7987
XCTAssertEqual(bitReader.int(fromBits: 14), 14582)
8088
}
81-
82-
func testUInt64() {
83-
let bitWriter = LsbBitWriter()
84-
bitWriter.write(unsignedNumber: UInt(UInt64.max), bitsCount: UInt64.bitWidth)
85-
86-
let byteReader = ByteReader.init(data: bitWriter.data)
87-
XCTAssertEqual(byteReader.uint64(), UInt64.max)
88-
}
8989

9090
}

Tests/BitByteDataTests/MsbBitWriterTests.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ class MsbBitWriterTests: XCTestCase {
4444
XCTAssertEqual(bitWriter.data, Data(bytes: [255, 217, 192]))
4545
}
4646

47+
func testWriteUnsignedNumber() {
48+
let bitWriter = MsbBitWriter()
49+
bitWriter.write(unsignedNumber: UInt(UInt64.max), bitsCount: UInt64.bitWidth)
50+
51+
let byteReader = ByteReader(data: bitWriter.data)
52+
XCTAssertEqual(byteReader.uint64(), UInt64.max)
53+
}
54+
4755
func testAppendByte() {
4856
let bitWriter = LsbBitWriter()
4957

@@ -81,13 +89,5 @@ class MsbBitWriterTests: XCTestCase {
8189
let bitReader = MsbBitReader(data: bitWriter.data)
8290
XCTAssertEqual(bitReader.int(fromBits: 14), 14582)
8391
}
84-
85-
func testUInt64() {
86-
let bitWriter = MsbBitWriter()
87-
bitWriter.write(unsignedNumber: UInt(UInt64.max), bitsCount: UInt64.bitWidth)
88-
89-
let byteReader = ByteReader.init(data: bitWriter.data)
90-
XCTAssertEqual(byteReader.uint64(), UInt64.max)
91-
}
9292

9393
}

0 commit comments

Comments
 (0)