diff --git a/ThScoreFileConverter.Benchmarks/Program.cs b/ThScoreFileConverter.Benchmarks/Program.cs index 638aeb41..2c40f793 100644 --- a/ThScoreFileConverter.Benchmarks/Program.cs +++ b/ThScoreFileConverter.Benchmarks/Program.cs @@ -3,4 +3,4 @@ using BenchmarkDotNet.Running; var config = DefaultConfig.Instance.AddDiagnoser(MemoryDiagnoser.Default); -_ = BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).RunAll(config, args); +_ = BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).RunAllJoined(config, args); diff --git a/ThScoreFileConverter.Benchmarks/Scenarios/IsDefined.cs b/ThScoreFileConverter.Benchmarks/Scenarios/IsDefined.cs new file mode 100644 index 00000000..1b560d40 --- /dev/null +++ b/ThScoreFileConverter.Benchmarks/Scenarios/IsDefined.cs @@ -0,0 +1,28 @@ +using BenchmarkDotNet.Attributes; +using EnumHelper_ = ThScoreFileConverter.Core.Helpers.EnumHelper; + +namespace ThScoreFileConverter.Benchmarks.Scenarios; + +#pragma warning disable CA1822 // Mark members as static + +public class IsDefined +{ + [GlobalSetup] + public void Setup() + { + _ = Enum.IsDefined(DayOfWeek.Sunday); + _ = EnumHelper_.IsDefined(DayOfWeek.Sunday); + } + + [Benchmark] + public bool NetCore() + { + return Enum.IsDefined(DayOfWeek.Sunday); + } + + [Benchmark] + public bool EnumHelper() + { + return EnumHelper_.IsDefined(DayOfWeek.Sunday); + } +} diff --git a/ThScoreFileConverter.Core.Tests/Helpers/EnumHelperTests.cs b/ThScoreFileConverter.Core.Tests/Helpers/EnumHelperTests.cs index 54c67c31..03694ab0 100644 --- a/ThScoreFileConverter.Core.Tests/Helpers/EnumHelperTests.cs +++ b/ThScoreFileConverter.Core.Tests/Helpers/EnumHelperTests.cs @@ -52,4 +52,11 @@ public void CartesianTest() CollectionAssert.That.AreEqual(expected, EnumHelper.Cartesian()); } + + [TestMethod] + public void IsDefinedTest() + { + Assert.IsTrue(EnumHelper.IsDefined(DayOfWeek.Sunday)); + Assert.IsFalse(EnumHelper.IsDefined((DayOfWeek)7)); + } } diff --git a/ThScoreFileConverter.Core/Helpers/EnumHelper.cs b/ThScoreFileConverter.Core/Helpers/EnumHelper.cs index a2ae6da0..78f11fe1 100644 --- a/ThScoreFileConverter.Core/Helpers/EnumHelper.cs +++ b/ThScoreFileConverter.Core/Helpers/EnumHelper.cs @@ -47,4 +47,25 @@ public static TEnum To(object value) { return EnumHelper.Enumerable.Cartesian(EnumHelper.Enumerable); } + + /// + /// Returns a boolean telling whether a given integral value exists in the type. + /// + /// The enumeration type. + /// The value of a constant in . + /// + /// if a given integral value exists in ; + /// otherwise. + /// + /// + /// To avoid circular references, do not call it from the following attributes + /// (use instead): + /// , , + /// , . + /// + public static bool IsDefined(TEnum value) + where TEnum : struct, Enum + { + return EnumHelper.Members.ContainsKey(value); + } }