/* Ċ Ġ* ĠCopyright Ġ( C ) Ġ 2 0 1 9 ĠSquare , ĠInc . Ċ Ġ* Ċ Ġ* ĠLicensed Ġunder Ġthe ĠApache ĠLicense , ĠVersion Ġ 2 . 0 Ġ( the Ġ" License "); Ċ Ġ* Ġyou Ġmay Ġnot Ġuse Ġthis Ġfile Ġexcept Ġin Ġcompliance Ġwith Ġthe ĠLicense . Ċ Ġ* ĠYou Ġmay Ġobtain Ġa Ġcopy Ġof Ġthe ĠLicense Ġat Ċ Ġ* Ċ Ġ* ĠĠĠĠĠ Ġhttp :// www . apache . org / licenses / LICENSE - 2 . 0 Ċ Ġ* Ċ Ġ* ĠUnless Ġrequired Ġby Ġapplicable Ġlaw Ġor Ġagreed Ġto Ġin Ġwriting , Ġsoftware Ċ Ġ* Ġdistributed Ġunder Ġthe ĠLicense Ġis Ġdistributed Ġon Ġan Ġ" AS ĠIS " ĠBASIS , Ċ Ġ* ĠWITHOUT ĠWARRANTIES ĠOR ĠCONDITIONS ĠOF ĠANY ĠKIND , Ġeither Ġexpress Ġor Ġimplied . Ċ Ġ* ĠSee Ġthe ĠLicense Ġfor Ġthe Ġspecific Ġlanguage Ġgoverning Ġpermissions Ġand Ċ Ġ* Ġlimitations Ġunder Ġthe ĠLicense . Ċ Ġ*/ Ċ Ċ package Ġok io Ċ Ċ import Ġkotlin . test . Test Ċ import Ġkotlin . test . assertEquals Ċ import Ġkotlin . test . assert Fails With Ċ Ċ /** Ċ Ġ* ĠTests Ġsole ly Ġfor Ġthe Ġbehavior Ġof ĠReal Buffered Source 's Ġimplementation . ĠFor Ġgeneric Ċ Ġ* ĠBuffered Source Ġbehavior Ġuse ĠBuffered Source Test . Ċ Ġ*/ Ċ class ĠCommon Real Buffered Source Test Ġ{ ĊĠ Ġ@ Test Ġfun ĠindexOf Stops Reading At Limit () Ġ{ ĊĠĠĠ Ġval Ġbuffer Ġ= ĠBuffer (). write Utf 8 (" abcdef ") ĊĠĠĠ Ġval Ġbuffered Source Ġ= Ġ( object Ġ: ĠSource Ġby Ġbuffer Ġ{ ĊĠĠĠĠĠ Ġoverride Ġfun Ġread ( sink : ĠBuffer , Ġbyte Count : ĠLong ): ĠLong Ġ{ ĊĠĠĠĠĠĠĠ Ġreturn Ġbuffer . read ( sink , Ġmin Of ( 1 , Ġbyte Count )) ĊĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ}). buffer () ĊĊĠĠĠ ĠassertEquals ( 6 , Ġbuffer . size ) ĊĠĠĠ ĠassertEquals (- 1 , Ġbuffered Source . indexOf (' e '. to Byte (), Ġ 0 , Ġ 4 )) ĊĠĠĠ ĠassertEquals ( 2 , Ġbuffer . size ) ĊĠ Ġ} ĊĊĠ Ġ@ Test Ġfun Ġrequire Tracks Buffer First () Ġ{ ĊĠĠĠ Ġval Ġsource Ġ= ĠBuffer () ĊĠĠĠ Ġsource . write Utf 8 (" bb ") ĊĊĠĠĠ Ġval Ġbuffered Source Ġ= Ġ( source Ġas ĠSource ). buffer () ĊĠĠĠ Ġbuffered Source . buffer . write Utf 8 (" aa ") ĊĊĠĠĠ Ġbuffered Source . require ( 2 ) ĊĠĠĠ ĠassertEquals ( 2 , Ġbuffered Source . buffer . size ) ĊĠĠĠ ĠassertEquals ( 2 , Ġsource . size ) ĊĠ Ġ} ĊĊĠ Ġ@ Test Ġfun Ġrequire Includes Buffer Bytes () Ġ{ ĊĠĠĠ Ġval Ġsource Ġ= ĠBuffer () ĊĠĠĠ Ġsource . write Utf 8 (" b ") ĊĊĠĠĠ Ġval Ġbuffered Source Ġ= Ġ( source Ġas ĠSource ). buffer () ĊĠĠĠ Ġbuffered Source . buffer . write Utf 8 (" a ") ĊĊĠĠĠ Ġbuffered Source . require ( 2 ) ĊĠĠĠ ĠassertEquals (" ab ", Ġbuffered Source . buffer . read Utf 8 ( 2 )) ĊĠ Ġ} ĊĊĠ Ġ@ Test Ġfun Ġrequire Ins ufficient Data () Ġ{ ĊĠĠĠ Ġval Ġsource Ġ= ĠBuffer () ĊĠĠĠ Ġsource . write Utf 8 (" a ") ĊĊĠĠĠ Ġval Ġbuffered Source Ġ= Ġ( source Ġas ĠSource ). buffer () ĊĊĠĠĠ Ġassert Fails With < EOF Exception > Ġ{ ĊĠĠĠĠĠ Ġbuffered Source . require ( 2 ) ĊĠĠĠ Ġ} ĊĠ Ġ} ĊĊĠ Ġ@ Test Ġfun Ġrequire Reads One Segment At AT ime () Ġ{ ĊĠĠĠ Ġval Ġsource Ġ= ĠBuffer () ĊĠĠĠ Ġsource . write Utf 8 (" a ". repeat ( Segment . SIZE )) ĊĠĠĠ Ġsource . write Utf 8 (" b ". repeat ( Segment . SIZE )) ĊĊĠĠĠ Ġval Ġbuffered Source Ġ= Ġ( source Ġas ĠSource ). buffer () ĊĊĠĠĠ Ġbuffered Source . require ( 2 ) ĊĠĠĠ ĠassertEquals ( Segment . SIZE . to Long (), Ġsource . size ) ĊĠĠĠ ĠassertEquals ( Segment . SIZE . to Long (), Ġbuffered Source . buffer . size ) ĊĠ Ġ} ĊĊĠ Ġ@ Test Ġfun Ġskip Reads One Segment At AT ime () Ġ{ ĊĠĠĠ Ġval Ġsource Ġ= ĠBuffer () ĊĠĠĠ Ġsource . write Utf 8 (" a ". repeat ( Segment . SIZE )) ĊĠĠĠ Ġsource . write Utf 8 (" b ". repeat ( Segment . SIZE )) ĊĠĠĠ Ġval Ġbuffered Source Ġ= Ġ( source Ġas ĠSource ). buffer () ĊĠĠĠ Ġbuffered Source . skip ( 2 ) ĊĠĠĠ ĠassertEquals ( Segment . SIZE . to Long (), Ġsource . size ) ĊĠĠĠ ĠassertEquals ( Segment . SIZE . to Long () Ġ- Ġ 2 L , Ġbuffered Source . buffer . size ) ĊĠ Ġ} ĊĊĠ Ġ@ Test Ġfun Ġskip Tracks Buffer First () Ġ{ ĊĠĠĠ Ġval Ġsource Ġ= ĠBuffer () ĊĠĠĠ Ġsource . write Utf 8 (" bb ") ĊĊĠĠĠ Ġval Ġbuffered Source Ġ= Ġ( source Ġas ĠSource ). buffer () ĊĠĠĠ Ġbuffered Source . buffer . write Utf 8 (" aa ") ĊĊĠĠĠ Ġbuffered Source . skip ( 2 ) ĊĠĠĠ ĠassertEquals ( 0 , Ġbuffered Source . buffer . size ) ĊĠĠĠ ĠassertEquals ( 2 , Ġsource . size ) ĊĠ Ġ} ĊĊĠ Ġ@ Test Ġfun Ġoperations After Close () Ġ{ ĊĠĠĠ Ġval Ġsource Ġ= ĠBuffer () ĊĠĠĠ Ġval Ġbuffered Source Ġ= Ġ( source Ġas ĠSource ). buffer () ĊĠĠĠ Ġbuffered Source . close () ĊĊĠĠĠ Ġ// ĠTest Ġa Ġsample Ġset Ġof Ġmethods . ĊĠĠĠ Ġassert Fails With < IllegalStateException > Ġ{ ĊĠĠĠĠĠ Ġbuffered Source . indexOf ( 1 . to Byte ()) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġassert Fails With < IllegalStateException > Ġ{ ĊĠĠĠĠĠ Ġbuffered Source . skip ( 1 ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġassert Fails With < IllegalStateException > Ġ{ ĊĠĠĠĠĠ Ġbuffered Source . read Byte () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġassert Fails With < IllegalStateException > Ġ{ ĊĠĠĠĠĠ Ġbuffered Source . read ByteString ( 1 0 ) ĊĠĠĠ Ġ} ĊĠ Ġ} ĊĊĠ Ġ/** ĊĠĠ Ġ* ĠWe Ġdon 't Ġwant Ġread All Ġto Ġbuffer Ġan Ġun bounded Ġamount Ġof Ġdata . ĠInstead Ġit ĊĠĠ Ġ* Ġshould Ġbuffer Ġa Ġsegment , Ġwrite Ġit , Ġand Ġrepeat . ĊĠĠ Ġ*/ ĊĠ Ġ@ Test Ġfun Ġread All Reads One Segment At AT ime () Ġ{ ĊĠĠĠ Ġval Ġwrite 1 Ġ= ĠBuffer (). write Utf 8 (" a ". repeat ( Segment . SIZE )) ĊĠĠĠ Ġval Ġwrite 2 Ġ= ĠBuffer (). write Utf 8 (" b ". repeat ( Segment . SIZE )) ĊĠĠĠ Ġval Ġwrite 3 Ġ= ĠBuffer (). write Utf 8 (" c ". repeat ( Segment . SIZE )) ĊĊĠĠĠ Ġval Ġsource Ġ= ĠBuffer (). write Utf 8 ( ĊĠĠĠĠĠ Ġ"${ " a ". repeat ( Segment . SIZE )} ${ " b ". repeat ( Segment . SIZE )} ${ " c ". repeat ( Segment . SIZE )}" ĊĠĠĠ Ġ) ĊĊĠĠĠ Ġval Ġmock Sink Ġ= ĠMock Sink () ĊĠĠĠ Ġval Ġbuffered Source Ġ= Ġ( source Ġas ĠSource ). buffer () ĊĠĠĠ ĠassertEquals ( Segment . SIZE . to Long () Ġ* Ġ 3 L , Ġbuffered Source . read All ( mock Sink )) ĊĠĠĠ Ġmock Sink . assert Log ( ĊĠĠĠĠĠ Ġ" write ($ write 1 , Ġ${ write 1 . size })", ĊĠĠĠĠĠ Ġ" write ($ write 2 , Ġ${ write 2 . size })", ĊĠĠĠĠĠ Ġ" write ($ write 3 , Ġ${ write 3 . size })" ĊĠĠĠ Ġ) ĊĠ Ġ} Ċ } Ċ