-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathStringEquality.cs
73 lines (60 loc) · 3.43 KB
/
StringEquality.cs
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using BenchmarkDotNet.Attributes;
using MicroBenchmarks;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
namespace System.Globalization.Tests
{
[BenchmarkCategory(Categories.Libraries, Categories.Runtime, Categories.NoWASM)]
public class StringEquality
{
private string _value, _same, _sameUpper, _diffAtFirstChar;
public static IEnumerable<(CultureInfo CultureInfo, CompareOptions CompareOptions)> GetOptions()
{
// Ordinal and OrdinalIgnoreCase use single execution path for all cultures, so we test it only for "en-US"
yield return (new CultureInfo("en-US"), CompareOptions.Ordinal);
yield return (new CultureInfo("en-US"), CompareOptions.OrdinalIgnoreCase);
// the most popular culture:
yield return (new CultureInfo("en-US"), CompareOptions.None);
yield return (new CultureInfo("en-US"), CompareOptions.IgnoreCase);
// two very common use cases:
yield return (CultureInfo.InvariantCulture, CompareOptions.None);
yield return (CultureInfo.InvariantCulture, CompareOptions.IgnoreCase);
// IgnoreSymbols and IgnoreNonSpace are rarely used, this is why we test it only for a single culture
yield return (new CultureInfo("en-US"), CompareOptions.IgnoreSymbols);
yield return (new CultureInfo("en-US"), CompareOptions.IgnoreNonSpace);
// Polish language has a lot of special characters, for example 'ch', 'rz', 'sz', 'cz' use two chars to express one ;)
// it also has a lot of characters with accent so we use it as an example of a "complex" language
yield return (new CultureInfo("pl-PL"), CompareOptions.None);
}
[ParamsSource(nameof(GetOptions))]
public (CultureInfo CultureInfo, CompareOptions CompareOptions) Options;
[Params(1024)] // single execution path = single test case
public int Count;
[GlobalSetup]
public void Setup()
{
// we are using part of Alice's Adventures in Wonderland text as test data
char[] characters = File.ReadAllText(CompressedFile.GetFilePath("alice29.txt")).Take(Count).ToArray();
_value = new string(characters);
_same = new string(characters);
_sameUpper = _same.ToUpper();
char[] copy = characters.ToArray();
copy[0] = (char)(copy[0] + 1);
_diffAtFirstChar = new string(copy);
}
[Benchmark] // the most work to do: the strings have same content, but don't point to the same memory
[MemoryRandomization]
public int Compare_Same() => Options.CultureInfo.CompareInfo.Compare(_value, _same, Options.CompareOptions);
[Benchmark] // the most work to do for IgnoreCase: every char needs to be compared and uppercased
[MemoryRandomization]
public int Compare_Same_Upper() => Options.CultureInfo.CompareInfo.Compare(_value, _sameUpper, Options.CompareOptions);
[Benchmark] // this should return quickly
[MemoryRandomization]
public int Compare_DifferentFirstChar() => Options.CultureInfo.CompareInfo.Compare(_value, _diffAtFirstChar, Options.CompareOptions);
}
}