forked from swiftlang/swift
- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSmallStringTestUtilities.swift
124 lines (110 loc) · 3.3 KB
/
SmallStringTestUtilities.swift
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
#if _runtime(_ObjC)
import Foundation
#endif
import StdlibUnittest
extensionString:Error{}
func verifySmallString(_ small:_SmallString, _ input:String,
file:String= #file, line:UInt= #line
){
letloc=SourceLoc(file, line, comment:"test data")
expectEqual(_SmallString.capacity, small.count + small.unusedCapacity,
stackTrace:SourceLocStack().with(loc))
lettiny=Array(input.utf8)
expectEqual(tiny.count, small.count, stackTrace:SourceLocStack().with(loc))
for(lhs, rhs)inzip(tiny, small){
expectEqual(lhs, rhs, stackTrace:SourceLocStack().with(loc))
}
letsmallFromUTF16=_SmallString(Array(input.utf16))
expectNotNil(smallFromUTF16,
stackTrace:SourceLocStack().with(loc))
expectEqualSequence(small, smallFromUTF16!,
stackTrace:SourceLocStack().with(loc))
// Test slicing
//
foriin0..<small.count {
forjin i...small.count {
expectEqualSequence(tiny[i..<j],small[i..<j],
stackTrace:SourceLocStack().with(loc))
if j < small.count {
expectEqualSequence(tiny[i...j],small[i...j],
stackTrace:SourceLocStack().with(loc))
}
}
}
// Test RAC and Mutable
varcopy= small
foriin0..<small.count /2{
lettmp=copy[i]
copy[i]=copy[copy.count -1- i]
copy[copy.count -1- i]= tmp
}
expectEqualSequence(small.reversed(), copy,
stackTrace:SourceLocStack().with(loc))
}
// Testing helper inits
extension_SmallString{
init?(_ codeUnits:Array<UInt8>){
guardlet smol = codeUnits.withUnsafeBufferPointer({
return_SmallString($0)
})else{
returnnil
}
self= smol
}
init?(_ codeUnits:Array<UInt16>){
letstr= codeUnits.withUnsafeBufferPointer{
returnString._uncheckedFromUTF16($0)
}
if !str._guts.isSmall {
returnnil
}
self.init(str._guts._object)
}
#if _runtime(_ObjC)
init?(_cocoaString ns:NSString){
#if _pointerBitWidth(_32)
returnnil
#else
guard_isObjCTaggedPointer(ns)else{returnnil}
self=(ns asString)._guts.asSmall //regular tagged NSStrings are guaranteed to bridge to SmallStrings
assert(_StringGuts(self).isSmall)
#endif
}
#endif
func _appending(_ other:_SmallString)->_SmallString?{
return_SmallString(self, appending: other)
}
func _repeated(_ n:Int)->_SmallString?{
varbase=self
lettoAppend=self
for_in0..<(n &-1){
guardlet s =_SmallString(
base, appending: toAppend)
else{returnnil}
base = s
}
return base
}
}
func expectSmall(_ str:String,
stackTrace:SourceLocStack=SourceLocStack(),
showFrame:Bool=true,
file:String= #file, line:UInt= #line
){
switch str._classify()._form {
case._small:return
default:expectationFailure("expected: small", trace:"",
stackTrace: stackTrace.pushIf(showFrame, file: file, line: line))
}
}
func expectCocoa(_ str:String,
stackTrace:SourceLocStack=SourceLocStack(),
showFrame:Bool=true,
file:String= #file, line:UInt= #line
){
switch str._classify()._form {
case._cocoa:return
default:expectationFailure("expected: cocoa", trace:"",
stackTrace: stackTrace.pushIf(showFrame, file: file, line: line))
}
}