Last active
December 19, 2015 05:09
-
-
Save aruss/5901732 to your computer and use it in GitHub Desktop.
Sequential Guid Generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Runtime.InteropServices; | |
namespace eConduct.Extensions | |
{ | |
public static class GuidGenerator | |
{ | |
[DllImport("rpcrt4.dll", SetLastError = true)] | |
private static extern int UuidCreateSequential(out Guid guid); | |
const int RPC_S_OK = 0; | |
public static Guid NewSequentialGuid() | |
{ | |
Guid guid; | |
int result = UuidCreateSequential(out guid); | |
if (result == RPC_S_OK) | |
return guid; | |
else | |
return Guid.NewGuid(); | |
} | |
} | |
} | |
// Test | |
using Microsoft.VisualStudio.TestTools.UnitTesting; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace eConduct.Extensions.Tests | |
{ | |
[TestClass] | |
public class GuidGeneratorTests | |
{ | |
[TestMethod] | |
public void GuidGenerator_NewSequentialGuid() | |
{ | |
var guids = new List<Tuple<int, Guid>>(); | |
for (var i = 0; i < 1000; i++) | |
{ | |
guids.Add(new Tuple<int, Guid>(i, GuidGenerator.NewSequentialGuid())); | |
} | |
guids = guids.OrderBy(c => c.Item2).ToList(); | |
for (int i = 0; i < guids.Count; i++) | |
{ | |
Assert.AreEqual(i, guids[i].Item1); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment