-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathCBORTests.swift
115 lines (92 loc) · 2.32 KB
/
CBORTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//
// CBORTests.swift
// PotentCodables
//
// Copyright © 2021 Outfox, inc.
//
//
// Distributed under the MIT License, See LICENSE for details.
//
@testable import PotentCBOR
@testable import PotentCodables
import XCTest
class CBORTests: XCTestCase {
static var allTests = [
("testSubscriptSetter", testSubscriptSetter),
("testNestedSubscriptSetter", testNestedSubscriptSetter),
("testNestedSubscriptSetterWithNewMap", testNestedSubscriptSetterWithNewMap),
]
func testSubscriptSetter() {
var cbor: CBOR = [
"foo": 1,
"bar": "a",
"zwii": "hd",
"tags": [
"a": "1",
"b": 2,
],
]
cbor["foo"] = "changed"
XCTAssertEqual(cbor["foo"], "changed")
}
func testNestedSubscriptSetter() {
var cbor: CBOR = [
"foo": 1,
"bar": "a",
"zwii": "hd",
"tags": [
"a": "1",
"b": 2,
],
]
cbor["tags"]?[2] = "changed"
XCTAssertEqual(cbor["tags"]?[2], "changed")
}
func testNestedSubscriptSetterWithNewMap() {
var cbor: CBOR = [
"foo": 1,
"bar": "a",
"zwii": "hd",
"tags": [
"a": "1",
"b": 2,
],
]
let nestedMap: CBOR.Map = [
"joe": "schmoe",
"age": 56,
]
cbor["tags"]?[2] = CBOR.map(nestedMap)
XCTAssertEqual(cbor["tags"]?[2], CBOR.map(nestedMap))
}
func testMapKeySerializationOrder() throws {
let cbor = try CBORSerialization.data(from: [
"c": 1,
"a": 2,
"b": 3,
])
XCTAssertEqual(cbor, Data([0xA3, 0x61, 0x63, 0x01, 0x61, 0x61, 0x02, 0x61, 0x62, 0x03]))
}
func testMapKeyDeserializationOrder() throws {
let map: CBOR = [
"c": 1,
"a": 2,
"b": 3,
]
XCTAssertEqual(map, try CBORSerialization.cbor(from: Data([0xA3, 0x61, 0x63, 0x01, 0x61, 0x61, 0x02, 0x61, 0x62, 0x03])))
}
func testRandomData() throws {
struct TestStruct: Codable {
var id: [Int]
var data: Data
}
let encoded = try CBOR.Encoder.default.encode(TestStruct(id: [1, 2, 3, 4, 5], data: Data(count: 16)))
for _ in 0 ..< 10000 {
var random = Data(capacity: encoded.count)
for _ in 0 ..< encoded.count {
random.append(UInt8.random(in: 0 ..< .max))
}
XCTAssertThrowsError(try CBOR.Decoder.default.decode(TestStruct.self, from: random))
}
}
}