Skip to content

Commit 4b3466f

Browse files
committed
Add tests and benchmarks for the new advance(by:) function of bit readers
1 parent eb9fff3 commit 4b3466f

5 files changed

Lines changed: 54 additions & 0 deletions

File tree

Tests/BitByteDataBenchmarks/LsbBitReaderBenchmarks.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ import BitByteData
88

99
class LsbBitReaderBenchmarks: XCTestCase {
1010

11+
func testAdvance() {
12+
self.measure {
13+
let bitReader = LsbBitReader(data: Data(count: 10_485_760)) // 10 MB
14+
15+
for _ in 0..<5_000_000 * 8 {
16+
bitReader.advance()
17+
}
18+
}
19+
}
20+
1121
func testBit() {
1222
self.measure {
1323
let bitReader = LsbBitReader(data: Data(count: 10_485_760)) // 10 MB

Tests/BitByteDataBenchmarks/MsbBitReaderBenchmarks.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ import BitByteData
88

99
class MsbBitReaderBenchmarks: XCTestCase {
1010

11+
func testAdvance() {
12+
self.measure {
13+
let bitReader = MsbBitReader(data: Data(count: 10_485_760)) // 10 MB
14+
15+
for _ in 0..<5_000_000 * 8 {
16+
bitReader.advance()
17+
}
18+
}
19+
}
20+
1121
func testBit() {
1222
self.measure {
1323
let bitReader = MsbBitReader(data: Data(count: 10_485_760)) // 10 MB

Tests/BitByteDataTests/LsbBitReaderTests.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,21 @@ class LsbBitReaderTests: XCTestCase {
1010

1111
private static let data = Data(bytes: [0x5A, 0xD6, 0x57, 0x14, 0xAB, 0xCC, 0x2D, 0x88, 0xEA, 0x00])
1212

13+
func testAdvance() {
14+
let bitReader = LsbBitReader(data: LsbBitReaderTests.data)
15+
16+
XCTAssertEqual(bitReader.bit(), 0)
17+
bitReader.advance()
18+
XCTAssertEqual(bitReader.bit(), 0)
19+
bitReader.advance()
20+
bitReader.advance()
21+
XCTAssertEqual(bitReader.bit(), 0)
22+
bitReader.advance(by: 4)
23+
XCTAssertEqual(bitReader.bit(), 1)
24+
25+
XCTAssertFalse(bitReader.isAligned)
26+
}
27+
1328
func testBit() {
1429
let bitReader = LsbBitReader(data: LsbBitReaderTests.data)
1530

Tests/BitByteDataTests/MsbBitReaderTests.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,21 @@ class MsbBitReaderTests: XCTestCase {
1010

1111
private static let data = Data(bytes: [0x5A, 0xD6, 0x57, 0x14, 0xAB, 0xCC, 0x2D, 0x88, 0xEA, 0x00])
1212

13+
func testAdvance() {
14+
let bitReader = MsbBitReader(data: MsbBitReaderTests.data)
15+
16+
XCTAssertEqual(bitReader.bit(), 0)
17+
bitReader.advance()
18+
XCTAssertEqual(bitReader.bit(), 0)
19+
bitReader.advance()
20+
bitReader.advance()
21+
XCTAssertEqual(bitReader.bit(), 0)
22+
bitReader.advance(by: 4)
23+
XCTAssertEqual(bitReader.bit(), 0)
24+
25+
XCTAssertFalse(bitReader.isAligned)
26+
}
27+
1328
func testBit() {
1429
let bitReader = MsbBitReader(data: MsbBitReaderTests.data)
1530

Tests/LinuxMain.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ extension ByteReaderTests {
3030
extension LsbBitReaderTests {
3131
static var allTests: [(String, (LsbBitReaderTests) -> () -> Void)] {
3232
return [
33+
("testAdvance", testAdvance),
3334
("testBit", testBit),
3435
("testBits", testBits),
3536
("testIntFromBits", testIntFromBits),
@@ -57,6 +58,7 @@ extension LsbBitReaderTests {
5758
extension MsbBitReaderTests {
5859
static var allTests: [(String, (MsbBitReaderTests) -> () -> Void)] {
5960
return [
61+
("testAdvance", testAdvance),
6062
("testBit", testBit),
6163
("testBits", testBits),
6264
("testIntFromBits", testIntFromBits),
@@ -130,6 +132,7 @@ extension ByteReaderBenchmarks {
130132
extension LsbBitReaderBenchmarks {
131133
static var allTests: [(String, (LsbBitReaderBenchmarks) -> () -> Void)] {
132134
return [
135+
("testAdvance", testAdvance),
133136
("testBit", testBit),
134137
("testBits", testBits),
135138
("testIntFromBits", testIntFromBits),
@@ -144,6 +147,7 @@ extension LsbBitReaderBenchmarks {
144147
extension MsbBitReaderBenchmarks {
145148
static var allTests: [(String, (MsbBitReaderBenchmarks) -> () -> Void)] {
146149
return [
150+
("testAdvance", testAdvance),
147151
("testBit", testBit),
148152
("testBits", testBits),
149153
("testIntFromBits", testIntFromBits),

0 commit comments

Comments
 (0)