Skip to content

Commit 3a39917

Browse files
committed
add fuzz test for CTZ
1 parent 2059efd commit 3a39917

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

test/Box2D.NET.Test/B2CTZTest.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// SPDX-FileCopyrightText: 2025 Ikpil Choi([email protected])
33
// SPDX-License-Identifier: MIT
44

5+
using System;
56
using System.Numerics;
67
using NUnit.Framework;
78
using static Box2D.NET.B2CTZs;
@@ -48,4 +49,24 @@ public void Test()
4849
Assert.That(b2PopCount64(value), Is.EqualTo(BitOperations.PopCount(value)), $"PopCount64 failed for {value}");
4950
}
5051
}
52+
53+
[Test]
54+
public void Test_Fuzz()
55+
{
56+
var rng = new Random((int)(DateTime.Now.Ticks / TimeSpan.TicksPerSecond));
57+
for (int i = 0; i < 1000000; i++)
58+
{
59+
// 무작위 uint 생성
60+
uint value32 = (uint)rng.Next(int.MinValue, int.MaxValue);
61+
ulong value64 = ((ulong)(uint)rng.Next(int.MinValue, int.MaxValue) << 32) | (uint)rng.Next(int.MinValue, int.MaxValue);
62+
63+
// 32비트 테스트
64+
Assert.That(b2CTZ32(value32), Is.EqualTo(BitOperations.TrailingZeroCount(value32)), $"CTZ32 failed for 0x{value32:X8}");
65+
Assert.That(b2CLZ32(value32), Is.EqualTo(BitOperations.LeadingZeroCount(value32)), $"CLZ32 failed for 0x{value32:X8}");
66+
67+
// 64비트 테스트
68+
Assert.That(b2CTZ64(value64), Is.EqualTo(BitOperations.TrailingZeroCount(value64)), $"CTZ64 failed for 0x{value64:X16}");
69+
Assert.That(b2PopCount64(value64), Is.EqualTo(BitOperations.PopCount(value64)), $"PopCount64 failed for 0x{value64:X16}");
70+
}
71+
}
5172
}

0 commit comments

Comments
 (0)