Skip to content

Commit fa45d35

Browse files
committed
Test DecodedValue integer equality and hashing
1 parent b37ea91 commit fa45d35

1 file changed

Lines changed: 39 additions & 0 deletions

File tree

Tests/BluetoothExplorerPluginEngineTests/CoreTests.swift

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,42 @@ struct RegistryTests {
146146
#expect(battery?.fields.first?.value == .uint(50))
147147
}
148148
}
149+
150+
@Suite("DecodedValue integer semantics")
151+
struct DecodedValueTests {
152+
153+
// CBOR cannot distinguish a non-negative signed integer from an unsigned one, so a plugin's
154+
// signed field arrives as `uint` whenever its value is non-negative. Native and WASM producers
155+
// of the same number must still compare equal.
156+
157+
@Test("Non-negative int and uint compare equal")
158+
func numericEquality() {
159+
#expect(DecodedValue.int(0) == DecodedValue.uint(0))
160+
#expect(DecodedValue.int(127) == DecodedValue.uint(127))
161+
#expect(DecodedValue.uint(42) == DecodedValue.int(42))
162+
#expect(DecodedValue.int(Int64.max) == DecodedValue.uint(UInt64(Int64.max)))
163+
}
164+
165+
@Test("Negative values never equal an unsigned value")
166+
func negativeInequality() {
167+
#expect(DecodedValue.int(-1) != DecodedValue.uint(1))
168+
#expect(DecodedValue.int(-59) != DecodedValue.uint(UInt64(bitPattern: -59)))
169+
// A uint beyond Int64.max has no signed counterpart.
170+
#expect(DecodedValue.uint(UInt64(Int64.max) + 1) != DecodedValue.int(Int64.min))
171+
}
172+
173+
@Test("Equal values hash equally")
174+
func hashing() {
175+
#expect(DecodedValue.int(7).hashValue == DecodedValue.uint(7).hashValue)
176+
// Hashable's contract: equal values must collapse in a Set.
177+
#expect(Set([DecodedValue.int(7), DecodedValue.uint(7)]).count == 1)
178+
#expect(Set([DecodedValue.int(-7), DecodedValue.uint(7)]).count == 2)
179+
}
180+
181+
@Test("Other cases stay distinct")
182+
func otherCases() {
183+
#expect(DecodedValue.int(1) != DecodedValue.double(1))
184+
#expect(DecodedValue.uint(1) != DecodedValue.bool(true))
185+
#expect(DecodedValue.string("1") != DecodedValue.uint(1))
186+
}
187+
}

0 commit comments

Comments
 (0)