+ Added an auto-incrementing Id to OSCADObjects

This commit is contained in:
Michael L Smith 2016-02-20 14:21:41 -08:00
parent e62111b1df
commit 4dc79e8bc2
4 changed files with 82 additions and 0 deletions

View File

@ -1,6 +1,7 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OSCADSharp.Solids;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@ -79,5 +80,45 @@ namespace OSCADSharp.UnitTests
Assert.IsFalse(sphere.GetType() == cube.GetType());
Assert.AreEqual(0, sphere.Children().Count());
}
[TestMethod]
public void OSCADObject_IdsAreSequentialAndDistinct()
{
var obj1 = new Sphere();
var obj2 = new Cube();
var obj3 = new Sphere();
Assert.IsTrue(obj1.Id < obj2.Id && obj2.Id < obj3.Id);
Assert.IsTrue(obj1.Id + 1 == obj2.Id);
Assert.IsTrue(obj2.Id + 1 == obj3.Id);
}
[TestMethod]
public void OSCADObject_ParallelObjectCreationDoesNotYieldDuplicateIds()
{
ConcurrentBag<OSCADObject> bag = new ConcurrentBag<OSCADObject>();
Parallel.For(0, 1000, (i) => {
bag.Add(new Sphere());
});
var ids = bag.Select(obj => obj.Id).ToList();
Assert.AreEqual(ids.Count, ids.Distinct().Count());
}
[TestMethod]
public void OSCADObject_EachOscadObjectChildHasDistinctId()
{
var obj = new Cube(5, 5, 10, true)
.Translate(0, 5, 10).Rotate(0, 90, 0)
.Translate(0, 0, 10).Scale(1, 1, 2);
List<uint> ids = obj.Children().Select(child => child.Id).ToList();
ids.Add(obj.Id);
Assert.AreEqual(ids.Count, ids.Distinct().Count());
}
}
}

View File

@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OSCADSharp
{
/// <summary>
/// Responsible for creating identifiers for objects
/// </summary>
internal static class Ids
{
private static uint globalId = 0;
private static object idLockObject = new object();
/// <summary>
/// Gets a unique auto-incrementing integer id
/// </summary>
/// <returns></returns>
internal static uint Get()
{
lock (idLockObject)
{
globalId++;
return globalId;
}
}
}
}

View File

@ -14,6 +14,16 @@ namespace OSCADSharp
/// </summary>
public abstract class OSCADObject
{
#region Fields
private uint id = Ids.Get();
/// <summary>
/// The unique Id of the object
/// these values auto-increment
/// </summary>
public uint Id { get { return this.id; } }
#endregion
#region Transforms
/// <summary>
/// Applies Color and/or Opacity to this object

View File

@ -42,6 +42,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Ids.cs" />
<Compile Include="Sizes.cs" />
<Compile Include="Spatial\Matrix.cs" />
<Compile Include="Transforms\HulledObject.cs" />