diff options
Diffstat (limited to 'examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/niobuffer')
3 files changed, 896 insertions, 0 deletions
diff --git a/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/niobuffer/BaseBufferTest.scala b/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/niobuffer/BaseBufferTest.scala new file mode 100644 index 0000000..a1f1b71 --- /dev/null +++ b/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/niobuffer/BaseBufferTest.scala @@ -0,0 +1,178 @@ +/* __ *\ +** ________ ___ / / ___ __ ____ Scala.js Test Suite ** +** / __/ __// _ | / / / _ | __ / // __/ (c) 2013, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \ http://scala-js.org/ ** +** /____/\___/_/ |_/____/_/ | |__/ /____/ ** +** |/____/ ** +\* */ +package scala.scalajs.testsuite.niobuffer + +import org.scalajs.jasminetest.JasmineTest + +import java.nio._ + +abstract class BaseBufferTest extends JasmineTest { + trait BaseFactory { + type BufferType <: Buffer + + val createsReadOnly: Boolean = false + + def allocBuffer(capacity: Int): BufferType + + def allocBuffer(pos: Int, limit: Int, capacity: Int): BufferType = { + val buf = allocBuffer(capacity) + buf.limit(limit).position(pos) + buf + } + } + + type Factory <: BaseFactory + + def commonTests(factory: Factory): Unit = { + import factory._ + + it("allocate") { + val buf = allocBuffer(10) + expect(buf.position).toEqual(0) + expect(buf.limit).toEqual(10) + expect(buf.capacity).toEqual(10) + + expect(allocBuffer(0).capacity).toEqual(0) + + expect(() => allocBuffer(-1)).toThrow + + val buf2 = allocBuffer(1, 5, 9) + expect(buf2.position()).toEqual(1) + expect(buf2.limit()).toEqual(5) + expect(buf2.capacity()).toEqual(9) + } + + it("isReadOnly()") { + val buf = allocBuffer(10) + if (createsReadOnly) + expect(buf.isReadOnly()).toBeTruthy + else + expect(buf.isReadOnly()).toBeFalsy + } + + it("position") { + val buf = allocBuffer(10) + buf.position(3) + expect(buf.position()).toEqual(3) + buf.position(10) + expect(buf.position()).toEqual(10) + buf.position(0) + expect(buf.position()).toEqual(0) + + expect(() => buf.position(-1)).toThrow + expect(() => buf.position(11)).toThrow + expect(buf.position()).toEqual(0) + + val buf2 = allocBuffer(1, 5, 9) + expect(buf2.position()).toEqual(1) + buf2.position(5) + expect(buf2.position()).toEqual(5) + expect(() => buf2.position(6)).toThrow + expect(buf2.position()).toEqual(5) + } + + it("limit") { + val buf = allocBuffer(10) + buf.position(3) + buf.limit(7) + expect(buf.limit()).toEqual(7) + expect(buf.position()).toEqual(3) + expect(() => buf.limit(11)).toThrow + expect(buf.limit()).toEqual(7) + expect(() => buf.limit(-1)).toThrow + expect(buf.limit()).toEqual(7) + expect(buf.position()).toEqual(3) + + buf.position(5) + buf.limit(4) + expect(buf.limit()).toEqual(4) + expect(buf.position()).toEqual(4) + } + + it("mark() and reset()") { + val buf = allocBuffer(10) + + // Initially, the mark should not be set + expect(() => buf.reset()).toThrow + + // Simple test + buf.position(3) + buf.mark() + buf.position(8) + buf.reset() + expect(buf.position()).toEqual(3) + + // reset() should not have cleared the mark + buf.position(5) + buf.reset() + expect(buf.position()).toEqual(3) + + // setting position() below the mark should clear the mark + buf.position(2) + expect(() => buf.reset()).toThrow + } + + it("clear()") { + val buf = allocBuffer(3, 6, 10) + buf.mark() + buf.position(4) + + buf.clear() + expect(buf.position()).toEqual(0) + expect(buf.limit()).toEqual(10) // the capacity + expect(buf.capacity()).toEqual(10) + expect(() => buf.reset()).toThrow + } + + it("flip()") { + val buf = allocBuffer(3, 6, 10) + buf.mark() + buf.position(4) + + buf.flip() + expect(buf.position()).toEqual(0) + expect(buf.limit()).toEqual(4) // old position + expect(buf.capacity()).toEqual(10) + expect(() => buf.reset()).toThrow + } + + it("rewind()") { + val buf = allocBuffer(3, 6, 10) + buf.mark() + buf.position(4) + + buf.rewind() + expect(buf.position()).toEqual(0) + expect(buf.limit()).toEqual(6) // unchanged + expect(buf.capacity()).toEqual(10) + expect(() => buf.reset()).toThrow + } + + it("remaining() and hasRemaining()") { + val buf = allocBuffer(3, 7, 10) + expect(buf.remaining()).toEqual(7-3) + expect(buf.hasRemaining()).toBeTruthy + + buf.position(6) + expect(buf.remaining()).toEqual(7-6) + expect(buf.hasRemaining()).toBeTruthy + + buf.limit(9) + expect(buf.remaining()).toEqual(9-6) + expect(buf.hasRemaining()).toBeTruthy + + buf.limit(2) + expect(buf.remaining()).toEqual(0) + expect(buf.hasRemaining()).toBeFalsy + + buf.position(0) + expect(buf.remaining()).toEqual(2) + expect(buf.hasRemaining()).toBeTruthy + } + } +} diff --git a/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/niobuffer/ByteBufferTest.scala b/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/niobuffer/ByteBufferTest.scala new file mode 100644 index 0000000..9ec831f --- /dev/null +++ b/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/niobuffer/ByteBufferTest.scala @@ -0,0 +1,330 @@ +/* __ *\ +** ________ ___ / / ___ __ ____ Scala.js Test Suite ** +** / __/ __// _ | / / / _ | __ / // __/ (c) 2013, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \ http://scala-js.org/ ** +** /____/\___/_/ |_/____/_/ | |__/ /____/ ** +** |/____/ ** +\* */ +package scala.scalajs.testsuite.niobuffer + +import java.nio._ + +import scala.scalajs.js +import js.JSConverters._ + +object ByteBufferTest extends BaseBufferTest { + trait Factory extends BaseFactory { + type BufferType = ByteBuffer + + def withContent(capacity: Int, content: Byte*): ByteBuffer = + withContent(0, capacity, capacity, content: _*) + + def withContent(pos: Int, limit: Int, capacity: Int, + content: Byte*): ByteBuffer = { + val buf = allocBuffer(pos, limit, capacity) + buf.put(content.toArray) + buf.position(pos) + buf + } + } + + def byteRange(start: Int, end: Int): Array[Byte] = + (start until end).map(_.toByte).toArray + + def defineTests(factory: Factory): Unit = { + import factory._ + + commonTests(factory) + + it("absolute get()") { + val buf = withContent(10, byteRange(0, 10): _*) + expect(buf.get(0)).toEqual(0) + expect(buf.position()).toEqual(0) + expect(buf.get(3)).toEqual(3) + expect(buf.position()).toEqual(0) + + expect(() => buf.get(-1)).toThrow + expect(() => buf.get(15)).toThrow + + buf.limit(4) + expect(() => buf.get(5)).toThrow + } + + + if (!createsReadOnly) { + it("absolute put()") { + val buf = allocBuffer(10) + buf.put(5, 42) + expect(buf.position()).toEqual(0) + buf.put(3, 2) + expect(buf.get(3)).toEqual(2) + expect(buf.get(5)).toEqual(42) + expect(buf.get(7)).toEqual(0) + + expect(() => buf.put(-1, 2)).toThrow + expect(() => buf.put(14, 9)).toThrow + + buf.limit(4) + expect(() => buf.put(4, 1)).toThrow + } + } else { + it("absolute put() - read-only") { + val buf = allocBuffer(10) + expect(() => buf.put(2, 1)).toThrow + expect(buf.get(2)).toEqual(0) + expect(buf.position()).toEqual(0) + + expect(() => buf.put(-2, 1)).toThrow + expect(() => buf.put(12, 1)).toThrow + } + } + + it("relative get()") { + val buf = withContent(10, byteRange(0, 10): _*) + expect(buf.get()).toEqual(0) + expect(buf.position()).toEqual(1) + buf.position(3) + expect(buf.get()).toEqual(3) + expect(buf.position()).toEqual(4) + + buf.limit(4) + expect(() => buf.get()).toThrow + } + + if (!createsReadOnly) { + it("relative put()") { + val buf = allocBuffer(10) + buf.put(5.toByte) + expect(buf.position()).toEqual(1) + expect(buf.get(0)).toEqual(5) + + buf.position(3) + buf.put(36.toByte) + expect(buf.position()).toEqual(4) + expect(buf.get(3)).toEqual(36) + + buf.position(10) + expect(() => buf.put(3.toByte)).toThrow + } + } else { + it("relative put() - read-only") { + val buf = allocBuffer(10) + expect(() => buf.put(5.toByte)).toThrow + expect(buf.position()).toEqual(0) + expect(buf.get(0)).toEqual(0) + + buf.position(10) + expect(() => buf.put(3.toByte)).toThrow + } + } + + it("relative bulk get()") { + val buf = withContent(10, byteRange(0, 10): _*) + val a = new Array[Byte](4) + buf.get(a) + expect(a.toJSArray).toEqual(js.Array(0, 1, 2, 3)) + expect(buf.position()).toEqual(4) + + buf.position(6) + buf.get(a, 1, 2) + expect(a.toJSArray).toEqual(js.Array(0, 6, 7, 3)) + expect(buf.position()).toEqual(8) + + expect(() => buf.get(a)).toThrow + expect(buf.position()).toEqual(8) + expect(a.toJSArray).toEqual(js.Array(0, 6, 7, 3)) + } + + if (!createsReadOnly) { + it("relative bulk put()") { + val buf = allocBuffer(10) + buf.put(Array[Byte](6, 7, 12)) + expect((0 to 3).map(buf.get(_)).toJSArray).toEqual(js.Array(6, 7, 12, 0)) + expect(buf.position()).toEqual(3) + + buf.position(2) + buf.put(Array[Byte](44, 55, 66, 77, 88), 2, 2) + expect((0 to 4).map(buf.get(_)).toJSArray).toEqual(js.Array(6, 7, 66, 77, 0)) + expect(buf.position()).toEqual(4) + + expect(() => buf.put(Array.fill[Byte](10)(0))).toThrow + expect(buf.position()).toEqual(4) + expect((0 to 4).map(buf.get(_)).toJSArray).toEqual(js.Array(6, 7, 66, 77, 0)) + } + } else { + it("relative bulk put() - read-only") { + val buf = allocBuffer(10) + expect(() => buf.put(Array[Byte](6, 7, 12))).toThrow + expect(buf.position()).toEqual(0) + expect(buf.get(0)).toEqual(0) + + buf.position(8) + expect(() => buf.put(Array[Byte](6, 7, 12))).toThrow + expect(buf.position()).toEqual(8) + expect(buf.get(8)).toEqual(0) + } + } + + if (!createsReadOnly) { + it("compact()") { + val buf = withContent(10, byteRange(0, 10): _*) + buf.position(6) + buf.mark() + + buf.compact() + expect(buf.position()).toEqual(4) + expect(buf.limit()).toEqual(10) + expect(() => buf.reset()).toThrow + + for (i <- 0 until 4) + expect(buf.get(i)).toEqual(i + 6) + } + } else { + it("compact() - read-only") { + val buf = allocBuffer(10) + expect(() => buf.compact()).toThrow + } + } + + it("slice()" + (if (createsReadOnly) " - read-only" else "")) { + val buf1 = withContent(10, byteRange(0, 10): _*) + buf1.position(3) + buf1.limit(7) + buf1.mark() + val buf2 = buf1.slice() + expect(buf2.position()).toEqual(0) + expect(buf2.limit()).toEqual(4) + expect(buf2.capacity()).toEqual(4) + expect(() => buf2.reset()).toThrow + expect(buf2.get(1)).toEqual(4) + + buf2.position(2) + expect(buf1.position()).toEqual(3) + + if (!createsReadOnly) { + buf2.put(89.toByte) + expect(buf1.get(5)).toEqual(89) + expect(buf2.position()).toEqual(3) + expect(buf1.position()).toEqual(3) + } + + expect(() => buf2.limit(5)).toThrow + expect(buf2.limit()).toEqual(4) + + buf2.limit(3) + expect(buf1.limit()).toEqual(7) + + if (!createsReadOnly) { + buf1.put(3, 23) + expect(buf2.get(0)).toEqual(23) + } + } + + it("duplicate()" + (if (createsReadOnly) " - read-only" else "")) { + val buf1 = withContent(10, byteRange(0, 10): _*) + buf1.position(3) + buf1.limit(7) + buf1.mark() + val buf2 = buf1.duplicate() + expect(buf2.position()).toEqual(3) + expect(buf2.limit()).toEqual(7) + expect(buf2.capacity()).toEqual(10) + expect(buf2.get(4)).toEqual(4) + + buf2.position(4) + expect(buf1.position()).toEqual(3) + expect(buf2.position()).toEqual(4) + + buf2.reset() + expect(buf2.position()).toEqual(3) + buf2.position(4) + + if (!createsReadOnly) { + buf2.put(89.toByte) + expect(buf1.get(4)).toEqual(89) + expect(buf2.position()).toEqual(5) + expect(buf1.position()).toEqual(3) + } + + buf2.limit(5) + expect(buf1.limit()).toEqual(7) + + if (!createsReadOnly) { + buf1.put(6, 23) + buf2.limit(10) + expect(buf2.get(6)).toEqual(23) + } + } + } + + describe("Allocated ByteBuffer") { + defineTests(new Factory { + def allocBuffer(capacity: Int): ByteBuffer = + ByteBuffer.allocate(capacity) + }) + } + + class WrappedByteBufferFactory extends Factory { + def allocBuffer(capacity: Int): ByteBuffer = + ByteBuffer.wrap(new Array[Byte](capacity)) + + override def allocBuffer(pos: Int, limit: Int, capacity: Int): ByteBuffer = + ByteBuffer.wrap(new Array[Byte](capacity), pos, limit-pos) + + override def withContent(pos: Int, limit: Int, capacity: Int, + content: Byte*): ByteBuffer = { + val after = capacity - (pos + content.size) + ByteBuffer.wrap( + (Seq.fill(pos)(0.toByte) ++ content ++ Seq.fill(after)(0.toByte)).toArray, + pos, limit-pos) + } + } + + describe("Wrapped ByteBuffer") { + defineTests(new WrappedByteBufferFactory) + } + + describe("Read-only wrapped ByteBuffer") { + defineTests(new WrappedByteBufferFactory { + override val createsReadOnly = true + + override def allocBuffer(capacity: Int): ByteBuffer = + super.allocBuffer(capacity).asReadOnlyBuffer() + + override def allocBuffer(pos: Int, limit: Int, capacity: Int): ByteBuffer = + super.allocBuffer(pos, limit, capacity).asReadOnlyBuffer() + + override def withContent(pos: Int, limit: Int, capacity: Int, + content: Byte*): ByteBuffer = + super.withContent(pos, limit, capacity, content: _*).asReadOnlyBuffer() + }) + } + + describe("Sliced ByteBuffer") { + defineTests(new Factory { + def allocBuffer(capacity: Int): ByteBuffer = { + if (capacity < 0) + throw new IllegalArgumentException + val buf = ByteBuffer.allocate(capacity+25) + buf.position(17) + buf.limit(17+capacity) + buf.slice() + } + + override def withContent(pos: Int, limit: Int, capacity: Int, + content: Byte*): ByteBuffer = { + if (!(0 <= pos && pos <= limit && limit <= capacity)) + throw new IllegalArgumentException + val buf = ByteBuffer.allocate(capacity+25) + buf.position(9+pos) + buf.put(content.toArray) + buf.position(9) + buf.limit(9+capacity) + val buf2 = buf.slice() + buf2.position(pos) + buf2.limit(limit) + buf2 + } + }) + } +} diff --git a/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/niobuffer/CharBufferTest.scala b/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/niobuffer/CharBufferTest.scala new file mode 100644 index 0000000..b13c478 --- /dev/null +++ b/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/niobuffer/CharBufferTest.scala @@ -0,0 +1,388 @@ +/* __ *\ +** ________ ___ / / ___ __ ____ Scala.js Test Suite ** +** / __/ __// _ | / / / _ | __ / // __/ (c) 2013, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \ http://scala-js.org/ ** +** /____/\___/_/ |_/____/_/ | |__/ /____/ ** +** |/____/ ** +\* */ +package scala.scalajs.testsuite.niobuffer + +import java.nio._ + +import scala.scalajs.js +import js.JSConverters._ + +object CharBufferTest extends BaseBufferTest { + trait Factory extends BaseFactory { + type BufferType = CharBuffer + + def withContent(capacity: Int, content: Char*): CharBuffer = + withContent(0, capacity, capacity, content: _*) + + def withContent(pos: Int, limit: Int, capacity: Int, + content: Char*): CharBuffer = { + val buf = allocBuffer(pos, limit, capacity) + buf.put(content.toArray) + buf.position(pos) + buf + } + } + + def zeros(n: Int): String = + "\u0000"*n + + def charRange(start: Int, end: Int): Array[Char] = + (start until end).map(_.toChar).toArray + + def defineTests(factory: Factory): Unit = { + import factory._ + + commonTests(factory) + + it("absolute get()") { + val buf = withContent(10, charRange(0, 10): _*) + expect(buf.get(0)).toEqual(0) + expect(buf.position()).toEqual(0) + expect(buf.get(3)).toEqual(3) + expect(buf.position()).toEqual(0) + + expect(() => buf.get(-1)).toThrow + expect(() => buf.get(15)).toThrow + + buf.limit(4) + expect(() => buf.get(5)).toThrow + } + + + if (!createsReadOnly) { + it("absolute put()") { + val buf = allocBuffer(10) + buf.put(5, 42) + expect(buf.position()).toEqual(0) + buf.put(3, 2) + expect(buf.get(3)).toEqual(2) + expect(buf.get(5)).toEqual(42) + expect(buf.get(7)).toEqual(0) + + expect(() => buf.put(-1, 2)).toThrow + expect(() => buf.put(14, 9)).toThrow + + buf.limit(4) + expect(() => buf.put(4, 1)).toThrow + } + } else { + it("absolute put() - read-only") { + val buf = allocBuffer(10) + expect(() => buf.put(2, 1)).toThrow + expect(buf.get(2)).toEqual(0) + expect(buf.position()).toEqual(0) + + expect(() => buf.put(-2, 1)).toThrow + expect(() => buf.put(12, 1)).toThrow + } + } + + it("relative get()") { + val buf = withContent(10, charRange(0, 10): _*) + expect(buf.get()).toEqual(0) + expect(buf.position()).toEqual(1) + buf.position(3) + expect(buf.get()).toEqual(3) + expect(buf.position()).toEqual(4) + + buf.limit(4) + expect(() => buf.get()).toThrow + } + + if (!createsReadOnly) { + it("relative put()") { + val buf = allocBuffer(10) + buf.put(5.toChar) + expect(buf.position()).toEqual(1) + expect(buf.get(0)).toEqual(5) + + buf.position(3) + buf.put(36.toChar) + expect(buf.position()).toEqual(4) + expect(buf.get(3)).toEqual(36) + + buf.position(10) + expect(() => buf.put(3.toChar)).toThrow + } + } else { + it("relative put() - read-only") { + val buf = allocBuffer(10) + expect(() => buf.put(5.toChar)).toThrow + expect(buf.position()).toEqual(0) + expect(buf.get(0)).toEqual(0) + + buf.position(10) + expect(() => buf.put(3.toChar)).toThrow + } + } + + it("relative bulk get()") { + val buf = withContent(10, charRange(0, 10): _*) + val a = new Array[Char](4) + buf.get(a) + expect(a.map(_.toInt).toJSArray).toEqual(js.Array(0, 1, 2, 3)) + expect(buf.position()).toEqual(4) + + buf.position(6) + buf.get(a, 1, 2) + expect(a.map(_.toInt).toJSArray).toEqual(js.Array(0, 6, 7, 3)) + expect(buf.position()).toEqual(8) + + expect(() => buf.get(a)).toThrow + expect(buf.position()).toEqual(8) + expect(a.map(_.toInt).toJSArray).toEqual(js.Array(0, 6, 7, 3)) + } + + if (!createsReadOnly) { + it("relative bulk put()") { + val buf = allocBuffer(10) + buf.put(Array[Char](6, 7, 12)) + expect((0 to 3).map(buf.get(_).toInt).toJSArray).toEqual(js.Array(6, 7, 12, 0)) + expect(buf.position()).toEqual(3) + + buf.position(2) + buf.put(Array[Char](44, 55, 66, 77, 88), 2, 2) + expect((0 to 4).map(buf.get(_).toInt).toJSArray).toEqual(js.Array(6, 7, 66, 77, 0)) + expect(buf.position()).toEqual(4) + + expect(() => buf.put(Array.fill[Char](10)(0))).toThrow + expect(buf.position()).toEqual(4) + expect((0 to 4).map(buf.get(_).toInt).toJSArray).toEqual(js.Array(6, 7, 66, 77, 0)) + } + } else { + it("relative bulk put() - read-only") { + val buf = allocBuffer(10) + expect(() => buf.put(Array[Char](6, 7, 12))).toThrow + expect(buf.position()).toEqual(0) + expect(buf.get(0)).toEqual(0) + + buf.position(8) + expect(() => buf.put(Array[Char](6, 7, 12))).toThrow + expect(buf.position()).toEqual(8) + expect(buf.get(8)).toEqual(0) + } + } + + if (!createsReadOnly) { + it("compact()") { + val buf = withContent(10, charRange(0, 10): _*) + buf.position(6) + buf.mark() + + buf.compact() + expect(buf.position()).toEqual(4) + expect(buf.limit()).toEqual(10) + expect(() => buf.reset()).toThrow + + for (i <- 0 until 4) + expect(buf.get(i).toInt).toEqual(i + 6) + } + } else { + it("compact() - read-only") { + val buf = allocBuffer(10) + expect(() => buf.compact()).toThrow + } + } + + it("slice()" + (if (createsReadOnly) " - read-only" else "")) { + val buf1 = withContent(10, charRange(0, 10): _*) + buf1.position(3) + buf1.limit(7) + buf1.mark() + val buf2 = buf1.slice() + expect(buf2.position()).toEqual(0) + expect(buf2.limit()).toEqual(4) + expect(buf2.capacity()).toEqual(4) + expect(() => buf2.reset()).toThrow + expect(buf2.get(1)).toEqual(4) + + buf2.position(2) + expect(buf1.position()).toEqual(3) + + if (!createsReadOnly) { + buf2.put(89.toChar) + expect(buf1.get(5)).toEqual(89) + expect(buf2.position()).toEqual(3) + expect(buf1.position()).toEqual(3) + } + + expect(() => buf2.limit(5)).toThrow + expect(buf2.limit()).toEqual(4) + + buf2.limit(3) + expect(buf1.limit()).toEqual(7) + + if (!createsReadOnly) { + buf1.put(3, 23) + expect(buf2.get(0)).toEqual(23) + } + } + + it("duplicate()" + (if (createsReadOnly) " - read-only" else "")) { + val buf1 = withContent(10, charRange(0, 10): _*) + buf1.position(3) + buf1.limit(7) + buf1.mark() + val buf2 = buf1.duplicate() + expect(buf2.position()).toEqual(3) + expect(buf2.limit()).toEqual(7) + expect(buf2.capacity()).toEqual(10) + expect(buf2.get(4)).toEqual(4) + + buf2.position(4) + expect(buf1.position()).toEqual(3) + expect(buf2.position()).toEqual(4) + + buf2.reset() + expect(buf2.position()).toEqual(3) + buf2.position(4) + + if (!createsReadOnly) { + buf2.put(89.toChar) + expect(buf1.get(4)).toEqual(89) + expect(buf2.position()).toEqual(5) + expect(buf1.position()).toEqual(3) + } + + buf2.limit(5) + expect(buf1.limit()).toEqual(7) + + if (!createsReadOnly) { + buf1.put(6, 23) + buf2.limit(10) + expect(buf2.get(6)).toEqual(23) + } + } + } + + describe("Allocated CharBuffer") { + defineTests(new Factory { + def allocBuffer(capacity: Int): CharBuffer = + CharBuffer.allocate(capacity) + }) + } + + class WrappedCharBufferFactory extends Factory { + def allocBuffer(capacity: Int): CharBuffer = + CharBuffer.wrap(new Array[Char](capacity)) + + override def allocBuffer(pos: Int, limit: Int, capacity: Int): CharBuffer = + CharBuffer.wrap(new Array[Char](capacity), pos, limit-pos) + + override def withContent(pos: Int, limit: Int, capacity: Int, + content: Char*): CharBuffer = { + val after = capacity - (pos + content.size) + CharBuffer.wrap( + (Seq.fill(pos)(0.toChar) ++ content ++ Seq.fill(after)(0.toChar)).toArray, + pos, limit-pos) + } + } + + describe("Wrapped CharBuffer") { + defineTests(new WrappedCharBufferFactory) + } + + describe("Read-only wrapped CharBuffer") { + defineTests(new WrappedCharBufferFactory { + override val createsReadOnly = true + + override def allocBuffer(capacity: Int): CharBuffer = + super.allocBuffer(capacity).asReadOnlyBuffer() + + override def allocBuffer(pos: Int, limit: Int, capacity: Int): CharBuffer = + super.allocBuffer(pos, limit, capacity).asReadOnlyBuffer() + + override def withContent(pos: Int, limit: Int, capacity: Int, + content: Char*): CharBuffer = + super.withContent(pos, limit, capacity, content: _*).asReadOnlyBuffer() + }) + } + + describe("CharBuffer wrapping a CharSequence") { + defineTests(new Factory { + override val createsReadOnly = true + + def allocBuffer(capacity: Int): CharBuffer = { + if (capacity < 0) + throw new IllegalArgumentException + CharBuffer.wrap(zeros(capacity)) + } + + override def allocBuffer(pos: Int, limit: Int, capacity: Int): CharBuffer = { + if (capacity < 0) + throw new IllegalArgumentException + CharBuffer.wrap(zeros(capacity), pos, limit-pos) + } + + override def withContent(pos: Int, limit: Int, capacity: Int, + content: Char*): CharBuffer = { + val after = capacity - (pos + content.size) + CharBuffer.wrap( + zeros(pos) + content.mkString + zeros(after), + pos, limit-pos) + } + }) + } + + describe("Sliced CharBuffer") { + defineTests(new Factory { + def allocBuffer(capacity: Int): CharBuffer = { + if (capacity < 0) + throw new IllegalArgumentException + val buf = CharBuffer.allocate(capacity+25) + buf.position(17) + buf.limit(17+capacity) + buf.slice() + } + + override def withContent(pos: Int, limit: Int, capacity: Int, + content: Char*): CharBuffer = { + if (!(0 <= pos && pos <= limit && limit <= capacity)) + throw new IllegalArgumentException + val buf = CharBuffer.allocate(capacity+25) + buf.position(9+pos) + buf.put(content.toArray) + buf.position(9) + buf.limit(9+capacity) + val buf2 = buf.slice() + buf2.position(pos) + buf2.limit(limit) + buf2 + } + }) + } + + describe("Sliced CharBuffer wrapping a CharSequence") { + defineTests(new Factory { + override val createsReadOnly = true + + def allocBuffer(capacity: Int): CharBuffer = { + if (capacity < 0) + throw new IllegalArgumentException + val buf = CharBuffer.wrap(zeros(capacity+25)) + buf.position(17) + buf.limit(17+capacity) + buf.slice() + } + + override def withContent(pos: Int, limit: Int, capacity: Int, + content: Char*): CharBuffer = { + if (!(0 <= pos && pos <= limit && limit <= capacity)) + throw new IllegalArgumentException + val after = (25+capacity) - (9+pos+content.size) + val buf = CharBuffer.wrap(zeros(9+pos) + content.mkString + zeros(after)) + buf.position(9) + buf.limit(9+capacity) + val buf2 = buf.slice() + buf2.position(pos) + buf2.limit(limit) + buf2 + } + }) + } +} |