Skip to content

Commit

Permalink
feat(Weaver): adding constant values for max value for number of bits (
Browse files Browse the repository at this point in the history
  • Loading branch information
James-Frowen committed Sep 20, 2021
1 parent a8798f0 commit 2f7c322
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 0 deletions.
78 changes: 78 additions & 0 deletions Assets/Mirage/Runtime/Serialization/WeaverAttributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,82 @@ public class VarIntBlocksAttribute : Attribute
public VarIntBlocksAttribute(int blockSize) { }
}
#pragma warning restore IDE0060 // Remove unused parameter

/// <summary>
/// The max value for N number of bits
/// </summary>
/// <example>
/// Using FromBitCount with <see cref="VarIntAttribute"/> because it uses max value not bit count
/// <code>
/// [VarInt(FromBitCount.b3, FromBitCount.b7, FromBitCount.b10, true)]
/// public int componentIndex;
/// </code>
/// </example>
public static class FromBitCount
{
public const ulong b1 = (1ul << 1) - 1;
public const ulong b2 = (1ul << 2) - 1;
public const ulong b3 = (1ul << 3) - 1;
public const ulong b4 = (1ul << 4) - 1;
public const ulong b5 = (1ul << 5) - 1;
public const ulong b6 = (1ul << 6) - 1;
public const ulong b7 = (1ul << 7) - 1;
public const ulong b8 = (1ul << 8) - 1;
public const ulong b9 = (1ul << 9) - 1;
public const ulong b10 = (1ul << 10) - 1;
public const ulong b11 = (1ul << 11) - 1;
public const ulong b12 = (1ul << 12) - 1;
public const ulong b13 = (1ul << 13) - 1;
public const ulong b14 = (1ul << 14) - 1;
public const ulong b15 = (1ul << 15) - 1;
public const ulong b16 = (1ul << 16) - 1;
public const ulong b17 = (1ul << 17) - 1;
public const ulong b18 = (1ul << 18) - 1;
public const ulong b19 = (1ul << 19) - 1;
public const ulong b20 = (1ul << 20) - 1;
public const ulong b21 = (1ul << 21) - 1;
public const ulong b22 = (1ul << 22) - 1;
public const ulong b23 = (1ul << 23) - 1;
public const ulong b24 = (1ul << 24) - 1;
public const ulong b25 = (1ul << 25) - 1;
public const ulong b26 = (1ul << 26) - 1;
public const ulong b27 = (1ul << 27) - 1;
public const ulong b28 = (1ul << 28) - 1;
public const ulong b29 = (1ul << 29) - 1;
public const ulong b30 = (1ul << 30) - 1;
public const ulong b31 = (1ul << 31) - 1;
public const ulong b32 = (1ul << 32) - 1;
public const ulong b33 = (1ul << 33) - 1;
public const ulong b34 = (1ul << 34) - 1;
public const ulong b35 = (1ul << 35) - 1;
public const ulong b36 = (1ul << 36) - 1;
public const ulong b37 = (1ul << 37) - 1;
public const ulong b38 = (1ul << 38) - 1;
public const ulong b39 = (1ul << 39) - 1;
public const ulong b40 = (1ul << 40) - 1;
public const ulong b41 = (1ul << 41) - 1;
public const ulong b42 = (1ul << 42) - 1;
public const ulong b43 = (1ul << 43) - 1;
public const ulong b44 = (1ul << 44) - 1;
public const ulong b45 = (1ul << 45) - 1;
public const ulong b46 = (1ul << 46) - 1;
public const ulong b47 = (1ul << 47) - 1;
public const ulong b48 = (1ul << 48) - 1;
public const ulong b49 = (1ul << 49) - 1;
public const ulong b50 = (1ul << 50) - 1;
public const ulong b51 = (1ul << 51) - 1;
public const ulong b52 = (1ul << 52) - 1;
public const ulong b53 = (1ul << 53) - 1;
public const ulong b54 = (1ul << 54) - 1;
public const ulong b55 = (1ul << 55) - 1;
public const ulong b56 = (1ul << 56) - 1;
public const ulong b57 = (1ul << 57) - 1;
public const ulong b58 = (1ul << 58) - 1;
public const ulong b59 = (1ul << 59) - 1;
public const ulong b60 = (1ul << 60) - 1;
public const ulong b61 = (1ul << 61) - 1;
public const ulong b62 = (1ul << 62) - 1;
public const ulong b63 = (1ul << 63) - 1;
public const ulong b64 = ulong.MaxValue;
}
}
51 changes: 51 additions & 0 deletions Assets/Tests/Editor/FromBitCountTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using Mirage.Serialization;
using NUnit.Framework;

namespace Mirage.Tests
{
[Description("Test first few cases to ensure logic doesn't have typo")]
class FromBitCountTest
{
[Test]
public void b1Is1()
{
Assert.That(FromBitCount.b1, Is.EqualTo(1));
}

[Test]
public void b2Is3()
{
Assert.That(FromBitCount.b2, Is.EqualTo(3));
}

[Test]
public void b3Is7()
{
Assert.That(FromBitCount.b3, Is.EqualTo(7));
}

[Test]
public void b4Is15()
{
Assert.That(FromBitCount.b4, Is.EqualTo(15));
}

[Test]
public void b8Is255()
{
Assert.That(FromBitCount.b8, Is.EqualTo(255));
}

[Test]
public void b32IsIntMax()
{
Assert.That(FromBitCount.b32, Is.EqualTo(uint.MaxValue));
}

[Test]
public void b64IsMax()
{
Assert.That(FromBitCount.b64, Is.EqualTo(ulong.MaxValue));
}
}
}
11 changes: 11 additions & 0 deletions Assets/Tests/Editor/FromBitCountTest.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2f7c322

Please sign in to comment.