Skip to content

Commit 5831b7d

Browse files
authored
Add missing tests and documentation in Utilities (#335)
1 parent a9fa9bf commit 5831b7d

File tree

6 files changed

+241
-1
lines changed

6 files changed

+241
-1
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using FluentAssertions;
4+
using NUnit.Framework;
5+
using Utilities.Extensions;
6+
7+
namespace Utilities.Tests.Extensions
8+
{
9+
public class DictionaryExtensionsTests
10+
{
11+
[Test]
12+
public void AddMany_ShouldThrowArgumentException_WhenKeyAlreadyExists()
13+
{
14+
var dictionary = new Dictionary<string, int> { ["one"] = 1 };
15+
var enumerable = new[] { ("one", 1), ("two", 2) };
16+
17+
var action = () => dictionary.AddMany(enumerable);
18+
19+
action.Should().Throw<ArgumentException>();
20+
}
21+
22+
[Test]
23+
public void AddMany_ShouldAddAllKeyValuePairs()
24+
{
25+
var dictionary = new Dictionary<string, int> { ["one"] = 1 };
26+
var enumerable = new[] { ("two", 2), ("three", 3) };
27+
28+
dictionary.AddMany(enumerable);
29+
30+
dictionary.Should().HaveCount(3);
31+
32+
dictionary.Should().ContainKey("one").WhichValue.Should().Be(1);
33+
dictionary.Should().ContainKey("two").WhichValue.Should().Be(2);
34+
dictionary.Should().ContainKey("three").WhichValue.Should().Be(3);
35+
}
36+
}
37+
}

Utilities.Tests/Extensions/MatrixExtensionsTests.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,5 +179,33 @@ public void Subtract_ShouldCalculateSubtractionResult(
179179
double[,] operand,
180180
double[,] result) =>
181181
source.Subtract(operand).Should().BeEquivalentTo(result);
182+
183+
[Test]
184+
public void RoundToNextInt_ShouldReturnRoundedMatrix()
185+
{
186+
var source = new[,]
187+
{
188+
{ -1.9, 1.9 },
189+
{ -1.5, 1.5 },
190+
{ -1.1, 1.1 },
191+
{ -0.9, 0.9 },
192+
{ -0.5, 0.5 },
193+
{ -0.1, 0.1 },
194+
};
195+
196+
var result = new double[,]
197+
{
198+
{ -2, 2 },
199+
{ -2, 2 },
200+
{ -1, 1 },
201+
{ -1, 1 },
202+
{ 0, 0 },
203+
{ 0, 0 },
204+
};
205+
206+
var actualResult = source.RoundToNextInt();
207+
208+
actualResult.Should().BeEquivalentTo(result);
209+
}
182210
}
183211
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
using FluentAssertions;
3+
using NUnit.Framework;
4+
using Utilities.Extensions;
5+
6+
namespace Utilities.Tests.Extensions
7+
{
8+
public class RandomExtensionsTests
9+
{
10+
[Test]
11+
public void NextVector_ShouldReturnNormalizedVector()
12+
{
13+
var random = new Random(0);
14+
15+
var result = random.NextVector(10);
16+
17+
result.Length.Should().Be(10);
18+
result.Magnitude().Should().BeApproximately(1.0, 1e-6);
19+
}
20+
}
21+
}
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
using System;
2+
using FluentAssertions;
3+
using NUnit.Framework;
4+
using Utilities.Extensions;
5+
6+
namespace Utilities.Tests.Extensions
7+
{
8+
public class VectorExtensionsTests
9+
{
10+
[Test]
11+
public void Copy_ShouldReturnCopyOfVector()
12+
{
13+
var vector = new double[] { 0, 1, 2, 3 };
14+
15+
var vectorCopy = vector.Copy();
16+
17+
vectorCopy.Should().BeEquivalentTo(vector);
18+
vectorCopy.Should().NotBeSameAs(vector);
19+
}
20+
21+
[Test]
22+
public void OuterProduct_ShouldCalculateOuterProduct()
23+
{
24+
var lhs = new double[] { -2, -1, 0, 1, 2 };
25+
var rhs = new double[] { 1, 2, 3 };
26+
27+
var result = new double[,]
28+
{
29+
{ -2, -4, -6 },
30+
{ -1, -2, -3 },
31+
{ 0, 0, 0 },
32+
{ 1, 2, 3 },
33+
{ 2, 4, 6 },
34+
};
35+
36+
var actualResult = lhs.OuterProduct(rhs);
37+
38+
actualResult.Should().BeEquivalentTo(result);
39+
}
40+
41+
[Test]
42+
public void Dot_ShouldThrowArgumentException_WhenDimensionsDoNotMatch()
43+
{
44+
var lhs = new double[] { 1, 2, 3 };
45+
var rhs = new double[] { 1, 2, 3, 4 };
46+
47+
var func = () => lhs.Dot(rhs);
48+
49+
func.Should().Throw<ArgumentException>()
50+
.WithMessage("Dot product arguments must have same dimension");
51+
}
52+
53+
[Test]
54+
public void Dot_ShouldCalculateDotProduct()
55+
{
56+
var lhs = new double[] { 1, 2, 3 };
57+
var rhs = new double[] { 4, 5, 6 };
58+
59+
var actualResult = lhs.Dot(rhs);
60+
61+
actualResult.Should().Be(32);
62+
}
63+
64+
[Test]
65+
public void Magnitude_ShouldCalculateMagnitude()
66+
{
67+
var vector = new double[] { -3, 4 };
68+
69+
var actualResult = vector.Magnitude();
70+
71+
actualResult.Should().BeApproximately(5.0, 0.0001);
72+
}
73+
74+
[Test]
75+
public void Scale_ShouldCalculateScale()
76+
{
77+
var vector = new double[] { -1, 0, 1 };
78+
var factor = 2;
79+
80+
var result = new double[] { -2, 0, 2 };
81+
82+
var actualResult = vector.Scale(factor);
83+
84+
actualResult.Should().BeEquivalentTo(result);
85+
}
86+
87+
[Test]
88+
public void ToColumnVector_ShouldReturnColumnVector()
89+
{
90+
var vector = new double[] { 1, 2, 3, 4 };
91+
var result = new double[,] { { 1 }, { 2 }, { 3 }, { 4 } };
92+
93+
var actualResult = vector.ToColumnVector();
94+
95+
actualResult.Should().BeEquivalentTo(result);
96+
}
97+
98+
[Test]
99+
public void ToRowVector_ShouldThrowInvalidOperationException_WhenSourceIsNotAColumnVector()
100+
{
101+
var source = new double[,] { { 1, 2 }, { 3, 4 }, { 5, 6 } };
102+
103+
var func = () => source.ToRowVector();
104+
105+
func.Should().Throw<InvalidOperationException>()
106+
.WithMessage("The column vector should have only 1 element in width.");
107+
}
108+
109+
[Test]
110+
public void ToRowVector_ShouldReturnRowVector()
111+
{
112+
var source = new double[,] { { 1 }, { 2 }, { 3 }, { 4 } };
113+
var result = new double[] { 1, 2, 3, 4 };
114+
115+
var actualResult = source.ToRowVector();
116+
117+
actualResult.Should().BeEquivalentTo(result);
118+
}
119+
120+
[Test]
121+
public void ToDiagonalMatrix_ShouldReturnDiagonalMatrix()
122+
{
123+
var source = new double[] { 1, 2, 3, 4 };
124+
var result = new double[,]
125+
{
126+
{ 1, 0, 0, 0 },
127+
{ 0, 2, 0, 0 },
128+
{ 0, 0, 3, 0 },
129+
{ 0, 0, 0, 4 },
130+
};
131+
132+
var actualResult = source.ToDiagonalMatrix();
133+
134+
actualResult.Should().BeEquivalentTo(result);
135+
}
136+
}
137+
}

Utilities/Extensions/DictionaryExtensions.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23

34
namespace Utilities.Extensions
45
{
56
public static class DictionaryExtensions
67
{
8+
/// <summary>
9+
/// Adds the specified key value tuples to the dictionary.
10+
/// </summary>
11+
/// <param name="keys">The dictionary.</param>
12+
/// <param name="enumerable">The collection of key value tuples to add.</param>
13+
/// <typeparam name="TKey">The type of the keys in the dictionary.</typeparam>
14+
/// <typeparam name="TValue">The type of the values in the dictionary.</typeparam>
15+
/// <exception cref="ArgumentException">
16+
/// A key from the <paramref name="enumerable"/> already exists in <paramref name="keys"/>.
17+
/// </exception>
718
public static void AddMany<TKey, TValue>(
819
this Dictionary<TKey, TValue> keys,
920
IEnumerable<(TKey, TValue)> enumerable) where TKey : notnull

Utilities/Extensions/RandomExtensions.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ namespace Utilities.Extensions
55
{
66
public static class RandomExtensions
77
{
8+
/// <summary>
9+
/// Returns a random normalized vector of the specified size.
10+
/// </summary>
11+
/// <param name="rand">The random number generator.</param>
12+
/// <param name="size">The size of the vector to return.</param>
13+
/// <returns>A random normalized vector.</returns>
814
public static double[] NextVector(this Random rand, int size)
915
{
1016
var vector = Enumerable.Range(0, size)

0 commit comments

Comments
 (0)