forked from SciSharp/NumSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestClass.cs
More file actions
68 lines (62 loc) · 2.72 KB
/
TestClass.cs
File metadata and controls
68 lines (62 loc) · 2.72 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
61
62
63
64
65
66
67
68
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NumSharp.Backends.Unmanaged;
namespace NumSharp.UnitTest
{
public class TestClass
{
public void AssertAreEqual(object expected, object given, string msg = null)
{
if (expected is string)
Assert.AreEqual(expected, given, msg ?? $"Expected '{expected}', given '{given}'");
else if (expected is Array && given is Array)
AssertSequenceEqual(expected as Array, given as Array);
else if (expected is ICollection && given is ICollection)
AssertSequenceEqual(expected as ICollection, given as ICollection);
else if (expected is IArraySlice && given is IArraySlice)
AssertSequenceEqual((expected as IArraySlice).ToArray(), (given as IArraySlice).ToArray());
else if (expected is IArraySlice && given is Array)
AssertSequenceEqual((expected as IArraySlice).ToArray(), given as Array);
else if (expected is Array && given is IArraySlice)
AssertSequenceEqual((expected as Array), (given as IArraySlice).ToArray());
else
Assert.AreEqual(expected, given, msg ?? $"Expected '{expected}', given '{given}'");
}
private void AssertSequenceEqual(ICollection a, ICollection b)
{
AssertSequenceEqual(a.OfType<object>().ToArray(), b.OfType<object>().ToArray());
}
private void AssertSequenceEqual(Array a, Array b)
{
Assert.AreEqual(a.Length, b.Length, $"Arrays are not of same length. Expected '{a.Length}', given '{b.Length}'");
for (int i = 0; i < a.Length; i++)
AssertAreEqual(a.GetValue(i), b.GetValue(i), $"Elements at index {i} differ. Expected '{a.GetValue(i)}', given '{b.GetValue(i)}'");
}
public NDArray arange(params int[] dims)
{
var rshape = new Shape(dims);
return np.arange(rshape.size).reshape(rshape);
}
public NDArray arange(ITuple tup)
{
var rshape = new Shape(yield(tup).ToArray());
return np.arange(rshape.size).reshape(rshape);
}
public NDArray array(ITuple tuple, params int[] vals)
{
return np.array(vals).reshape(yield(tuple).ToArray());
}
private IEnumerable<int> yield(ITuple tup)
{
for (int i = 0; i < tup.Length; i++)
{
yield return (int) tup[i];
}
}
}
}