@@ -3,7 +3,7 @@ namespace MLAPI.Tests.NetworkingManagerComponents.Binary
3
3
{
4
4
using MLAPI . NetworkingManagerComponents . Binary ;
5
5
using NUnit . Framework ;
6
-
6
+ using static MLAPI . NetworkingManagerComponents . Binary . BitStream ;
7
7
8
8
[ TestFixture ]
9
9
public class BitStreamTest
@@ -12,40 +12,30 @@ public class BitStreamTest
12
12
[ Test ]
13
13
public void TestEmptyStream ( )
14
14
{
15
-
16
15
BitStream bitStream = new BitStream ( new byte [ 100 ] ) ;
17
16
// ideally an empty stream should take no space
18
- Assert . That ( bitStream . Length , Is . EqualTo ( 0 ) ) ;
17
+ Assert . That ( bitStream . Length , Is . EqualTo ( 100 ) ) ;
19
18
}
20
19
21
20
[ Test ]
22
21
public void TestBool ( )
23
22
{
24
-
25
23
BitStream bitStream = new BitStream ( new byte [ 100 ] ) ;
26
24
bitStream . WriteBit ( true ) ;
27
25
28
- // this is failing , I just wrote something, how is it possible
29
- // that the length is still 0?
30
- Assert . That ( bitStream . Length , Is . EqualTo ( 1 ) ) ;
26
+ // we only wrote 1 bit , so the size should be as small as possible
27
+ // which is 1 byte, regardless of how big the buffer is
28
+ Assert . That ( bitStream . Length , Is . EqualTo ( 100 ) ) ;
31
29
}
32
30
33
31
[ Test ]
34
32
public void TestGrow ( )
35
33
{
36
- // stream should grow to accomodate input
34
+ // stream should not grow when given a buffer
37
35
BitStream bitStream = new BitStream ( new byte [ 0 ] ) ;
38
- bitStream . WriteInt64 ( long . MaxValue ) ;
39
-
40
- }
41
-
42
- [ Test ]
43
- public void TestGrow2 ( )
44
- {
45
- // stream should grow to accomodate input
46
- BitStream bitStream = new BitStream ( new byte [ 1 ] ) ;
47
- bitStream . WriteInt64 ( long . MaxValue ) ;
48
-
36
+ Assert . That (
37
+ ( ) => { bitStream . WriteInt64 ( long . MaxValue ) ; } ,
38
+ Throws . TypeOf < CapacityException > ( ) ) ;
49
39
}
50
40
51
41
[ Test ]
@@ -55,30 +45,30 @@ public void TestInOutBool()
55
45
56
46
BitStream outStream = new BitStream ( buffer ) ;
57
47
outStream . WriteBit ( true ) ;
58
- outStream . Flush ( ) ;
48
+ outStream . WriteBit ( false ) ;
49
+ outStream . WriteBit ( true ) ;
59
50
60
51
61
52
// the bit should now be stored in the buffer, lets see if it comes out
62
53
63
54
BitStream inStream = new BitStream ( buffer ) ;
64
- //bool result = inStream.ReadBit();
65
-
66
- Assert . Fail ( "There is no read bit method" ) ;
67
55
56
+ Assert . That ( inStream . ReadBit ( ) , Is . True ) ;
57
+ Assert . That ( inStream . ReadBit ( ) , Is . False ) ;
58
+ Assert . That ( inStream . ReadBit ( ) , Is . True ) ;
68
59
}
69
60
70
61
71
62
[ Test ]
72
63
public void TestInOutPacked64Bit ( )
73
64
{
74
65
byte [ ] buffer = new byte [ 100 ] ;
75
-
66
+
76
67
long someNumber = 1469598103934656037 ;
77
68
78
69
79
70
BitStream outStream = new BitStream ( buffer ) ;
80
71
outStream . WriteInt64Packed ( someNumber ) ;
81
- outStream . Flush ( ) ;
82
72
83
73
84
74
// the bit should now be stored in the buffer, lets see if it comes out
@@ -99,15 +89,15 @@ public void TestInOutBytes()
99
89
100
90
BitStream outStream = new BitStream ( buffer ) ;
101
91
outStream . WriteByte ( someNumber ) ;
102
- outStream . Flush ( ) ;
103
92
104
93
105
94
// the bit should now be stored in the buffer, lets see if it comes out
106
95
107
96
BitStream inStream = new BitStream ( buffer ) ;
108
- //byte result = inStream.ReadByte();
97
+ Assert . That ( inStream . ReadByte ( ) , Is . EqualTo ( someNumber ) ) ;
109
98
110
- Assert . Fail ( "Read byte should return byte, but it returns int" ) ;
99
+ // wtf this is standard behaviour
100
+ //Assert.Fail("Read byte should return byte, but it returns int");
111
101
}
112
102
113
103
[ Test ]
@@ -120,7 +110,6 @@ public void TestInOutInt16()
120
110
121
111
BitStream outStream = new BitStream ( buffer ) ;
122
112
outStream . WriteInt16 ( someNumber ) ;
123
- outStream . Flush ( ) ;
124
113
125
114
126
115
// the bit should now be stored in the buffer, lets see if it comes out
@@ -141,7 +130,6 @@ public void TestInOutInt32()
141
130
142
131
BitStream outStream = new BitStream ( buffer ) ;
143
132
outStream . WriteInt32 ( someNumber ) ;
144
- outStream . Flush ( ) ;
145
133
146
134
147
135
// the bit should now be stored in the buffer, lets see if it comes out
@@ -163,7 +151,6 @@ public void TestInOutMultiple()
163
151
BitStream outStream = new BitStream ( buffer ) ;
164
152
outStream . WriteInt16 ( someNumber ) ;
165
153
outStream . WriteInt16 ( someNumber2 ) ;
166
- outStream . Flush ( ) ;
167
154
168
155
169
156
// the bit should now be stored in the buffer, lets see if it comes out
@@ -175,5 +162,39 @@ public void TestInOutMultiple()
175
162
Assert . That ( result , Is . EqualTo ( someNumber ) ) ;
176
163
Assert . That ( result2 , Is . EqualTo ( someNumber2 ) ) ;
177
164
}
165
+
166
+ [ Test ]
167
+ public void TestLength ( )
168
+ {
169
+ BitStream inStream = new BitStream ( 4 ) ;
170
+ Assert . That ( inStream . Length , Is . EqualTo ( 0 ) ) ;
171
+ inStream . WriteByte ( 1 ) ;
172
+ Assert . That ( inStream . Length , Is . EqualTo ( 1 ) ) ;
173
+ inStream . WriteByte ( 2 ) ;
174
+ Assert . That ( inStream . Length , Is . EqualTo ( 2 ) ) ;
175
+ inStream . WriteByte ( 3 ) ;
176
+ Assert . That ( inStream . Length , Is . EqualTo ( 3 ) ) ;
177
+ inStream . WriteByte ( 4 ) ;
178
+ Assert . That ( inStream . Length , Is . EqualTo ( 4 ) ) ;
179
+ }
180
+
181
+ [ Test ]
182
+ public void TestCapacityGrowth ( )
183
+ {
184
+ BitStream inStream = new BitStream ( 4 ) ;
185
+ Assert . That ( inStream . Capacity , Is . EqualTo ( 4 ) ) ;
186
+
187
+ inStream . WriteByte ( 1 ) ;
188
+ inStream . WriteByte ( 2 ) ;
189
+ inStream . WriteByte ( 3 ) ;
190
+ inStream . WriteByte ( 4 ) ;
191
+ inStream . WriteByte ( 5 ) ;
192
+
193
+ // buffer should grow and the reported length
194
+ // should not waste any space
195
+ // note MemoryStream makes a distinction between Length and Capacity
196
+ Assert . That ( inStream . Length , Is . EqualTo ( 5 ) ) ;
197
+ Assert . That ( inStream . Capacity , Is . GreaterThanOrEqualTo ( 5 ) ) ;
198
+ }
178
199
}
179
200
}
0 commit comments