From 09e8e5f34353b3a03123c80b226b4aa1f9e1f078 Mon Sep 17 00:00:00 2001 From: ThatBen Date: Thu, 10 Jul 2025 10:47:35 +0200 Subject: [PATCH] Fix for power-of-two selection in purchaseParams --- Tests/CodexReleaseTests/Utils/PurchaseParams.cs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/Tests/CodexReleaseTests/Utils/PurchaseParams.cs b/Tests/CodexReleaseTests/Utils/PurchaseParams.cs index 483d6f36..eef539aa 100644 --- a/Tests/CodexReleaseTests/Utils/PurchaseParams.cs +++ b/Tests/CodexReleaseTests/Utils/PurchaseParams.cs @@ -33,7 +33,7 @@ namespace CodexReleaseTests.Utils var numSlotBlocks = 1 + ((numBlocks - 1) / Nodes); // round-up div. // Next power of two: - var numSlotBlocksPow2 = NextPowerOf2(numSlotBlocks); + var numSlotBlocksPow2 = IsOrNextPowerOf2(numSlotBlocks); return new ByteSize(blockSize.SizeInBytes * numSlotBlocksPow2); } @@ -51,8 +51,9 @@ namespace CodexReleaseTests.Utils return new ByteSize(blockSize.SizeInBytes * totalBlocks); } - private int NextPowerOf2(int n) + private int IsOrNextPowerOf2(int n) { + if (IsPowerOfTwo(n)) return n; n = n - 1; var lg = Convert.ToInt32(Math.Round(Math.Log2(Convert.ToDouble(n)))); return 1 << (lg + 1); @@ -63,5 +64,10 @@ namespace CodexReleaseTests.Utils var x = size.SizeInBytes; return (x != 0) && ((x & (x - 1)) == 0); } + + private static bool IsPowerOfTwo(int x) + { + return (x != 0) && ((x & (x - 1)) == 0); + } } }