2121import static org .junit .jupiter .api .Assertions .assertThrows ;
2222import static org .junit .jupiter .api .Assertions .assertTrue ;
2323
24+ import java .nio .charset .StandardCharsets ;
25+
2426import org .junit .jupiter .api .Test ;
2527
2628/**
@@ -101,6 +103,20 @@ public void testHasSpace() {
101103 assertTrue (cbb .hasSpace ());
102104 }
103105
106+ @ Test
107+ public void testHasSpaceInt () {
108+ final CircularByteBuffer cbb = new CircularByteBuffer (1 );
109+ assertTrue (cbb .hasSpace (1 ));
110+ cbb .add ((byte ) 1 );
111+ assertFalse (cbb .hasSpace (1 ));
112+ assertEquals (1 , cbb .read ());
113+ assertTrue (cbb .hasSpace (1 ));
114+ cbb .add ((byte ) 2 );
115+ assertFalse (cbb .hasSpace (1 ));
116+ assertEquals (2 , cbb .read ());
117+ assertTrue (cbb .hasSpace (1 ));
118+ }
119+
104120 @ Test
105121 public void testPeekWithExcessiveLength () {
106122 assertFalse (new CircularByteBuffer ().peek (new byte [] { 1 , 3 , 5 , 7 , 9 }, 0 , 6 ));
@@ -125,4 +141,25 @@ public void testPeekWithNegativeLength() {
125141 public void testPeekWithValidArguments () {
126142 assertFalse (new CircularByteBuffer ().peek (new byte [] { 5 , 10 , 15 , 20 , 25 }, 0 , 5 ));
127143 }
144+
145+ @ Test
146+ public void testReadByteArray () {
147+ final CircularByteBuffer cbb = new CircularByteBuffer ();
148+ final String string = "0123456789" ;
149+ final byte [] bytesIn = string .getBytes (StandardCharsets .UTF_8 );
150+ cbb .add (bytesIn , 0 , 10 );
151+ final byte [] bytesOut = new byte [10 ];
152+ cbb .read (bytesOut , 0 , 10 );
153+ assertEquals (string , new String (bytesOut , StandardCharsets .UTF_8 ));
154+ }
155+
156+ @ Test
157+ public void testReadByteArrayIllegalArgumentException () {
158+ final CircularByteBuffer cbb = new CircularByteBuffer ();
159+ final byte [] bytesOut = new byte [10 ];
160+ // targetOffset < 0
161+ assertThrows (IllegalArgumentException .class , () -> cbb .read (bytesOut , -1 , 10 ));
162+ // targetOffset >= targetBuffer.length
163+ assertThrows (IllegalArgumentException .class , () -> cbb .read (bytesOut , 0 , bytesOut .length + 1 ));
164+ }
128165}
0 commit comments