-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathJumpSearcherTests.cs
67 lines (55 loc) · 2.17 KB
/
JumpSearcherTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using Algorithms.Search;
using NUnit.Framework;
using System;
using System.Linq;
using FluentAssertions;
namespace Algorithms.Tests.Search;
public class JumpSearcherTests
{
[Test]
public void FindIndex_ItemPresent_ItemCorrect([Random(1, 1000, 100)] int n)
{
// Arrange
var searcher = new JumpSearcher<int>();
var sortedArray = Enumerable.Range(0, n).Select(_ => TestContext.CurrentContext.Random.Next(1_000_000)).OrderBy(x => x).ToArray();
var expectedIndex = TestContext.CurrentContext.Random.Next(sortedArray.Length);
// Act
var actualIndex = searcher.FindIndex(sortedArray, sortedArray[expectedIndex]);
// Assert
sortedArray[actualIndex].Should().Be(sortedArray[expectedIndex]);
}
[Test]
public void FindIndex_ItemMissing_MinusOneReturned([Random(1, 1000, 10)] int n, [Random(-100, 1100, 10)] int missingItem)
{
// Arrange
var searcher = new JumpSearcher<int>();
var sortedArray = Enumerable.Range(0, n).Select(_ => TestContext.CurrentContext.Random.Next(1_000_000)).Where(x => x != missingItem).OrderBy(x => x).ToArray();
var expectedIndex = -1;
// Act
var actualIndex = searcher.FindIndex(sortedArray, missingItem);
// Assert
Assert.That(actualIndex, Is.EqualTo(expectedIndex));
}
[Test]
public void FindIndex_ArrayEmpty_MinusOneReturned([Random(-100, 1100, 10)] int missingItem)
{
// Arrange
var searcher = new JumpSearcher<int>();
var sortedArray = Array.Empty<int>();
var expectedIndex = -1;
// Act
var actualIndex = searcher.FindIndex(sortedArray, missingItem);
// Assert
Assert.That(actualIndex, Is.EqualTo(expectedIndex));
}
[TestCase(null, "abc")]
[TestCase(new[] { "abc", "def", "ghi" }, null)]
[TestCase(null, null)]
public void FindIndex_ArrayNull_ItemNull_ArgumentNullExceptionThrown(string[] sortedArray, string searchItem)
{
// Arrange
var searcher = new JumpSearcher<string>();
// Act, Assert
_ = Assert.Throws<ArgumentNullException>(() => searcher.FindIndex(sortedArray, searchItem));
}
}