-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCustomOffsetArrays.cs
99 lines (81 loc) · 3.38 KB
/
CustomOffsetArrays.cs
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
using Xunit;
namespace System.Runtime.Serialization.BinaryFormat.Tests;
public class CustomOffsetArrays : ReadTests
{
[Fact]
public void CanReadSingleDimensionalArrayOfIntegersWithCustomOffset()
{
const int lowerBound = 1;
Array input = Array.CreateInstance(typeof(int), lengths: [3], lowerBounds: [lowerBound]);
for (int i = lowerBound; i < lowerBound + input.Length; i++)
{
input.SetValue(value: i, index: i);
}
ArrayRecord arrayRecord = (ArrayRecord)PayloadReader.Read(Serialize(input));
Assert.Equal(input, arrayRecord.ToArray(input.GetType()));
}
[Fact]
public void CanReadRectangularArrayOfStringsWithCustomOffsets()
{
const int lowerBound = 10;
Array input = Array.CreateInstance(typeof(string), lengths: [7, 5], lowerBounds: [lowerBound, lowerBound]);
for (int i = lowerBound; i < lowerBound + input.GetLength(0); i++)
{
for (int j = lowerBound; j < lowerBound + input.GetLength(1); j++)
{
input.SetValue(value: $"{i}. {j}", index1: i, index2: j);
}
}
ArrayRecord arrayRecord = (ArrayRecord)PayloadReader.Read(Serialize(input));
Assert.Equal(input, arrayRecord.ToArray(input.GetType()));
}
[Serializable]
public class ComplexType3D
{
public int I, J, K;
}
[Fact]
public void CanReadRectangularArraysOfComplexTypes_3D()
{
const int lowerBoundI = 1, lowerBoundJ = 2, lowerBoundK = 2;
Array input = Array.CreateInstance(typeof(ComplexType3D),
lengths: [17, 5, 3], lowerBounds: [lowerBoundI, lowerBoundJ, lowerBoundK]);
for (int i = 0; i < input.GetLength(0); i++)
{
for (int j = 0; j < input.GetLength(1); j++)
{
for (int k = 0; k < input.GetLength(2); k++)
{
input.SetValue(
new ComplexType3D() { I = i, J = j, K = k },
i + lowerBoundI, j + lowerBoundJ, k + lowerBoundK);
}
}
}
ArrayRecord arrayRecord = (ArrayRecord)PayloadReader.Read(Serialize(input));
Assert.Equal((uint)input.Length, arrayRecord.Length);
Array output = arrayRecord.ToArray(input.GetType());
for (int i = 0; i < input.GetLength(0); i++)
{
for (int j = 0; j < input.GetLength(1); j++)
{
for (int k = 0; k < input.GetLength(2); k++)
{
ComplexType3D expected = (ComplexType3D)input.GetValue(i + lowerBoundI, j + lowerBoundJ, k + lowerBoundK)!;
ClassRecord got = (ClassRecord)output.GetValue(i + lowerBoundI, j + lowerBoundJ, k + lowerBoundK)!;
Assert.Equal(expected.I, got.GetInt32(nameof(ComplexType3D.I)));
Assert.Equal(expected.J, got.GetInt32(nameof(ComplexType3D.J)));
Assert.Equal(expected.K, got.GetInt32(nameof(ComplexType3D.K)));
}
}
}
}
[Fact]
public void JaggedCustomOffset()
{
Array input = Array.CreateInstance(typeof(uint[]), [5], [1]);
ArrayRecord arrayRecord = (ArrayRecord)PayloadReader.Read(Serialize(input));
Array output = arrayRecord.ToArray(expectedArrayType: input.GetType());
Assert.Equal(input, output);
}
}