-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathStringUtilTests.cs
168 lines (150 loc) · 6.7 KB
/
StringUtilTests.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
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
using System;
using System.Collections.Generic;
using System.Linq;
using CompatBot.EventHandlers;
using CompatBot.Utils;
using CompatBot.Utils.Extensions;
using DuoVia.FuzzyStrings;
using HomoglyphConverter;
using NUnit.Framework;
namespace Tests;
[TestFixture]
public class StringUtilTests
{
[TestCase("🇭🇷", ExpectedResult = 2)]
[TestCase("\u200d", ExpectedResult = 0)]
[TestCase("㌀", ExpectedResult = 1)]
[TestCase("a\u0304\u0308bc\u0327", ExpectedResult = 3)]
[TestCase("Megamouse", ExpectedResult = 9)]
public int VisibleLengthTest(string input) => input.GetVisibleLength();
[Test]
public void VisibleTrimTest()
{
Assert.Multiple(() =>
{
Assert.That("abc".TrimVisible(100), Is.EqualTo("abc"));
Assert.That("abc".TrimVisible(3), Is.EqualTo("abc"));
Assert.That("abc".TrimVisible(2), Is.EqualTo("a…"));
});
}
[TestCase("cockatrice", "сockаtrice")]
[TestCase("cockatrice", "çöćķåťřĩĉȅ")]
public void DiacriticsDetectionTest(string strA, string strB)
{
Assert.That(strA.EqualsIgnoringDiacritics(strB), Is.True);
}
[TestCase("cockatrice", "cockatrice")]
[TestCase("сосkаtriсе", "cockatrice")]
[TestCase("cocкatrice", "cockatrice")]
[TestCase("cockatrice", "cockatrice")]
[TestCase("соc͏katrice", "cockatrice")]
[TestCase("çöćķåťřĩĉȅ", "cockatrice")]
[TestCase("с◌ckåťřĩĉȅ", "cockatrice")]
[TestCase("jò̵͗s̷̑͠ẻ̵͝p̸̆̂h̸͐̿", "joseph")]
public void HomoglyphDetectionTest(string strA, string strB)
{
Assert.That(strA.StripInvisibleAndDiacritics().ToCanonicalForm(), Is.EqualTo(strB));
}
[TestCase("jò̵͗s̷̑͠ẻ̵͝p̸̆̂h̸͐̿", "joseph")]
public void StripZalgoTest(string input, string expected)
{
var stripped = UsernameZalgoMonitor.StripZalgo(input, null, 0ul);
Assert.That(stripped, Is.EqualTo(expected));
}
[Test]
public void SubstringTest()
{
var contentId = "UP2611-NPUB31848_00-HDDBOOTPERSONA05";
var productCode = "NPUB31848";
Assert.That(contentId[7..16], Is.EqualTo(productCode));
}
[TestCase("a grey and white cat sitting in front of a window", ExpectedResult = "a grey and white kot sitting in front of a window")]
public string FixKotTest(string input) => input.FixKot();
[TestCase("“F”", ExpectedResult = @"(\xE2\x80\x9C)F(\xE2\x80\x9D)")]
[TestCase("·! 0", ExpectedResult = @"(\xC2\xB7)! 0")]
[TestCase("𫞠", ExpectedResult = @"(\xF0\xAB\x9E\xA0)")] // 0x0002_B7A0
[TestCase("", ExpectedResult = @"(\xF4\x8F\xBF\xBD)")] // 0x0010_FFFD
public string RegexPatterReEncoding(string input) => input.ToLatin8BitRegexPattern();
[TestCase("minesweeeper", "minesweeper")]
[TestCase("minesweeeeeeeeeeeeeeeeeeper", "minesweeper")]
[TestCase("ee", "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee")]
[TestCase("aaaaaaaaa", "aaaaaaaaa")]
[TestCase("South Fort Union", "West Fort Union")]
public void DiceCoefficientRangeTest(string strA, string strB)
{
var coef = DiceCoefficient(strA, strB);
Assert.That(coef, Is.GreaterThanOrEqualTo(0.0).And.LessThanOrEqualTo(1.0));
//Assert.That(DiceCoefficientOptimized.DiceCoefficient(strA, strB), Is.EqualTo(coef));
//Assert.That(DiceCoefficientExtensions.DiceCoefficient(strA, strB), Is.EqualTo(coef));
(strB, strA) = (strA, strB);
var coefB = DiceCoefficient(strA, strB);
Assert.That(coefB, Is.EqualTo(coef));
//Assert.That(DiceCoefficientOptimized.DiceCoefficient(strA, strB), Is.EqualTo(coef));
//Assert.That(DiceCoefficientExtensions.DiceCoefficient(strA, strB), Is.EqualTo(coef));
}
[Test]
public void DistanceTest()
{
var strA = @"
""Beware of the man who works hard to learn something, learns it, and finds
himself no wiser than before,"" Bokonon tells us. ""He is full of murderous
resentment of people who are ignorant without having come by their
ignorance the hard way.""
― Kurt Vonnegut, ""Cat's Cradle""
".Trim();
var strB = @"
""Beware of the man who works hard to learn something, learns it, and finds himself no wiser than before,"" Bokonon tells us. ""He is full of murderous resentment of people who are ignorant without having come by their ignorance the hard way.""
-- Kurt Vonnegut, ""Cat's Cradle""
".Trim();
var coef = DiceCoefficientOptimized.DiceIshCoefficientIsh(strA, strB);
Assert.That(coef, Is.GreaterThan(0.95), "Dice Coefficient");
}
[TestCase("Metal Gear Solid 4", "MGS4", "MGS4", "MGS4", "MGS4")]
[TestCase("UTC-11", "U1", "UTC1", "U11", "UTC11")]
[TestCase("camelCaseString13", "c", "cCS", "c13", "cCS13")]
public void AcronymGenerationTest(string input, string expectedDefault, string expectedWithUpper, string expectedWithDigits, string expectedWithUpperAndDigits)
{
Assert.Multiple(() =>
{
Assert.That(input.GetAcronym(), Is.EqualTo(expectedDefault));
Assert.That(input.GetAcronym(includeAllCaps: true), Is.EqualTo(expectedWithUpper));
Assert.That(input.GetAcronym(includeAllDigits: true), Is.EqualTo(expectedWithDigits));
Assert.That(input.GetAcronym(includeAllCaps: true, includeAllDigits: true), Is.EqualTo(expectedWithUpperAndDigits));
});
}
public static double DiceCoefficient(string input, string comparedTo)
{
var ngrams = input.ToBiGrams()[1..^1];
var compareToNgrams = comparedTo.ToBiGrams()[1..^1];
return DiceCoefficient(ngrams, compareToNgrams);
}
public static double DiceCoefficient(string[] nGrams, string[] compareToNGrams)
{
var nGramMap = new Dictionary<string, int>(nGrams.Length);
var compareToNGramMap = new Dictionary<string, int>(compareToNGrams.Length);
var nGramSet = new HashSet<string>();
var compareToNGramSet = new HashSet<string>();
foreach (var nGram in nGrams)
{
if (nGramSet.Add(nGram))
nGramMap[nGram] = 1;
else
nGramMap[nGram]++;
}
foreach (var nGram in compareToNGrams)
{
if (compareToNGramSet.Add(nGram))
compareToNGramMap[nGram] = 1;
else
compareToNGramMap[nGram]++;
}
nGramSet.IntersectWith(compareToNGramSet);
if (nGramSet.Count == 0)
return 0.0d;
var matches = 0;
foreach (var nGram in nGramSet)
matches += Math.Min(nGramMap[nGram], compareToNGramMap[nGram]);
double totalBigrams = nGrams.Length + compareToNGrams.Length;
return (2 * matches) / totalBigrams;
}
}