-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLEB128Tests.cs
146 lines (121 loc) · 4.62 KB
/
LEB128Tests.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
namespace Prowl.Echo.Test
{
public class LEB128Tests
{
[Theory]
[InlineData(0UL)]
[InlineData(1UL)]
[InlineData(127UL)]
[InlineData(128UL)]
[InlineData(16383UL)]
[InlineData(16384UL)]
[InlineData(ulong.MaxValue)]
public void TestUnsignedRoundTrip(ulong value)
{
using var stream = new MemoryStream();
using var writer = new BinaryWriter(stream);
// Write the value
LEB128.WriteUnsigned(writer, value);
// Read it back
stream.Position = 0;
using var reader = new BinaryReader(stream);
var result = LEB128.ReadUnsigned(reader);
Assert.Equal(value, result);
}
[Theory]
[InlineData(0L)]
[InlineData(1L)]
[InlineData(-1L)]
[InlineData(63L)]
[InlineData(-64L)]
[InlineData(64L)]
[InlineData(-65L)]
[InlineData(8191L)]
[InlineData(-8192L)]
[InlineData(long.MaxValue)]
[InlineData(long.MinValue)]
public void TestSignedRoundTrip(long value)
{
using var stream = new MemoryStream();
using var writer = new BinaryWriter(stream);
// Write the value
LEB128.WriteSigned(writer, value);
// Read it back
stream.Position = 0;
using var reader = new BinaryReader(stream);
var result = LEB128.ReadSigned(reader);
Assert.Equal(value, result);
}
[Fact]
public void TestUnsignedKnownValues()
{
// Test cases from DWARF LEB128 specification
void TestValue(ulong value, byte[] expected)
{
using var stream = new MemoryStream();
using var writer = new BinaryWriter(stream);
LEB128.WriteUnsigned(writer, value);
Assert.Equal(expected, stream.ToArray());
stream.Position = 0;
using var reader = new BinaryReader(stream);
Assert.Equal(value, LEB128.ReadUnsigned(reader));
}
TestValue(2, new byte[] { 0x02 });
TestValue(127, new byte[] { 0x7F });
TestValue(128, new byte[] { 0x80, 0x01 });
TestValue(129, new byte[] { 0x81, 0x01 });
TestValue(130, new byte[] { 0x82, 0x01 });
TestValue(12857, new byte[] { 0xB9, 0x64 });
}
[Fact]
public void TestSignedKnownValues()
{
// Test cases from DWARF LEB128 specification
void TestValue(long value, byte[] expected)
{
using var stream = new MemoryStream();
using var writer = new BinaryWriter(stream);
LEB128.WriteSigned(writer, value);
Assert.Equal(expected, stream.ToArray());
stream.Position = 0;
using var reader = new BinaryReader(stream);
Assert.Equal(value, LEB128.ReadSigned(reader));
}
TestValue(2, new byte[] { 0x02 });
TestValue(-2, new byte[] { 0x7E });
TestValue(127, new byte[] { 0xFF, 0x00 });
TestValue(-127, new byte[] { 0x81, 0x7F });
TestValue(128, new byte[] { 0x80, 0x01 });
TestValue(-128, new byte[] { 0x80, 0x7F });
TestValue(129, new byte[] { 0x81, 0x01 });
TestValue(-129, new byte[] { 0xFF, 0x7E });
}
[Fact]
public void TestOverflowDetection()
{
using var stream = new MemoryStream();
using var writer = new BinaryWriter(stream);
// Write 10 bytes with continuation bits set
for (int i = 0; i < 10; i++)
writer.Write((byte)0xFF);
stream.Position = 0;
using var reader = new BinaryReader(stream);
Assert.Throws<OverflowException>(() => LEB128.ReadUnsigned(reader));
stream.Position = 0;
Assert.Throws<OverflowException>(() => LEB128.ReadSigned(reader));
}
[Fact]
public void TestIncompleteRead()
{
using var stream = new MemoryStream();
using var writer = new BinaryWriter(stream);
// Write incomplete LEB128 value
writer.Write((byte)0x80);
stream.Position = 0;
using var reader = new BinaryReader(stream);
Assert.Throws<EndOfStreamException>(() => LEB128.ReadUnsigned(reader));
stream.Position = 0;
Assert.Throws<EndOfStreamException>(() => LEB128.ReadSigned(reader));
}
}
}