/
Specs.bas
493 lines (383 loc) · 13.7 KB
/
Specs.bas
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
Attribute VB_Name = "Specs"
Public Function Specs() As SpecSuite
#If Mac Then
' Mac
InlineRunner.RunSuite RunSpecs(UseNative:=False)
SpeedTest CompareToNative:=False
#Else
' Windows
InlineRunner.RunSuite RunSpecs(UseNative:=True)
InlineRunner.RunSuite RunSpecs(UseNative:=False)
SpeedTest CompareToNative:=True
#End If
End Function
Public Function RunSpecs(Optional UseNative As Boolean = False) As SpecSuite
Dim Specs As New SpecSuite
If UseNative Then
Specs.Description = "Scripting.Dictionary"
Else
Specs.Description = "VBA-Dictionary"
End If
Dim Dict As Object
Dim Items As Variant
Dim Keys As Variant
Dim Key As Variant
Dim Item As Variant
On Error Resume Next
' Properties
' ------------------------- '
With Specs.It("should get count of items")
Set Dict = CreateDictionary(UseNative)
Dict.Add "A", 123
Dict.Add "B", 3.14
Dict.Add "C", "ABC"
.Expect(Dict.Count).ToEqual 3
Dict.Remove "C"
.Expect(Dict.Count).ToEqual 2
End With
With Specs.It("should get item by key")
Set Dict = CreateDictionary(UseNative)
Dict.Add "A", 123
Dict.Add "B", 3.14
Dict.Add "C", "ABC"
.Expect(Dict.Item("B")).ToEqual 3.14
.Expect(Dict.Item("D")).ToBeEmpty
.Expect(Dict("B")).ToEqual 3.14
.Expect(Dict("D")).ToBeEmpty
End With
With Specs.It("should let/set item by key")
Set Dict = CreateDictionary(UseNative)
Dict.Add "A", 123
Dict("A") = 456
Dict.Add "B", 3.14
Dict.Add "C", "ABC"
Dict.Item("D") = True
Dict("C") = "DEF"
Set Dict.Item("B") = CreateDictionary(UseNative)
Dict.Item("B").Add "key", "B"
Set Dict("A") = CreateDictionary(UseNative)
Dict("A").Add "key", "A"
.Expect(Dict.Item("A")("key")).ToEqual "A"
.Expect(Dict.Item("B")("key")).ToEqual "B"
.Expect(Dict.Item("C")).ToEqual "DEF"
.Expect(Dict.Item("D")).ToEqual True
End With
With Specs.It("should change key")
Set Dict = CreateDictionary(UseNative)
Dict.Add "A", 123
Dict.Add "B", 3.14
Dict.Add "C", "ABC"
Dict.Key("B") = "PI"
.Expect(Dict("PI")).ToEqual 3.14
End With
With Specs.It("should use CompareMode")
Set Dict = CreateDictionary(UseNative)
Dict.CompareMode = 0
Dict.Add "A", 123
Dict("a") = 456
Dict.Add "B", 3.14
Dict.Add "C", "ABC"
.Expect(Dict("A")).ToEqual 123
.Expect(Dict("a")).ToEqual 456
Set Dict = CreateDictionary(UseNative)
Dict.CompareMode = 1
Dict.Add "A", 123
Dict("a") = 456
Dict.Add "B", 3.14
Dict.Add "C", "ABC"
.Expect(Dict("A")).ToEqual 456
.Expect(Dict("a")).ToEqual 456
End With
With Specs.It("should allow Variant for key")
Set Dict = CreateDictionary(UseNative)
Key = "A"
Dict(Key) = 123
.Expect(Dict(Key)).ToEqual 123
Key = "B"
Set Dict(Key) = CreateDictionary(UseNative)
.Expect(Dict(Key).Count).ToEqual 0
End With
' Methods
' ------------------------- '
With Specs.It("should add an item")
Set Dict = CreateDictionary(UseNative)
Dict.Add "A", 123
Dict.Add "B", 3.14
Dict.Add "C", "ABC"
Dict.Add "D", True
Dict.Add "E", Array(1, 2, 3)
Dict.Add "F", Dict
.Expect(Dict("A")).ToEqual 123
.Expect(Dict("B")).ToEqual 3.14
.Expect(Dict("C")).ToEqual "ABC"
.Expect(Dict("D")).ToEqual True
.Expect(Dict("E")(1)).ToEqual 2
.Expect(Dict("F")("C")).ToEqual "ABC"
End With
With Specs.It("should check if an item exists")
Set Dict = CreateDictionary(UseNative)
Dict.Add "Exists", 123
.Expect(Dict.Exists("Exists")).ToEqual True
.Expect(Dict.Exists("Doesn't Exist")).ToEqual False
End With
With Specs.It("should get an array of all items")
Set Dict = CreateDictionary(UseNative)
.Expect(Dict.Items).RunMatcher "Specs.ToBeAnEmptyArray", "to be an empty array"
Dict.Add "A", 123
Dict.Add "B", 3.14
Dict.Add "C", "ABC"
Dict.Add "D", True
Items = Dict.Items
.Expect(UBound(Items)).ToEqual 3
.Expect(Items(0)).ToEqual 123
.Expect(Items(3)).ToEqual True
Dict.Remove "A"
Dict.Remove "B"
Dict.Remove "C"
Dict.Remove "D"
.Expect(Dict.Items).RunMatcher "Specs.ToBeAnEmptyArray", "to be an empty array"
End With
With Specs.It("should get an array of all keys")
Set Dict = CreateDictionary(UseNative)
.Expect(Dict.Keys).RunMatcher "Specs.ToBeAnEmptyArray", "to be an empty array"
Dict.Add "A", 123
Dict.Add "B", 3.14
Dict.Add "C", "ABC"
Dict.Add "D", True
Keys = Dict.Keys
.Expect(UBound(Keys)).ToEqual 3
.Expect(Keys(0)).ToEqual "A"
.Expect(Keys(3)).ToEqual "D"
Dict.RemoveAll
.Expect(Dict.Keys).RunMatcher "Specs.ToBeAnEmptyArray", "to be an empty array"
End With
With Specs.It("should remove item")
Set Dict = CreateDictionary(UseNative)
Dict.Add "A", 123
Dict.Add "B", 3.14
Dict.Add "C", "ABC"
Dict.Add "D", True
.Expect(Dict.Count).ToEqual 4
Dict.Remove "C"
.Expect(Dict.Count).ToEqual 3
End With
With Specs.It("should remove all items")
Set Dict = CreateDictionary(UseNative)
Dict.Add "A", 123
Dict.Add "B", 3.14
Dict.Add "C", "ABC"
Dict.Add "D", True
.Expect(Dict.Count).ToEqual 4
Dict.RemoveAll
.Expect(Dict.Count).ToEqual 0
End With
' Other
' ------------------------- '
With Specs.It("should For Each over keys")
Set Dict = CreateDictionary(UseNative)
Set Keys = New Collection
For Each Key In Dict.Keys
Keys.Add Key
Next Key
.Expect(Keys.Count).ToEqual 0
Dict.Add "A", 123
Dict.Add "B", 3.14
Dict.Add "C", "ABC"
Dict.Add "D", True
Set Keys = New Collection
For Each Key In Dict.Keys
Keys.Add Key
Next Key
.Expect(Keys.Count).ToEqual 4
.Expect(Keys(1)).ToEqual "A"
.Expect(Keys(4)).ToEqual "D"
Err.Clear
End With
With Specs.It("should For Each over items")
Set Dict = CreateDictionary(UseNative)
Set Items = New Collection
For Each Item In Dict.Items
Items.Add Item
Next Item
.Expect(Items.Count).ToEqual 0
Dict.Add "A", 123
Dict.Add "B", 3.14
Dict.Add "C", "ABC"
Dict.Add "D", True
Set Items = New Collection
For Each Item In Dict.Items
Items.Add Item
Next Item
.Expect(Items.Count).ToEqual 4
.Expect(Items(1)).ToEqual 123
.Expect(Items(4)).ToEqual True
Err.Clear
End With
With Specs.It("should have UBound of -1 for empty Keys and Items")
Set Dict = CreateDictionary(UseNative)
.Expect(UBound(Dict.Keys)).ToEqual -1
.Expect(UBound(Dict.Items)).ToEqual -1
.Expect(Err.Number).ToEqual 0
Err.Clear
End With
' Errors
' ------------------------- '
Err.Clear
With Specs.It("should throw 5 when changing CompareMode with items in Dictionary")
Set Dict = CreateDictionary(UseNative)
Dict.Add "A", 123
Dict.CompareMode = vbTextCompare
.Expect(Err.Number).ToEqual 5
Err.Clear
End With
With Specs.It("should throw 457 on Add if key exists")
Set Dict = CreateDictionary(UseNative)
Dict.Add "A", 123
Dict.Add "A", 456
.Expect(Err.Number).ToEqual 457
Err.Clear
Dict.RemoveAll
Dict.Add "A", 123
Dict.Add "a", 456
.Expect(Err.Number).ToEqual 0
Err.Clear
Dict.RemoveAll
Dict.CompareMode = vbTextCompare
Dict.Add "A", 123
Dict.Add "a", 456
.Expect(Err.Number).ToEqual 457
Err.Clear
End With
With Specs.It("should throw 32811 on Remove if key doesn't exist")
Set Dict = CreateDictionary(UseNative)
Dict.Remove "A"
.Expect(Err.Number).ToEqual 32811
Err.Clear
End With
On Error GoTo 0
Set RunSpecs = Specs
End Function
Public Sub SpeedTest(Optional CompareToNative As Boolean = False)
Dim Counts As Variant
Counts = Array(5000, 5000, 5000, 5000, 7500, 7500, 7500, 7500)
Dim Baseline As Collection
If CompareToNative Then
Set Baseline = RunSpeedTest(Counts, True)
End If
Dim Results As Collection
Set Results = RunSpeedTest(Counts, False)
Debug.Print vbNewLine & "SpeedTest Results:" & vbNewLine
PrintResults "Add", Baseline, Results, 0
PrintResults "Iterate", Baseline, Results, 1
End Sub
Public Sub PrintResults(Test As String, Baseline As Collection, Results As Collection, Index As Integer)
Dim BaselineAvg As Single
Dim ResultsAvg As Single
Dim i As Integer
If Not Baseline Is Nothing Then
For i = 1 To Baseline.Count
BaselineAvg = BaselineAvg + Baseline(i)(Index)
Next i
BaselineAvg = BaselineAvg / Baseline.Count
End If
For i = 1 To Results.Count
ResultsAvg = ResultsAvg + Results(i)(Index)
Next i
ResultsAvg = ResultsAvg / Results.Count
Dim Result As String
Result = Test & ": " & Format(Round(ResultsAvg, 0), "#,##0") & " ops./s"
If Not Baseline Is Nothing Then
Result = Result & " vs. " & Format(Round(BaselineAvg, 0), "#,##0") & " ops./s "
If ResultsAvg < BaselineAvg Then
Result = Result & Format(Round(BaselineAvg / ResultsAvg, 0), "#,##0") & "x slower"
ElseIf BaselineAvg > ResultsAvg Then
Result = Result & Format(Round(ResultsAvg / BaselineAvg, 0), "#,##0") & "x faster"
End If
End If
Result = Result
If Results.Count > 1 Then
Result = Result & vbNewLine
For i = 1 To Results.Count
Result = Result & " " & Format(Round(Results(i)(Index), 0), "#,##0")
If Not Baseline Is Nothing Then
Result = Result & " vs. " & Format(Round(Baseline(i)(Index), 0), "#,##0")
End If
Result = Result & vbNewLine
Next i
End If
Debug.Print Result
End Sub
Public Function RunSpeedTest(Counts As Variant, Optional UseNative As Boolean = False) As Collection
Dim Results As New Collection
Dim CountIndex As Integer
Dim Dict As Object
Dim i As Long
Dim AddResult As Double
Dim Key As Variant
Dim Value As Variant
Dim IterateResult As Double
Dim Timer As New PreciseTimer
For CountIndex = LBound(Counts) To UBound(Counts)
Timer.StartTimer
Set Dict = CreateDictionary(UseNative)
For i = 1 To Counts(CountIndex)
Dict.Add CStr(i), i
Next i
' Convert to seconds
AddResult = Timer.TimeElapsed / 1000#
' Convert to ops./s
If AddResult > 0 Then
AddResult = Counts(CountIndex) / AddResult
Else
' Due to single precision, timer resolution is 0.01 ms, set to 0.005 ms
AddResult = Counts(CountIndex) / 0.005
End If
Timer.StartTimer
For Each Key In Dict.Keys
Value = Dict.Item(Key)
Next Key
' Convert to seconds
IterateResult = Timer.TimeElapsed / 1000#
' Convert to ops./s
If IterateResult > 0 Then
IterateResult = Counts(CountIndex) / IterateResult
Else
' Due to single precision, timer resolution is 0.01 ms, set to 0.005 ms
IterateResult = Counts(CountIndex) / 0.005
End If
Results.Add Array(AddResult, IterateResult)
Next CountIndex
Set RunSpeedTest = Results
End Function
Public Function CreateDictionary(Optional UseNative As Boolean = False) As Object
If UseNative Then
Set CreateDictionary = CreateObject("Scripting.Dictionary")
Else
Set CreateDictionary = New Dictionary
End If
End Function
Public Function ToBeAnEmptyArray(Actual As Variant) As Variant
Dim UpperBound As Long
Err.Clear
On Error Resume Next
' First, make sure it's an array
If IsArray(Actual) = False Then
' we weren't passed an array, return True
ToBeAnEmptyArray = True
Else
' Attempt to get the UBound of the array. If the array is
' unallocated, an error will occur.
UpperBound = UBound(Actual, 1)
If (Err.Number <> 0) Then
ToBeAnEmptyArray = True
Else
' Check for case of -1 UpperBound (Scripting.Dictionary.Keys/Items)
Err.Clear
If LBound(Actual) > UpperBound Then
ToBeAnEmptyArray = True
Else
ToBeAnEmptyArray = False
End If
End If
End If
End Function