Skip to content

Instantly share code, notes, and snippets.

@loic-sharma
Created February 6, 2018 21:40
Show Gist options
  • Save loic-sharma/a74da77d2f488596d196793408a919ae to your computer and use it in GitHub Desktop.
Save loic-sharma/a74da77d2f488596d196793408a919ae to your computer and use it in GitHub Desktop.
Flag tests (for revocation)
using System;
using System.Linq;
public class Program
{
[Flags]
public enum E
{
None = 0,
A = 1,
B = 2,
C = 4,
D = 8
}
public static void Main()
{
// ONLY
Test(new[] { E.None });
Test(new[] { E.None, E.B });
Test(new[] { E.None, E.C });
Test(new[] { E.None, E.B, E.C });
Test(new[] { E.B });
Test(new[] { E.C });
Test(new[] { E.B, E.C });
Test(new[] { E.B | E.C });
Console.WriteLine("Contains other");
// Other
Test(new[] { E.A });
Test(new[] { E.A, E.B });
Test(new[] { E.A | E.B });
Test(new[] { E.None, E.B, E.A });
Test(new[] { E.None, E.B, E.A | E.B });
}
public static void Test(E[] flags)
{
var unknown = E.B | E.C;
if (flags.Any(f => (f & ~unknown) != E.None))
{
Console.WriteLine($"Flags {string.Join(", ", flags)} contain something other than {unknown}");
}
else
{
Console.WriteLine($"Flags {string.Join(", ", flags)} are ONLY {unknown}, or {nameof(E.None)}");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment