<commit_before>/*                     __                                               *\
**     ________ ___   / /  ___      __ ____  Scala.js Test Suite        **
**    / __/ __// _ | / /  / _ | __ / // __/  (c) 2013, LAMP/EPFL        **
**  __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \    http://scala-js.org/       **
** /____/\___/_/ |_/____/_/ | |__/ /____/                               **
**                          |/____/                                     **
\*                                                                      */
package org.scalajs.testsuite.javalib.io

import java.io._

import scala.language.implicitConversions

import org.junit.Test
import org.junit.Assert._

import org.scalajs.testsuite.utils.AssertThrows._
import org.scalajs.testsuite.utils.Platform.executingInJVM

class InputStreamTest extends CommonStreamsTests {

  class DummyInputStream(val length: Int) extends InputStream {
    private var i: Int = 0
    def read(): Int = if (i < length) { i += 1; i } else -1
  }

  @Test def should_provide_a_default_implementation_of_read_to_an_array(): Unit = {
    val stream = new DummyInputStream(200)

    val buf = new Array[Byte](50)

    // Should read first 50 bytes (next: 51)
    assertEquals(50, stream.read(buf))
    assertArrayEquals((1 to 50).toArray.map(_.toByte), buf)

    // Should read another 20 (next: 71)
    assertEquals(20, stream.read(buf, 10, 20))
    assertArrayEquals(((1 to 10) ++ (51 to 70) ++ (31 to 50)).toArray.map(_.toByte), buf)

    // Test some Exception conditions
    if (executingInJVM) {
      expectThrows(classOf[NullPointerException], stream.read(null, 0, 10))
      expectThrows(classOf[IndexOutOfBoundsException], stream.read(buf, -1, 10))
      expectThrows(classOf[IndexOutOfBoundsException], stream.read(buf, 0, -1))
      expectThrows(classOf[IndexOutOfBoundsException], stream.read(buf, 10, 100))
    } else {
      expectThrows(classOf[Exception], stream.read(null, 0, 10))
      expectThrows(classOf[Exception], stream.read(buf, -1, 10))
      expectThrows(classOf[Exception], stream.read(buf, 0, -1))
      expectThrows(classOf[Exception], stream.read(buf, 10, 100))
    }
    // Buffer should be unmodified
    assertArrayEquals(
        ((1 to 10) ++ (51 to 70) ++ (31 to 50)).toArray.map(_.toByte), buf)

    // Should read nothing (next: 71)
    assertEquals(0, stream.read(buf, 10, 0))
    assertArrayEquals(
        ((1 to 10) ++ (51 to 70) ++ (31 to 50)).toArray.map(_.toByte), buf)

    // Skip 40 bytes (next: 111)
    assertEquals(40L, stream.skip(40))

    // Read 50 bytes, should wrap (next: 161)
    assertEquals(50, stream.read(buf))
    assertArrayEquals(<commit_msg>Fix #2049: Remove NPE tests on buf in InputStream.read(buf, ...).<commit_after>/*                     __                                               *\
**     ________ ___   / /  ___      __ ____  Scala.js Test Suite        **
**    / __/ __// _ | / /  / _ | __ / // __/  (c) 2013, LAMP/EPFL        **
**  __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \    http://scala-js.org/       **
** /____/\___/_/ |_/____/_/ | |__/ /____/                               **
**                          |/____/                                     **
\*                                                                      */
package org.scalajs.testsuite.javalib.io

import java.io._

import scala.language.implicitConversions

import org.junit.Test
import org.junit.Assert._

import org.scalajs.testsuite.utils.AssertThrows._

class InputStreamTest extends CommonStreamsTests {

  class DummyInputStream(val length: Int) extends InputStream {
    private var i: Int = 0
    def read(): Int = if (i < length) { i += 1; i } else -1
  }

  @Test def should_provide_a_default_implementation_of_read_to_an_array(): Unit = {
    val stream = new DummyInputStream(200)

    val buf = new Array[Byte](50)

    // Should read first 50 bytes (next: 51)
    assertEquals(50, stream.read(buf))
    assertArrayEquals((1 to 50).toArray.map(_.toByte), buf)

    // Should read another 20 (next: 71)
    assertEquals(20, stream.read(buf, 10, 20))
    assertArrayEquals(((1 to 10) ++ (51 to 70) ++ (31 to 50)).toArray.map(_.toByte), buf)

    // Test some Exception conditions
    expectThrows(classOf[IndexOutOfBoundsException], stream.read(buf, -1, 10))
    expectThrows(classOf[IndexOutOfBoundsException], stream.read(buf, 0, -1))
    expectThrows(classOf[IndexOutOfBoundsException], stream.read(buf, 10, 100))

    // Buffer should be unmodified
    assertArrayEquals(
        ((1 to 10) ++ (51 to 70) ++ (31 to 50)).toArray.map(_.toByte), buf)

    // Should read nothing (next: 71)
    assertEquals(0, stream.read(buf, 10, 0))
    assertArrayEquals(
        ((1 to 10) ++ (51 to 70) ++ (31 to 50)).toArray.map(_.toByte), buf)

    // Skip 40 bytes (next: 111)
    assertEquals(40L, stream.skip(40))

    // Read 50 bytes, should wrap (next: 161)
    assertEquals(50, stream.read(buf))
    assertArrayEquals(
{'fraction_non_alphanumeric': 0.14014155712841253, 'fraction_numerical': 0.04287158746208291, 'mean_word_length': 2.1165721487082547, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2375139', 'n_tokens_mistral': 1730, 'n_tokens_neox': 1527, 'n_words': 351}