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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
|
/*
This source file is part of the Swift System open source project
Copyright (c) 2020 Apple Inc. and the Swift System project authors
Licensed under Apache License v2.0 with Runtime Library Exception
See https://swift.org/LICENSE.txt for license information
*/
// Tests for PlatformString, SystemString, and FilePath's forwarding APIs
// TODO: Adapt test to Windows
import XCTest
#if SYSTEM_PACKAGE
@testable import SystemPackage
#else
@testable import System
#endif
private func makeRaw(
_ str: String
) -> [CInterop.PlatformChar] {
var str = str
var array = str.withUTF8 { $0.withMemoryRebound(to: CChar.self, Array.init) }
array.append(0)
return array
}
private func makeStr(
_ raw: [CInterop.PlatformChar]
) -> String {
precondition((raw.last ?? 0) == 0)
// FIXME: Unfortunately, this relies on some of the same mechanisms tested...
return raw.withUnsafeBufferPointer {
String(platformString: $0.baseAddress!)
}
}
struct StringTest: TestCase {
// The error-corrected string
var string: String
// The raw contents
var raw: [CInterop.PlatformChar]
var isValid: Bool
var file: StaticString
var line: UInt
func runAllTests() {
precondition((raw.last ?? 0) == 0, "invalid test case")
// Test idempotence post-validation
string.withPlatformString {
if isValid {
let len = system_platform_strlen($0)
expectEqual(raw.count, 1+len, "Validation idempotence")
expectEqualSequence(raw, UnsafeBufferPointer(start: $0, count: 1+len),
"Validation idempotence")
}
let str = String(platformString: $0)
expectEqualSequence(
string.unicodeScalars, str.unicodeScalars, "Validation idempotence")
let validStr = String(validatingPlatformString: $0)
expectEqual(string, validStr, "Validation idempotence")
}
// Test String, SystemString, FilePath construction
let sysStr = SystemString(string)
expectEqualSequence(string.unicodeScalars, sysStr.string.unicodeScalars)
expectEqual(string, String(decoding: sysStr))
expectEqual(string, String(validating: sysStr))
let sysRaw = raw.withUnsafeBufferPointer {
SystemString(platformString: $0.baseAddress!)
}
expectEqual(string, String(decoding: sysRaw))
expectEqual(isValid, nil != String(validating: sysRaw))
expectEqual(isValid, sysStr == sysRaw)
let strDecoded = raw.withUnsafeBufferPointer {
String(platformString: $0.baseAddress!)
}
expectEqualSequence(string.unicodeScalars, strDecoded.unicodeScalars)
let strValidated = raw.withUnsafeBufferPointer {
String(validatingPlatformString: $0.baseAddress!)
}
expectEqual(isValid, strValidated != nil, "String(validatingPlatformString:)")
expectEqual(isValid, strDecoded == strValidated,
"String(validatingPlatformString:)")
// Test null insertion
let rawChars = raw.lazy.map { SystemChar($0) }
expectEqual(sysRaw, SystemString(rawChars.dropLast()))
sysRaw.withSystemChars { // TODO: assuming we want null in withSysChars
expectEqualSequence(rawChars, $0, "rawChars")
}
// Whether the content has normalized separators
let hasNormalSeparators: Bool = {
var normalSys = sysRaw
normalSys._normalizeSeparators()
return normalSys == sysRaw
}()
let fpStr = FilePath(string)
if hasNormalSeparators {
expectEqualSequence(
string.unicodeScalars, fpStr.string.unicodeScalars, "FilePath from string")
expectEqual(string, String(decoding: fpStr), "FilePath from string")
expectEqual(string, String(validating: fpStr), "FilePath from string")
expectEqual(sysStr, fpStr._storage, "FilePath from string")
}
let fpRaw = FilePath(sysRaw)
if hasNormalSeparators {
expectEqual(string, String(decoding: fpRaw), "raw FilePath")
expectEqual(isValid, nil != String(validating: fpRaw), "raw FilePath")
expectEqual(sysRaw, fpRaw._storage, "raw FilePath")
expectEqual(isValid, fpStr == fpRaw, "raw FilePath")
}
fpRaw.withPlatformString { fp0 in
fpRaw._storage.withPlatformString { storage0 in
expectEqual(fp0, storage0,
"FilePath withPlatformString address forwarding")
}
}
let isComponent = string == fpRaw.components.first?.string
if hasNormalSeparators && isComponent {
// Test FilePath.Component
let compStr = FilePath.Component(string)!
expectEqualSequence(
string.unicodeScalars, compStr.string.unicodeScalars, "Component from string")
expectEqual(string, String(decoding: compStr), "Component from string")
expectEqual(string, String(validating: compStr), "Component from string")
expectEqual(sysStr, compStr._slice.base, "Component from string")
let compRaw = FilePath.Component(sysRaw)!
expectEqual(string, String(decoding: compRaw), "raw Component")
expectEqual(isValid, nil != String(validating: compRaw), "raw Component")
expectEqual(sysRaw, compRaw._slice.base, "raw Component")
expectEqual(isValid, compStr == compRaw, "raw Component")
// TODO: Below works after we add last component optimization
// compRaw.withPlatformString { fp0 in
// compRaw.slice.base.withPlatformString { storage0 in
// expectEqual(fp0, storage0,
// "Component withPlatformString address forwarding")
// }
// }
}
sysRaw.withPlatformString {
let len = system_platform_strlen($0)
expectEqual(raw.count, 1+len, "SystemString.withPlatformString")
expectEqualSequence(raw, UnsafeBufferPointer(start: $0, count: 1+len),
"SystemString.withPlatformString")
if hasNormalSeparators {
expectEqual(sysRaw, FilePath(platformString: $0)._storage)
if isComponent {
expectEqual(
sysRaw, FilePath.Component(platformString: $0)!._slice.base)
}
}
}
}
}
extension StringTest {
static func valid(
_ str: String,
file: StaticString = #file, line: UInt = #line
) -> StringTest {
StringTest(
string: str,
raw: makeRaw(str),
isValid: true,
file: file, line: line)
}
static func invalid(
_ raw: [CInterop.PlatformChar],
file: StaticString = #file, line: UInt = #line
) -> StringTest {
StringTest(
string: makeStr(raw),
raw: raw,
isValid: false,
file: file, line: line)
}
}
final class SystemStringTest: XCTestCase {
let validCases: Array<StringTest> = [
.valid(""),
.valid("a"),
.valid("abc"),
.valid("/あ/🧟♀️"),
.valid("/あ\\🧟♀️"),
.valid("/あ\\🧟♀️///"),
.valid("あ_🧟♀️"),
.valid("/a/b/c"),
.valid("/a/b/c//"),
.valid(#"\a\b\c\\"#),
.valid("_a_b_c"),
]
#if os(Windows)
let invalidCases: Array<StringTest> = [
// TODO: Unpaired surrogates
]
#else
let invalidCases: Array<StringTest> = [
.invalid([CChar(bitPattern: 0x80), 0]),
.invalid([CChar(bitPattern: 0xFF), 0]),
.invalid([0x2F, 0x61, 0x2F, 0x62, 0x2F, CChar(bitPattern: 0x83), 0]),
]
#endif
func testPlatformString() {
for test in validCases {
test.runAllTests()
}
for test in invalidCases {
test.runAllTests()
}
}
// TODO: More exhaustive RAC+RRC SystemString tests
func testAdHoc() {
var str: SystemString = "abc"
str.append(SystemChar(ascii: "d"))
XCTAssert(str == "abcd")
XCTAssert(str.count == 4)
XCTAssert(str.count == str.length)
str.reserveCapacity(100)
XCTAssert(str == "abcd")
}
}
extension SystemStringTest {
func test_String_initWithArrayConversion() {
let source: [CInterop.PlatformChar] = [0x61, 0x62, 0, 0x63]
let str = String(platformString: source)
source.withUnsafeBufferPointer {
XCTAssertEqual(str, String(platformString: $0.baseAddress!))
}
}
@available(*, deprecated) // silence the warning for using a deprecated api
func test_String_initWithStringConversion() {
let source = "ab\0c"
var str: String
str = String(platformString: source)
source.withPlatformString {
XCTAssertEqual(str, String(platformString: $0))
}
str = String(platformString: "")
XCTAssertEqual(str.isEmpty, true)
}
@available(*, deprecated) // silence the warning for using a deprecated api
func test_String_initWithInoutConversion() {
var c: CInterop.PlatformChar = 0
let str = String(platformString: &c)
// Any other value of `c` would violate the null-terminated precondition
XCTAssertEqual(str.isEmpty, true)
}
func test_String_validatingPlatformStringWithArrayConversion() {
var source: [CInterop.PlatformChar] = [0x61, 0x62, 0, 0x63]
var str: String?
str = String(validatingPlatformString: source)
source.withUnsafeBufferPointer {
XCTAssertEqual(str, String(validatingPlatformString: $0.baseAddress!))
}
source[1] = CInterop.PlatformChar(truncatingIfNeeded: 0xffff)
str = String(validatingPlatformString: source)
XCTAssertNil(str)
}
@available(*, deprecated) // silence the warning for using a deprecated api
func test_String_validatingPlatformStringWithStringConversion() {
let source = "ab\0c"
var str: String?
str = String(validatingPlatformString: source)
XCTAssertNotNil(str)
source.withPlatformString {
XCTAssertEqual(str, String.init(validatingPlatformString: $0))
}
str = String(validatingPlatformString: "")
XCTAssertNotNil(str)
XCTAssertEqual(str?.isEmpty, true)
}
@available(*, deprecated) // silence the warning for using a deprecated api
func test_String_validatingPlatformStringWithInoutConversion() {
var c: CInterop.PlatformChar = 0
let str = String(validatingPlatformString: &c)
// Any other value of `c` would violate the null-terminated precondition
XCTAssertNotNil(str)
XCTAssertEqual(str?.isEmpty, true)
}
func test_FilePath_initWithArrayConversion() {
let source: [CInterop.PlatformChar] = [0x61, 0x62, 0, 0x63]
let path = FilePath(platformString: source)
source.withUnsafeBufferPointer {
XCTAssertEqual(path, FilePath(platformString: $0.baseAddress!))
}
}
@available(*, deprecated) // silence the warning for using a deprecated api
func test_FilePath_initWithStringConversion() {
let source = "ab\0c"
var path: FilePath
path = FilePath(platformString: source)
source.withPlatformString {
XCTAssertEqual(path, FilePath(platformString: $0))
}
path = FilePath(platformString: "")
XCTAssertEqual(path.string.isEmpty, true)
}
@available(*, deprecated) // silence the warning for using a deprecated api
func test_FilePath_initWithInoutConversion() {
var c: CInterop.PlatformChar = 0
let path = FilePath(platformString: &c)
// Any other value of `c` would violate the null-terminated precondition
XCTAssertEqual(path.string.isEmpty, true)
}
func test_FilePathComponent_initWithArrayConversion() {
var source: [CInterop.PlatformChar] = [0x61, 0x62, 0, 0x63]
var component: FilePath.Component?
component = FilePath.Component(platformString: source)
source.withUnsafeBufferPointer {
XCTAssertEqual(component, .init(platformString: $0.baseAddress!))
}
source[1] = CInterop.PlatformChar(truncatingIfNeeded: 0xffff)
component = FilePath.Component(platformString: source)
source.withUnsafeBufferPointer {
XCTAssertEqual(component, .init(platformString: $0.baseAddress!))
}
}
@available(*, deprecated) // silence the warning for using a deprecated api
func test_FilePathComponent_initWithStringConversion() {
let source = "ab\0c"
var component: FilePath.Component?
component = FilePath.Component(platformString: source)
source.withPlatformString {
XCTAssertEqual(component, FilePath.Component(platformString: $0))
}
component = FilePath.Component(platformString: "")
XCTAssertNil(component)
}
@available(*, deprecated) // silence the warning for using a deprecated api
func test_FilePathComponent_initWithInoutConversion() {
var c: CInterop.PlatformChar = 0
let component = FilePath.Component(platformString: &c)
XCTAssertNil(component)
}
func test_FilePathRoot_initWithArrayConversion() {
let source: [CInterop.PlatformChar]
#if os(Windows)
source = [0x41, 0x3a, 0x5c, 0, 0x7f]
#else // unix
source = [0x2f, 0, 0x7f]
#endif
var root: FilePath.Root?
root = FilePath.Root(platformString: source)
source.withUnsafeBufferPointer {
XCTAssertEqual(root, FilePath.Root(platformString: $0.baseAddress!))
}
}
@available(*, deprecated) // silence the warning for using a deprecated api
func test_FilePathRoot_initWithStringConversion() {
#if os(Windows)
let source = "C:\\\0 and the rest"
#else // unix
let source = "/\0 and the rest"
#endif
var root: FilePath.Root?
root = FilePath.Root(platformString: source)
source.withPlatformString {
XCTAssertEqual(root, FilePath.Root(platformString: $0))
}
root = FilePath.Root(platformString: "")
XCTAssertNil(root)
}
@available(*, deprecated) // silence the warning for using a deprecated api
func test_FilePathRoot_initWithInoutConversion() {
var c: CInterop.PlatformChar = 0
let root = FilePath.Root(platformString: &c)
XCTAssertNil(root)
}
}
|