-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBinaryIndexedTreeTests.cs
More file actions
60 lines (53 loc) · 1.73 KB
/
Copy pathBinaryIndexedTreeTests.cs
File metadata and controls
60 lines (53 loc) · 1.73 KB
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
using System;
using System.Linq;
using Xunit;
namespace AlgorithmsAndDataStructures.Tests.DataStructures.BinaryIndexedTree;
public class BinaryIndexedTreeTests
{
[Fact]
public void CanConstructFromArray()
{
var sut = AlgorithmsAndDataStructures.DataStructures.BinaryIndexedTrees.BinaryIndexedTree.FromArray(new[]
{ 1, 2, 3, 4, 5 });
Assert.Equal(15, sut.GetSum(4));
}
[Fact]
public void CanUpdateValue()
{
var sut = AlgorithmsAndDataStructures.DataStructures.BinaryIndexedTrees.BinaryIndexedTree.FromArray(new[]
{ 1, 2, 3, 4, 5 });
Assert.Equal(15, sut.GetSum(4));
sut.SetValue(2, 2);
Assert.Equal(17, sut.GetSum(4));
}
[Fact]
public void CanQueryRangeValue()
{
var sut = AlgorithmsAndDataStructures.DataStructures.BinaryIndexedTrees.BinaryIndexedTree.FromArray(new[]
{ 1, 2, 3, 4, 5 });
Assert.Equal(3, sut.GetSum(0, 1));
Assert.Equal(6, sut.GetSum(0, 2));
Assert.Equal(10, sut.GetSum(0, 3));
Assert.Equal(15, sut.GetSum(0, 4));
Assert.Equal(12, sut.GetSum(2, 4));
}
[Fact]
public void RangeSumIsCorrect()
{
var sut = new AlgorithmsAndDataStructures.DataStructures.BinaryIndexedTrees.BinaryIndexedTree();
var random = new Random();
var data = new int[99];
for (var i = 0; i < 99; i++)
{
var number = random.Next(200);
data[i] = number;
sut.SetValue(i, number);
}
for (var i = 0; i < 100; i++)
{
var range = random.Next(0, 98);
var expectedSum = data.Take(range + 1).Sum();
Assert.Equal(expectedSum, sut.GetSum(range));
}
}
}