bitfwdcommunity / RLPSwift

@@ -16,14 +16,14 @@
Loading
16 16
    }
17 17
    
18 18
    func testEncodeLengthSmallerThanOrEqualTo55() {
19 -
        XCTAssertEqual(RLP.encodeLength(0x00, offset: 0x80), Data(bytes: [0x80]))
20 -
        XCTAssertEqual(RLP.encodeLength(0x37, offset: 0x80), Data(bytes: [0xb7]))
19 +
        XCTAssertEqual(RLP.encodeLength(0x00, offset: 0x80), Data([0x80]))
20 +
        XCTAssertEqual(RLP.encodeLength(0x37, offset: 0x80), Data([0xb7]))
21 21
    }
22 22
    
23 23
    func testEncodeLengthGreaterThan55() {
24 -
        XCTAssertEqual(RLP.encodeLength(0x38, offset: 0x80), Data(bytes: [0xb8, 0x0, 0x0, 0x0, 0x38]))
25 -
        XCTAssertEqual(RLP.encodeLength(0x0400, offset: 0x80), Data(bytes: [0xb9, 0x0, 0x0, 0x04, 0x0]))
26 -
        XCTAssertEqual(RLP.encodeLength(0x11170, offset: 0x80), Data(bytes: [0xba, 0x0, 0x01, 0x11, 0x70]))
24 +
        XCTAssertEqual(RLP.encodeLength(0x38, offset: 0x80), Data([0xb8, 0x0, 0x0, 0x0, 0x38]))
25 +
        XCTAssertEqual(RLP.encodeLength(0x0400, offset: 0x80), Data([0xb9, 0x0, 0x0, 0x04, 0x0]))
26 +
        XCTAssertEqual(RLP.encodeLength(0x11170, offset: 0x80), Data([0xba, 0x0, 0x01, 0x11, 0x70]))
27 27
    }
28 28
}
29 29
@@ -32,30 +32,30 @@
Loading
32 32
extension RLPTests {
33 33
    func testEncodeEmptyData() {
34 34
        let subject = Data()
35 -
        XCTAssertEqual(RLP.encode(subject), Data(bytes: [0x80]))
35 +
        XCTAssertEqual(RLP.encode(subject), Data([0x80]))
36 36
    }
37 37
    
38 38
    func testEncodeDataWithSingleByte() {
39 -
        XCTAssertEqual(RLP.encode(Data(bytes: [0x30])), Data(bytes: [0x30]))
40 -
        XCTAssertEqual(RLP.encode(Data(bytes: [0x61])), Data(bytes: [0x61]))
41 -
        XCTAssertEqual(RLP.encode(Data(bytes: [0x7b])), Data(bytes: [0x7b]))
39 +
        XCTAssertEqual(RLP.encode(Data([0x30])), Data([0x30]))
40 +
        XCTAssertEqual(RLP.encode(Data([0x61])), Data([0x61]))
41 +
        XCTAssertEqual(RLP.encode(Data([0x7b])), Data([0x7b]))
42 42
    }
43 43
    
44 44
    func testEncodeDataSmallerThan55Bytes() {
45 -
        XCTAssertEqual(RLP.encode(Data(bytes: [0x31, 0x30, 0x30])), Data(bytes: [0x83, 0x31, 0x30, 0x30]))
46 -
        XCTAssertEqual(RLP.encode(Data(bytes: [0x4c, 0x6f, 0x72, 0x65, 0x6d, 0x20, 0x69, 0x70, 0x73, 0x75, 0x6d, 0x20, 0x64, 0x6f, 0x6c, 0x6f, 0x72, 0x20, 0x73, 0x69, 0x74, 0x20, 0x61, 0x6d, 0x65, 0x74])), Data(bytes: [0x9a, 0x4c, 0x6f, 0x72, 0x65, 0x6d, 0x20, 0x69, 0x70, 0x73, 0x75, 0x6d, 0x20, 0x64, 0x6f, 0x6c, 0x6f, 0x72, 0x20, 0x73, 0x69, 0x74, 0x20, 0x61, 0x6d, 0x65, 0x74]))
45 +
        XCTAssertEqual(RLP.encode(Data([0x31, 0x30, 0x30])), Data([0x83, 0x31, 0x30, 0x30]))
46 +
        XCTAssertEqual(RLP.encode(Data([0x4c, 0x6f, 0x72, 0x65, 0x6d, 0x20, 0x69, 0x70, 0x73, 0x75, 0x6d, 0x20, 0x64, 0x6f, 0x6c, 0x6f, 0x72, 0x20, 0x73, 0x69, 0x74, 0x20, 0x61, 0x6d, 0x65, 0x74])), Data([0x9a, 0x4c, 0x6f, 0x72, 0x65, 0x6d, 0x20, 0x69, 0x70, 0x73, 0x75, 0x6d, 0x20, 0x64, 0x6f, 0x6c, 0x6f, 0x72, 0x20, 0x73, 0x69, 0x74, 0x20, 0x61, 0x6d, 0x65, 0x74]))
47 47
    }
48 48
    
49 49
    func testEncodeDataEqualTo55Bytes() {
50 -
        let subject = Data(bytes: [UInt8](repeating: 0x61, count: 55))
50 +
        let subject = Data([UInt8](repeating: 0x61, count: 55))
51 51
        let expectedBytes = [0xb7] + subject
52 -
        XCTAssertEqual(RLP.encode(subject), Data(bytes: expectedBytes))
52 +
        XCTAssertEqual(RLP.encode(subject), Data(expectedBytes))
53 53
    }
54 54
    
55 55
    func testEncodeTheoreticalRepresentationOfTwo() {
56 56
        let subject: [Any] = [ [], [[]], [ [], [[]] ] ]
57 57
        
58 -
        XCTAssertEqual(try RLP.encode(nestedArrayOfData: subject), Data(bytes: [0xc7, 0xc0, 0xc1, 0xc0, 0xc3, 0xc0, 0xc1, 0xc0]))
58 +
        XCTAssertEqual(try RLP.encode(nestedArrayOfData: subject), Data([0xc7, 0xc0, 0xc1, 0xc0, 0xc3, 0xc0, 0xc1, 0xc0]))
59 59
    }
60 60
}
61 61
@@ -63,29 +63,29 @@
Loading
63 63
extension RLPTests {
64 64
65 65
    func testEncodeEmptyString() {
66 -
        XCTAssertEqual(try? RLP.encode(""), Data(bytes: [0x80]))
66 +
        XCTAssertEqual(try? RLP.encode(""), Data([0x80]))
67 67
    }
68 68
    
69 69
    func testEncodeStringWithSingleByte() {
70 -
        XCTAssertEqual(try? RLP.encode("0"), Data(bytes: [0x30]))
71 -
        XCTAssertEqual(try? RLP.encode("a"), Data(bytes: [0x61]))
72 -
        XCTAssertEqual(try? RLP.encode("{"), Data(bytes: [0x7b]))
70 +
        XCTAssertEqual(try? RLP.encode("0"), Data([0x30]))
71 +
        XCTAssertEqual(try? RLP.encode("a"), Data([0x61]))
72 +
        XCTAssertEqual(try? RLP.encode("{"), Data([0x7b]))
73 73
    }
74 74
    
75 75
    func testEncodeStringSmallerThan55Bytes() {
76 -
        XCTAssertEqual(try? RLP.encode("100"), Data(bytes: [0x83, 0x31, 0x30, 0x30]))
77 -
        XCTAssertEqual(try? RLP.encode("Lorem ipsum dolor sit amet"), Data(bytes: [0x9a, 0x4c, 0x6f, 0x72, 0x65, 0x6d, 0x20, 0x69, 0x70, 0x73, 0x75, 0x6d, 0x20, 0x64, 0x6f, 0x6c, 0x6f, 0x72, 0x20, 0x73, 0x69, 0x74, 0x20, 0x61, 0x6d, 0x65, 0x74]))
76 +
        XCTAssertEqual(try? RLP.encode("100"), Data([0x83, 0x31, 0x30, 0x30]))
77 +
        XCTAssertEqual(try? RLP.encode("Lorem ipsum dolor sit amet"), Data([0x9a, 0x4c, 0x6f, 0x72, 0x65, 0x6d, 0x20, 0x69, 0x70, 0x73, 0x75, 0x6d, 0x20, 0x64, 0x6f, 0x6c, 0x6f, 0x72, 0x20, 0x73, 0x69, 0x74, 0x20, 0x61, 0x6d, 0x65, 0x74]))
78 78
    }
79 79
    
80 80
    func testEncodeStringEqualTo55Bytes() {
81 81
        let subject = [String](repeating: "a", count: 55).reduce("", +)
82 82
        let expectedBytes: [UInt8] = [0xb7] + [UInt8](repeating: 0x61, count: 55)
83 -
        XCTAssertEqual(try? RLP.encode(subject), Data(bytes: expectedBytes))
83 +
        XCTAssertEqual(try? RLP.encode(subject), Data(expectedBytes))
84 84
    }
85 85
86 86
    func testEncodeStringGreaterThan55Bytes() {
87 87
        let stringWith400a = [String](repeating: "a", count: 400).reduce("", +)
88 88
        let expectedBytes: [UInt8] = [0xb9, 0x00, 0x00, 0x01, 0x90] + [UInt8](repeating: 0x61, count: 400)
89 -
        XCTAssertEqual(try? RLP.encode(stringWith400a), Data(bytes: expectedBytes))
89 +
        XCTAssertEqual(try? RLP.encode(stringWith400a), Data(expectedBytes))
90 90
    }
91 91
}

@@ -29,19 +29,19 @@
Loading
29 29
    static func encodeLength(_ length: UInt32, offset: UInt8) -> Data {
30 30
        if length < 56 {
31 31
            let lengthByte = offset + UInt8(length)
32 -
            return Data(bytes: [lengthByte])
32 +
            return Data([lengthByte])
33 33
        } else {
34 34
            let firstByte = offset + 55 + binaryLength(of: length)
35 35
            var bytes = [firstByte]
36 36
            bytes.append(contentsOf: length.byteArrayLittleEndian)
37 -
            return Data(bytes: bytes)
37 +
            return Data(bytes)
38 38
        }
39 39
    }
40 40
}
41 41
42 42
// MARK: Data encoding
43 43
public extension RLP {
44 -
    public static func encode(_ data: Data) -> Data {
44 +
    static func encode(_ data: Data) -> Data {
45 45
        if data.count == 1,
46 46
            0x00...0x7f ~= data[0] {
47 47
            return data
@@ -52,7 +52,7 @@
Loading
52 52
        }
53 53
    }
54 54
    
55 -
    public static func encode(nestedArrayOfData array: [Any]) throws -> Data {
55 +
    static func encode(nestedArrayOfData array: [Any]) throws -> Data {
56 56
        var output = Data()
57 57
        
58 58
        for item in array {
@@ -72,7 +72,7 @@
Loading
72 72
73 73
// MARK: String encoding
74 74
public extension RLP {
75 -
    public static func encode(_ string: String, with encoding: String.Encoding = .ascii) throws -> Data {
75 +
    static func encode(_ string: String, with encoding: String.Encoding = .ascii) throws -> Data {
76 76
        guard let data = string.data(using: encoding) else {
77 77
            throw Error.stringToData
78 78
        }
@@ -82,7 +82,7 @@
Loading
82 82
        return bytes
83 83
    }
84 84
    
85 -
    public static func encode(nestedArrayOfString array: [Any], encodeStringsWith encoding: String.Encoding = .ascii) throws -> Data {
85 +
    static func encode(nestedArrayOfString array: [Any], encodeStringsWith encoding: String.Encoding = .ascii) throws -> Data {
86 86
        var output = Data()
87 87
        
88 88
        for item in array {
Files Coverage
Source 67.19%
Tests 100.00%
Project Totals (4 files) 81.74%
15.1
TRAVIS_RUBY_VERSION=default
TRAVIS_OS_NAME=osx

No yaml found.

Create your codecov.yml to customize your Codecov experience

Sunburst
The inner-most circle is the entire project, moving away from the center are folders then, finally, a single file. The size and color of each slice is representing the number of statements and the coverage, respectively.
Icicle
The top section represents the entire project. Proceeding with folders and finally individual files. The size and color of each slice is representing the number of statements and the coverage, respectively.
Grid
Each block represents a single file in the project. The size and color of each block is represented by the number of statements and the coverage, respectively.
Loading