-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
BitOperationsPopCount.cs
56 lines (48 loc) · 2.1 KB
/
BitOperationsPopCount.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
//
using System;
using System.Numerics;
using System.Runtime.CompilerServices;
using Xunit;
namespace BitOperationsPopCountTest
{
public class Program
{
private static int _errorCode = 100;
[Fact]
public static int TestEntryPoint()
{
// PopCount calls with a constant argument are folded
Test(0U, BitOperations.PopCount(0U), 0);
Test(1U, BitOperations.PopCount(1U), 1);
Test(2U, BitOperations.PopCount(2U), 1);
Test(1111U, BitOperations.PopCount(1111U), 6);
Test(unchecked((uint)-101), BitOperations.PopCount(unchecked((uint)-101)), 29);
Test(4294967294U, BitOperations.PopCount(4294967294U), 31);
Test(4294967295U, BitOperations.PopCount(4294967295U), 32);
Test(0UL, BitOperations.PopCount(0UL), 0);
Test(1UL, BitOperations.PopCount(1UL), 1);
Test(2UL, BitOperations.PopCount(2UL), 1);
Test(1111UL, BitOperations.PopCount(1111UL), 6);
Test(4294967294UL, BitOperations.PopCount(4294967294UL), 31);
Test(4294967295UL, BitOperations.PopCount(4294967295UL), 32);
Test(unchecked((ulong)-101), BitOperations.PopCount(unchecked((ulong)-101)), 61);
Test(18446744073709551614UL, BitOperations.PopCount(18446744073709551614UL), 63);
Test(18446744073709551615UL, BitOperations.PopCount(18446744073709551615UL), 64);
return _errorCode;
}
[MethodImpl(MethodImplOptions.NoInlining)]
private static void Test(ulong input, int output, int expected)
{
if (output != expected)
{
Console.WriteLine($"BitOperations.PopCount with a constant argument failed.");
Console.WriteLine($"Input: {input}");
Console.WriteLine($"Output: {output}");
Console.WriteLine($"Expected: {expected}");
_errorCode++;
}
}
}
}