ArrayTensorExtensionsTests.cs 3.34 KB
Newer Older
gaoqiong's avatar
gaoqiong committed
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using Xunit;
using Microsoft.ML.OnnxRuntime.Tensors;
using System.Collections.Generic;
using System.Linq;

namespace Microsoft.ML.OnnxRuntime.Tests.ArrayTensorExtensions
{
    public class ArrayTensorExtensionsTests
    {
        static void CheckValues(IEnumerable<int> expected, DenseTensor<int> tensor) 
        {
            foreach (var pair in expected.Zip(tensor.Buffer.ToArray(), Tuple.Create))
            {
                Assert.Equal(pair.Item1, pair.Item2);
            }
        }

        [Fact]
        public void ConstructFrom1D()
        {
            var array = new int[] { 1, 2, 3, 4 };
            var tensor = array.ToTensor();

            var expectedDims = new int[] { 4 };
            Assert.Equal(tensor.Length, array.Length);
            Assert.Equal(expectedDims, tensor.Dimensions.ToArray());
            CheckValues(array.Cast<int>(), tensor);
        }

        [Fact]
        public void ConstructFrom2D()
        {
            var array = new int[,] { { 1, 2 } , { 3, 4 } };
            var tensor = array.ToTensor();

            var expectedDims = new int[] { 2, 2 };
            Assert.Equal(tensor.Length, array.Length);
            Assert.Equal(expectedDims, tensor.Dimensions.ToArray());
            CheckValues(array.Cast<int>(), tensor);
        }

        [Fact]
        public void ConstructFrom3D()
        {
            var array = new int[,,] { { { 1, 2 }, { 3, 4 } }, 
                                      { { 5, 6 }, { 7, 8 } } };
            var tensor = array.ToTensor();

            var expectedDims = new int[] { 2, 2, 2 };
            Assert.Equal(tensor.Length, array.Length);
            Assert.Equal(expectedDims, tensor.Dimensions.ToArray());
            CheckValues(array.Cast<int>(), tensor);
        }

        [Fact]
        public void ConstructFrom3DWithDim1()
        {
            var array = new int[,,] { { { 1, 2 } }, 
                                      { { 3, 4 } } };
            var tensor = array.ToTensor();

            var expectedDims = new int[] { 2, 1, 2 };
            Assert.Equal(tensor.Length, array.Length);
            Assert.Equal(expectedDims, tensor.Dimensions.ToArray());
            CheckValues(array.Cast<int>(), tensor);
        }

        [Fact]
        public void ConstructFrom4D()
        {
            var array = new int[,,,] {
                { { { 1, 2 }, { 3, 4 } },
                  { { 5, 6 }, { 7, 8 } } }
            };
            var tensor = array.ToTensor();

            var expectedDims = new int[] { 1, 2, 2, 2 };
            Assert.Equal(tensor.Length, array.Length);
            Assert.Equal(expectedDims, tensor.Dimensions.ToArray());
            CheckValues(array.Cast<int>(), tensor);
        }

        [Fact]
        public void ConstructFrom5D()
        {
            var array = new int[,,,,] {
                { { { { 1, 2 }, { 3, 4 } },
                    { { 5, 6 }, { 7, 8 } } } }
            };

            // 5D requires cast to Array
            Array a = (Array)array;
            var tensor = a.ToTensor<int>();

            var expectedDims = new int[] { 1, 1, 2, 2, 2 };
            Assert.Equal(tensor.Length, array.Length);
            Assert.Equal(expectedDims, tensor.Dimensions.ToArray());
            CheckValues(array.Cast<int>(), tensor);
        }
    }
}