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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import CompletionScoring
import Foundation
import XCTest
class BinaryCodingTests: XCTestCase {
func roundTrip<V: BinaryCodable>(_ encoded: V) throws -> V {
try V(binaryCodedRepresentation: encoded.binaryCodedRepresentation(contentVersion: 0))
}
func testRoundTripping(_ encoded: some BinaryCodable & Equatable) {
XCTAssertNoThrow(
try {
let decoded = try roundTrip(encoded)
XCTAssertEqual(encoded, decoded)
}()
)
}
func testRoundTrippingAll(_ encodedValues: [some BinaryCodable & Equatable]) {
for encodedValue in encodedValues {
testRoundTripping(encodedValue)
}
}
func testIntegers() {
func test<I: FiniteInteger>(_ type: I.Type) {
testRoundTrippingAll([I.min, I(exactly: -1), I.zero, I(exactly: 1), I.max])
}
test(Int.self)
test(Int8.self)
test(Int16.self)
test(Int32.self)
test(Int64.self)
test(UInt.self)
test(UInt8.self)
test(UInt16.self)
test(UInt32.self)
test(UInt64.self)
}
func testFloats() {
func test<F: BinaryFloatingPoint & BinaryCodable>(_ type: F.Type) {
let values = [
F.zero, F.leastNonzeroMagnitude, F.leastNormalMagnitude, F(1), F.greatestFiniteMagnitude, F.infinity,
]
for value in values {
testRoundTripping(value)
testRoundTripping(-value)
}
let decodedNan = try? roundTrip(F.nan)
XCTAssert(decodedNan?.isNaN == true)
}
test(Float.self)
test(Double.self)
}
func testBools() {
testRoundTrippingAll([false, true])
}
func testStrings() {
testRoundTripping("a")
testRoundTrippingAll(["", "a", "é", " ", "\n", "aa"])
testRoundTrippingAll(["🤷", "🤷🏻", "🤷🏼", "🤷🏽", "🤷🏾", "🤷🏿", "🤷♀️", "🤷🏻♀️", "🤷🏼♀️", "🤷🏽♀️", "🤷🏾♀️", "🤷🏿♀️", "🤷♂️", "🤷🏻♂️", "🤷🏼♂️", "🤷🏽♂️", "🤷🏾♂️", "🤷🏿♂️"])
}
func testArrays() {
testRoundTrippingAll([[], [0], [0, 1]])
}
func testDictionaries() {
testRoundTrippingAll([[:], [0: false], [0: false, 1: true]])
}
func testSemanticClassificationComponents() {
testRoundTrippingAll([.keyword] as [CompletionKind])
testRoundTrippingAll(
[
.keyword, .enumCase, .variable, .function, .initializer, .argumentLabels, .type, .other, .unknown,
.unspecified,
] as [CompletionKind]
)
testRoundTrippingAll([.chainedMember, .commonKeywordAtCurrentPosition] as [Flair])
testRoundTrippingAll(
[
.imported(distance: 0), .imported(distance: 1), .importable, .inapplicable, .unknown, .invalid,
.unspecified,
] as [ModuleProximity]
)
testRoundTrippingAll([.none, .unspecified] as [Popularity])
testRoundTrippingAll(
[
.local, .argument, .container, .inheritedContainer, .outerContainer, .global, .inapplicable, .unknown,
.unspecified,
] as [ScopeProximity]
)
testRoundTrippingAll(
[.project(fileSystemHops: nil), .project(fileSystemHops: 1), .sdk, .inapplicable, .unknown, .unspecified]
as [StructuralProximity]
)
testRoundTrippingAll(
[.compatible, .convertible, .incompatible, .inapplicable, .unknown, .unspecified]
as [SynchronicityCompatibility]
)
testRoundTrippingAll(
[.compatible, .unrelated, .invalid, .inapplicable, .unknown, .unspecified] as [TypeCompatibility]
)
testRoundTrippingAll(
[.available, .softDeprecated, .deprecated, .unknown, .inapplicable, .unspecified] as [Availability]
)
testRoundTripping(
SemanticClassification(
availability: .softDeprecated,
completionKind: .function,
flair: .chainedCallToSuper,
moduleProximity: .importable,
popularity: .unspecified,
scopeProximity: .container,
structuralProximity: .sdk,
synchronicityCompatibility: .convertible,
typeCompatibility: .compatible
)
)
testRoundTripping(
SemanticClassification(
availability: .deprecated,
completionKind: .type,
flair: .chainedMember,
moduleProximity: .imported(distance: 2),
popularity: .none,
scopeProximity: .global,
structuralProximity: .project(fileSystemHops: 4),
synchronicityCompatibility: .compatible,
typeCompatibility: .unrelated
)
)
}
}
protocol FiniteInteger: BinaryInteger, BinaryCodable {
static var min: Self { get }
static var max: Self { get }
static var zero: Self { get }
}
extension Int: FiniteInteger {}
extension Int8: FiniteInteger {}
extension Int16: FiniteInteger {}
extension Int32: FiniteInteger {}
extension Int64: FiniteInteger {}
extension UInt: FiniteInteger {}
extension UInt8: FiniteInteger {}
extension UInt16: FiniteInteger {}
extension UInt32: FiniteInteger {}
extension UInt64: FiniteInteger {}
|