forked from dotnet/fsharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringModule.fs
308 lines (224 loc) · 9.11 KB
/
StringModule.fs
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
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.
namespace FSharp.Core.UnitTests.Collections
open System
open NUnit.Framework
open FSharp.Core.UnitTests.LibraryTestFx
// Various tests for the:
// Microsoft.FSharp.Collections.seq type
(*
[Test Strategy]
Make sure each method works on:
* Few charachter string ("foo")
* Empty string ("")
* Null string (null)
*)
[<TestFixture>]
type StringModule() =
[<Test>]
member this.Concat() =
let e1 = String.concat null ["foo"]
Assert.AreEqual("foo", e1)
let e2 = String.concat "" []
Assert.AreEqual("", e2)
let e3 = String.concat "foo" []
Assert.AreEqual("", e3)
let e4 = String.concat "" [null]
Assert.AreEqual("", e4)
let e5 = String.concat "" [""]
Assert.AreEqual("", e5)
let e6 = String.concat "foo" ["bar"]
Assert.AreEqual("bar", e6)
let e7 = String.concat "foo" ["bav";"baz"]
Assert.AreEqual("bavfoobaz", e7)
let e8 = String.concat "foo" [null;"baz";null;"bar"]
Assert.AreEqual("foobazfoofoobar", e8)
CheckThrowsArgumentNullException(fun () -> String.concat "foo" null |> ignore)
[<Test>]
member this.Iter() =
let result = ref 0
do String.iter (fun c -> result := !result + (int c)) "foo"
Assert.AreEqual(324, !result)
do result := 0
do String.iter (fun c -> result := !result + (int c)) null
Assert.AreEqual(0, !result)
[<Test>]
member this.IterI() =
let result = ref 0
do String.iteri(fun i c -> result := !result + (i*(int c))) "foo"
Assert.AreEqual(333, !result)
result := 0
do String.iteri(fun i c -> result := !result + (i*(int c))) null
Assert.AreEqual(0, !result)
[<Test>]
member this.Map() =
let e1 = String.map (fun c -> c) "foo"
Assert.AreEqual("foo", e1)
let e2 = String.map (fun c -> c) null
Assert.AreEqual("", e2)
[<Test>]
member this.MapI() =
let e1 = String.mapi (fun i c -> char(int c + i)) "foo"
Assert.AreEqual("fpq", e1)
let e2 = String.mapi (fun i c -> c) null
Assert.AreEqual("", e2)
[<Test>]
member this.Filter() =
let e1 = String.filter (fun c -> true) "foo"
Assert.AreEqual("foo", e1)
let e2 = String.filter (fun c -> true) null
Assert.AreEqual("", e2)
let e3 = String.filter (fun c -> c <> 'o') "foo bar"
Assert.AreEqual("f bar", e3)
let e4 = String.filter (fun c -> c <> 'o') ""
Assert.AreEqual("", e4)
[<Test>]
member this.Collect() =
let e1 = String.collect (fun c -> "a"+string c) "foo"
Assert.AreEqual("afaoao", e1)
let e2 = String.collect (fun c -> null) "hello"
Assert.AreEqual("", e2)
let e3 = String.collect (fun c -> "") null
Assert.AreEqual("", e3)
[<Test>]
member this.Init() =
let e1 = String.init 0 (fun i -> "foo")
Assert.AreEqual("", e1)
let e2 = String.init 2 (fun i -> "foo"+string(i))
Assert.AreEqual("foo0foo1", e2)
let e3 = String.init 2 (fun i -> null)
Assert.AreEqual("", e3)
CheckThrowsArgumentException(fun () -> String.init -1 (fun c -> "") |> ignore)
[<Test>]
member this.Replicate() =
let e1 = String.replicate 0 "Snickersnee"
Assert.AreEqual("", e1)
let e2 = String.replicate 2 "Collywobbles, "
Assert.AreEqual("Collywobbles, Collywobbles, ", e2)
let e3 = String.replicate 2 null
Assert.AreEqual("", e3)
let e4 = String.replicate 300_000 ""
Assert.AreEqual("", e4)
let e5 = String.replicate 23 "天地玄黃,宇宙洪荒。"
Assert.AreEqual(230 , e5.Length)
Assert.AreEqual("天地玄黃,宇宙洪荒。天地玄黃,宇宙洪荒。", e5.Substring(0, 20))
// This tests the cut-off point for the O(log(n)) algorithm with a prime number
let e6 = String.replicate 84673 "!!!"
Assert.AreEqual(84673 * 3, e6.Length)
// This tests the cut-off point for the O(log(n)) algorithm with a 2^x number
let e7 = String.replicate 1024 "!!!"
Assert.AreEqual(1024 * 3, e7.Length)
let e8 = String.replicate 1 "What a wonderful world"
Assert.AreEqual("What a wonderful world", e8)
let e9 = String.replicate 3 "أضعت طريقي! أضعت طريقي" // means: I'm lost
Assert.AreEqual("أضعت طريقي! أضعت طريقيأضعت طريقي! أضعت طريقيأضعت طريقي! أضعت طريقي", e9)
let e10 = String.replicate 4 "㏖ ㏗ ℵ "
Assert.AreEqual("㏖ ㏗ ℵ ㏖ ㏗ ℵ ㏖ ㏗ ℵ ㏖ ㏗ ℵ ", e10)
let e11 = String.replicate 5 "5"
Assert.AreEqual("55555", e11)
CheckThrowsArgumentException(fun () -> String.replicate -1 "foo" |> ignore)
[<Test>]
member this.Forall() =
let e1 = String.forall (fun c -> true) ""
Assert.AreEqual(true, e1)
let e2 = String.forall (fun c -> c='o') "foo"
Assert.AreEqual(false, e2)
let e3 = String.forall (fun c -> true) "foo"
Assert.AreEqual(true, e3)
let e4 = String.forall (fun c -> false) "foo"
Assert.AreEqual(false, e4)
let e5 = String.forall (fun c -> true) (String.replicate 1000000 "x")
Assert.AreEqual(true, e5)
let e6 = String.forall (fun c -> false) null
Assert.AreEqual(true, e6)
[<Test>]
member this.Exists() =
let e1 = String.exists (fun c -> true) ""
Assert.AreEqual(false, e1)
let e2 = String.exists (fun c -> c='o') "foo"
Assert.AreEqual(true, e2)
let e3 = String.exists (fun c -> true) "foo"
Assert.AreEqual(true, e3)
let e4 = String.exists (fun c -> false) "foo"
Assert.AreEqual(false, e4)
let e5 = String.exists (fun c -> false) (String.replicate 1000000 "x")
Assert.AreEqual(false, e5)
[<Test>]
member this.Length() =
let e1 = String.length ""
Assert.AreEqual(0, e1)
let e2 = String.length "foo"
Assert.AreEqual(3, e2)
let e3 = String.length null
Assert.AreEqual(0, e3)
[<Test>]
member this.``Slicing with both index reverse behaves as expected``() =
let str = "abcde"
Assert.That(str.[^3..^1], Is.EquivalentTo(str.[1..3]))
[<Test>]
member this.``Indexer with reverse index behaves as expected``() =
let str = "abcde"
Assert.That(str.[^1], Is.EqualTo('d'))
[<Test>]
member this.SlicingUnboundedEnd() =
let str = "123456"
Assert.AreEqual(str.[-1..], str)
Assert.AreEqual(str.[0..], str)
Assert.AreEqual(str.[1..], "23456")
Assert.AreEqual(str.[2..], "3456")
Assert.AreEqual(str.[5..], "6")
Assert.AreEqual(str.[6..], (""))
Assert.AreEqual(str.[7..], (""))
[<Test>]
member this.SlicingUnboundedStart() =
let str = "123456"
Assert.AreEqual(str.[..(-1)], (""))
Assert.AreEqual(str.[..0], "1")
Assert.AreEqual(str.[..1], "12")
Assert.AreEqual(str.[..2], "123")
Assert.AreEqual(str.[..3], "1234")
Assert.AreEqual(str.[..4], "12345")
Assert.AreEqual(str.[..5], "123456")
Assert.AreEqual(str.[..6], "123456")
Assert.AreEqual(str.[..7], "123456")
[<Test>]
member this.SlicingBoundedStartEnd() =
let str = "123456"
Assert.AreEqual(str.[*], str)
Assert.AreEqual(str.[0..0], "1")
Assert.AreEqual(str.[0..1], "12")
Assert.AreEqual(str.[0..2], "123")
Assert.AreEqual(str.[0..3], "1234")
Assert.AreEqual(str.[0..4], "12345")
Assert.AreEqual(str.[0..5], "123456")
Assert.AreEqual(str.[1..1], "2")
Assert.AreEqual(str.[1..2], "23")
Assert.AreEqual(str.[1..3], "234")
Assert.AreEqual(str.[1..4], "2345")
Assert.AreEqual(str.[1..5], "23456")
Assert.AreEqual(str.[0..1], "12")
Assert.AreEqual(str.[1..1], "2")
Assert.AreEqual(str.[2..1], (""))
Assert.AreEqual(str.[3..1], (""))
Assert.AreEqual(str.[4..1], (""))
[<Test>]
member this.SlicingEmptyString() =
let empty = ""
Assert.AreEqual(empty.[*], (""))
Assert.AreEqual(empty.[5..3], (""))
Assert.AreEqual(empty.[0..], (""))
Assert.AreEqual(empty.[0..0], (""))
Assert.AreEqual(empty.[0..1], (""))
Assert.AreEqual(empty.[3..5], (""))
[<Test>]
member this.SlicingOutOfBounds() =
let str = "123456"
Assert.AreEqual(str.[..6], "123456")
Assert.AreEqual(str.[6..], (""))
Assert.AreEqual(str.[0..(-1)], (""))
Assert.AreEqual(str.[1..(-1)], (""))
Assert.AreEqual(str.[1..0], (""))
Assert.AreEqual(str.[0..6], "123456")
Assert.AreEqual(str.[1..6], "23456")
Assert.AreEqual(str.[-1..1], "12")
Assert.AreEqual(str.[-3..(-4)], (""))
Assert.AreEqual(str.[-4..(-3)], (""))