-
Notifications
You must be signed in to change notification settings - Fork 3
/
SequentialGuidTests.cs
181 lines (153 loc) · 4.1 KB
/
SequentialGuidTests.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
169
170
171
172
173
174
175
176
177
178
179
180
181
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Numerics;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Sander.SequentialGuid.Tests
{
[TestClass]
public sealed class SequentialGuidTests
{
[TestMethod]
public void EmptyGuidTest()
{
var guid = Guid.Empty;
Trace.WriteLine(guid.ToBigInteger());
var sequential = new SequentialGuid(guid);
for (var i = 1; i < 100000; i++)
{
var next = sequential.Next();
var bi = next.ToBigInteger();
Trace.WriteLine($"{bi}:{next}");
Assert.AreEqual(i, bi);
}
}
[TestMethod]
public void MultiThreadTest()
{
for (var i = 10_000; i < 12_000; i++)
{
var guid = Guid.NewGuid();
var sequential = new SequentialGuid(guid);
Enumerable.Range(0, i)
.AsParallel()
.WithDegreeOfParallelism(512)
.WithExecutionMode(ParallelExecutionMode.ForceParallelism)
.ForAll(_ => sequential.Next());
var next = sequential.Next();
var original = guid.ToBigInteger();
var diff = next.ToBigInteger() - original;
Trace.WriteLine($"{guid} - {next} = {diff}");
Assert.AreEqual(i + 1, diff);
}
}
[TestMethod]
public void CurrentTest()
{
var guid = Guid.NewGuid();
var sequential = new SequentialGuid(guid);
Assert.AreEqual(guid, sequential.Current);
sequential.Next();
Assert.AreNotEqual(guid, sequential.Current);
}
[TestMethod]
public void OriginalTest()
{
var guid = Guid.NewGuid();
var sequential = new SequentialGuid(guid);
Assert.AreEqual(guid, sequential.Original);
sequential.Next();
Assert.AreNotEqual(sequential.Original, sequential.Current);
}
[TestMethod]
public void MultiInstanceTest()
{
Enumerable.Range(1, 255)
.AsParallel()
.WithDegreeOfParallelism(100)
.WithExecutionMode(ParallelExecutionMode.ForceParallelism)
.ForAll(x =>
{
var guid = Guid.NewGuid();
var original = guid.ToBigInteger();
var sequential = new SequentialGuid(guid, (byte)x);
for (var i = 1; i < 100000; i++)
{
var next = sequential.Next();
var bi = BigInteger.Add(original, i * x);
var expected = GuidHelper.FromBigInteger(bi);
if (expected != next)
{
Trace.WriteLine($"{x}({i}): Original = {guid}. Next: {next} vs {expected}");
Trace.Flush();
}
Assert.AreEqual(expected, next);
}
});
}
[TestMethod]
[ExpectedException(typeof(OverflowException))]
public void OverFlowTest()
{
var guid = GuidHelper.MaxValue;
var sequential = new SequentialGuid(guid);
sequential.Next();
}
[TestMethod]
public void AlmostOverFlowTest()
{
var guid = GuidHelper.MaxValue;
var bi = guid.ToBigInteger() - 1;
var almostMax = GuidHelper.FromBigInteger(bi);
var sequential = new SequentialGuid(almostMax);
var next = sequential.Next();
Trace.WriteLine($"{almostMax}: {next}");
Assert.AreEqual(guid, next);
}
[TestMethod]
public void CheckSortingTest()
{
for (byte i = 1; i < 0xFF; i++)
{
var guid = Guid.NewGuid();
var sequential = new SequentialGuid(guid, i);
var count = 10_000;
var guids = new List<Guid>(count);
var strings = new List<string>(count);
for (var j = 0; j < count; j++)
{
var s = sequential.Next();
guids.Add(s);
strings.Add(s.ToString("N"));
}
strings.Sort();
for (var j = 0; j < count; j++)
{
var g = new Guid(strings[j]);
if (g != guids[j])
Trace.WriteLine($"Expected {g}, got {guids[j]}");
Assert.AreEqual(g, guids[j]);
}
}
}
[TestMethod]
public void MultithreadDuplicateTest()
{
for (var i = 10_000; i < 12_000; i++)
{
var bag = new ConcurrentBag<Guid>();
var guid = Guid.NewGuid();
var sequential = new SequentialGuid(guid);
Enumerable.Range(0, i)
.AsParallel()
.WithDegreeOfParallelism(512)
.WithExecutionMode(ParallelExecutionMode.ForceParallelism)
.ForAll(_ => bag.Add(sequential.Next()));
var distinct = bag.Distinct().ToList();
Assert.AreEqual(distinct.Count, bag.Count);
}
}
}
}