Skip to content

Commit

Permalink
Major Changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Taiizor committed Dec 20, 2024
1 parent 4c3d994 commit 201e51b
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 5 deletions.
51 changes: 48 additions & 3 deletions demo/UUID.Demo/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,52 @@ static async Task Main(string[] args)
Console.WriteLine($" {uuid} - Time: {uuid.Time:yyyy-MM-dd HH:mm:ss.fff}");
}

Console.WriteLine("\n14. Thread-Safe UUID Generation:");
Console.WriteLine("\n14. UUIDv7 Comparison Methods:");

// IsOrderedAfter demo
UUID earlier = UUID.New();
Thread.Sleep(10); // 10ms wait
UUID later = UUID.New();

Console.WriteLine("IsOrderedAfter Demo:");
Console.WriteLine($"Earlier UUID: {earlier}");
Console.WriteLine($"Later UUID: {later}");
Console.WriteLine($"Is later ordered after earlier? {later.IsOrderedAfter(earlier)}");
Console.WriteLine($"Is earlier ordered after later? {earlier.IsOrderedAfter(later)}");

// CompareTimestamps demo
Console.WriteLine("\nCompareTimestamps Demo:");
int comparison = UUID.CompareTimestamps(earlier, later);
Console.WriteLine($"Comparing timestamps (earlier vs later): {comparison}");
Console.WriteLine($"Comparing timestamps (later vs earlier): {UUID.CompareTimestamps(later, earlier)}");
Console.WriteLine($"Comparing timestamps (same UUID): {UUID.CompareTimestamps(earlier, earlier)}");

// AreMonotonicallyOrdered demo
Console.WriteLine("\nAreMonotonicallyOrdered Demo:");
UUID[] orderedArray = new UUID[5];
for (int i = 0; i < orderedArray.Length; i++)
{
orderedArray[i] = UUID.New();
Thread.Sleep(1); // 1ms wait
}

Console.WriteLine("Ordered UUID array:");
foreach (UUID uuid in orderedArray)
{
Console.WriteLine($" {uuid} - Time: {uuid.Time:HH:mm:ss.fff}");
}
Console.WriteLine($"Is array monotonically ordered? {UUID.AreMonotonicallyOrdered(orderedArray)}");

// Sırayı boz ve tekrar kontrol et
(orderedArray[1], orderedArray[3]) = (orderedArray[3], orderedArray[1]); // Swap elements
Console.WriteLine("\nAfter swapping elements:");
foreach (UUID uuid in orderedArray)
{
Console.WriteLine($" {uuid} - Time: {uuid.Time:HH:mm:ss.fff}");
}
Console.WriteLine($"Is array still monotonically ordered? {UUID.AreMonotonicallyOrdered(orderedArray)}");

Console.WriteLine("\n15. Thread-Safe UUID Generation:");
HashSet<UUID> set = new();
List<Task> tasks = new();

Expand All @@ -276,7 +321,7 @@ static async Task Main(string[] args)
await Task.WhenAll(tasks);
Console.WriteLine($"Generated {set.Count} unique UUIDs across multiple threads");

Console.WriteLine("\n15. Comparing UUID and Guid initialization behaviors:\n");
Console.WriteLine("\n16. Comparing UUID and Guid initialization behaviors:\n");

// UUID initialization - always creates a unique identifier
UUID uuid3 = new();
Expand Down Expand Up @@ -312,7 +357,7 @@ static async Task Main(string[] args)
Console.WriteLine($"Are they equal? {newGuid1 == newGuid2}");
Console.WriteLine($"Is first empty? {newGuid1 == default}");

Console.WriteLine("\n16. Bulk UUID Generation Performance:");
Console.WriteLine("\n17. Bulk UUID Generation Performance:");
const int batchSize = 1000;
Console.WriteLine($"Generating {batchSize} UUIDs in batch...");

Expand Down
111 changes: 111 additions & 0 deletions test/UUID.Tests/UUIDComparisonTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
using System;
using System.Threading;
using Xunit;

namespace UUIDTests
{
public class UUIDComparisonTests
{
[Fact]
public void IsOrderedAfter_WithDifferentTimestamps_ShouldReturnCorrectOrder()
{
// Arrange
UUID earlier = UUID.New();
Thread.Sleep(2); // Ensure different timestamp
UUID later = UUID.New();

// Assert
Assert.True(later.IsOrderedAfter(earlier), "Later UUID should be ordered after earlier UUID");
Assert.False(earlier.IsOrderedAfter(later), "Earlier UUID should not be ordered after later UUID");
}

[Fact]
public void IsOrderedAfter_WithSameTimestamp_ShouldUseCounter()
{
// Arrange & Act
UUID[] uuids = new UUID[5];
for (int i = 0; i < uuids.Length; i++)
{
uuids[i] = UUID.New();
}

// Assert
for (int i = 1; i < uuids.Length; i++)
{
Assert.True(uuids[i].IsOrderedAfter(uuids[i - 1]),
"UUIDs with same timestamp should be ordered by counter");
}
}

[Fact]
public void CompareTimestamps_ShouldReturnCorrectOrder()
{
// Arrange
UUID earlier = UUID.New();
Thread.Sleep(2); // Ensure different timestamp
UUID later = UUID.New();

// Assert
Assert.True(UUID.CompareTimestamps(earlier, later) < 0,
"Earlier timestamp should compare as less than later timestamp");
Assert.True(UUID.CompareTimestamps(later, earlier) > 0,
"Later timestamp should compare as greater than earlier timestamp");
Assert.Equal(0, UUID.CompareTimestamps(earlier, earlier));
}

[Fact]
public void AreMonotonicallyOrdered_WithNullOrEmptyArray_ShouldReturnTrue()
{
// Assert
Assert.True(UUID.AreMonotonicallyOrdered(null),
"Null array should be considered monotonically ordered");
Assert.True(UUID.AreMonotonicallyOrdered(Array.Empty<UUID>()),
"Empty array should be considered monotonically ordered");
Assert.True(UUID.AreMonotonicallyOrdered(new[] { UUID.New() }),
"Single-element array should be considered monotonically ordered");
}

[Fact]
public void AreMonotonicallyOrdered_WithOrderedArray_ShouldReturnTrue()
{
// Arrange
UUID[] uuids = new UUID[5];
for (int i = 0; i < uuids.Length; i++)
{
uuids[i] = UUID.New();
Thread.Sleep(1); // Ensure different timestamps
}

// Assert
Assert.True(UUID.AreMonotonicallyOrdered(uuids),
"Array with increasing timestamps should be monotonically ordered");
}

[Fact]
public void AreMonotonicallyOrdered_WithUnorderedArray_ShouldReturnFalse()
{
// Arrange
UUID[] uuids = new UUID[3];
uuids[0] = UUID.New();
Thread.Sleep(2);
uuids[1] = UUID.New();
uuids[2] = uuids[0]; // Make array unordered by reusing earlier UUID

// Assert
Assert.False(UUID.AreMonotonicallyOrdered(uuids),
"Array with out-of-order UUIDs should not be monotonically ordered");
}

[Fact]
public void GetMonotonicCounter_ShouldExtractLast12Bits()
{
// Arrange
UUID uuid = new(0, 0xFFF); // Set all counter bits to 1
UUID uuid2 = new(0, 0x000); // Set all counter bits to 0

// Assert
Assert.Equal((ushort)0xFFF, uuid.GetMonotonicCounter());
Assert.Equal((ushort)0x000, uuid2.GetMonotonicCounter());
}
}
}
4 changes: 2 additions & 2 deletions test/UUID.Tests/UUIDConstructorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -446,8 +446,8 @@ public void TestThreadSafetyWithMonotonicCounter()
// Arrange
const int threadCount = 10;
const int uuidsPerThread = 1000;
ConcurrentBag<UUID> allUuids = new ConcurrentBag<UUID>();
List<Task> tasks = new List<Task>();
ConcurrentBag<UUID> allUuids = new();
List<Task> tasks = new();

// Act
for (int i = 0; i < threadCount; i++)
Expand Down

0 comments on commit 201e51b

Please sign in to comment.