Files
swift-mirror/test/stdlib/NSSlowString.swift
Michael Ilseman a3e892fca5 [stdlib] Relax ephemeralString sanity check
Creating an ephemeral string from a non-contiguous substring does not
reuse the same storage. Relax a sanity check assuming that it did.
2017-06-28 15:48:17 -07:00

79 lines
2.6 KiB
Swift
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// RUN: %target-clang -fobjc-arc %S/Inputs/NSSlowString/NSSlowString.m -c -o %t/NSSlowString.o
// RUN: %target-build-swift -I %S/Inputs/NSSlowString/ %t/NSSlowString.o %s -o %t/a.out
// RUN: %target-run %t/a.out
// REQUIRES: executable_test
// REQUIRES: objc_interop
import Foundation
import NSSlowString
import Swift
import StdlibUnittest
let tests = TestSuite("NonContiguousStrings")
// Perform expected test checks
func checkSingleForm<S: StringProtocol>(
_ s: S, expectedCount: Int, expectedCodeUnitCount: Int?
) {
expectEqual(expectedCount, Int(s.count))
if let cuCount = expectedCodeUnitCount {
expectEqual(cuCount, Int(s.utf16.count))
}
// Now check various reversed properties
let reversedCharacters = Array<Character>(s.reversed())
expectEqual(Int(s.count), reversedCharacters.count)
expectEqualSequence(s.reversed(), reversedCharacters)
expectEqual(String(s), String(reversedCharacters.reversed()))
}
func check(
_ s: String, expectedCount count: Int, expectedCodeUnitCount cuCount: Int
) {
checkSingleForm(s, expectedCount: count, expectedCodeUnitCount: cuCount)
// Substring tests
checkSingleForm(s[...], expectedCount: count, expectedCodeUnitCount: cuCount)
checkSingleForm(s.dropFirst(), expectedCount: count-1, expectedCodeUnitCount: nil)
checkSingleForm(s.dropLast(), expectedCount: count-1, expectedCodeUnitCount: nil)
checkSingleForm(s.dropLast().dropFirst(), expectedCount: count-2, expectedCodeUnitCount: nil)
}
tests.test("Unicode 9 grapheme breaking") {
// Test string lengths that correspond to smaller than our fixed size code
// unit buffer, larger than it, and exactly it.
let strSmall = NSSlowString(string: "a👍👩👩👧👦")
let strBig = NSSlowString(string: "abcdefg👍👩👩👧👦")
let strJustRight = NSSlowString(string: "abc👍👩👩👧👦")
check(strSmall as String, expectedCount: 3, expectedCodeUnitCount: 14)
check(strBig as String, expectedCount: 9, expectedCodeUnitCount: 20)
check(strJustRight as String, expectedCount: 5, expectedCodeUnitCount: 16)
}
tests.test("Zalgo") {
// Check that we handle absurdly long graphemes
var zalgo = "a👩👩👧👦c"
for combo in 0x300...0x36f {
zalgo.append(String(UnicodeScalar(combo)!))
}
check(
NSSlowString(string: zalgo) as String,
expectedCount: 3,
expectedCodeUnitCount: 125
)
// Check for interspersed zalgo and emoji
var megaZalgo = zalgo + zalgo + zalgo + zalgo
check(
NSSlowString(string: megaZalgo) as String,
expectedCount: megaZalgo.count,
expectedCodeUnitCount: megaZalgo.utf16.count
)
}
runAllTests()