Skip to content

Commit

Permalink
Fixed an off by 1 issue related to maxmium GeneratorId. (#62)
Browse files Browse the repository at this point in the history
* Fixed an off by 1 issue related to maxmium GeneratorId.

Max generator id was being calculated as 1 higher than the actual max.
This has been corrected and the message in the exception thrown when the given
generator id is invalid has been appropriately re-worded to include 0
and maxGeneratorId.

* Added unit tests for generator id max/min boundaries with +/- 1
variants
  • Loading branch information
Throwy authored May 26, 2024
1 parent 8696810 commit 6e28227
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
6 changes: 3 additions & 3 deletions IdGen/IdGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ public IdGenerator(int generatorId, IdGeneratorOptions options)
_generatorid = generatorId;
Options = options ?? throw new ArgumentNullException(nameof(options));

var maxgeneratorid = 1U << Options.IdStructure.GeneratorIdBits;
var maxgeneratorid = (1U << Options.IdStructure.GeneratorIdBits) - 1;

if (_generatorid < 0 || _generatorid >= maxgeneratorid)
if (_generatorid < 0 || _generatorid > maxgeneratorid)
{
throw new ArgumentOutOfRangeException(nameof(generatorId), $"GeneratorId must be between 0 and {maxgeneratorid - 1}.");
throw new ArgumentOutOfRangeException(nameof(generatorId), $"GeneratorId must be from 0 to {maxgeneratorid}.");
}

// Precalculate some values
Expand Down
26 changes: 23 additions & 3 deletions IdGenTests/IdGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,36 @@ public void GeneratorId_ShouldBeMasked_WhenReadFromProperty()
Assert.AreEqual((1 << g.Options.IdStructure.GeneratorIdBits) - 1, g.Id);
}

[TestMethod]
public void Constructor_DoesNotThrow_OnMaxGeneratorId()
{
var structure = new IdStructure(41, 10, 12);
// 1023 is the max generator id for 10 bits.
var maxgeneratorid = 1023;
new IdGenerator(maxgeneratorid, new IdGeneratorOptions(structure));
}

[TestMethod]
public void Constructor_DoesNotThrow_OnGeneratorId_0()
{
new IdGenerator(0, new IdGeneratorOptions(new IdStructure(41, 10, 12)));
}

[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void Constructor_Throws_OnNull_Options()
=> new IdGenerator(1024, null!);


[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void Constructor_Throws_OnInvalidGeneratorId_Positive()
=> new IdGenerator(1024, new IdGeneratorOptions(new IdStructure(41, 10, 12)));
public void Constructor_Throws_OnInvalidGeneratorId_Positive_MaxPlusOne()
{
var structure = new IdStructure(41, 10, 12);
// 1023 is the max generator id for 10 bits.
var maxgeneratorid = 1023;
int maxPlusOne = maxgeneratorid + 1;
new IdGenerator(maxPlusOne, new IdGeneratorOptions(structure));
}

[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
Expand Down

0 comments on commit 6e28227

Please sign in to comment.