public static int?chooseBestSum(int t, int k, List <int> ls) { int best = 0; var combinations = IterTools.Combinations(ls, k); foreach (var c in combinations) { best = (c.Sum() > best && c.Sum() <= t) ? c.Sum() : best; } if (best > 0) { return(best); } else { return(null); } }
[InlineData(3, 2, 5, 1, 6)] // Use page size 3 to allow insertions without bounds changes. public async Task Execute(int maxLeavesPerPage, int minExisting, int maxExisting, int minUpdated, int maxUpdated) { var config = new Catalog2RegistrationConfiguration(); var options = new SimpleOptions <Catalog2RegistrationConfiguration>(config); var logger = new NullLogger <HiveMerger>(); var target = new HiveMerger(options, logger); config.MaxLeavesPerPage = maxLeavesPerPage; var allExisting = Enumerable.Range(minExisting, (maxExisting - minExisting) + 1).Select(m => new NuGetVersion(m, 0, 0)).ToList(); var allUpdated = Enumerable.Range(minUpdated, (maxUpdated - minUpdated) + 1).Select(m => new NuGetVersion(m, 0, 0)).ToList(); var versionToNormalized = allExisting.Concat(allUpdated).Distinct().ToDictionary(v => v, v => v.ToNormalizedString()); // Enumerate all of the updated version sets, which is up to 9 versions and for each set every // combination of either PackageDelete or PackageDetails for each version. var deleteOrNotDelete = new[] { true, false }; var updatedCases = IterTools .SubsetsOf(allUpdated) .SelectMany(vs => IterTools.CombinationsOfTwo(vs.ToList(), deleteOrNotDelete)) .Select(ts => ts.Select(t => new VersionAction(t.Item1, t.Item2)).OrderBy(v => v.Version).ToList()) .ToList(); // Enumerate all of the updated version sets, which is up to 7 versions. var existingCases = IterTools .SubsetsOf(allExisting) .Select(vs => vs.OrderBy(v => v).ToList()) .ToList(); // Build all of the test cases which is every pairing of updated and existing cases. var testCases = new ConcurrentBag <TestCase>(); foreach (var existing in existingCases) { foreach (var updated in updatedCases) { testCases.Add(new TestCase(existing, updated)); } } var completed = 0; var total = testCases.Count; var outputLock = new object(); await ParallelAsync .Repeat(async() => { await Task.Yield(); while (testCases.TryTake(out var testCase)) { try { await ExecuteTestCaseAsync(config, target, versionToNormalized, testCase); var thisCompleted = Interlocked.Increment(ref completed); if (thisCompleted % 20000 == 0) { lock (outputLock) { _output.WriteLine($"Progress: {1.0 * thisCompleted / total:P}"); } } } catch (Exception ex) { lock (outputLock) { _output.WriteLine(string.Empty); _output.WriteLine("Test Case Failure"); _output.WriteLine(new string('=', 50)); _output.WriteLine(ex.Message); _output.WriteLine(string.Empty); _output.WriteLine("Existing: " + string.Join(", ", testCase.Existing)); _output.WriteLine("Changes: " + string.Join(", ", testCase.Updated.Select(t => $"{(t.IsDelete ? '-' : '+')}{t.Version}"))); _output.WriteLine(new string('=', 50)); _output.WriteLine(string.Empty); return; } } } }, degreeOfParallelism : 8); _output.WriteLine($"Progress: {1.0 * completed / total:P}"); _output.WriteLine($"Total test cases: {total}"); _output.WriteLine($"Completed test cases: {completed}"); Assert.Equal(total, completed); }