File: NSSlowString.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (118 lines) | stat: -rw-r--r-- 4,462 bytes parent folder | download
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
// RUN: mkdir -p %t
// 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-codesign %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("Iterator") {
  let native = "abcπŸ‘πŸ‘©β€πŸ‘©β€πŸ‘§β€πŸ‘¦de\u{0301}f"
  let opaque = NSSlowString(string: "abcπŸ‘πŸ‘©β€πŸ‘©β€πŸ‘§β€πŸ‘¦de\u{0301}f") as String
  expectEqualSequence(opaque, native)
  expectEqualSequence(opaque.unicodeScalars, native.unicodeScalars)
  expectEqualSequence(opaque.utf16, native.utf16)
  expectEqualSequence(opaque.utf8, native.utf8)

  expectEqualSequence(opaque.reversed(), native.reversed())
  expectEqualSequence(opaque.unicodeScalars.reversed(), native.unicodeScalars.reversed())
  expectEqualSequence(opaque.utf16.reversed(), native.utf16.reversed())
  expectEqualSequence(opaque.utf8.reversed(), native.utf8.reversed())
}

tests.test("String-to-integer parsing") {
  let native = "1234"
  let opaque = NSSlowString(string: "1234") as String
  
  expectEqual(Int(opaque, radix: 16)!, Int(native, radix: 16)!)
  expectEqual(Int(opaque, radix: 15)!, Int(native, radix: 15)!)
  expectEqual(Int(opaque, radix: 10)!, Int(native, radix: 10)!)
  expectEqual(Int(opaque, radix:  8)!, Int(native, radix:  8)!)
  expectEqual(Int(opaque, radix:  5)!, Int(native, radix:  5)!)
  
  expectEqual(UInt16(opaque, radix: 16)!, UInt16(native, radix: 16)!)
  expectEqual(UInt16(opaque, radix: 15)!, UInt16(native, radix: 15)!)
  expectEqual(UInt16(opaque, radix: 10)!, UInt16(native, radix: 10)!)
  expectEqual(UInt16(opaque, radix:  8)!, UInt16(native, radix:  8)!)
  expectEqual(UInt16(opaque, radix:  5)!, UInt16(native, radix:  5)!)
}

tests.test("Unicode 9 grapheme breaking")
    .xfail(.osxMinor(10, 9, reason: "Mac OS X 10.9 has an old version of ICU"))
    .xfail(.iOSMajor(7, reason: "iOS 7 has an old version of ICU"))
    .code {

	// 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")
    .xfail(.osxMinor(10, 9, reason: "Mac OS X 10.9 has an old version of ICU"))
    .xfail(.iOSMajor(7, reason: "iOS 7 has an old version of ICU"))
    .code {

	// 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()