-
-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
LowerBound compare function to return int to allow for early exit. Ad…
…ded tests
- Loading branch information
1 parent
d9d162d
commit 127ed8c
Showing
6 changed files
with
94 additions
and
86 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using FineCodeCoverage.Engine.Model; | ||
using NUnit.Framework; | ||
|
||
namespace FineCodeCoverageTests | ||
{ | ||
internal class FileLineCoverage_Tests | ||
{ | ||
[TestCaseSource(nameof(Cases))] | ||
public void GetLines_Test(IEnumerable<int> lineNumbers, int startLineNumber, int endLineNumber, IEnumerable<int> expectedLineNumbers) | ||
{ | ||
var fileLineCoverage = new FileLineCoverage(); | ||
fileLineCoverage.Add("fp", lineNumbers.Select(n => new FineCodeCoverage.Engine.Cobertura.Line { Number = n })); | ||
fileLineCoverage.Completed(); | ||
|
||
var lines = fileLineCoverage.GetLines("fp", startLineNumber, endLineNumber); | ||
Assert.That(lines.Select(l => l.Number), Is.EqualTo(expectedLineNumbers)); | ||
} | ||
|
||
static object[] Cases = | ||
{ | ||
new object[] { new int[] { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}, 19, 20, new int[]{ 19,20} }, | ||
new object[] { new int[] { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}, 12, 13, new int[]{ 12,13} }, | ||
new object[] { new int[] { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}, 6, 7, new int[]{ 6,7} }, | ||
new object[] {Enumerable.Empty<int>(), 0, 4,Enumerable.Empty<int>() }, | ||
new object[] { new int[] { 3,2,1}, 0, 4, new int[]{ 1,2,3} }, | ||
new object[] { new int[] { 3,2,1}, 0, 3, new int[]{ 1,2,3} }, | ||
new object[] { new int[] { 3,2,1}, 1, 2, new int[]{ 1,2} }, | ||
new object[] { new int[] { 3,2,1}, 2, 2, new int[]{ 2} }, | ||
new object[] { new int[] { 3,2,1}, 4, 5, Enumerable.Empty<int>() } | ||
}; | ||
} | ||
} |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace FineCodeCoverage.Core.Utilities | ||
{ | ||
public static class ListExtensions | ||
{ | ||
// To be performed on a sorted list | ||
// Returns -1 for empty list or when all elements are outside the lower bounds | ||
// Compare fn to return 0 for element considered the lower bound | ||
// > 0 for lower bound greater than element | ||
|
||
public static int LowerBound<T>(this IList<T> list, Func<T, int> compare) | ||
{ | ||
int first = 0; | ||
int count = list.Count; | ||
if (count == 0) return -1; | ||
|
||
while (count > 0) | ||
{ | ||
int step = count / 2; | ||
int index = first + step; | ||
var result = compare(list[index]); | ||
if (result == 0) | ||
{ | ||
return index; | ||
} | ||
else if (result > 0) | ||
{ | ||
first = ++index; | ||
count -= step + 1; | ||
} | ||
else | ||
{ | ||
count = step; | ||
} | ||
} | ||
|
||
return first != list.Count ? first : -1; | ||
} | ||
} | ||
} |
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