/
DeltaStreamReaderTests.cs
176 lines (157 loc) · 6.46 KB
/
DeltaStreamReaderTests.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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
using System;
using System.Collections.ObjectModel;
using System.IO;
using Nerdbank.GitVersioning.ManagedGit;
using Xunit;
namespace ManagedGit
{
// Test case borrowed from https://stefan.saasen.me/articles/git-clone-in-haskell-from-the-bottom-up/#format-of-the-delta-representation
public class DeltaStreamReaderTests
{
[Fact]
public void ReadCopyInstruction()
{
using (Stream stream = new MemoryStream(
new byte[]
{
0b_10110000,
0b_11010001,
0b_00000001
}))
{
var instruction = DeltaStreamReader.Read(stream).Value;
Assert.Equal(DeltaInstructionType.Copy, instruction.InstructionType);
Assert.Equal(0, instruction.Offset);
Assert.Equal(465, instruction.Size);
}
}
[Fact]
public void ReadCopyInstruction_Memory()
{
var stream = new byte[]
{
0b_10110000,
0b_11010001,
0b_00000001
};
var memory = new ReadOnlyMemory<byte>(stream);
var instruction = DeltaStreamReader.Read(ref memory).Value;
Assert.Equal(0, memory.Length);
Assert.Equal(DeltaInstructionType.Copy, instruction.InstructionType);
Assert.Equal(0, instruction.Offset);
Assert.Equal(465, instruction.Size);
}
[Fact]
public void ReadInsertInstruction()
{
using (Stream stream = new MemoryStream(new byte[] { 0b_00010111 }))
{
var instruction = DeltaStreamReader.Read(stream).Value;
Assert.Equal(DeltaInstructionType.Insert, instruction.InstructionType);
Assert.Equal(0, instruction.Offset);
Assert.Equal(23, instruction.Size);
}
}
[Fact]
public void ReadInsertInstruction_Memory()
{
var stream = new byte[] { 0b_00010111 };
var memory = new ReadOnlyMemory<byte>(stream);
var instruction = DeltaStreamReader.Read(ref memory).Value;
Assert.Equal(0, memory.Length);
Assert.Equal(DeltaInstructionType.Insert, instruction.InstructionType);
Assert.Equal(0, instruction.Offset);
Assert.Equal(23, instruction.Size);
}
[Fact]
public void ReadStreamTest()
{
using (Stream stream = new MemoryStream(
new byte[]
{
0b_10110011, 0b_11001110, 0b_00000001, 0b_00100111, 0b_00000001,
0b_10110011, 0b_01011111, 0b_00000011, 0b_01101100, 0b_00010000, 0b_10010011,
0b_11110101, 0b_00000010, 0b_01101011, 0b_10110011, 0b_11001011, 0b_00010011,
0b_01000110, 0b_00000011}))
{
Collection<DeltaInstruction> instructions = new Collection<DeltaInstruction>();
DeltaInstruction? current;
while ((current = DeltaStreamReader.Read(stream)) is not null)
{
instructions.Add(current.Value);
}
Assert.Collection(
instructions,
instruction =>
{
Assert.Equal(DeltaInstructionType.Copy, instruction.InstructionType);
Assert.Equal(462, instruction.Offset);
Assert.Equal(295, instruction.Size);
},
instruction =>
{
Assert.Equal(DeltaInstructionType.Copy, instruction.InstructionType);
Assert.Equal(863, instruction.Offset);
Assert.Equal(4204, instruction.Size);
},
instruction =>
{
Assert.Equal(DeltaInstructionType.Copy, instruction.InstructionType);
Assert.Equal(757, instruction.Offset);
Assert.Equal(107, instruction.Size);
},
instruction =>
{
Assert.Equal(DeltaInstructionType.Copy, instruction.InstructionType);
Assert.Equal(5067, instruction.Offset);
Assert.Equal(838, instruction.Size);
});
}
}
[Fact]
public void ReadStreamTest_Memory()
{
var stream =
new byte[]
{
0b_10110011, 0b_11001110, 0b_00000001, 0b_00100111, 0b_00000001,
0b_10110011, 0b_01011111, 0b_00000011, 0b_01101100, 0b_00010000, 0b_10010011,
0b_11110101, 0b_00000010, 0b_01101011, 0b_10110011, 0b_11001011, 0b_00010011,
0b_01000110, 0b_00000011};
var memory = new ReadOnlyMemory<byte>(stream);
Collection<DeltaInstruction> instructions = new Collection<DeltaInstruction>();
DeltaInstruction? current;
while ((current = DeltaStreamReader.Read(ref memory)) is not null)
{
instructions.Add(current.Value);
}
Assert.Equal(0, memory.Length);
Assert.Collection(
instructions,
instruction =>
{
Assert.Equal(DeltaInstructionType.Copy, instruction.InstructionType);
Assert.Equal(462, instruction.Offset);
Assert.Equal(295, instruction.Size);
},
instruction =>
{
Assert.Equal(DeltaInstructionType.Copy, instruction.InstructionType);
Assert.Equal(863, instruction.Offset);
Assert.Equal(4204, instruction.Size);
},
instruction =>
{
Assert.Equal(DeltaInstructionType.Copy, instruction.InstructionType);
Assert.Equal(757, instruction.Offset);
Assert.Equal(107, instruction.Size);
},
instruction =>
{
Assert.Equal(DeltaInstructionType.Copy, instruction.InstructionType);
Assert.Equal(5067, instruction.Offset);
Assert.Equal(838, instruction.Size);
});
}
}
}