-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathAGStringTests.swift
More file actions
141 lines (115 loc) · 4.4 KB
/
AGStringTests.swift
File metadata and controls
141 lines (115 loc) · 4.4 KB
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
//
// AGStringTests.swift
// AGStringTests
//
// Created by AGString on 2019. 11. 3..
// Copyright © 2019 AGString. All rights reserved.
//
@testable import AGString
import XCTest
class AGStringTests: XCTestCase {
static var allTests = [
("testExample", testExample),
]
func testExample() {
var str = "Hello, World!"
XCTAssert(str[1] == "e")
XCTAssert(str[1..<3] == "el")
XCTAssert(str[7...9] == "Wor")
XCTAssert(str[7...] == "World!")
XCTAssert(str[..<5] == "Hello")
XCTAssert(str[...5] == "Hello,")
str[1] = "a"
XCTAssert(str == "Hallo, World!")
str[1..<6] = "uge"
XCTAssert(str == "Huge World!")
str[5...7] = "Go"
XCTAssert(str == "Huge Gold!")
str[9...] = "!!!"
XCTAssert(str == "Huge Gold!!!")
str[..<4] = "Good"
XCTAssert(str == "Good Gold!!!")
str[...3] = "Goodbye"
XCTAssert(str == "Goodbye Gold!!!")
}
func testZfill() {
let str = "abc"
let str2 = str.zfill(2)
XCTAssert(str2 == "abc")
let str3 = str.zfill(3)
XCTAssert(str3 == "abc")
let str4 = str.zfill(10)
XCTAssert(str4 == "0000000abc")
let str5 = str.zfill(10, with: "x")
XCTAssert(str5 == "xxxxxxxabc")
}
func testOccurence() {
let str = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
XCTAssert(str.occurence(of: " ") == 68)
let str2 = String(repeating: "a", count: 1000)
measure {
str2.occurence(of: "a")
}
}
func testLtrim() {
XCTAssert(" abcd ".trimLeft() == "abcd ")
XCTAssert(" abcd".trimLeft() == "abcd")
XCTAssert("".trimLeft() == "")
XCTAssert("abcd ".trimLeft() == "abcd ")
XCTAssert("Hello, World!".trimLeft() == "Hello, World!")
let str = " a"
measure {
str.trimLeft()
}
}
func testRtrim() {
XCTAssert(" abcd ".trimRight() == " abcd")
XCTAssert(" abcd".trimRight() == " abcd")
XCTAssert("".trimRight() == "")
XCTAssert("abcd ".trimRight() == "abcd")
XCTAssert("Hello, World!".trimRight() == "Hello, World!")
let str = "a "
measure {
str.trimRight()
}
}
func testTrim() {
XCTAssert(" abcd ".trim() == "abcd")
XCTAssert(" abcd".trim() == "abcd")
XCTAssert("".trim() == "")
XCTAssert("abcd ".trim() == "abcd")
XCTAssert("Hello, World!".trim() == "Hello, World!")
let str = " a "
measure {
str.trim()
}
}
func testSliced() {
XCTAssertEqual("abcdef".sliced(from: "a", to: "d"), "abcd")
XCTAssertEqual("abcdef".sliced(from: "a", to: "a"), "a")
XCTAssertEqual("abcdef".sliced(from: "a", to: "d", inversed: true), "dcba")
XCTAssertEqual("abcdef".sliced(from: "a", to: "a", inversed: true), "a")
XCTAssertEqual("abcdef".sliced(from: "a", to: "g"), "")
XCTAssertEqual("abcdef".sliced(from: "g", to: "g"), "")
}
func testSlice() {
var string = "abcdef"
string.slice(from: "a", to: "d")
XCTAssertEqual(string, "abcd")
string = "abcdef"
string.slice(from: "a", to: "a")
XCTAssertEqual(string, "a")
string = "abcdef"
string.slice(from: "a", to: "d", inversed: true)
XCTAssertEqual(string, "dcba")
string = "abcdef"
string.slice(from: "a", to: "a", inversed: true)
XCTAssertEqual(string, "a")
string = "abcdef"
string.slice(from: "a", to: "g")
XCTAssertEqual(string, "")
string = "abcdef"
string.slice(from: "g", to: "g")
XCTAssertEqual(string, "")
}
}