Created
May 9, 2021 14:35
-
-
Save Titowisk/5ff2a1c91c4aa506302e9aa781312c65 to your computer and use it in GitHub Desktop.
ExceptApproach x AnyApproach
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using BenchmarkDotNet.Attributes; | |
using BenchmarkDotNet.Running; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace ExceptAproachXAnyAproach | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var summary = BenchmarkRunner.Run(typeof(Program).Assembly); | |
Console.Read(); | |
} | |
} | |
/// <summary> | |
/// Validate if a provided collection of permissionsIds are valid based on a valid collection | |
/// of permissionsIds. | |
/// If any permissionId is invalid, the whole collection fails the validation | |
/// </summary> | |
public class ValidatePermissionsIds | |
{ | |
//public IEnumerable<int> PermissionIds { get; set; } | |
//public IEnumerable<int> ValidPermissionsIds { get; set; } | |
public IEnumerable<object[]> Data() | |
{ | |
// provided permissionsIds, validPermissionsIds | |
yield return new object[] { new List<int>() { 1, 2, 3 }, new List<int>() { 1, 2, 3 } }; // valid collection | |
yield return new object[] { new List<int>() { 1, 2, 3 }, new List<int>() { 2, 3, 4 } }; // invalid collection because of value: 1 | |
yield return new object[] { Enumerable.Range(1, 100).ToList(), Enumerable.Range(1, 100).ToList() }; // valid collection | |
yield return new object[] { Enumerable.Range(1, 100).ToList(), Enumerable.Range(2, 101).ToList() }; // invalid collection because of value: 1 | |
} | |
[Benchmark] | |
[ArgumentsSource(nameof(Data))] | |
public bool ExceptApproach(IEnumerable<int> permissionIds, IEnumerable<int> validPermissionsIds) | |
{ | |
var invalidPermissions = permissionIds.Except(validPermissionsIds); | |
return !invalidPermissions.Any(); | |
} | |
[Benchmark] | |
[ArgumentsSource(nameof(Data))] | |
public bool AnyApproach(IEnumerable<int> permissionIds, IEnumerable<int> validPermissionsIds) | |
{ | |
var atLeastOneInvalidPermission = permissionIds.Any(id => !validPermissionsIds.Contains(id)); | |
return !atLeastOneInvalidPermission; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment