<commit_before>package com.twitter.io

import com.twitter.io.Buf.{ByteArray, Processor}
import java.nio.{ByteBuffer, CharBuffer}
import java.nio.charset.{StandardCharsets => JChar}
import java.util.Arrays
import org.junit.runner.RunWith
import org.scalacheck.{Arbitrary, Gen, Prop}
import org.scalatest.FunSuite
import org.scalatest.junit.{AssertionsForJUnit, JUnitRunner}
import org.scalatest.mockito.MockitoSugar
import org.scalatest.prop.{Checkers, GeneratorDrivenPropertyChecks}
import scala.collection.mutable

@RunWith(classOf[JUnitRunner])
class BufTest extends FunSuite
  with MockitoSugar
  with GeneratorDrivenPropertyChecks
  with Checkers
  with AssertionsForJUnit {

  val AllCharsets = Seq(
    JChar.ISO_8859_1,
    JChar.US_ASCII,
    JChar.UTF_8,
    JChar.UTF_16,
    JChar.UTF_16BE,
    JChar.UTF_16LE
  )

  private[this] val bufs: Seq[Buf] =
    Seq(
      Buf.Empty,
      Buf.ByteArray.Owned(Array[Byte](0, 1, 2)),
      Buf.ByteBuffer.Owned(java.nio.ByteBuffer.wrap(Array[Byte](0, 1, 2))),
      Buf.Utf8("abc"),
      Buf.Utf8("abc").concat(Buf.Utf8("def"))
    )

  test("Buf.slice validates inputs must be non-negative") {
    bufs.foreach { buf =>
      withClue(buf.toString) {
        intercept[IllegalArgumentException] {
          buf.slice(-1, 0)
        }
        intercept[IllegalArgumentException] {
          buf.slice(0, -1)
        }
      }
    }
  }

  test("Buf.slice returns empty buf") {
    bufs.foreach { buf =>
      withClue(buf.toString) {
        assert(Buf.Empty == buf.slice(5, 0))
        assert(Buf.Empty == buf.slice(buf.length + 1, buf.length + 2))
      }
    }
  }

  test("Buf.slice is no-op when from and until are covering") {
    bufs.foreach { buf =>
      withClue(buf.toString) {
        assert(buf == buf.slice(0, buf.length))
        assert(buf == buf.slice(0, buf.length + 1))
      }
    }
  }

  test("Buf.write(Array[Byte], offset) validates output array is large enough") {
    // we skip Empty because we cannot create an array
    // too small for this test.
    bufs.filterNot(_.isEmpty).foreach { buf =>
      withClue(buf.toString) {
        val notBigEnough = new Array[Byte](buf.length - 1)
        intercept[IllegalArgumentException] {
          buf.write(notBigEnough, 0)
        }
      }
    }
  }

  test("Buf.write(ByteBuffer) validates output ByteBuffer is large enough") {
    // we skip Empty because we cannot create an array
    // too small for this test.
    bufs.filterNot(_.isEmpty).foreach { buf =>
      withClue(buf.toString) {
        val notBigEnough = ByteBuffer.allocate(buf.length - 1)
        intercept[IllegalArgumentException] {
          buf.write(notBigEnough)
        }
      }
    }
  }

  test("Buf.write(ByteBuffer)") {
    for (head <- bufs; tail <- bufs) {
      val totalLength = head.length + tail.length
      val byteBuffer = ByteBuffer.allocate(totalLength)
      head.write(byteBuffer)
      tail.write(byteBuffer)
      byteBuffer.flip()

      assert(byteBuffer.remaining == totalLength)
      assert(Buf.ByteBuffer.Owned(byteBuffer) == head.concat(tail))
    }
  }

  test("Buf.write validates offset") {
    bufs.foreach { buf =>
      withClue(buf.toString) {
        val bigEnough = new Array[Byte](buf.length)
        intercept[IllegalArgumentException] {
          // negative offsets are not allowed
          buf.write(bigEnough, -1)
          // not enough room in the output after the offset
          buf.write(bigEnough, 2)
        }
      }
    }
  }

  test("Buf.ByteArray.slice") {
    val arr = Array.range(0, 16).map(_.toByte)
    val buf = Buf.ByteArray.Owned(arr)
    for (i <- 0 until arr.length; j <- i until arr.length) {
      val w = new Array[Byte](j-i)
      buf.slice(i, j).write(w, 0)<commit_msg>finagle-core/finagle-netty4: Fix Buf.write(ByteBuffer) implementations<commit_after>package com.twitter.io

import com.twitter.io.Buf.{ByteArray, Processor}
import java.nio.{ByteBuffer, CharBuffer}
import java.nio.charset.{StandardCharsets => JChar}
import java.util.Arrays
import org.junit.runner.RunWith
import org.scalacheck.{Arbitrary, Gen, Prop}
import org.scalatest.FunSuite
import org.scalatest.junit.{AssertionsForJUnit, JUnitRunner}
import org.scalatest.mockito.MockitoSugar
import org.scalatest.prop.{Checkers, GeneratorDrivenPropertyChecks}
import scala.collection.mutable

@RunWith(classOf[JUnitRunner])
class BufTest extends FunSuite
  with MockitoSugar
  with GeneratorDrivenPropertyChecks
  with Checkers
  with AssertionsForJUnit {

  val AllCharsets = Seq(
    JChar.ISO_8859_1,
    JChar.US_ASCII,
    JChar.UTF_8,
    JChar.UTF_16,
    JChar.UTF_16BE,
    JChar.UTF_16LE
  )

  private[this] val bufs: Seq[Buf] =
    Seq(
      Buf.Empty,
      Buf.ByteArray.Owned(Array[Byte](0, 1, 2)),
      Buf.ByteBuffer.Owned(java.nio.ByteBuffer.wrap(Array[Byte](0, 1, 2))),
      Buf.Utf8("abc"),
      Buf.Utf8("abc").concat(Buf.Utf8("def"))
    )

  test("Buf.slice validates inputs must be non-negative") {
    bufs.foreach { buf =>
      withClue(buf.toString) {
        intercept[IllegalArgumentException] {
          buf.slice(-1, 0)
        }
        intercept[IllegalArgumentException] {
          buf.slice(0, -1)
        }
      }
    }
  }

  test("Buf.slice returns empty buf") {
    bufs.foreach { buf =>
      withClue(buf.toString) {
        assert(Buf.Empty == buf.slice(5, 0))
        assert(Buf.Empty == buf.slice(buf.length + 1, buf.length + 2))
      }
    }
  }

  test("Buf.slice is no-op when from and until are covering") {
    bufs.foreach { buf =>
      withClue(buf.toString) {
        assert(buf == buf.slice(0, buf.length))
        assert(buf == buf.slice(0, buf.length + 1))
      }
    }
  }

  test("Buf.write(Array[Byte], offset) validates output array is large enough") {
    // we skip Empty because we cannot create an array
    // too small for this test.
    bufs.filterNot(_.isEmpty).foreach { buf =>
      withClue(buf.toString) {
        val notBigEnough = new Array[Byte](buf.length - 1)
        intercept[IllegalArgumentException] {
          buf.write(notBigEnough, 0)
        }
      }
    }
  }

  test("Buf.write(ByteBuffer) validates output ByteBuffer is large enough") {
    // we skip Empty because we cannot create an array
    // too small for this test.
    bufs.filterNot(_.isEmpty).foreach { buf =>
      withClue(buf.toString) {
        val notBigEnough = ByteBuffer.allocate(buf.length - 1)
        val clonedIndexes = notBigEnough.duplicate()
        val ex = intercept[IllegalArgumentException] {
          buf.write(notBigEnough)
        }
        assert(ex.getMessage.startsWith("Output too small"))
        // Make sure the indexes of the output buffer were not modified
        assert(notBigEnough == clonedIndexes)
      }
    }
  }

  test("Buf.write(ByteBuffer)") {
    for (head <- bufs; tail <- bufs) {
      val totalLength = head.length + tail.length
      // add some extra space to ensure that the write method handles it correctly
      val byteBuffer = ByteBuffer.allocate(totalLength + 10)
      head.write(byteBuffer)
      tail.write(byteBuffer)
      assert(byteBuffer.remaining == 10)
      byteBuffer.flip()

      assert(byteBuffer.remaining == totalLength)
      assert(Buf.ByteBuffer.Owned(byteBuffer) == head.concat(tail))
    }
  }

  test("Buf.write validates offset") {
    bufs.foreach { buf =>
      withClue(buf.toString) {
        val bigEnough = new Array[Byte](buf.length)
        intercept[IllegalArgumentException] {
          // negative offsets are not allowed
          buf.write(bigEnough, -1)
          // not enough room in the output after the offset
          buf.write(bigEnough, 2)
        }
      }
    }
  }

  test("Buf.ByteArray.slice") {
    val arr = Array.range(0, 16).map(_.toByte)
    val buf = Buf.ByteArray.Owned(arr)
    for (i <- 0 until arr.length; j <- i until arr.length) {
      val w = new Array[Byte](j-i)
      buf.slice(i, j).write(w, 0)
{'fraction_non_alphanumeric': 0.12176329417708728, 'fraction_numerical': 0.011241631931287103, 'mean_word_length': 2.662349676225717, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4225518', 'n_tokens_mistral': 2706, 'n_tokens_neox': 2588, 'n_words': 547}