Skip to content

Commit dc9f94f

Browse files
committed
add RegistrationSetTests
1 parent 4547902 commit dc9f94f

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using Microsoft.VisualStudio.TestTools.UnitTesting;
5+
using Unity.Registration;
6+
using Unity.Storage;
7+
using Unity.Tests.TestObjects;
8+
9+
namespace Unity.Tests.v5.Storage
10+
{
11+
[TestClass]
12+
public class RegistrationSetTests
13+
{
14+
[TestMethod]
15+
public void ShouldHandleCollisions()
16+
{
17+
var (s1, s2) = MakeCollision();
18+
19+
var registrationSet = new RegistrationSet();
20+
var registration1 = new InternalRegistration();
21+
var registration2 = new InternalRegistration();
22+
var registration3 = new InternalRegistration();
23+
24+
registrationSet.Add(typeof(IService), s1, registration1);
25+
Assert.AreEqual(1, registrationSet.Count);
26+
registrationSet.Add(typeof(IService), s2, registration2);
27+
Assert.AreEqual(2, registrationSet.Count);
28+
registrationSet.Add(typeof(IService), s1, registration3);
29+
Assert.AreEqual(2, registrationSet.Count);
30+
}
31+
32+
private static (string, string) MakeCollision()
33+
{
34+
var strings = new Dictionary<int, string>();
35+
var random = new Random();
36+
var size = 10;
37+
38+
var builder = new StringBuilder(size);
39+
while (true)
40+
{
41+
for (var j = 0; j < size; j++)
42+
builder.Append((char) random.Next('a', 'z' + 1));
43+
44+
var str = builder.ToString();
45+
var hash = str.GetHashCode();
46+
if (strings.TryGetValue(hash, out var other))
47+
return (str, other);
48+
49+
strings[hash] = str;
50+
builder.Clear();
51+
}
52+
}
53+
54+
}
55+
}

0 commit comments

Comments
 (0)