File: MultiDimArrayTest.cs

package info (click to toggle)
robotraconteur 1.2.7-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 101,380 kB
  • sloc: cpp: 1,149,268; cs: 87,653; java: 58,127; python: 26,897; ansic: 356; sh: 152; makefile: 90; xml: 51
file content (136 lines) | stat: -rw-r--r-- 4,758 bytes parent folder | download
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RobotRaconteur;
using System.IO;

namespace RobotRaconteurNETTest
{
class MultiDimArrayTest
{
    public static string testdatapath = "";

    public static void Test()
    {
        TestDouble();
        TestByte();
    }

    public static void TestDouble()
    {
        // cSpell: ignore testmdarray
        MultiDimArray m1 = LoadDoubleArrayFromFile(Path.Combine(testdatapath, "testmdarray1.bin"));
        MultiDimArray m2 = LoadDoubleArrayFromFile(Path.Combine(testdatapath, "testmdarray2.bin"));
        MultiDimArray m3 = LoadDoubleArrayFromFile(Path.Combine(testdatapath, "testmdarray3.bin"));
        MultiDimArray m4 = LoadDoubleArrayFromFile(Path.Combine(testdatapath, "testmdarray4.bin"));
        MultiDimArray m5 = LoadDoubleArrayFromFile(Path.Combine(testdatapath, "testmdarray5.bin"));

        m1.AssignSubArray(new uint[] { 2, 2, 3, 3, 4 }, m2, new uint[] { 0, 2, 0, 0, 0 }, new uint[] { 1, 5, 5, 2, 1 });

        ca<double>((double[])m1.Array_, (double[])m3.Array_);

        MultiDimArray m6 = new MultiDimArray(new uint[] { 2, 2, 1, 1, 10 }, new double[40]);
        m1.RetrieveSubArray(new uint[] { 4, 2, 2, 8, 0 }, m6, new uint[] { 0, 0, 0, 0, 0 },
                            new uint[] { 2, 2, 1, 1, 10 });
        ca<double>((double[])m4.Array_, (double[])m6.Array_);

        MultiDimArray m7 = new MultiDimArray(new uint[] { 4, 4, 4, 4, 10 }, new double[2560]);
        m1.RetrieveSubArray(new uint[] { 4, 2, 2, 8, 0 }, m7, new uint[] { 2, 1, 2, 1, 0 },
                            new uint[] { 2, 2, 1, 1, 10 });
        ca<double>((double[])m5.Array_, (double[])m7.Array_);
    }

    public static void TestByte()
    {
        MultiDimArray m1 = LoadByteArrayFromFile(Path.Combine(testdatapath, "testmdarray_b1.bin"));
        MultiDimArray m2 = LoadByteArrayFromFile(Path.Combine(testdatapath, "testmdarray_b2.bin"));
        MultiDimArray m3 = LoadByteArrayFromFile(Path.Combine(testdatapath, "testmdarray_b3.bin"));
        MultiDimArray m4 = LoadByteArrayFromFile(Path.Combine(testdatapath, "testmdarray_b4.bin"));
        MultiDimArray m5 = LoadByteArrayFromFile(Path.Combine(testdatapath, "testmdarray_b5.bin"));

        m1.AssignSubArray(new uint[] { 50, 100 }, m2, new uint[] { 20, 25 }, new uint[] { 200, 200 });

        ca<byte>((byte[])m1.Array_, (byte[])m3.Array_);

        MultiDimArray m6 = new MultiDimArray(new uint[] { 200, 200 }, new byte[40000]);
        m1.RetrieveSubArray(new uint[] { 65, 800 }, m6, new uint[] { 0, 0 }, new uint[] { 200, 200 });
        ca<byte>((byte[])m4.Array_, (byte[])m6.Array_);

        MultiDimArray m7 = new MultiDimArray(new uint[] { 512, 512 }, new byte[512 * 512]);
        m1.RetrieveSubArray(new uint[] { 65, 800 }, m7, new uint[] { 100, 230 }, new uint[] { 200, 200 });
        ca<byte>((byte[])m5.Array_, (byte[])m7.Array_);
    }

    public static void ca<T>(T[] v1, T[] v2)
        where T : IComparable, IComparable<T>
    {
        RRAssert.AreEqual(v1.Length, v2.Length);

        for (int i = 0; i < v1.Length; i++)
        {
            RRAssert.AreEqual<T>(v1[i], v2[i]);
        }
    }

    public static MultiDimArray LoadDoubleArrayFromFile(string fname)
    {
        FileStream f = new FileStream(fname, FileMode.Open, FileAccess.Read);
        MultiDimArray a = LoadDoubleArray(f);
        f.Close();
        return a;
    }

    public static MultiDimArray LoadDoubleArray(Stream s)
    {
        BinaryReader r = new BinaryReader(s);
        int dimcount = r.ReadInt32();
        uint[] dims = new uint[dimcount];
        uint count = 1;
        for (int i = 0; i < dimcount; i++)
        {
            dims[i] = (uint)r.ReadInt32();
            count *= dims[i];
        }

        double[] real = new double[count];

        for (uint i = 0; i < count; i++)
        {
            real[i] = r.ReadDouble();
        }

        return new MultiDimArray(dims, real);
    }

    public static MultiDimArray LoadByteArrayFromFile(string fname)
    {
        FileStream f = new FileStream(fname, FileMode.Open, FileAccess.Read);
        MultiDimArray a = LoadByteArray(f);
        f.Close();
        return a;
    }

    public static MultiDimArray LoadByteArray(Stream s)
    {
        BinaryReader r = new BinaryReader(s);
        int dimcount = r.ReadInt32();
        uint[] dims = new uint[dimcount];
        uint count = 1;
        for (int i = 0; i < dimcount; i++)
        {
            dims[i] = (uint)r.ReadInt32();
            count *= dims[i];
        }

        byte[] real = new byte[count];

        for (int i = 0; i < count; i++)
        {
            real[i] = r.ReadByte();
        }

        return new MultiDimArray(dims, real);
    }
}
}