palate 0.3.9

File type detection combining tft and hyperpolyglot
Documentation
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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
Attribute VB_Name = "Specs"
Private pForDisplay As Boolean
Private pUseNative As Boolean

Public Sub SpeedTest()
    #If Mac Then
        ' Mac
        ExecuteSpeedTest CompareToNative:=False
    #Else
        ' Windows
        ExecuteSpeedTest CompareToNative:=True
    #End If
End Sub

Sub ToggleNative(Optional Enabled As Boolean = True)
    Dim Code As CodeModule
    Dim Lines As Variant
    Dim i As Integer
    
    Set Code = ThisWorkbook.VBProject.VBComponents("Dictionary").CodeModule
    Lines = Split(Code.Lines(1, 50), vbNewLine)
    
    For i = 0 To UBound(Lines)
        If InStr(1, Lines(i), "#Const UseScriptingDictionaryIfAvailable") Then
            Code.ReplaceLine i + 1, "#Const UseScriptingDictionaryIfAvailable = " & Enabled
            Exit Sub
        End If
    Next i
End Sub

Public Sub RunSpecs()
    DisplayRunner.IdCol = 1
    DisplayRunner.DescCol = 1
    DisplayRunner.ResultCol = 2
    DisplayRunner.OutputStartRow = 4
    
    pForDisplay = True
    DisplayRunner.RunSuite Specs()
    pForDisplay = False
End Sub

Public Function Specs() As SpecSuite
    Dim UseNative As Boolean

#If Mac Then
    UseNative = False
#Else
    If pUseNative Then
        UseNative = True
        pUseNative = False
    Else
        If Not pForDisplay Then
            ' Run native specs first
            pUseNative = True
            Specs
        End If
        
        UseNative = False
    End If
#End If

    Set Specs = 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
    Dim A As New Collection
    Dim B As New Dictionary
    
    ' 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 item by key")
        Set Dict = CreateDictionary(UseNative)
        
        Dict.Add "A", 123
        Dict.Add "B", 3.14
        Dict.Add "C", "ABC"
        
        ' Let + New
        Dict("D") = True
        
        ' Let + Replace
        Dict("A") = 456
        Dict("B") = 3.14159
        
        ' Should have correct values
        .Expect(Dict("A")).ToEqual 456
        .Expect(Dict("B")).ToEqual 3.14159
        .Expect(Dict("C")).ToEqual "ABC"
        .Expect(Dict("D")).ToEqual True
        
        ' Should have correct order
        .Expect(Dict.Keys()(0)).ToEqual "A"
        .Expect(Dict.Keys()(1)).ToEqual "B"
        .Expect(Dict.Keys()(2)).ToEqual "C"
        .Expect(Dict.Keys()(3)).ToEqual "D"
    End With
    
    With Specs.It("should set item by key")
        Set Dict = CreateDictionary(UseNative)

        Dict.Add "A", 123
        Dict.Add "B", 3.14
        Dict.Add "C", "ABC"

        ' Set + New
        Set Dict("D") = CreateDictionary(UseNative)
        Dict("D").Add "key", "D"

        ' Set + Replace
        Set Dict("A") = CreateDictionary(UseNative)
        Dict("A").Add "key", "A"
        Set Dict("B") = CreateDictionary(UseNative)
        Dict("B").Add "key", "B"

        ' Should have correct values
        .Expect(Dict.Item("A")("key")).ToEqual "A"
        .Expect(Dict.Item("B")("key")).ToEqual "B"
        .Expect(Dict.Item("C")).ToEqual "ABC"
        .Expect(Dict.Item("D")("key")).ToEqual "D"

        ' Should have correct order
        .Expect(Dict.Keys()(0)).ToEqual "A"
        .Expect(Dict.Keys()(1)).ToEqual "B"
        .Expect(Dict.Keys()(2)).ToEqual "C"
        .Expect(Dict.Keys()(3)).ToEqual "D"
    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
    
    With Specs.It("should handle numeric keys")
        Set Dict = CreateDictionary(UseNative)
        
        Dict.Add 3, 1
        Dict.Add 2, 2
        Dict.Add 1, 3
        Dict.Add "3", 4
        Dict.Add "2", 5
        Dict.Add "1", 6

        .Expect(Dict(3)).ToEqual 1
        .Expect(Dict(2)).ToEqual 2
        .Expect(Dict(1)).ToEqual 3
        .Expect(Dict("3")).ToEqual 4
        .Expect(Dict("2")).ToEqual 5
        .Expect(Dict("1")).ToEqual 6
        
        .Expect(Dict.Keys()(0)).ToEqual 3
        .Expect(Dict.Keys()(1)).ToEqual 2
        .Expect(Dict.Keys()(2)).ToEqual 1
        .Expect(TypeName(Dict.Keys()(0))).ToEqual "Integer"
        .Expect(TypeName(Dict.Keys()(1))).ToEqual "Integer"
        .Expect(TypeName(Dict.Keys()(2))).ToEqual "Integer"
    End With
    
    With Specs.It("should handle boolean keys")
        Set Dict = CreateDictionary(UseNative)
        
        Dict.Add True, 1
        Dict.Add False, 2
        
        .Expect(Dict(True)).ToEqual 1
        .Expect(Dict(False)).ToEqual 2
        
        .Expect(Dict.Keys()(0)).ToEqual True
        .Expect(Dict.Keys()(1)).ToEqual False
        .Expect(TypeName(Dict.Keys()(0))).ToEqual "Boolean"
        .Expect(TypeName(Dict.Keys()(1))).ToEqual "Boolean"
    End With
    
    With Specs.It("should handle object keys")
        Set Dict = CreateDictionary(UseNative)
        
        Set A = New Collection
        Set B = New Dictionary
        
        A.Add 123
        B.Add "a", 456
        
        Dict.Add A, "123"
        Dict.Add B, "456"
        
        .Expect(Dict(A)).ToEqual "123"
        .Expect(Dict(B)).ToEqual "456"
        
        Dict.Remove B
        Dict.Key(A) = B
        
        .Expect(Dict(B)).ToEqual "123"
    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"
    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
    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
    End With
    
    ' Errors
    ' ------------------------- '
    Err.Clear
    On Error Resume Next
    
    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
    
    With Specs.It("should throw 457 for Boolean key quirks")
        Set Dict = CreateDictionary(UseNative)
        
        Dict.Add True, "abc"
        Dict.Add -1, "def"
        
        .Expect(Err.Number).ToEqual 457
        Err.Clear
        
        Dict.Add False, "abc"
        Dict.Add 0, "def"
        
        .Expect(Err.Number).ToEqual 457
        Err.Clear
    End With
    
    On Error GoTo 0
    InlineRunner.RunSuite Specs
End Function

Public Sub ExecuteSpeedTest(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