diff options
Diffstat (limited to 'examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib')
32 files changed, 6367 insertions, 0 deletions
diff --git a/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/ArraysTest.scala b/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/ArraysTest.scala new file mode 100644 index 0000000..d0d97f1 --- /dev/null +++ b/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/ArraysTest.scala @@ -0,0 +1,749 @@ +/* __ *\ +** ________ ___ / / ___ __ ____ Scala.js Test Suite ** +** / __/ __// _ | / / / _ | __ / // __/ (c) 2013, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \ http://scala-js.org/ ** +** /____/\___/_/ |_/____/_/ | |__/ /____/ ** +** |/____/ ** +\* */ +package scala.scalajs.testsuite.javalib + +import language.implicitConversions + +import scala.scalajs.js +import scala.scalajs.js.JSConverters._ + +import org.scalajs.jasminetest.JasmineTest + +import java.util.{ Arrays, Comparator } + +import scala.reflect.ClassTag + +object ArraysTest extends ArraysTest + +/** This is also used in the typedarray package to test scala.Arrays backed + * by TypedArrays + */ +trait ArraysTest extends JasmineTest { + + // Just in here, we allow ourselves to do this + implicit def array2jsArray[T](arr: Array[T]): js.Array[T] = arr.toJSArray + + /** Overridden by typedarray tests */ + def Array[T : ClassTag](v: T*): scala.Array[T] = scala.Array(v: _*) + + /** Overridden by typedarray tests */ + def testBody(suite: => Unit) = describe("java.util.Arrays")(suite) + + val stringComparator = new Comparator[String]() { + def compare(s1: String, s2: String) = s1.compareTo(s2) + } + + val intComparator = new Comparator[Int]() { + def compare(i1: Int, i2: Int) = i1 - i2 + } + + testBody { + + it("should respond to `sort` for Int") { + val scalaInts = Array(5, 3, 6, 1, 2, 4) + val ints = new Array[Object](scalaInts.length) + for (i <- 0 until scalaInts.length) + ints(i) = scalaInts(i).asInstanceOf[Object] + val sorted = Array(1, 2, 3, 4, 5, 6) + + Arrays.sort(ints, intComparator.asInstanceOf[Comparator[Object]]) + expect(ints).toEqual(Array(1, 2, 3, 4, 5, 6)) + } + + it("should respond to `sort` for String") { + val scalajs: Array[Object] = Array("S", "c", "a", "l", "a", ".", "j", "s") + val sorted = Array(".", "S", "a", "a", "c", "j", "l", "s") + + Arrays.sort(scalajs, stringComparator.asInstanceOf[Comparator[Object]]) + expect(scalajs).toEqual(sorted) + } + + it("should respond to `fill` for Boolean") { + val booleans = new Array[Boolean](6) + Arrays.fill(booleans, false) + expect(booleans).toEqual(Array(false, false, false, false, false, false)) + + Arrays.fill(booleans, true) + expect(booleans).toEqual(Array(true, true, true, true, true, true)) + } + + it("should respond to `fill` with start and end index for Boolean") { + val booleans = new Array[Boolean](6) + Arrays.fill(booleans, 1, 4, true) + expect(booleans).toEqual(Array(false, true, true, true, false, false)) + } + + it("should respond to `fill` for Byte") { + val bytes = new Array[Byte](6) + Arrays.fill(bytes, 42.toByte) + expect(bytes).toEqual(Array[Byte](42, 42, 42, 42, 42, 42)) + + Arrays.fill(bytes, -1.toByte) + expect(bytes).toEqual(Array[Byte](-1, -1, -1, -1, -1, -1)) + } + + it("should respond to `fill` with start and end index for Byte") { + val bytes = new Array[Byte](6) + Arrays.fill(bytes, 1, 4, 42.toByte) + expect(bytes).toEqual(Array[Byte](0, 42, 42, 42, 0, 0)) + + Arrays.fill(bytes, 2, 5, -1.toByte) + expect(bytes).toEqual(Array[Byte](0, 42, -1, -1, -1, 0)) + } + + it("should respond to `fill` for Short") { + val shorts = new Array[Short](6) + Arrays.fill(shorts, 42.toShort) + expect(shorts).toEqual(Array[Short](42, 42, 42, 42, 42, 42)) + + Arrays.fill(shorts, -1.toShort) + expect(shorts).toEqual(Array[Short](-1, -1, -1, -1, -1, -1)) + } + + it("should respond to `fill` with start and end index for Short") { + val shorts = new Array[Short](6) + Arrays.fill(shorts, 1, 4, 42.toShort) + expect(shorts).toEqual(Array[Short](0, 42, 42, 42, 0, 0)) + + Arrays.fill(shorts, 2, 5, -1.toShort) + expect(shorts).toEqual(Array[Short](0, 42, -1, -1, -1, 0)) + } + + it("should respond to `fill` for Int") { + val ints = new Array[Int](6) + Arrays.fill(ints, 42) + expect(ints).toEqual(Array(42, 42, 42, 42, 42, 42)) + + Arrays.fill(ints, -1) + expect(ints).toEqual(Array(-1, -1, -1, -1, -1, -1)) + } + + it("should respond to `fill` with start and end index for Int") { + val ints = new Array[Int](6) + Arrays.fill(ints, 1, 4, 42) + expect(ints).toEqual(Array(0, 42, 42, 42, 0, 0)) + + Arrays.fill(ints, 2, 5, -1) + expect(ints).toEqual(Array(0, 42, -1, -1, -1, 0)) + } + + it("should respond to `fill` for Long") { + val longs = new Array[Long](6) + Arrays.fill(longs, 42L) + expect(longs).toEqual(Array(42L, 42L, 42L, 42L, 42L, 42L)) + + Arrays.fill(longs, -1L) + expect(longs).toEqual(Array(-1L, -1L, -1L, -1L, -1L, -1L)) + } + + it("should respond to `fill` with start and end index for Long") { + val longs = new Array[Long](6) + Arrays.fill(longs, 1, 4, 42L) + expect(longs).toEqual(Array(0L, 42L, 42L, 42L, 0L, 0L)) + + Arrays.fill(longs, 2, 5, -1L) + expect(longs).toEqual(Array(0L, 42L, -1L, -1L, -1L, 0L)) + } + + it("should respond to `fill` for Float") { + val floats = new Array[Float](6) + Arrays.fill(floats, 42.0f) + expect(floats).toEqual(Array(42.0f, 42.0f, 42.0f, 42.0f, 42.0f, 42.0f)) + + Arrays.fill(floats, -1.0f) + expect(floats).toEqual(Array(-1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f)) + } + + it("should respond to `fill` with start and end index for Float") { + val floats = new Array[Float](6) + Arrays.fill(floats, 1, 4, 42.0f) + expect(floats).toEqual(Array(0.0f, 42.0f, 42.0f, 42.0f, 0.0f, 0.0f)) + + Arrays.fill(floats, 2, 5, -1.0f) + expect(floats).toEqual(Array(0.0f, 42.0f, -1.0f, -1.0f, -1.0f, 0.0f)) + } + + it("should respond to `fill` for Double") { + val doubles = new Array[Double](6) + Arrays.fill(doubles, 42.0) + expect(doubles).toEqual(Array(42.0, 42.0, 42.0, 42.0, 42.0, 42.0)) + + Arrays.fill(doubles, -1.0f) + expect(doubles).toEqual(Array(-1.0, -1.0, -1.0, -1.0, -1.0, -1.0)) + } + + it("should respond to `fill` with start and end index for Double") { + val doubles = new Array[Double](6) + Arrays.fill(doubles, 1, 4, 42.0) + expect(doubles).toEqual(Array(0.0, 42.0, 42.0, 42.0, 0.0, 0.0)) + + Arrays.fill(doubles, 2, 5, -1.0) + expect(doubles).toEqual(Array(0.0, 42.0, -1.0, -1.0, -1.0, 0.0)) + } + + it("should respond to `fill` for AnyRef") { + val array = new Array[AnyRef](6) + Arrays.fill(array, "a") + expect(array).toEqual(Array[AnyRef]("a", "a", "a", "a", "a", "a")) + + Arrays.fill(array, "b") + expect(array).toEqual(Array[AnyRef]("b", "b", "b", "b", "b", "b")) + } + + it("should respond to `fill` with start and end index for AnyRef") { + val bytes = new Array[AnyRef](6) + Arrays.fill(bytes, 1, 4, "a") + expect(bytes).toEqual(Array[AnyRef](null, "a", "a", "a", null, null)) + + Arrays.fill(bytes, 2, 5, "b") + expect(bytes).toEqual(Array[AnyRef](null, "a", "b", "b", "b", null)) + } + + it("should respond to `binarySearch` with start index, end index and key for Long") { + val longs: Array[Long] = Array(1, 2, 3, 5, 6, 7) + var ret = Arrays.binarySearch(longs, 0, 6, 5) + expect(ret).toEqual(3) + + ret = Arrays.binarySearch(longs, 0, 6, 0) + expect(ret).toEqual(-1) + + ret = Arrays.binarySearch(longs, 0, 6, 4) + expect(ret).toEqual(-4) + + ret = Arrays.binarySearch(longs, 0, 6, 8) + expect(ret).toEqual(-7) + } + + it("should respond to `binarySearch` with key for Long") { + val longs: Array[Long] = Array(1, 2, 3, 5, 6, 7) + var ret = Arrays.binarySearch(longs, 5) + expect(ret).toEqual(3) + + ret = Arrays.binarySearch(longs, 0) + expect(ret).toEqual(-1) + + ret = Arrays.binarySearch(longs, 4) + expect(ret).toEqual(-4) + + ret = Arrays.binarySearch(longs, 8) + expect(ret).toEqual(-7) + } + + it("should respond to `binarySearch` with start index, end index and key for Int") { + val ints: Array[Int] = Array(1, 2, 3, 5, 6, 7) + var ret = Arrays.binarySearch(ints, 0, 6, 5) + expect(ret).toEqual(3) + + ret = Arrays.binarySearch(ints, 0, 6, 0) + expect(ret).toEqual(-1) + + ret = Arrays.binarySearch(ints, 0, 6, 4) + expect(ret).toEqual(-4) + + ret = Arrays.binarySearch(ints, 0, 6, 8) + expect(ret).toEqual(-7) + } + + it("should respond to `binarySearch` with key for Int") { + val ints: Array[Int] = Array(1, 2, 3, 5, 6, 7) + var ret = Arrays.binarySearch(ints, 5) + expect(ret).toEqual(3) + + ret = Arrays.binarySearch(ints, 0) + expect(ret).toEqual(-1) + + ret = Arrays.binarySearch(ints, 4) + expect(ret).toEqual(-4) + + ret = Arrays.binarySearch(ints, 8) + expect(ret).toEqual(-7) + } + + it("should respond to `binarySearch` with start index, end index and key for Short") { + val shorts: Array[Short] = Array(1, 2, 3, 5, 6, 7) + var ret = Arrays.binarySearch(shorts, 0, 6, 5.toShort) + expect(ret).toEqual(3) + + ret = Arrays.binarySearch(shorts, 0, 6, 0.toShort) + expect(ret).toEqual(-1) + + ret = Arrays.binarySearch(shorts, 0, 6, 4.toShort) + expect(ret).toEqual(-4) + + ret = Arrays.binarySearch(shorts, 0, 6, 8.toShort) + expect(ret).toEqual(-7) + } + + it("should respond to `binarySearch` with key for Short") { + val shorts: Array[Short] = Array(1, 2, 3, 5, 6, 7) + var ret = Arrays.binarySearch(shorts, 5.toShort) + expect(ret).toEqual(3) + + ret = Arrays.binarySearch(shorts, 0.toShort) + expect(ret).toEqual(-1) + + ret = Arrays.binarySearch(shorts, 4.toShort) + expect(ret).toEqual(-4) + + ret = Arrays.binarySearch(shorts, 8.toShort) + expect(ret).toEqual(-7) + } + + it("should respond to `binarySearch` with start index, end index and key for Char") { + val chars: Array[Char] = Array('b', 'c', 'd', 'f', 'g', 'h') + var ret = Arrays.binarySearch(chars, 0, 6, 'f') + expect(ret).toEqual(3) + + ret = Arrays.binarySearch(chars, 0, 6, 'a') + expect(ret).toEqual(-1) + + ret = Arrays.binarySearch(chars, 0, 6, 'e') + expect(ret).toEqual(-4) + + ret = Arrays.binarySearch(chars, 0, 6, 'i') + expect(ret).toEqual(-7) + } + + it("should respond to `binarySearch` with key for Char") { + val chars: Array[Char] = Array('b', 'c', 'd', 'f', 'g', 'h') + var ret = Arrays.binarySearch(chars, 'f') + expect(ret).toEqual(3) + + ret = Arrays.binarySearch(chars, 'a') + expect(ret).toEqual(-1) + + ret = Arrays.binarySearch(chars, 'e') + expect(ret).toEqual(-4) + + ret = Arrays.binarySearch(chars, 'i') + expect(ret).toEqual(-7) + } + + it("should respond to `binarySearch` with start index, end index and key for Double") { + val doubles: Array[Double] = Array(0.1, 0.2, 0.3, 0.5, 0.6, 0.7) + var ret = Arrays.binarySearch(doubles, 0, 6, 0.5) + expect(ret).toEqual(3) + + ret = Arrays.binarySearch(doubles, 0, 6, 0.0) + expect(ret).toEqual(-1) + + ret = Arrays.binarySearch(doubles, 0, 6, 0.4) + expect(ret).toEqual(-4) + + ret = Arrays.binarySearch(doubles, 0, 6, 0.8) + expect(ret).toEqual(-7) + } + + it("should respond to `binarySearch` with key for Double") { + val doubles: Array[Double] = Array(0.1, 0.2, 0.3, 0.5, 0.6, 0.7) + var ret = Arrays.binarySearch(doubles, 0.5) + expect(ret).toEqual(3) + + ret = Arrays.binarySearch(doubles, 0.0) + expect(ret).toEqual(-1) + + ret = Arrays.binarySearch(doubles, 0.4) + expect(ret).toEqual(-4) + + ret = Arrays.binarySearch(doubles, 0.8) + expect(ret).toEqual(-7) + } + + it("should respond to `binarySearch` with start index, end index and key for Float") { + val floats: Array[Float] = Array(0.1f, 0.2f, 0.3f, 0.5f, 0.6f, 0.7f) + var ret = Arrays.binarySearch(floats, 0, 6, 0.5f) + expect(ret).toEqual(3) + + ret = Arrays.binarySearch(floats, 0, 6, 0.0f) + expect(ret).toEqual(-1) + + ret = Arrays.binarySearch(floats, 0, 6, 0.4f) + expect(ret).toEqual(-4) + + ret = Arrays.binarySearch(floats, 0, 6, 0.8f) + expect(ret).toEqual(-7) + } + + it("should respond to `binarySearch` with key for Float") { + val floats: Array[Float] = Array(0.1f, 0.2f, 0.3f, 0.5f, 0.6f, 0.7f) + var ret = Arrays.binarySearch(floats, 0.5f) + expect(ret).toEqual(3) + + ret = Arrays.binarySearch(floats, 0.0f) + expect(ret).toEqual(-1) + + ret = Arrays.binarySearch(floats, 0.4f) + expect(ret).toEqual(-4) + + ret = Arrays.binarySearch(floats, 0.8f) + expect(ret).toEqual(-7) + } + + it("should respond to `binarySearch` with start index, end index and key for AnyRef") { + val strings: Array[AnyRef] = Array("aa", "abc", "cc", "zz", "zzzs", "zzzt") + var ret = Arrays.binarySearch(strings, 0, 6, "zz") + expect(ret).toEqual(3) + + ret = Arrays.binarySearch(strings, 0, 6, "a") + expect(ret).toEqual(-1) + + ret = Arrays.binarySearch(strings, 0, 6, "cd") + expect(ret).toEqual(-4) + + ret = Arrays.binarySearch(strings, 0, 6, "zzzz") + expect(ret).toEqual(-7) + } + + it("should respond to `binarySearch` with key for AnyRef") { + val strings: Array[AnyRef] = Array("aa", "abc", "cc", "zz", "zzzs", "zzzt") + var ret = Arrays.binarySearch(strings, "zz") + expect(ret).toEqual(3) + + ret = Arrays.binarySearch(strings, "a") + expect(ret).toEqual(-1) + + ret = Arrays.binarySearch(strings, "cd") + expect(ret).toEqual(-4) + + ret = Arrays.binarySearch(strings, "zzzz") + expect(ret).toEqual(-7) + } + + it("should check ranges of input to `binarySearch`") { + def expectException(block: => Unit)(expected: PartialFunction[Throwable, Unit]): Unit = { + val catchAll: PartialFunction[Throwable, Unit] = { + case e: Throwable => expect(e.getClass.getName).toBe("not thrown") + } + + try { + block + expect("exception").toBe("thrown") + } catch expected orElse catchAll + } + + val array = Array(0, 1, 3, 4) + + expectException({ Arrays.binarySearch(array, 3, 2, 2) }) { + case exception: IllegalArgumentException => + expect(exception.getMessage).toBe("fromIndex(3) > toIndex(2)") + } + + // start/end comparison is made before index ranges checks + expectException({ Arrays.binarySearch(array, 7, 5, 2) }) { + case exception: IllegalArgumentException => + expect(exception.getMessage).toBe("fromIndex(7) > toIndex(5)") + } + + expectException({ Arrays.binarySearch(array, -1, 4, 2) }) { + case exception: ArrayIndexOutOfBoundsException => + expect(exception.getMessage).toBe("Array index out of range: -1") + } + + expectException({ Arrays.binarySearch(array, 0, 5, 2) }) { + case exception: ArrayIndexOutOfBoundsException => + expect(exception.getMessage).toBe("Array index out of range: 5") + } + } + + it("should respond to `copyOf` with key for Int") { + val ints: Array[Int] = Array(1, 2, 3) + val intscopy = Arrays.copyOf(ints, 5) + expect(intscopy).toEqual(Array(1, 2, 3, 0, 0)) + } + + it("should respond to `copyOf` with key for Long") { + val longs: Array[Long] = Array(1, 2, 3) + val longscopy = Arrays.copyOf(longs, 5) + expect(longscopy).toEqual(Array[Long](1, 2, 3, 0, 0)) + } + + it("should respond to `copyOf` with key for Short") { + val shorts: Array[Short] = Array(1, 2, 3) + val shortscopy = Arrays.copyOf(shorts, 5) + expect(shortscopy).toEqual(Array[Short](1, 2, 3, 0, 0)) + } + + it("should respond to `copyOf` with key for Byte") { + val bytes: Array[Byte] = Array(42, 43, 44) + val floatscopy = Arrays.copyOf(bytes, 5) + expect(floatscopy).toEqual(Array[Byte](42, 43, 44, 0, 0)) + } + + it("should respond to `copyOf` with key for Char") { + val chars: Array[Char] = Array('a', 'b', '0') + val charscopy = Arrays.copyOf(chars, 5) + expect(charscopy(4)).toEqual(0.toChar) + } + + it("should respond to `copyOf` with key for Double") { + val doubles: Array[Double] = Array(0.1, 0.2, 0.3) + val doublescopy = Arrays.copyOf(doubles, 5) + expect(doublescopy).toEqual(Array[Double](0.1, 0.2, 0.3, 0, 0)) + } + + it("should respond to `copyOf` with key for Float") { + val floats: Array[Float] = Array(0.1f, 0.2f, 0.3f) + val floatscopy = Arrays.copyOf(floats, 5) + expect(floatscopy).toEqual(Array[Float](0.1f, 0.2f, 0.3f, 0f, 0f)) + } + + it("should respond to `copyOf` with key for Boolean") { + val bools: Array[Boolean] = Array(false, true, false) + val boolscopy = Arrays.copyOf(bools, 5) + expect(boolscopy).toEqual(Array[Boolean](false, true, false, false, false)) + } + + it("should respond to `copyOf` with key for AnyRef") { + val anyrefs: Array[AnyRef] = Array("a", "b", "c") + val anyrefscopy = Arrays.copyOf(anyrefs, 5) + expect(anyrefscopy.getClass() == classOf[Array[AnyRef]]).toBeTruthy + expect(anyrefscopy).toEqual(Array[AnyRef]("a", "b", "c", null, null)) + + val sequences: Array[CharSequence] = Array("a", "b", "c") + val sequencescopy = Arrays.copyOf(sequences, 2) + expect(sequencescopy.getClass() == classOf[Array[CharSequence]]) + expect(sequencescopy).toEqual(Array[CharSequence]("a", "b")) + } + + it("should respond to `copyOfRange` for AnyRef") { + val anyrefs: Array[AnyRef] = Array("a", "b", "c", "d", "e") + val anyrefscopy = Arrays.copyOfRange(anyrefs, 2, 4) + expect(anyrefscopy.getClass() == classOf[Array[AnyRef]]).toBeTruthy + expect(anyrefscopy).toEqual(Array[AnyRef]("c", "d")) + + val sequences: Array[CharSequence] = Array("a", "b", "c", "d", "e") + val sequencescopy = Arrays.copyOfRange(sequences, 1, 5) + expect(sequencescopy.getClass() == classOf[Array[CharSequence]]) + expect(sequencescopy).toEqual(Array[CharSequence]("b", "c", "d", "e")) + } + + it("should respond to `hashCode` for Boolean") { + expect(Arrays.hashCode(null: Array[Boolean])).toEqual(0) + expect(Arrays.hashCode(Array[Boolean]())).toEqual(1) + expect(Arrays.hashCode(Array[Boolean](false))).toEqual(1268) + expect(Arrays.hashCode(Array[Boolean](true, false))).toEqual(40359) + } + + it("should respond to `hashCode` for Chars") { + expect(Arrays.hashCode(null: Array[Char])).toEqual(0) + expect(Arrays.hashCode(Array[Char]())).toEqual(1) + expect(Arrays.hashCode(Array[Char]('a'))).toEqual(128) + expect(Arrays.hashCode(Array[Char]('c', '&'))).toEqual(4068) + expect(Arrays.hashCode(Array[Char]('-', '5', 'q'))).toEqual(74792) + expect(Arrays.hashCode(Array[Char]('.', ' ', '\u4323', 'v', '~'))).toEqual(88584920) + } + + it("should respond to `hashCode` for Bytes") { + expect(Arrays.hashCode(null: Array[Byte])).toEqual(0) + expect(Arrays.hashCode(Array[Byte]())).toEqual(1) + expect(Arrays.hashCode(Array[Byte](1))).toEqual(32) + expect(Arrays.hashCode(Array[Byte](7, -125))).toEqual(1053) + expect(Arrays.hashCode(Array[Byte](3, 0, 45))).toEqual(32719) + expect(Arrays.hashCode(Array[Byte](0, 45, 100, 1, 1))).toEqual(30065878) + } + + it("should respond to `hashCode` for Shorts") { + expect(Arrays.hashCode(null: Array[Short])).toEqual(0) + expect(Arrays.hashCode(Array[Short]())).toEqual(1) + expect(Arrays.hashCode(Array[Short](1))).toEqual(32) + expect(Arrays.hashCode(Array[Short](7, -125))).toEqual(1053) + expect(Arrays.hashCode(Array[Short](3, 0, 4534))).toEqual(37208) + expect(Arrays.hashCode(Array[Short](0, 45, 100, 1, 1))).toEqual(30065878) + } + + it("should respond to `hashCode` for Ints") { + expect(Arrays.hashCode(null: Array[Int])).toEqual(0) + expect(Arrays.hashCode(Array[Int]())).toEqual(1) + expect(Arrays.hashCode(Array[Int](1))).toEqual(32) + expect(Arrays.hashCode(Array[Int](7, -125))).toEqual(1053) + expect(Arrays.hashCode(Array[Int](3, 0, 4534))).toEqual(37208) + expect(Arrays.hashCode(Array[Int](0, 45, 100, 1, 1, Int.MaxValue))).toEqual(-1215441431) + } + + it("should respond to `hashCode` for Longs") { + expect(Arrays.hashCode(null: Array[Long])).toEqual(0) + expect(Arrays.hashCode(Array[Long]())).toEqual(1) + expect(Arrays.hashCode(Array[Long](1L))).toEqual(32) + expect(Arrays.hashCode(Array[Long](7L, -125L))).toEqual(1302) + expect(Arrays.hashCode(Array[Long](3L, 0L, 4534L))).toEqual(37208) + expect(Arrays.hashCode(Array[Long](0L, 45L, 100L, 1L, 1L, Int.MaxValue))).toEqual(-1215441431) + expect(Arrays.hashCode(Array[Long](0L, 34573566354545L, 100L, 1L, 1L, Int.MaxValue))).toEqual(-1952288964) + } + + it("should respond to `hashCode` for Floats") { + expect(Arrays.hashCode(null: Array[Float])).toEqual(0) + expect(Arrays.hashCode(Array[Float]())).toEqual(1) + expect(Arrays.hashCode(Array[Float](1f))).toEqual(32) + expect(Arrays.hashCode(Array[Float](7.2f, -125.2f))).toEqual(-2082726591) + expect(Arrays.hashCode(Array[Float](302.1f, 0.0f, 4534f))).toEqual(-1891539602) + expect(Arrays.hashCode(Array[Float](0.0f, 45f, -100f, 1.1f, -1f, 3567f))).toEqual(-1591440133) + } + + it("should respond to `hashCode` for Doubles") { + expect(Arrays.hashCode(null: Array[Double])).toEqual(0) + expect(Arrays.hashCode(Array[Double]())).toEqual(1) + expect(Arrays.hashCode(Array[Double](1.1))).toEqual(-1503133662) + expect(Arrays.hashCode(Array[Double](7.3, -125.23))).toEqual(-2075734168) + expect(Arrays.hashCode(Array[Double](3.9, 0.2, 4534.9))).toEqual(-557562564) + expect(Arrays.hashCode(Array[Double](0.1, 45.1, -100.0, 1.1, 1.7))).toEqual(-1750344582) + expect(Arrays.hashCode(Array[Double](0.0, 34573566354545.9, 100.2, 1.1, 1.2, Int.MaxValue))).toEqual(-1764602991) + } + + it("should respond to `hashCode` for AnyRef") { + expect(Arrays.hashCode(null: Array[AnyRef])).toEqual(0) + expect(Arrays.hashCode(Array[AnyRef]())).toEqual(1) + expect(Arrays.hashCode(Array[AnyRef](null, null))).toEqual(961) + expect(Arrays.hashCode(Array[AnyRef]("a", "b", null))).toEqual(126046) + expect(Arrays.hashCode(Array[AnyRef](null, "a", "b", null, "fooooo"))).toEqual(-1237252983) + } + + it("should respond to `equals` for Booleans") { + val a1 = Array(true, false) + + expect(Arrays.equals(a1, a1)).toBeTruthy + expect(Arrays.equals(a1, Array(true, false))).toBeTruthy + + expect(Arrays.equals(a1, Array(true))).toBeFalsy + expect(Arrays.equals(a1, Array(false))).toBeFalsy + expect(Arrays.equals(a1, Array[Boolean]())).toBeFalsy + expect(Arrays.equals(a1, Array(false, true))).toBeFalsy + expect(Arrays.equals(a1, Array(false, true, false))).toBeFalsy + } + + it("should respond to `equals` for Bytes") { + val a1 = Array[Byte](1, -7, 10) + + expect(Arrays.equals(null: Array[Byte], null: Array[Byte])).toBeTruthy + expect(Arrays.equals(a1, a1)).toBeTruthy + expect(Arrays.equals(a1, Array[Byte](1, -7, 10))).toBeTruthy + + expect(Arrays.equals(a1, null)).toBeFalsy + expect(Arrays.equals(a1, Array[Byte](3))).toBeFalsy + expect(Arrays.equals(a1, Array[Byte](1))).toBeFalsy + expect(Arrays.equals(a1, Array[Byte]())).toBeFalsy + expect(Arrays.equals(a1, Array[Byte](1, -7, 11))).toBeFalsy + expect(Arrays.equals(a1, Array[Byte](1, -7, 11, 20))).toBeFalsy + } + + it("should respond to `equals` for Chars") { + val a1 = Array[Char]('a', '0', '-') + + expect(Arrays.equals(null: Array[Char], null: Array[Char])).toBeTruthy + expect(Arrays.equals(a1, a1)).toBeTruthy + expect(Arrays.equals(a1, Array[Char]('a', '0', '-'))).toBeTruthy + + expect(Arrays.equals(a1, null)).toBeFalsy + expect(Arrays.equals(a1, Array[Char]('z'))).toBeFalsy + expect(Arrays.equals(a1, Array[Char]('a'))).toBeFalsy + expect(Arrays.equals(a1, Array[Char]())).toBeFalsy + expect(Arrays.equals(a1, Array[Char]('a', '0', '+'))).toBeFalsy + expect(Arrays.equals(a1, Array[Char]('a', '0', '-', 'z'))).toBeFalsy + } + + it("should respond to `equals` for Shorts") { + val a1 = Array[Short](1, -7, 10) + + expect(Arrays.equals(null: Array[Short], null: Array[Short])).toBeTruthy + expect(Arrays.equals(a1, a1)).toBeTruthy + expect(Arrays.equals(a1, Array[Short](1, -7, 10))).toBeTruthy + + expect(Arrays.equals(a1, null)).toBeFalsy + expect(Arrays.equals(a1, Array[Short](3))).toBeFalsy + expect(Arrays.equals(a1, Array[Short](1))).toBeFalsy + expect(Arrays.equals(a1, Array[Short]())).toBeFalsy + expect(Arrays.equals(a1, Array[Short](1, -7, 11))).toBeFalsy + expect(Arrays.equals(a1, Array[Short](1, -7, 11, 20))).toBeFalsy + } + + it("should respond to `equals` for Ints") { + val a1 = Array[Int](1, -7, 10) + + expect(Arrays.equals(null: Array[Int], null: Array[Int])).toBeTruthy + expect(Arrays.equals(a1, a1)).toBeTruthy + expect(Arrays.equals(a1, Array[Int](1, -7, 10))).toBeTruthy + + expect(Arrays.equals(a1, null)).toBeFalsy + expect(Arrays.equals(a1, Array[Int](3))).toBeFalsy + expect(Arrays.equals(a1, Array[Int](1))).toBeFalsy + expect(Arrays.equals(a1, Array[Int]())).toBeFalsy + expect(Arrays.equals(a1, Array[Int](1, -7, 11))).toBeFalsy + expect(Arrays.equals(a1, Array[Int](1, -7, 11, 20))).toBeFalsy + } + + it("should respond to `equals` for Longs") { + val a1 = Array[Long](1L, -7L, 10L) + + expect(Arrays.equals(null: Array[Long], null: Array[Long])).toBeTruthy + expect(Arrays.equals(a1, a1)).toBeTruthy + expect(Arrays.equals(a1, Array[Long](1L, -7L, 10L))).toBeTruthy + + expect(Arrays.equals(a1, null)).toBeFalsy + expect(Arrays.equals(a1, Array[Long](3L))).toBeFalsy + expect(Arrays.equals(a1, Array[Long](1L))).toBeFalsy + expect(Arrays.equals(a1, Array[Long]())).toBeFalsy + expect(Arrays.equals(a1, Array[Long](1L, -7L, 11L))).toBeFalsy + expect(Arrays.equals(a1, Array[Long](1L, -7L, 11L, 20L))).toBeFalsy + } + + it("should respond to `equals` for Floats") { + val a1 = Array[Float](1.1f, -7.4f, 10.0f) + + expect(Arrays.equals(null: Array[Float], null: Array[Float])).toBeTruthy + expect(Arrays.equals(a1, a1)).toBeTruthy + expect(Arrays.equals(a1, Array[Float](1.1f, -7.4f, 10.0f))).toBeTruthy + + expect(Arrays.equals(a1, null)).toBeFalsy + expect(Arrays.equals(a1, Array[Float](3.0f))).toBeFalsy + expect(Arrays.equals(a1, Array[Float](1.1f))).toBeFalsy + expect(Arrays.equals(a1, Array[Float]())).toBeFalsy + expect(Arrays.equals(a1, Array[Float](1.1f, -7.4f, 11.0f))).toBeFalsy + expect(Arrays.equals(a1, Array[Float](1.1f, -7.4f, 10.0f, 20.0f))).toBeFalsy + } + + it("should respond to `equals` for Doubles") { + val a1 = Array[Double](1.1, -7.4, 10.0) + + expect(Arrays.equals(null: Array[Double], null: Array[Double])).toBeTruthy + expect(Arrays.equals(a1, a1)).toBeTruthy + expect(Arrays.equals(a1, Array[Double](1.1, -7.4, 10.0))).toBeTruthy + + expect(Arrays.equals(a1, null)).toBeFalsy + expect(Arrays.equals(a1, Array[Double](3.0))).toBeFalsy + expect(Arrays.equals(a1, Array[Double](1.1))).toBeFalsy + expect(Arrays.equals(a1, Array[Double]())).toBeFalsy + expect(Arrays.equals(a1, Array[Double](1.1, -7.4, 11.0))).toBeFalsy + expect(Arrays.equals(a1, Array[Double](1.1, -7.4, 10.0, 20.0))).toBeFalsy + } + + it("should respond to `equals` for AnyRefs") { + class A(private val x: Int) { + override def equals(that: Any) = that match { + case that: A => this.x == that.x + case _ => false + } + } + + def A(x: Int) = new A(x) + + val a1 = Array[AnyRef](A(1), A(-7), A(10)) + + expect(Arrays.equals(null: Array[AnyRef], null: Array[AnyRef])).toBeTruthy + expect(Arrays.equals(a1, a1)).toBeTruthy + expect(Arrays.equals(a1, Array[AnyRef](A(1), A(-7), A(10)))).toBeTruthy + + expect(Arrays.equals(a1, null)).toBeFalsy + expect(Arrays.equals(a1, Array[AnyRef](A(3)))).toBeFalsy + expect(Arrays.equals(a1, Array[AnyRef](A(1)))).toBeFalsy + expect(Arrays.equals(a1, Array[AnyRef]())).toBeFalsy + expect(Arrays.equals(a1, Array[AnyRef](A(1), null, A(11)))).toBeFalsy + expect(Arrays.equals(a1, Array[AnyRef](A(1), A(-7), A(11), A(20)))).toBeFalsy + } + + } + +} diff --git a/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/AtomicTest.scala b/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/AtomicTest.scala new file mode 100644 index 0000000..c4f065a --- /dev/null +++ b/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/AtomicTest.scala @@ -0,0 +1,119 @@ +/* __ *\ +** ________ ___ / / ___ __ ____ Scala.js Test Suite ** +** / __/ __// _ | / / / _ | __ / // __/ (c) 2013, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \ http://scala-js.org/ ** +** /____/\___/_/ |_/____/_/ | |__/ /____/ ** +** |/____/ ** +\* */ +package scala.scalajs.testsuite.javalib + +import scala.language.implicitConversions + +import scala.scalajs.js + +import org.scalajs.jasminetest.JasmineTest + +object AtomicTest extends JasmineTest { + + describe("java.util.concurrent.AtomicLong") { + + it("Should have all the normal operations") { + val atomic = new java.util.concurrent.atomic.AtomicLong(10) + expect(atomic.get()).toEqual(10) + atomic.set(20) + expect(atomic.get()).toEqual(20) + expect(atomic.getAndIncrement()).toEqual(20) + expect(atomic.get()).toEqual(21) + expect(atomic.getAndDecrement()).toEqual(21) + expect(atomic.get()).toEqual(20) + expect(atomic.getAndSet(0)).toEqual(20) + expect(atomic.get()).toEqual(0) + expect(atomic.incrementAndGet()).toEqual(1) + expect(atomic.get()).toEqual(1) + expect(atomic.decrementAndGet()).toEqual(0) + expect(atomic.get()).toEqual(0) + expect(atomic.addAndGet(10)).toEqual(10) + expect(atomic.get()).toEqual(10) + expect(atomic.intValue).toEqual(10) + expect(atomic.longValue).toEqual(10L) + expect(atomic.floatValue).toEqual(10.0f) + expect(atomic.doubleValue).toEqual(10) + expect(atomic.compareAndSet(1, 20)).toEqual(false) + expect(atomic.get()).toEqual(10) + expect(atomic.compareAndSet(10, 20)).toEqual(true) + expect(atomic.get()).toEqual(20) + } + } + describe("java.util.concurrent.AtomicInteger") { + it("Should have all the normal operations") { + val atomic = new java.util.concurrent.atomic.AtomicInteger(10) + expect(atomic.get()).toEqual(10) + atomic.set(20) + expect(atomic.get()).toEqual(20) + expect(atomic.getAndIncrement()).toEqual(20) + expect(atomic.get()).toEqual(21) + expect(atomic.getAndDecrement()).toEqual(21) + expect(atomic.get()).toEqual(20) + expect(atomic.getAndSet(0)).toEqual(20) + expect(atomic.get()).toEqual(0) + expect(atomic.incrementAndGet()).toEqual(1) + expect(atomic.get()).toEqual(1) + expect(atomic.decrementAndGet()).toEqual(0) + expect(atomic.get()).toEqual(0) + expect(atomic.addAndGet(10)).toEqual(10) + expect(atomic.get()).toEqual(10) + expect(atomic.intValue).toEqual(10) + expect(atomic.longValue).toEqual(10L) + expect(atomic.floatValue).toEqual(10.0f) + expect(atomic.doubleValue).toEqual(10) + expect(atomic.compareAndSet(1, 20)).toEqual(false) + expect(atomic.get()).toEqual(10) + expect(atomic.compareAndSet(10, 20)).toEqual(true) + expect(atomic.get()).toEqual(20) + } + } + describe("java.util.concurrent.AtomicBoolean") { + it("Should have all the normal operations") { + val atomic = new java.util.concurrent.atomic.AtomicBoolean(true) + expect(atomic.get()).toEqual(true) + atomic.set(false) + expect(atomic.get()).toEqual(false) + expect(atomic.compareAndSet(true, true)).toEqual(false) + expect(atomic.get()).toEqual(false) + expect(atomic.compareAndSet(false, true)).toEqual(true) + expect(atomic.get()).toEqual(true) + expect(atomic.getAndSet(false)).toEqual(true) + expect(atomic.get()).toEqual(false) + } + } + describe("java.util.concurrent.AtomicReference") { + it("Should have all the normal operations") { + val thing1 = Foo(5) + val thing1bis = Foo(5) // equals(), but not the same reference + val thing2 = Foo(10) + + implicit def foo2js(foo: Foo): js.Any = foo.asInstanceOf[js.Any] + + // sanity + expect(thing1 == thing1bis).toBeTruthy + expect(thing1 == thing2).toBeFalsy + expect(thing1).toBe(thing1) + expect(thing1).not.toBe(thing1bis) + + // actual test + val atomic = new java.util.concurrent.atomic.AtomicReference(thing1) + expect(atomic.get()).toBe(thing1) + atomic.set(thing2) + expect(atomic.get()).toBe(thing2) + expect(atomic.compareAndSet(thing1, thing1)).toEqual(false) + expect(atomic.get()).toBe(thing2) + expect(atomic.compareAndSet(thing2, thing1)).toEqual(true) + expect(atomic.get()).toBe(thing1) + expect(atomic.compareAndSet(thing1bis, thing2)).toEqual(false) + expect(atomic.getAndSet(thing2)).toBe(thing1) + expect(atomic.get()).toBe(thing2) + } + } + + case class Foo(i: Int) +} diff --git a/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/BooleanTest.scala b/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/BooleanTest.scala new file mode 100644 index 0000000..87c65e9 --- /dev/null +++ b/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/BooleanTest.scala @@ -0,0 +1,62 @@ +/* __ *\ +** ________ ___ / / ___ __ ____ Scala.js Test Suite ** +** / __/ __// _ | / / / _ | __ / // __/ (c) 2013, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \ http://scala-js.org/ ** +** /____/\___/_/ |_/____/_/ | |__/ /____/ ** +** |/____/ ** +\* */ +package scala.scalajs.testsuite.javalib + +import java.lang.{Boolean => JBoolean} + +import org.scalajs.jasminetest.JasmineTest + +/** + * tests the implementation of the java standard library Boolean + */ +object BooleanTest extends JasmineTest { + + describe("java.lang.Boolean") { + + it("should provide `booleanValue`") { + expect(JBoolean.TRUE.booleanValue()).toBe(true) + expect(JBoolean.FALSE.booleanValue()).toBe(false) + expect(() => (null: JBoolean).booleanValue()).toThrow + } + + it("should provide `compareTo`") { + def compare(x: Boolean, y: Boolean): Int = + new JBoolean(x).compareTo(new JBoolean(y)) + + expect(compare(false, false)).toEqual(0) + expect(compare(false, true)).toBeLessThan(0) + expect(compare(true, false)).toBeGreaterThan(0) + expect(compare(true, true)).toEqual(0) + } + + it("should be a Comparable") { + def compare(x: Any, y: Any): Int = + x.asInstanceOf[Comparable[Any]].compareTo(y) + + expect(compare(false, false)).toEqual(0) + expect(compare(false, true)).toBeLessThan(0) + expect(compare(true, false)).toBeGreaterThan(0) + expect(compare(true, true)).toEqual(0) + } + + it("should parse strings") { + def test(s: String, v: Boolean): Unit = { + expect(JBoolean.parseBoolean(s)).toEqual(v) + expect(JBoolean.valueOf(s).booleanValue()).toEqual(v) + expect(new JBoolean(s).booleanValue()).toEqual(v) + } + + test("false", false) + test("true", true) + test("TrUe", true) + test(null, false) + test("truee", false) + } + + } +} diff --git a/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/ByteTest.scala b/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/ByteTest.scala new file mode 100644 index 0000000..b033c59 --- /dev/null +++ b/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/ByteTest.scala @@ -0,0 +1,64 @@ +/* __ *\ +** ________ ___ / / ___ __ ____ Scala.js Test Suite ** +** / __/ __// _ | / / / _ | __ / // __/ (c) 2013, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \ http://scala-js.org/ ** +** /____/\___/_/ |_/____/_/ | |__/ /____/ ** +** |/____/ ** +\* */ +package scala.scalajs.testsuite.javalib + +import java.lang.{Byte => JByte} + +import org.scalajs.jasminetest.JasmineTest + +/** + * tests the implementation of the java standard library Byte + */ +object ByteTest extends JasmineTest { + + describe("java.lang.Byte") { + + it("should provide `compareTo`") { + def compare(x: Byte, y: Byte): Int = + new JByte(x).compareTo(new JByte(y)) + + expect(compare(0.toByte, 5.toByte)).toBeLessThan(0) + expect(compare(10.toByte, 9.toByte)).toBeGreaterThan(0) + expect(compare(-2.toByte, -1.toByte)).toBeLessThan(0) + expect(compare(3.toByte, 3.toByte)).toEqual(0) + } + + it("should be a Comparable") { + def compare(x: Any, y: Any): Int = + x.asInstanceOf[Comparable[Any]].compareTo(y) + + expect(compare(0.toByte, 5.toByte)).toBeLessThan(0) + expect(compare(10.toByte, 9.toByte)).toBeGreaterThan(0) + expect(compare(-2.toByte, -1.toByte)).toBeLessThan(0) + expect(compare(3.toByte, 3.toByte)).toEqual(0) + } + + it("should parse strings") { + def test(s: String, v: Byte): Unit = { + expect(JByte.parseByte(s)).toEqual(v) + expect(JByte.valueOf(s).byteValue()).toEqual(v) + expect(new JByte(s).byteValue()).toEqual(v) + } + + test("0", 0) + test("5", 5) + test("127", 127) + test("-100", -100) + } + + it("should reject invalid strings when parsing") { + def test(s: String): Unit = + expect(() => JByte.parseByte(s)).toThrow + + test("abc") + test("") + test("200") // out of range + } + + } +} diff --git a/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/CharacterTest.scala b/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/CharacterTest.scala new file mode 100644 index 0000000..f206221 --- /dev/null +++ b/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/CharacterTest.scala @@ -0,0 +1,672 @@ +/* __ *\ +** ________ ___ / / ___ __ ____ Scala.js Test Suite ** +** / __/ __// _ | / / / _ | __ / // __/ (c) 2013, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \ http://scala-js.org/ ** +** /____/\___/_/ |_/____/_/ | |__/ /____/ ** +** |/____/ ** +\* */ +package scala.scalajs.testsuite.javalib + +import org.scalajs.jasminetest.JasmineTest +import scala.scalajs.js + +object CharacterTest extends JasmineTest { + + describe("java.lang.Character") { + + it("should provide `isISOControl`") { + val isoControlChars = (('\u0000' to '\u001F') ++ ('\u007F' to '\u009F')).map(_.toInt).toSet + isoControlChars foreach { c => + expect(Character.isISOControl(c)).toEqual(true) + } + + val randomInts = List.fill(100)(scala.util.Random.nextInt) + ((-1000 to 1000) ++ randomInts).filterNot(isoControlChars) foreach { c => + expect(Character.isISOControl(c)).toEqual(false) + } + } + + it("should provide `digit`") { + expect(Character.digit('a', 16)).toEqual(10) + expect(Character.digit('}', 5)).toEqual(-1) + expect(Character.digit('1', 50)).toEqual(-1) + expect(Character.digit('1', 36)).toEqual(1) + expect(Character.digit('Z', 36)).toEqual(35) + expect(Character.digit('\uFF22', 20)).toEqual(11) + } + + it("should provide isDigit") { + expect(Character.isDigit('a')).toBeFalsy + expect(Character.isDigit('0')).toBeTruthy + expect(Character.isDigit('5')).toBeTruthy + expect(Character.isDigit('9')).toBeTruthy + expect(Character.isDigit('z')).toBeFalsy + expect(Character.isDigit(' ')).toBeFalsy + } + + it("should provide `compareTo`") { + def compare(x: Char, y: Char): Int = + new Character(x).compareTo(new Character(y)) + + expect(compare('0', '5')).toBeLessThan(0) + expect(compare('o', 'g')).toBeGreaterThan(0) + expect(compare('A', 'a')).toBeLessThan(0) + expect(compare('b', 'b')).toEqual(0) + } + + it("should be a Comparable") { + def compare(x: Any, y: Any): Int = + x.asInstanceOf[Comparable[Any]].compareTo(y) + + expect(compare('0', '5')).toBeLessThan(0) + expect(compare('o', 'g')).toBeGreaterThan(0) + expect(compare('A', 'a')).toBeLessThan(0) + expect(compare('b', 'b')).toEqual(0) + } + + it("should provide isIdentifierIgnorable") { + for (c <- '\u0000' to '\u0008') + expect(Character.isIdentifierIgnorable(c)).toBeTruthy + + for (c <- '\u000E' to '\u001B') + expect(Character.isIdentifierIgnorable(c)).toBeTruthy + + for (c <- '\u007F' to '\u009F') + expect(Character.isIdentifierIgnorable(c)).toBeTruthy + + // Exhaustive list of Cf category. Unicode 7.0.0 + expect(Character.isIdentifierIgnorable('\u00AD')).toBeTruthy + expect(Character.isIdentifierIgnorable('\u0600')).toBeTruthy + expect(Character.isIdentifierIgnorable('\u0601')).toBeTruthy + expect(Character.isIdentifierIgnorable('\u0602')).toBeTruthy + expect(Character.isIdentifierIgnorable('\u0603')).toBeTruthy + expect(Character.isIdentifierIgnorable('\u0604')).toBeTruthy + expect(Character.isIdentifierIgnorable('\u0605')).toBeTruthy + expect(Character.isIdentifierIgnorable('\u061C')).toBeTruthy + expect(Character.isIdentifierIgnorable('\u06DD')).toBeTruthy + expect(Character.isIdentifierIgnorable('\u070F')).toBeTruthy + expect(Character.isIdentifierIgnorable('\u180E')).toBeTruthy + expect(Character.isIdentifierIgnorable('\u200B')).toBeTruthy + expect(Character.isIdentifierIgnorable('\u200C')).toBeTruthy + expect(Character.isIdentifierIgnorable('\u200D')).toBeTruthy + expect(Character.isIdentifierIgnorable('\u200E')).toBeTruthy + expect(Character.isIdentifierIgnorable('\u200F')).toBeTruthy + expect(Character.isIdentifierIgnorable('\u202A')).toBeTruthy + expect(Character.isIdentifierIgnorable('\u202B')).toBeTruthy + expect(Character.isIdentifierIgnorable('\u202C')).toBeTruthy + expect(Character.isIdentifierIgnorable('\u202D')).toBeTruthy + expect(Character.isIdentifierIgnorable('\u202E')).toBeTruthy + expect(Character.isIdentifierIgnorable('\u2060')).toBeTruthy + expect(Character.isIdentifierIgnorable('\u2061')).toBeTruthy + expect(Character.isIdentifierIgnorable('\u2062')).toBeTruthy + expect(Character.isIdentifierIgnorable('\u2063')).toBeTruthy + expect(Character.isIdentifierIgnorable('\u2064')).toBeTruthy + expect(Character.isIdentifierIgnorable('\u2066')).toBeTruthy + expect(Character.isIdentifierIgnorable('\u2067')).toBeTruthy + expect(Character.isIdentifierIgnorable('\u2068')).toBeTruthy + expect(Character.isIdentifierIgnorable('\u2069')).toBeTruthy + expect(Character.isIdentifierIgnorable('\u206A')).toBeTruthy + expect(Character.isIdentifierIgnorable('\u206B')).toBeTruthy + expect(Character.isIdentifierIgnorable('\u206C')).toBeTruthy + expect(Character.isIdentifierIgnorable('\u206D')).toBeTruthy + expect(Character.isIdentifierIgnorable('\u206E')).toBeTruthy + expect(Character.isIdentifierIgnorable('\u206F')).toBeTruthy + expect(Character.isIdentifierIgnorable('\uFEFF')).toBeTruthy + expect(Character.isIdentifierIgnorable('\uFFF9')).toBeTruthy + expect(Character.isIdentifierIgnorable('\uFFFA')).toBeTruthy + expect(Character.isIdentifierIgnorable('\uFFFB')).toBeTruthy + + // BUG in JDK? 17B4 should be "Mn", Java says "Cf" + //expect(Character.isIdentifierIgnorable('\u17b4')).toBeTruthy + + // 100 randomly generated negatives + expect(Character.isIdentifierIgnorable('\u745a')).toBeFalsy + expect(Character.isIdentifierIgnorable('\ub445')).toBeFalsy + expect(Character.isIdentifierIgnorable('\ub23a')).toBeFalsy + expect(Character.isIdentifierIgnorable('\ub029')).toBeFalsy + expect(Character.isIdentifierIgnorable('\ufb5c')).toBeFalsy + expect(Character.isIdentifierIgnorable('\u1b67')).toBeFalsy + expect(Character.isIdentifierIgnorable('\u943b')).toBeFalsy + expect(Character.isIdentifierIgnorable('\ue766')).toBeFalsy + expect(Character.isIdentifierIgnorable('\uad12')).toBeFalsy + expect(Character.isIdentifierIgnorable('\ub80b')).toBeFalsy + expect(Character.isIdentifierIgnorable('\u7341')).toBeFalsy + expect(Character.isIdentifierIgnorable('\ubc73')).toBeFalsy + expect(Character.isIdentifierIgnorable('\uabb9')).toBeFalsy + expect(Character.isIdentifierIgnorable('\ub34b')).toBeFalsy + expect(Character.isIdentifierIgnorable('\u1063')).toBeFalsy + expect(Character.isIdentifierIgnorable('\u272f')).toBeFalsy + expect(Character.isIdentifierIgnorable('\u3801')).toBeFalsy + expect(Character.isIdentifierIgnorable('\u53a6')).toBeFalsy + expect(Character.isIdentifierIgnorable('\u2ec2')).toBeFalsy + expect(Character.isIdentifierIgnorable('\u540c')).toBeFalsy + expect(Character.isIdentifierIgnorable('\uc85f')).toBeFalsy + expect(Character.isIdentifierIgnorable('\ud2c8')).toBeFalsy + expect(Character.isIdentifierIgnorable('\u551b')).toBeFalsy + expect(Character.isIdentifierIgnorable('\uc0a1')).toBeFalsy + expect(Character.isIdentifierIgnorable('\ud25a')).toBeFalsy + expect(Character.isIdentifierIgnorable('\u2b98')).toBeFalsy + expect(Character.isIdentifierIgnorable('\u398b')).toBeFalsy + expect(Character.isIdentifierIgnorable('\ubc77')).toBeFalsy + expect(Character.isIdentifierIgnorable('\u54cc')).toBeFalsy + expect(Character.isIdentifierIgnorable('\uc9a0')).toBeFalsy + expect(Character.isIdentifierIgnorable('\ud10f')).toBeFalsy + expect(Character.isIdentifierIgnorable('\uf7e1')).toBeFalsy + expect(Character.isIdentifierIgnorable('\u0f29')).toBeFalsy + expect(Character.isIdentifierIgnorable('\uafcd')).toBeFalsy + expect(Character.isIdentifierIgnorable('\uf187')).toBeFalsy + expect(Character.isIdentifierIgnorable('\u6287')).toBeFalsy + expect(Character.isIdentifierIgnorable('\uacb6')).toBeFalsy + expect(Character.isIdentifierIgnorable('\uff99')).toBeFalsy + expect(Character.isIdentifierIgnorable('\ub59e')).toBeFalsy + expect(Character.isIdentifierIgnorable('\uf630')).toBeFalsy + expect(Character.isIdentifierIgnorable('\ufaec')).toBeFalsy + expect(Character.isIdentifierIgnorable('\ua7d7')).toBeFalsy + expect(Character.isIdentifierIgnorable('\u3eab')).toBeFalsy + expect(Character.isIdentifierIgnorable('\u54a5')).toBeFalsy + expect(Character.isIdentifierIgnorable('\u393a')).toBeFalsy + expect(Character.isIdentifierIgnorable('\uc621')).toBeFalsy + expect(Character.isIdentifierIgnorable('\u766c')).toBeFalsy + expect(Character.isIdentifierIgnorable('\ud64c')).toBeFalsy + expect(Character.isIdentifierIgnorable('\u8beb')).toBeFalsy + expect(Character.isIdentifierIgnorable('\u44e2')).toBeFalsy + expect(Character.isIdentifierIgnorable('\ub6f6')).toBeFalsy + expect(Character.isIdentifierIgnorable('\u58b6')).toBeFalsy + expect(Character.isIdentifierIgnorable('\u3bad')).toBeFalsy + expect(Character.isIdentifierIgnorable('\u3c28')).toBeFalsy + expect(Character.isIdentifierIgnorable('\ufbfd')).toBeFalsy + expect(Character.isIdentifierIgnorable('\u585f')).toBeFalsy + expect(Character.isIdentifierIgnorable('\u7227')).toBeFalsy + expect(Character.isIdentifierIgnorable('\ucea7')).toBeFalsy + expect(Character.isIdentifierIgnorable('\u2c82')).toBeFalsy + expect(Character.isIdentifierIgnorable('\u686d')).toBeFalsy + expect(Character.isIdentifierIgnorable('\u120d')).toBeFalsy + expect(Character.isIdentifierIgnorable('\uf3db')).toBeFalsy + expect(Character.isIdentifierIgnorable('\u320a')).toBeFalsy + expect(Character.isIdentifierIgnorable('\ud96e')).toBeFalsy + expect(Character.isIdentifierIgnorable('\u85eb')).toBeFalsy + expect(Character.isIdentifierIgnorable('\u9648')).toBeFalsy + expect(Character.isIdentifierIgnorable('\u08a4')).toBeFalsy + expect(Character.isIdentifierIgnorable('\u9db7')).toBeFalsy + expect(Character.isIdentifierIgnorable('\u82c7')).toBeFalsy + expect(Character.isIdentifierIgnorable('\ufe12')).toBeFalsy + expect(Character.isIdentifierIgnorable('\u0eaf')).toBeFalsy + expect(Character.isIdentifierIgnorable('\u96dc')).toBeFalsy + expect(Character.isIdentifierIgnorable('\u3a2a')).toBeFalsy + expect(Character.isIdentifierIgnorable('\uc72e')).toBeFalsy + expect(Character.isIdentifierIgnorable('\u3745')).toBeFalsy + expect(Character.isIdentifierIgnorable('\ubcf9')).toBeFalsy + expect(Character.isIdentifierIgnorable('\u5f66')).toBeFalsy + expect(Character.isIdentifierIgnorable('\u9be1')).toBeFalsy + expect(Character.isIdentifierIgnorable('\ud81d')).toBeFalsy + expect(Character.isIdentifierIgnorable('\u3ca3')).toBeFalsy + expect(Character.isIdentifierIgnorable('\u3e82')).toBeFalsy + expect(Character.isIdentifierIgnorable('\u7ce4')).toBeFalsy + expect(Character.isIdentifierIgnorable('\u33ca')).toBeFalsy + expect(Character.isIdentifierIgnorable('\ue725')).toBeFalsy + expect(Character.isIdentifierIgnorable('\uef49')).toBeFalsy + expect(Character.isIdentifierIgnorable('\ue2cf')).toBeFalsy + expect(Character.isIdentifierIgnorable('\udcf0')).toBeFalsy + expect(Character.isIdentifierIgnorable('\u5f2e')).toBeFalsy + expect(Character.isIdentifierIgnorable('\u2a63')).toBeFalsy + expect(Character.isIdentifierIgnorable('\ud2d2')).toBeFalsy + expect(Character.isIdentifierIgnorable('\u8023')).toBeFalsy + expect(Character.isIdentifierIgnorable('\ua957')).toBeFalsy + expect(Character.isIdentifierIgnorable('\u10ba')).toBeFalsy + expect(Character.isIdentifierIgnorable('\uf85f')).toBeFalsy + expect(Character.isIdentifierIgnorable('\uc40d')).toBeFalsy + expect(Character.isIdentifierIgnorable('\u2509')).toBeFalsy + expect(Character.isIdentifierIgnorable('\u0d8e')).toBeFalsy + expect(Character.isIdentifierIgnorable('\u9db8')).toBeFalsy + expect(Character.isIdentifierIgnorable('\u824d')).toBeFalsy + expect(Character.isIdentifierIgnorable('\u5670')).toBeFalsy + expect(Character.isIdentifierIgnorable('\u6005')).toBeFalsy + expect(Character.isIdentifierIgnorable('\ub8de')).toBeFalsy + expect(Character.isIdentifierIgnorable('\uff5c')).toBeFalsy + expect(Character.isIdentifierIgnorable('\ub36d')).toBeFalsy + expect(Character.isIdentifierIgnorable('\u0cf2')).toBeFalsy + expect(Character.isIdentifierIgnorable('\u82f6')).toBeFalsy + expect(Character.isIdentifierIgnorable('\u9206')).toBeFalsy + expect(Character.isIdentifierIgnorable('\u95e1')).toBeFalsy + expect(Character.isIdentifierIgnorable('\u990f')).toBeFalsy + expect(Character.isIdentifierIgnorable('\u9fc7')).toBeFalsy + expect(Character.isIdentifierIgnorable('\udffb')).toBeFalsy + expect(Character.isIdentifierIgnorable('\u0ecb')).toBeFalsy + expect(Character.isIdentifierIgnorable('\u7563')).toBeFalsy + expect(Character.isIdentifierIgnorable('\uf0ff')).toBeFalsy + expect(Character.isIdentifierIgnorable('\u6b2e')).toBeFalsy + expect(Character.isIdentifierIgnorable('\u894c')).toBeFalsy + expect(Character.isIdentifierIgnorable('\u8f06')).toBeFalsy + expect(Character.isIdentifierIgnorable('\uffa9')).toBeFalsy + expect(Character.isIdentifierIgnorable('\u37b0')).toBeFalsy + expect(Character.isIdentifierIgnorable('\u3e04')).toBeFalsy + + } + + it("should provide isUnicodeIdentifierStart") { + // 100 randomly generated positives and 100 randomly generated negatives + + expect(Character.isUnicodeIdentifierStart('\ud6d5')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\u3f9c')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\u3a40')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\u53af')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\u1636')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\u4884')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\ucba4')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\u1ee4')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\u6dec')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\u10d4')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\u631f')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\u3661')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\u55f8')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\ub4ef')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\ud509')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\u65b5')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\u316b')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\ub270')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\u7f0f')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\uff84')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\u11cc')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\u0294')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\u51b1')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\u9ae2')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\u304a')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\ud5c7')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\u3b4b')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\u5e42')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\u51fc')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\uc148')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\uc1ae')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\u7372')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\uc116')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\u5d29')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\u8753')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\u50f8')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\u3f9d')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\u1f44')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\ucd43')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\u9126')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\u8d2e')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\u4f5c')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\u66d7')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\ua30b')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\u140b')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\ub264')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\u7b35')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\u15e4')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\ubb37')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\u34e3')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\uac3e')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\ubd0e')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\ub641')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\u1580')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\u30c1')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\ub0c8')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\u8681')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\u7f14')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\u4142')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\u56c1')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\u0444')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\u9964')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\ub5c0')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\u43d8')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\u479e')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\u0853')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\ube08')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\u9346')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\uf9c1')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\u0e8a')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\u212c')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\u810c')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\u8089')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\u1331')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\ua5f7')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\u5e5e')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\u613b')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\u34a7')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\ud15b')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\uc1fc')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\u92f1')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\u3ae6')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\ufceb')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\u7584')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\ufe98')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\ubb23')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\u7961')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\u4445')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\u4d5f')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\u61cb')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\u5176')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\ub987')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\u906a')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\u4317')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\u93ad')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\u825a')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\u7ff8')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\u533a')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\u5617')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\ufcc6')).toBeTruthy + expect(Character.isUnicodeIdentifierStart('\ue398')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\ueab6')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\ue7bc')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\uf8ab')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\ue27f')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\uebea')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\ueedc')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\uf091')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\u2785')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\u287b')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\uf042')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\u20f9')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\u23d6')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\udc5b')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\ued16')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\u1b6b')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\ue7ba')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\uf7fa')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\u2125')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\uea97')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\ue624')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\ufbb8')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\u2730')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\udb89')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\ue30d')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\u2e24')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\uf03e')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\uda27')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\u28fc')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\u9ffe')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\ude19')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\u0b70')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\uddfc')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\ued53')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\ue8cb')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\udccc')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\u00a3')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\u0bed')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\u0c68')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\uf47b')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\u0f96')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\ue9c3')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\uf784')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\uef4b')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\udee1')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\u2f61')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\uf622')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\u19f9')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\ud86a')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\ued83')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\uf7e4')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\uecce')).toBeFalsy + + // BUG in JDK? A699 should be "Ll", Java says "Cn" + // expect(Character.isUnicodeIdentifierStart('\ua699')).toBeFalsy + + expect(Character.isUnicodeIdentifierStart('\uaa5f')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\udf24')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\u2e0e')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\uf322')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\ue137')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\ued19')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\u21ab')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\ue972')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\udbf2')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\uf54c')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\u4dd3')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\u2769')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\ue363')).toBeFalsy + + // BUG in JDK? 1BBB should be "Lo", Java says "Cn" + // expect(Character.isUnicodeIdentifierStart('\u1bbb')).toBeFalsy + + expect(Character.isUnicodeIdentifierStart('\ueae7')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\u2bf3')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\ue704')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\u1c7f')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\uf52b')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\ue9e3')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\u259b')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\uf250')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\uf42f')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\ue244')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\u20d9')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\ua881')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\u0ee6')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\u2203')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\u0fc7')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\u07fc')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\udb86')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\u2a70')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\u2bb7')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\uecf0')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\ude48')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\u0a3b')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\u20b8')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\uf898')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\u23e6')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\ud8ba')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\uda1e')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\udc12')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\u2a06')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\u0888')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\ud9ec')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\uf81f')).toBeFalsy + expect(Character.isUnicodeIdentifierStart('\uf817')).toBeFalsy + } + + it("should provide isUnicodeIdentifierPart") { + // 100 randomly generated positives and 100 randomly generated negatives + + expect(Character.isUnicodeIdentifierPart('\u48d3')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\u0905')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\u8f51')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\u9bcb')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\ud358')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\u1538')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\uffcf')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\u83ec')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\u3a89')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\ub63a')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\ufe24')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\u2d62')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\u15ca')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\u4fa4')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\u47d1')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\u831c')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\u84e6')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\u7783')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\ua03c')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\u6ecf')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\u147f')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\u67a9')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\u8b6c')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\u3410')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\u2cc0')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\ua332')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\u9733')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\u5df3')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\u3fd7')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\u6611')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\u55b4')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\u8bc8')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\u6f74')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\u6c97')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\u6a86')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\u6000')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\u614f')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\u206e')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\ua801')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\u9edf')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\ub42c')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\u7fcd')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\u8a60')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\u182f')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\u5d0a')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\uaf9c')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\u9d4b')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\u5088')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\uc1a6')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\ubbe4')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\uad25')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\u4653')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\u8add')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\u3d1c')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\u80a8')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\u810e')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\uc1d2')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\ub984')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\u9d13')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\u37c2')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\u13cd')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\u53f9')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\u98b7')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\u57f3')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\ub554')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\u0176')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\ua318')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\u9704')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\u8d52')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\u940a')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\u0fa5')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\u38d1')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\u3b33')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\u93bb')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\u03bd')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\u4c88')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\ud67d')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\ubcbf')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\u3867')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\u4368')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\u8f2d')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\u049a')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\u4c01')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\u5589')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\u5e71')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\ua1fd')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\u3a4a')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\uc111')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\ub465')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\u95af')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\ubf2c')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\u8488')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\u4317')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\u6b77')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\u8995')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\u7467')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\u16b7')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\u3ca0')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\u5332')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\u8654')).toBeTruthy + expect(Character.isUnicodeIdentifierPart('\ua8c8')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\ue3ca')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\uebee')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\u270e')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\uf0ac')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\ue9ec')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\u296a')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\u33fd')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\ue5f4')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\ueb01')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\uf38b')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\u2e6f')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\uea69')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\uf155')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\u0f0e')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\ueb80')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\ud959')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\ue25e')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\uf566')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\ue4a3')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\uec44')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\u3297')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\u3214')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\u1bfd')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\u4dd0')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\uea99')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\u309b')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\uf592')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\uf4dd')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\udfaf')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\udd38')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\uf820')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\uaacd')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\uff5b')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\ude36')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\ue33b')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\udbce')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\ue1f6')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\uf78a')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\ueb44')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\uebd4')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\u1df7')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\u2f10')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\u1cbf')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\u2362')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\uebeb')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\u2ede')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\u221d')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\u2021')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\udf41')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\u05f5')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\u24ab')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\uee15')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\uf175')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\uf35c')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\udc7b')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\ud883')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\uf341')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\ueec6')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\u2f57')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\uff64')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\ue6a4')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\uec34')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\u22a5')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\uf5ac')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\u3360')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\u28b0')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\uf678')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\ue0e4')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\u233f')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\u0afa')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\u2013')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\ud7af')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\ud98e')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\ud8a5')).toBeFalsy + + // BUG in JDK? A79E should be "Lu", Java says "Cn" + // expect(Character.isUnicodeIdentifierPart('\ua79e')).toBeFalsy + + expect(Character.isUnicodeIdentifierPart('\u1806')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\ue07a')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\u2748')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\uabad')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\uec5c')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\ue832')).toBeFalsy + + // BUG in JDK? 08A9 should be "Lo", Java says "Cn" + // expect(Character.isUnicodeIdentifierPart('\u08a9')).toBeFalsy + + expect(Character.isUnicodeIdentifierPart('\ue4bd')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\u208a')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\uf840')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\uf570')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\uef1e')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\u2bd4')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\ue385')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\udc18')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\u0af0')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\u244a')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\uf01e')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\uf114')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\ue9c4')).toBeFalsy + + // BUG in JDK? AAF4 should be "Lm", Java says "Cn" + // expect(Character.isUnicodeIdentifierPart('\uaaf4')).toBeFalsy + + expect(Character.isUnicodeIdentifierPart('\uf7b9')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\udd2f')).toBeFalsy + expect(Character.isUnicodeIdentifierPart('\u2d2c')).toBeFalsy + } + + } +} diff --git a/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/ClassTest.scala b/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/ClassTest.scala new file mode 100644 index 0000000..424edd6 --- /dev/null +++ b/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/ClassTest.scala @@ -0,0 +1,30 @@ +/* __ *\ +** ________ ___ / / ___ __ ____ Scala.js Test Suite ** +** / __/ __// _ | / / / _ | __ / // __/ (c) 2013, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \ http://scala-js.org/ ** +** /____/\___/_/ |_/____/_/ | |__/ /____/ ** +** |/____/ ** +\* */ +package scala.scalajs.testsuite.javalib + +import org.scalajs.jasminetest.JasmineTest +import scala.scalajs.js + +object ClassTest extends JasmineTest { + + describe("java.lang.Class") { + + it("should provide getSimpleName()") { + expect(classOf[java.lang.Integer].getSimpleName()).toEqual("Integer") + expect(classOf[java.lang.Class[_]].getSimpleName()).toEqual("Class") + expect(classOf[scala.collection.Map[_, _]].getSimpleName()).toEqual("Map") + expect(classOf[ClassTestClass#InnerClass].getSimpleName()).toEqual("InnerClass") + } + + } + +} + +class ClassTestClass { + class InnerClass +} diff --git a/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/DateTest.scala b/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/DateTest.scala new file mode 100644 index 0000000..f62f926 --- /dev/null +++ b/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/DateTest.scala @@ -0,0 +1,87 @@ +/* __ *\ +** ________ ___ / / ___ __ ____ Scala.js Test Suite ** +** / __/ __// _ | / / / _ | __ / // __/ (c) 2013, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \ http://scala-js.org/ ** +** /____/\___/_/ |_/____/_/ | |__/ /____/ ** +** |/____/ ** +\* */ +package scala.scalajs.testsuite.javalib + +import java.util.Date + +import org.scalajs.jasminetest.JasmineTest + +/** + * tests the implementation of the java standard library Date + */ +object DateTest extends JasmineTest { + + describe("java.lang.Date") { + + it("should provide `compareTo`") { + def compare(x: Date, y: Date): Int = { + x.compareTo(y) + } + + expect(compare(new Date(97, 11, 5, 0, 0), new Date(98, 11, 5, 0, 0))).toBeLessThan(0) + expect(compare(new Date(98, 11, 5, 0, 0), new Date(97, 11, 5, 0, 0))).toBeGreaterThan(0) + expect(compare(new Date(97, 11, 5, 0, 0), new Date(97, 11, 5))).toEqual(0) + expect(compare(new Date(97, 11, 5, 0, 0), new Date(97, 11, 5, 0, 1))).toBeLessThan(0) + expect(compare(new Date(97, 11, 5), new Date(97, 11, 5, 0, 0))).toEqual(0) + } + + it("should be a Comparable") { + def compare(x: Any, y: Any): Int = + x.asInstanceOf[Comparable[Any]].compareTo(y) + + expect(compare(new Date(97, 11, 5, 0, 0), new Date(98, 11, 5, 0, 0))).toBeLessThan(0) + expect(compare(new Date(98, 11, 5, 0, 0), new Date(97, 11, 5, 0, 0))).toBeGreaterThan(0) + expect(compare(new Date(97, 11, 5, 0, 0), new Date(97, 11, 5))).toEqual(0) + expect(compare(new Date(97, 11, 5, 0, 0), new Date(97, 11, 5, 0, 1))).toBeLessThan(0) + expect(compare(new Date(97, 11, 5), new Date(97, 11, 5, 0, 0))).toEqual(0) + } + + it("should parse strings") { + def test(s: String, v: Date): Unit = + expect(new Date(s).compareTo(v)).toEqual(0) + + test("Nov 5 1997 5:23:27 GMT", new Date(Date.UTC(97, 10, 5, 5, 23, 27))) + test("Nov 1 1997 GMT", new Date(Date.UTC(97,10,1, 0, 0, 0))) + test("Jan 1 1970 18:11:01 GMT", new Date(Date.UTC(70,0,1,18,11,1))) + } + + it("should provide after") { + expect(new Date(97, 11, 5, 0, 0).after(new Date(98, 11, 5, 0, 0))).toBe(false) + expect(new Date(99, 11, 5, 0, 0).after(new Date(98, 11, 5, 0, 0))).toBe(true) + expect(new Date(99, 11, 5, 0, 0).after(new Date(99, 11, 5, 0, 0))).toBe(false) + } + + it("should provide before") { + expect(new Date(97, 11, 5, 0, 0).before(new Date(98, 11, 5, 0, 0))).toBe(true) + expect(new Date(99, 11, 5, 0, 0).before(new Date(98, 11, 5, 0, 0))).toBe(false) + expect(new Date(99, 11, 5, 0, 0).before(new Date(99, 11, 5, 0, 0))).toBe(false) + } + + it("should provide clone") { + def testClone(date: Date) = { + val cloned = date.clone() + date == cloned + } + + expect(testClone(new Date(97, 11, 5, 0, 0))).toBe(true) + expect(testClone(new Date(92, 14, 6, 2, 1))).toBe(true) + expect(testClone(new Date(4, 1, 2, 3, 0, 0))).toBe(true) + } + + it("should respond to getYear") { + def testYear(year: Int) = { + val date = new Date() + date.setYear(year) + expect(date.getYear).toEqual(year) + } + testYear(1940) + testYear(1920) + testYear(2030) + } + } +} diff --git a/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/DoubleTest.scala b/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/DoubleTest.scala new file mode 100644 index 0000000..59c6e10 --- /dev/null +++ b/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/DoubleTest.scala @@ -0,0 +1,217 @@ +/* __ *\ +** ________ ___ / / ___ __ ____ Scala.js Test Suite ** +** / __/ __// _ | / / / _ | __ / // __/ (c) 2013, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \ http://scala-js.org/ ** +** /____/\___/_/ |_/____/_/ | |__/ /____/ ** +** |/____/ ** +\* */ +package scala.scalajs.testsuite.javalib + +import org.scalajs.jasminetest.JasmineTest +import scala.scalajs.js + +import java.lang.{Double => JDouble} + +import scala.util.Try + +object DoubleTest extends JasmineTest { + + describe("java.lang.Double") { + + it("should provide proper `equals`") { + expect(Double.box(0.0) == Double.box(-0.0)).toBeTruthy + expect(Double.box(Double.NaN) == Double.box(Double.NaN)).toBeTruthy + } + + it("hashCode") { + def hashCodeNotInlined(x: Any): Int = { + var y = x // do not inline + y.hashCode + } + + def test(x: Double, expected: Int): Unit = { + expect(x.hashCode).toEqual(expected) + expect(hashCodeNotInlined(x)).toEqual(expected) + } + + test(0.0, 0) + test(-0.0, 0) + test(1234.0, 1234) + test(1.5, 1073217536) + test(Math.PI, 340593891) + test(-54.0, -54) + + test(Double.MinPositiveValue, 1) + test(Double.MinValue, 1048576) + test(Double.MaxValue, -2146435072) + + test(Double.NaN, 2146959360) + test(Double.PositiveInfinity, 2146435072) + test(Double.NegativeInfinity, -1048576) + } + + it("should provide `toString` with integer values when an integer") { + expect(0.0.toString).toEqual("0") + expect(-0.0.toString).toEqual("0") + expect(Double.NaN.toString).toEqual("NaN") + expect(Double.PositiveInfinity.toString).toEqual("Infinity") + expect(Double.NegativeInfinity.toString).toEqual("-Infinity") + expect(5.0.toString).toEqual("5") + expect(-5.0.toString).toEqual("-5") + expect(1.2.toString).toEqual("1.2") + } + + it("should parse strings") { + expect("0.0".toDouble).toEqual(0.0f) + expect("NaN".toDouble.isNaN).toBeTruthy + expect(Try("asdf".toDouble).isFailure).toBeTruthy + + def test(s: String, v: Double): Unit = { + expect(JDouble.parseDouble(s)).toBeCloseTo(v) + expect(JDouble.valueOf(s).doubleValue()).toBeCloseTo(v) + expect(new JDouble(s).doubleValue()).toBeCloseTo(v) + } + + test("0", 0.0) + test("5.3", 5.3) + test("127e2", 12700.0) + test("127E-2", 1.27) + test("1E+1", 10) + test("-123.4", -123.4) + test("65432.1", 65432.10) + test("-87654.321", -87654.321) + test("+.3f", 0.3) + } + + it("should reject invalid strings when parsing") { + def test(s: String): Unit = + expect(() => JDouble.parseDouble(s)).toThrow + + test("4.3.5") + test("4e3.5") + test("hello world") + test("--4") + test("4E-3.2") + } + + it("should provide `compareTo`") { + def compare(x: Double, y: Double): Int = + new JDouble(x).compareTo(new JDouble(y)) + + expect(compare(0.0, 5.5)).toBeLessThan(0) + expect(compare(10.5, 10.2)).toBeGreaterThan(0) + expect(compare(-2.1, -1.0)).toBeLessThan(0) + expect(compare(3.14, 3.14)).toEqual(0) + + // From compareTo's point of view, NaN is equal to NaN + expect(compare(Double.NaN, Double.NaN)).toEqual(0) + } + + it("should be a Comparable") { + def compare(x: Any, y: Any): Int = + x.asInstanceOf[Comparable[Any]].compareTo(y) + + expect(compare(0.0, 5.5)).toBeLessThan(0) + expect(compare(10.5, 10.2)).toBeGreaterThan(0) + expect(compare(-2.1, -1.0)).toBeLessThan(0) + expect(compare(3.14, 3.14)).toEqual(0) + + // From compareTo's point of view, NaN is equal to NaN + expect(compare(Double.NaN, Double.NaN)).toEqual(0) + } + + it("should provide isInfinite - #515") { + expect(Double.PositiveInfinity.isInfinite).toBeTruthy + expect(Double.NegativeInfinity.isInfinite).toBeTruthy + expect((1.0/0).isInfinite).toBeTruthy + expect((-1.0/0).isInfinite).toBeTruthy + expect((0.0).isInfinite).toBeFalsy + } + + it("isNaN") { + def f(v: Double): Boolean = { + var v2 = v // do not inline + v2.isNaN + } + + expect(f(Double.NaN)).toBeTruthy + + expect(f(Double.PositiveInfinity)).toBeFalsy + expect(f(Double.NegativeInfinity)).toBeFalsy + expect(f(1.0 / 0)).toBeFalsy + expect(f(-1.0 / 0)).toBeFalsy + expect(f(0.0)).toBeFalsy + expect(f(3.0)).toBeFalsy + expect(f(-1.5)).toBeFalsy + } + + it("longBitsToDouble") { + def isZero(v: Double, neg: Boolean): Boolean = { + (v == 0.0) && (1 / v == ( + if (neg) Double.NegativeInfinity + else Double.PositiveInfinity)) + } + + import JDouble.{longBitsToDouble => f} + + // Specials + expect(f(0x7ff0000000000000L)).toEqual(Double.PositiveInfinity) + expect(f(0xfff0000000000000L)).toEqual(Double.NegativeInfinity) + expect(isZero(f(0x0000000000000000L), false)).toBeTruthy + expect(isZero(f(0x8000000000000000L), true)).toBeTruthy + expect(f(0x7ff8000000000000L).isNaN).toBeTruthy // canonical NaN + + // Non-canonical NaNs + expect(f(0x7ff0000000000001L).isNaN).toBeTruthy // smallest positive NaN + expect(f(0x7ff15ab515d3cca1L).isNaN).toBeTruthy // an arbitrary positive NaN + expect(f(0x7fffffffffffffffL).isNaN).toBeTruthy // largest positive NaN + expect(f(0xfff0000000000001L).isNaN).toBeTruthy // smallest negative NaN + expect(f(0xfff15ab515d3cca1L).isNaN).toBeTruthy // an arbitrary negative NaN + expect(f(0xffffffffffffffffL).isNaN).toBeTruthy // largest negative NaN + + // Normal forms + expect(f(0x0010000000000000L)).toEqual(2.2250738585072014e-308) // smallest pos normal form + expect(f(0x7fefffffffffffffL)).toEqual(1.7976931348623157e308) // largest pos normal form + expect(f(0x4d124568bc6584caL)).toEqual(1.8790766677624813e63) // an arbitrary pos normal form + expect(f(0x8010000000000000L)).toEqual(-2.2250738585072014e-308) // smallest neg normal form + expect(f(0xffefffffffffffffL)).toEqual(-1.7976931348623157e308) // largest neg normal form + expect(f(0xcd124568bc6584caL)).toEqual(-1.8790766677624813e63) // an arbitrary neg normal form + + // Subnormal forms + expect(f(0x0000000000000001L)).toEqual(Double.MinPositiveValue) // smallest pos subnormal form + expect(f(0x000fffffffffffffL)).toEqual(2.225073858507201e-308) // largest pos subnormal form + expect(f(0x000c5d44ae45cb60L)).toEqual(1.719471609939382e-308) // an arbitrary pos subnormal form + expect(f(0x8000000000000001L)).toEqual(-Double.MinPositiveValue) // smallest neg subnormal form + expect(f(0x800fffffffffffffL)).toEqual(-2.225073858507201e-308) // largest neg subnormal form + expect(f(0x800c5d44ae45cb60L)).toEqual(-1.719471609939382e-308) // an arbitrary neg subnormal form + } + + it("doubleToLongBits") { + import JDouble.{doubleToLongBits => f} + + // Specials + expect(f(Double.PositiveInfinity) == 0x7ff0000000000000L).toBeTruthy + expect(f(Double.NegativeInfinity) == 0xfff0000000000000L) + expect(f(0.0) == 0x0000000000000000L).toBeTruthy + expect(f(-0.0) == 0x8000000000000000L).toBeTruthy + expect(f(Double.NaN) == 0x7ff8000000000000L).toBeTruthy // canonical NaN + + // Normal forms + expect(f(2.2250738585072014e-308) == 0x0010000000000000L).toBeTruthy // smallest pos normal form + expect(f(1.7976931348623157e308) == 0x7fefffffffffffffL).toBeTruthy // largest pos normal form + expect(f(1.8790766677624813e63) == 0x4d124568bc6584caL).toBeTruthy // an arbitrary pos normal form + expect(f(-2.2250738585072014e-308) == 0x8010000000000000L).toBeTruthy // smallest neg normal form + expect(f(-1.7976931348623157e308) == 0xffefffffffffffffL).toBeTruthy // largest neg normal form + expect(f(-1.8790766677624813e63) == 0xcd124568bc6584caL).toBeTruthy // an arbitrary neg normal form + + // Subnormal forms + expect(f(Double.MinPositiveValue) == 0x0000000000000001L).toBeTruthy // smallest pos subnormal form + expect(f(2.225073858507201e-308) == 0x000fffffffffffffL).toBeTruthy // largest pos subnormal form + expect(f(1.719471609939382e-308) == 0x000c5d44ae45cb60L).toBeTruthy // an arbitrary pos subnormal form + expect(f(-Double.MinPositiveValue) == 0x8000000000000001L).toBeTruthy // smallest neg subnormal form + expect(f(-2.225073858507201e-308) == 0x800fffffffffffffL).toBeTruthy // largest neg subnormal form + expect(f(-1.719471609939382e-308) == 0x800c5d44ae45cb60L).toBeTruthy // an arbitrary neg subnormal form + } + + } +} diff --git a/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/FloatTest.scala b/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/FloatTest.scala new file mode 100644 index 0000000..e45a34a --- /dev/null +++ b/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/FloatTest.scala @@ -0,0 +1,221 @@ +/* __ *\ +** ________ ___ / / ___ __ ____ Scala.js Test Suite ** +** / __/ __// _ | / / / _ | __ / // __/ (c) 2013, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \ http://scala-js.org/ ** +** /____/\___/_/ |_/____/_/ | |__/ /____/ ** +** |/____/ ** +\* */ +package scala.scalajs.testsuite.javalib + +import org.scalajs.jasminetest.JasmineTest +import scala.scalajs.js + +import java.lang.{Float => JFloat} + +import scala.util.Try + +object FloatTest extends JasmineTest { + + describe("java.lang.Float") { + + it("should provide proper `equals`") { + expect(Float.box(0.0f) == Float.box(-0.0f)).toBeTruthy + expect(Float.box(Float.NaN) == Float.box(Float.NaN)).toBeTruthy + } + + it("hashCode") { + def hashCodeNotInlined(x: Any): Int = { + var y = x // do not inline + y.hashCode + } + + def test(x: Float, expected: Int): Unit = { + expect(x.hashCode).toEqual(expected) + expect(hashCodeNotInlined(x)).toEqual(expected) + } + + test(0.0f, 0) + test(-0.0f, 0) + test(1234.0f, 1234) + test(1.5f, 1073217536) + test(-54f, -54) + + test(Float.MinPositiveValue, 916455424) + test(Float.MinValue, 670040063) + test(Float.MaxValue, -1477443585) + + test(Float.NaN, 2146959360) + test(Float.PositiveInfinity, 2146435072) + test(Float.NegativeInfinity, -1048576) + } + + it("should provide `toString` with integer values when an integer") { + expect(0.0f.toString).toEqual("0") + expect(-0.0f.toString).toEqual("0") + expect(Float.NaN.toString).toEqual("NaN") + expect(Float.PositiveInfinity.toString).toEqual("Infinity") + expect(Float.NegativeInfinity.toString).toEqual("-Infinity") + expect(5.0f.toString).toEqual("5") + expect(-5.0f.toString).toEqual("-5") + + // We need to explicitly cut the string here, since floats are + // represented by doubles (but the literal is emitted as + // float). Therefore there may be some imprecision. This is + // documented as semantic difference. + expect(1.2f.toString.substring(0,3)).toEqual("1.2") + } + + it("should parse strings") { + expect("0.0".toFloat).toEqual(0.0f) + expect("NaN".toFloat.isNaN).toBeTruthy + expect(Try("asdf".toFloat).isFailure).toBeTruthy + + def test(s: String, v: Float): Unit = { + expect(JFloat.parseFloat(s)).toBeCloseTo(v) + expect(JFloat.valueOf(s).floatValue()).toBeCloseTo(v) + expect(new JFloat(s).floatValue()).toBeCloseTo(v) + } + + test("0", 0.0f) + test("5.3", 5.3f) + test("127e2", 12700.0f) + test("127E-2", 1.27f) + test("1E+1", 10f) + test("-123.4", -123.4f) + test("65432.1", 65432.10f) + test("-87654.321", -87654.321f) + test("+.3f", 0.3f) + } + + it("should reject invalid strings when parsing") { + def test(s: String): Unit = + expect(() => JFloat.parseFloat(s)).toThrow + + test("4.3.5") + test("4e3.5") + test("hello world") + test("--4") + test("4E-3.2") + } + + it("should provide `compareTo`") { + def compare(x: Float, y: Float): Int = + new JFloat(x).compareTo(new JFloat(y)) + + expect(compare(0.0f, 5.5f)).toBeLessThan(0) + expect(compare(10.5f, 10.2f)).toBeGreaterThan(0) + expect(compare(-2.1f, -1.0f)).toBeLessThan(0) + expect(compare(3.14f, 3.14f)).toEqual(0) + + // From compareTo's point of view, NaN is equal to NaN + expect(compare(Float.NaN, Float.NaN)).toEqual(0) + } + + it("should be a Comparable") { + def compare(x: Any, y: Any): Int = + x.asInstanceOf[Comparable[Any]].compareTo(y) + + expect(compare(0.0f, 5.5f)).toBeLessThan(0) + expect(compare(10.5f, 10.2f)).toBeGreaterThan(0) + expect(compare(-2.1f, -1.0f)).toBeLessThan(0) + expect(compare(3.14f, 3.14f)).toEqual(0) + + // From compareTo's point of view, NaN is equal to NaN + expect(compare(Float.NaN, Float.NaN)).toEqual(0) + } + + it("should provide isInfinite - #515") { + expect(Float.PositiveInfinity.isInfinite).toBeTruthy + expect(Float.NegativeInfinity.isInfinite).toBeTruthy + expect((1f/0).isInfinite).toBeTruthy + expect((-1f/0).isInfinite).toBeTruthy + expect(0f.isInfinite).toBeFalsy + } + + it("isNaN") { + def f(v: Float): Boolean = { + var v2 = v // do not inline + v2.isNaN + } + + expect(f(Float.NaN)).toBeTruthy + + expect(f(Float.PositiveInfinity)).toBeFalsy + expect(f(Float.NegativeInfinity)).toBeFalsy + expect(f(1f / 0)).toBeFalsy + expect(f(-1f / 0)).toBeFalsy + expect(f(0f)).toBeFalsy + expect(f(3f)).toBeFalsy + expect(f(-1.5f)).toBeFalsy + } + + it("intBitsToFloat") { + def isZero(v: Float, neg: Boolean): Boolean = { + (v == 0.0f) && (1 / v == ( + if (neg) Float.NegativeInfinity + else Float.PositiveInfinity)) + } + + import JFloat.{intBitsToFloat => f} + + // Specials + expect(f(0x7f800000)).toEqual(Float.PositiveInfinity) + expect(f(0xff800000)).toEqual(Float.NegativeInfinity) + expect(isZero(f(0x00000000), false)).toBeTruthy + expect(isZero(f(0x80000000), true)).toBeTruthy + expect(f(0x7fc00000).isNaN).toBeTruthy // canonical NaN + + // Non-canonical NaNs + expect(f(0x7f800001).isNaN).toBeTruthy // smallest positive NaN + expect(f(0x7f915ab5).isNaN).toBeTruthy // an arbitrary positive NaN + expect(f(0x7fffffff).isNaN).toBeTruthy // largest positive NaN + expect(f(0xff800001).isNaN).toBeTruthy // smallest negative NaN + expect(f(0xff915ab5).isNaN).toBeTruthy // an arbitrary negative NaN + expect(f(0xffffffff).isNaN).toBeTruthy // largest negative NaN + + // Normal forms + expect(f(0x00800000)).toEqual(1.17549435e-38f) // smallest pos normal form + expect(f(0x7f7fffff)).toEqual(3.4028234e38f) // largest pos normal form + expect(f(0x4d124568)).toEqual(1.53376384e8f) // an arbitrary pos normal form + expect(f(0x80800000)).toEqual(-1.17549435e-38f) // smallest neg normal form + expect(f(0xff7fffff)).toEqual(-3.4028234e38f) // largest neg normal form + expect(f(0xcd124568)).toEqual(-1.53376384e8f) // an arbitrary neg normal form + + // Subnormal forms + expect(f(0x00000001)).toEqual(Float.MinPositiveValue) // smallest pos subnormal form + expect(f(0x007fffff)).toEqual(1.1754942e-38f) // largest pos subnormal form + expect(f(0x007c5d44)).toEqual(1.1421059e-38f) // an arbitrary pos subnormal form + expect(f(0x80000001)).toEqual(-Float.MinPositiveValue) // smallest neg subnormal form + expect(f(0x807fffff)).toEqual(-1.1754942e-38f) // largest neg subnormal form + expect(f(0x807c5d44)).toEqual(-1.1421059e-38f) // an arbitrary neg subnormal form + } + + it("floatToIntBits") { + import JFloat.{floatToIntBits => f} + + // Specials + expect(f(Float.PositiveInfinity)).toEqual(0x7f800000) + expect(f(Float.NegativeInfinity)).toEqual(0xff800000) + expect(f(0.0f)).toEqual(0x00000000) + expect(f(-0.0f)).toEqual(0x80000000) + expect(f(Float.NaN)).toEqual(0x7fc00000) // canonical NaN + + // Normal forms + expect(f(1.17549435e-38f)).toEqual(0x00800000) // smallest pos normal form + expect(f(3.4028234e38f)).toEqual(0x7f7fffff) // largest pos normal form + expect(f(1.53376384e8f)).toEqual(0x4d124568) // an arbitrary pos normal form + expect(f(-1.17549435e-38f)).toEqual(0x80800000) // smallest neg normal form + expect(f(-3.4028234e38f)).toEqual(0xff7fffff) // largest neg normal form + expect(f(-1.53376384e8f)).toEqual(0xcd124568) // an arbitrary neg normal form + + // Subnormal forms + expect(f(Float.MinPositiveValue)).toEqual(0x00000001) // smallest pos subnormal form + expect(f(1.1754942e-38f)).toEqual(0x007fffff) // largest pos subnormal form + expect(f(1.1421059e-38f)).toEqual(0x007c5d44) // an arbitrary pos subnormal form + expect(f(-Float.MinPositiveValue)).toEqual(0x80000001) // smallest neg subnormal form + expect(f(-1.1754942e-38f)).toEqual(0x807fffff) // largest neg subnormal form + expect(f(-1.1421059e-38f)).toEqual(0x807c5d44) // an arbitrary neg subnormal form + } + + } +} diff --git a/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/FormatterTest.scala b/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/FormatterTest.scala new file mode 100644 index 0000000..e7a705c --- /dev/null +++ b/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/FormatterTest.scala @@ -0,0 +1,241 @@ +/* __ *\ +** ________ ___ / / ___ __ ____ Scala.js Test Suite ** +** / __/ __// _ | / / / _ | __ / // __/ (c) 2013, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \ http://scala-js.org/ ** +** /____/\___/_/ |_/____/_/ | |__/ /____/ ** +** |/____/ ** +\* */ +package scala.scalajs.testsuite.javalib + +import scala.scalajs.js +import org.scalajs.jasminetest.JasmineTest + +import java.util.{ Formatter, Formattable, FormattableFlags } + +import java.lang.{ + Double => JDouble, + Float => JFloat, + Integer => JInteger, + Long => JLong, + Byte => JByte, + Short => JShort, + Boolean => JBoolean, + String => JString +} + + +object FormatterTest extends JasmineTest { + + class HelperClass + class FormattableClass extends Formattable { + var frm: Formatter = _ + var flags: Int = _ + var width: Int = _ + var precision: Int = _ + var calls = 0 + def formatTo(frm: Formatter, flags: Int, width: Int, precision: Int) = { + this.calls += 1 + this.flags = flags + this.width = width + this.precision = precision + frm.out().append("foobar") + } + + def expectCalled(times: Int, flags: Int, width: Int, precision: Int) = { + expect(this.calls).toEqual(times) + expect(this.flags).toEqual(flags) + expect(this.width).toEqual(width) + expect(this.precision).toEqual(precision) + } + + } + + def expectF(format: String, args: AnyRef*) = { + val fmt = new Formatter() + val res = fmt.format(format, args:_*).toString() + fmt.close() + expect(res) + } + + def expectFC(format: String, flags: Int, width: Int, precision: Int) = { + val fc = new FormattableClass + val exp = expectF(format, fc) + fc.expectCalled(1, flags, width, precision) + exp + } + + def expectThrow(format: String, args: AnyRef*) = { + val fmt = new Formatter() + expect(() => fmt.format(format, args:_*)).toThrow + } + + describe("java.util.Formatter") { + + // Explicitly define these as `var`'s to avoid any compile-time constant folding + var IntMax: Int = Int.MaxValue + var IntMin: Int = Int.MinValue + var ByteMax: Byte = Byte.MaxValue + var ByteMin: Byte = Byte.MinValue + var ShortMax: Short = Short.MaxValue + var ShortMin: Short = Short.MinValue + + it("should provide 'b' conversion") { + expectF("%b", null).toEqual("false") + expectF("%b", true: JBoolean).toEqual(JString.valueOf(true)) + expectF("%b", false: JBoolean).toEqual(JString.valueOf(false)) + expectF("%b", new HelperClass).toEqual("true") + } + + it("should provide 'h' conversion") { + val x = new HelperClass + expectF("%h", x).toEqual(Integer.toHexString(x.hashCode())) + expectF("%H", x).toEqual(Integer.toHexString(x.hashCode()).toUpperCase()) + expectF("%h", null).toEqual("null") + } + + it("should provide 's' conversion") { + expectFC("%s", 0, -1, -1).toEqual("foobar") + expectFC("%-s", FormattableFlags.LEFT_JUSTIFY, -1, -1).toEqual("foobar") + expectFC("%-10s", FormattableFlags.LEFT_JUSTIFY, 10, -1).toEqual("foobar") + expectFC("%#-10.2s", FormattableFlags.LEFT_JUSTIFY | + FormattableFlags.ALTERNATE, 10, 2).toEqual("foobar") + expectFC("%#10.2S", FormattableFlags.UPPERCASE | + FormattableFlags.ALTERNATE, 10, 2).toEqual("foobar") + expectF("%10s", "hello").toEqual(" hello") + expectF("%-10s", "hello").toEqual("hello ") + expectThrow("%#s", "hello") + } + + it("should provide 'c' conversion") { + expectF("%-5c", new Character('!')).toEqual("! ") + } + + it("should provide 'd' conversion") { + expectF("%d", new Integer(5)).toEqual("5") + expectF("%05d", new Integer(5)).toEqual("00005") + expectF("%5d", new Integer(-10)).toEqual(" -10") + expectF("%05d", new Integer(-10)).toEqual("-0010") + } + + it("should provide 'o' conversion") { + expectF("%o", new JInteger(8)).toEqual("10") + expectF("%05o", new JInteger(16)).toEqual("00020") + expectF("%5o", new JInteger(-10)).toEqual("37777777766") + expectF("%05o", new JInteger(-10)).toEqual("37777777766") + expectF("%o", new JByte(8.toByte)).toEqual("10") + expectF("%05o", new JByte(16.toByte)).toEqual("00020") + expectF("%14o", new JByte(-10.toByte)).toEqual(" 37777777766") + expectF("%05o", new JByte(-10.toByte)).toEqual("37777777766") + expectF("%o", new JShort(8.toShort)).toEqual("10") + expectF("%05o", new JShort(16.toShort)).toEqual("00020") + expectF("%5o", new JShort(-10.toShort)).toEqual("37777777766") + expectF("%015o",new JShort(-10.toShort)).toEqual("000037777777766") + expectF("%05o", new JLong(-5L)).toEqual("1777777777777777777773") + } + + it("should provide 'x' conversion") { + expectF("%0#5x", new JInteger(5)).toEqual("0x005") + expectF("%#5x", new JInteger(5)).toEqual(" 0x5") + expectF("%#5X", new JInteger(5)).toEqual(" 0X5") + expectF("%x", new JInteger(-3)).toEqual("fffffffd") + expectF("%x", new JByte(-4.toByte)).toEqual("fffffffc") + expectF("%0#5x", new JByte(5.toByte)).toEqual("0x005") + expectF("%#5x", new JByte(5.toByte)).toEqual(" 0x5") + expectF("%#5X", new JByte(5.toByte)).toEqual(" 0X5") + expectF("%x", new JByte(-3.toByte)).toEqual("fffffffd") + expectF("%0#5x", new JShort(5.toShort)).toEqual("0x005") + expectF("%#5x", new JShort(5.toShort)).toEqual(" 0x5") + expectF("%#5X", new JShort(5.toShort)).toEqual(" 0X5") + expectF("%x", new JShort(-3.toShort)).toEqual("fffffffd") + expectF("%x", new JLong(-5L)).toEqual("fffffffffffffffb") + expectF("%X", new JLong(26L)).toEqual("1A") + } + + it("should provide 'e' conversion") { + expectF("%e", new JDouble(1000)).toEqual("1.000000e+03") + expectF("%.0e", new JDouble(1.2e100)).toEqual("1e+100") + // We use 1.51e100 in this test, since we seem to have a floating + // imprecision at exactly 1.5e100 that yields to a rounding error + // towards (1e+100 instead of 2e+100) + expectF("%.0e", new JDouble(1.51e100)).toEqual("2e+100") + expectF("%10.2e", new JDouble(1.2e100)).toEqual(" 1.20e+100") + expectF("%012.4e", new JFloat(1.2e-21f)).toEqual("001.2000e-21") + expectF("%012.4E", new JFloat(1.2e-21f)).toEqual("001.2000E-21") + expectF("%(015.4e", new JFloat(-1.2e-21f)).toEqual("(0001.2000e-21)") + + // Tests with infinity and NaN + expectF("%e", new JDouble(Double.PositiveInfinity)).toEqual("Infinity") + expectF("%e", new JDouble(Double.NegativeInfinity)).toEqual("-Infinity") + expectF("%010e", new JDouble(Double.PositiveInfinity)).toEqual(" Infinity") + expectF("%-10e", new JDouble(Double.PositiveInfinity)).toEqual("Infinity ") + expectF("%(e", new JDouble(Double.NegativeInfinity)).toEqual("(Infinity)") + expectF("%010e", new JDouble(Double.NaN)).toEqual(" NaN") + } + + it("should provide 'g' conversion") { + expectF("%g", new JDouble(.5e-4)).toEqual("5.00000e-05") + expectF("%g", new JDouble(3e-4)).toEqual("0.000300000") + expectF("%.3g", new JDouble(3e-4)).toEqual("0.000300") + expectF("%.2g", new JDouble(1e-3)).toEqual("0.0010") + expectF("%g", new JDouble(3e5)).toEqual("300000") + expectF("%.3g", new JDouble(3e5)).toEqual("3.00e+05") + expectF("%04g", new JDouble(Double.NaN)).toEqual(" NaN") + } + + it("should provide 'f' conversion") { + expectF("%f", new JDouble(3.3)).toEqual("3.300000") + expectF("%0(9.4f", new JDouble(-4.6)).toEqual("(04.6000)") + expectF("%f", new JFloat(3e10f)).toEqual("30000001024.000000") + expectF("%f", new JDouble(3e10)).toEqual("30000000000.000000") + expectF("%04f", new JDouble(Double.NaN)).toEqual(" NaN") + } + + it("should support '%%'") { + expectF("%d%%%d", new JInteger(1), new JInteger(2)).toEqual("1%2") + } + + it("should support '%n'") { + expectF("%d%n%d", new JInteger(1), new JInteger(2)).toEqual("1\n2") + } + + it("should survive `null` and `undefined`") { + expectF("%s", null).toEqual("null") + expectF("%s", js.undefined).toEqual("undefined") + } + + it("should allow 'f' string interpolation to survive `null` and `undefined`") { + expect(f"${null}%s").toEqual("null") + expect(f"${js.undefined}%s").toEqual("undefined") + } + + it("should allow positional arguments") { + expectF("%2$d %1$d", new JInteger(1), new JInteger(2)).toEqual("2 1") + expectF("%2$d %2$d %d", new JInteger(1), new JInteger(2)).toEqual("2 2 1") + expectF("%2$d %<d %d", new JInteger(1), new JInteger(2)).toEqual("2 2 1") + } + + it("should fail when called after close") { + val f = new Formatter() + f.close() + expect(() => f.toString()).toThrow + } + + it("should fail with bad format specifier") { + expectThrow("hello world%") + expectThrow("%%%") + expectThrow("%q") + expectThrow("%1") + expectThrow("%_f") + } + + it("should fail with not enough arguments") { + expectThrow("%f") + expectThrow("%d%d%d", new JInteger(1), new JInteger(1)) + expectThrow("%10$d", new JInteger(1)) + } + + } + + +} + diff --git a/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/IntegerTest.scala b/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/IntegerTest.scala new file mode 100644 index 0000000..5a01de4 --- /dev/null +++ b/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/IntegerTest.scala @@ -0,0 +1,161 @@ +/* __ *\ +** ________ ___ / / ___ __ ____ Scala.js Test Suite ** +** / __/ __// _ | / / / _ | __ / // __/ (c) 2013, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \ http://scala-js.org/ ** +** /____/\___/_/ |_/____/_/ | |__/ /____/ ** +** |/____/ ** +\* */ +package scala.scalajs.testsuite.javalib + +import org.scalajs.jasminetest.JasmineTest +import scala.scalajs.js + +object IntegerTest extends JasmineTest { + + describe("java.lang.Integer") { + + // Explicitly define these as `var`'s to avoid any compile-time constant folding + var MaxValue: Int = Int.MaxValue + var MinValue: Int = Int.MinValue + + it("should provide `reverseBytes` used by scala.Enumeration") { + expect(Integer.reverseBytes(0xdeadbeef)).toEqual(0xefbeadde) + } + + it("should provide `rotateLeft`") { + expect(Integer.rotateLeft(0x689cd401, 0)).toEqual(0x689cd401) + expect(Integer.rotateLeft(0x689cd401, 1)).toEqual(0xd139a802) + expect(Integer.rotateLeft(0x689cd401, 8)).toEqual(0x9cd40168) + expect(Integer.rotateLeft(0x689cd401, 13)).toEqual(0x9a802d13) + expect(Integer.rotateLeft(0x689cd401, 32)).toEqual(0x689cd401) + expect(Integer.rotateLeft(0x689cd401, 33)).toEqual(0xd139a802) + expect(Integer.rotateLeft(0x689cd401, 43)).toEqual(0xe6a00b44) + expect(Integer.rotateLeft(0x689cd401, -1)).toEqual(0xb44e6a00) + expect(Integer.rotateLeft(0x689cd401, -28)).toEqual(0x89cd4016) + expect(Integer.rotateLeft(0x689cd401, -39)).toEqual(0x2d139a8) + } + + it("should provide `rotateRight`") { + expect(Integer.rotateRight(0x689cd401, 0)).toEqual(0x689cd401) + expect(Integer.rotateRight(0x689cd401, 1)).toEqual(0xb44e6a00) + expect(Integer.rotateRight(0x689cd401, 8)).toEqual(0x1689cd4) + expect(Integer.rotateRight(0x689cd401, 13)).toEqual(0xa00b44e6) + expect(Integer.rotateRight(0x689cd401, 32)).toEqual(0x689cd401) + expect(Integer.rotateRight(0x689cd401, 33)).toEqual(0xb44e6a00) + expect(Integer.rotateRight(0x689cd401, 43)).toEqual(0x802d139a) + expect(Integer.rotateRight(0x689cd401, -1)).toEqual(0xd139a802) + expect(Integer.rotateRight(0x689cd401, -28)).toEqual(0x1689cd40) + expect(Integer.rotateRight(0x689cd401, -39)).toEqual(0x4e6a00b4) + } + + it("should provide `bitCount` used by Map") { + abstract sealed class Status + case object Used extends Status + case object Current extends Status + case object OneMove extends Status + case object MultipleMoves extends Status + case object Other extends Status + + val map = Map(Used -> 0, Other -> 0, Current -> 0, MultipleMoves -> 1, OneMove -> 2) + + expect(map.size).toEqual(5) + expect(map(MultipleMoves)).toEqual(1) + } + + it("should provide `numberOfTrailingZeros`") { + expect(Integer.numberOfTrailingZeros(0xa3c49000)).toEqual(12) + expect(Integer.numberOfTrailingZeros(0x43f49020)).toEqual(5) + expect(Integer.numberOfTrailingZeros(0x43c08000)).toEqual(15) + expect(Integer.numberOfTrailingZeros(0)).toEqual(32) + } + + it("should provide `toBinaryString` for values in range") { + expect(Integer.toBinaryString(-1)).toEqual("11111111111111111111111111111111") + expect(Integer.toBinaryString(-10001)).toEqual("11111111111111111101100011101111") + expect(Integer.toBinaryString(MinValue)).toEqual("10000000000000000000000000000000") + expect(Integer.toBinaryString(MaxValue)).toEqual("1111111111111111111111111111111") + } + + it("should provide `toHexString` for values in range") { + expect(Integer.toHexString(-1)).toEqual("ffffffff") + expect(Integer.toHexString(-10001)).toEqual("ffffd8ef") + expect(Integer.toHexString(MinValue)).toEqual("80000000") + expect(Integer.toHexString(-2147000002)).toEqual("8007613e") + expect(Integer.toHexString(MaxValue)).toEqual("7fffffff") + } + + it("should provide `toOctalString` for values in range") { + expect(Integer.toOctalString(-1)).toEqual("37777777777") + expect(Integer.toOctalString(-10001)).toEqual("37777754357") + expect(Integer.toOctalString(MinValue)).toEqual("20000000000") + expect(Integer.toOctalString(MaxValue)).toEqual("17777777777") + } + + it("should provide `compareTo`") { + def compare(x: Int, y: Int): Int = + new Integer(x).compareTo(new Integer(y)) + + expect(compare(0, 5)).toBeLessThan(0) + expect(compare(10, 9)).toBeGreaterThan(0) + expect(compare(-2, -1)).toBeLessThan(0) + expect(compare(3, 3)).toEqual(0) + } + + it("should be a Comparable") { + def compare(x: Any, y: Any): Int = + x.asInstanceOf[Comparable[Any]].compareTo(y) + + expect(compare(0, 5)).toBeLessThan(0) + expect(compare(10, 9)).toBeGreaterThan(0) + expect(compare(-2, -1)).toBeLessThan(0) + expect(compare(3, 3)).toEqual(0) + } + + it("should parse strings") { + def test(s: String, v: Int, radix: Int = 10): Unit = { + expect(Integer.parseInt(s, radix)).toEqual(v) + expect(Integer.valueOf(s, radix).intValue()).toEqual(v) + if (radix == 10) + expect(new Integer(s).intValue()).toEqual(v) + } + + test("0", 0) + test("5", 5) + test("127", 127) + test("-100", -100) + test("30000", 30000) + test("-90000", -90000) + test("Kona", 411787, 27) + test("+42", 42) + test("-0", 0) + test("-FF", -255, 16) + } + + it("should reject invalid strings when parsing") { + def test(s: String, radix: Int = 10): Unit = + expect(() => Integer.parseInt(s, radix)).toThrow + + test("abc") + test("5a") + test("2147483648") + test("99", 8) + test("-") + test("") + } + + it("should parse strings in base 16") { + def test(s: String, v: Int): Unit = { + expect(Integer.parseInt(s, 16)).toEqual(v) + expect(Integer.valueOf(s, 16).intValue()).toEqual(v) + } + + test("0", 0x0) + test("5", 0x5) + test("ff", 0xff) + test("-24", -0x24) + test("30000", 0x30000) + test("-90000", -0x90000) + } + + } +} diff --git a/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/LongTest.scala b/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/LongTest.scala new file mode 100644 index 0000000..86783c3 --- /dev/null +++ b/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/LongTest.scala @@ -0,0 +1,178 @@ +/* __ *\ +** ________ ___ / / ___ __ ____ Scala.js Test Suite ** +** / __/ __// _ | / / / _ | __ / // __/ (c) 2013, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \ http://scala-js.org/ ** +** /____/\___/_/ |_/____/_/ | |__/ /____/ ** +** |/____/ ** +\* */ +package scala.scalajs.testsuite.javalib + +import java.lang.{Long => JLong} + +import org.scalajs.jasminetest.JasmineTest + +/** + * tests the implementation of the java standard library Long + * requires jsinterop/LongTest to work to make sense + */ +object LongTest extends JasmineTest { + + describe("java.lang.Long") { + it("should provide `reverseBytes`") { + expect(JLong.reverseBytes(0xf5ab689cd401ff14L) == 0x14ff01d49c68abf5L).toBeTruthy + } + + it("should provide `rotateLeft`") { + expect(JLong.rotateLeft(0xf5ab689cd401ff14L, 0) == 0xf5ab689cd401ff14L).toBeTruthy + expect(JLong.rotateLeft(0xf5ab689cd401ff14L, 1) == 0xeb56d139a803fe29L).toBeTruthy + expect(JLong.rotateLeft(0xf5ab689cd401ff14L, 8) == 0xab689cd401ff14f5L).toBeTruthy + expect(JLong.rotateLeft(0xf5ab689cd401ff14L, 13) == 0x6d139a803fe29eb5L).toBeTruthy + expect(JLong.rotateLeft(0xf5ab689cd401ff14L, 64) == 0xf5ab689cd401ff14L).toBeTruthy + expect(JLong.rotateLeft(0xf5ab689cd401ff14L, 65) == 0xeb56d139a803fe29L).toBeTruthy + expect(JLong.rotateLeft(0xf5ab689cd401ff14L, 80) == 0x689cd401ff14f5abL).toBeTruthy + expect(JLong.rotateLeft(0xf5ab689cd401ff14L, -1) == 0x7ad5b44e6a00ff8aL).toBeTruthy + expect(JLong.rotateLeft(0xf5ab689cd401ff14L, -56) == 0xab689cd401ff14f5L).toBeTruthy + expect(JLong.rotateLeft(0xf5ab689cd401ff14L, -70) == 0x53d6ada2735007fcL).toBeTruthy + } + + it("should provide `rotateRight`") { + expect(JLong.rotateRight(0xf5ab689cd401ff14L, 0) == 0xf5ab689cd401ff14L).toBeTruthy + expect(JLong.rotateRight(0xf5ab689cd401ff14L, 1) == 0x7ad5b44e6a00ff8aL).toBeTruthy + expect(JLong.rotateRight(0xf5ab689cd401ff14L, 8) == 0x14f5ab689cd401ffL).toBeTruthy + expect(JLong.rotateRight(0xf5ab689cd401ff14L, 13) == 0xf8a7ad5b44e6a00fL).toBeTruthy + expect(JLong.rotateRight(0xf5ab689cd401ff14L, 64) == 0xf5ab689cd401ff14L).toBeTruthy + expect(JLong.rotateRight(0xf5ab689cd401ff14L, 65) == 0x7ad5b44e6a00ff8aL).toBeTruthy + expect(JLong.rotateRight(0xf5ab689cd401ff14L, 80) == 0xff14f5ab689cd401L).toBeTruthy + expect(JLong.rotateRight(0xf5ab689cd401ff14L, -1) == 0xeb56d139a803fe29L).toBeTruthy + expect(JLong.rotateRight(0xf5ab689cd401ff14L, -56) == 0x14f5ab689cd401ffL).toBeTruthy + expect(JLong.rotateRight(0xf5ab689cd401ff14L, -70) == 0x6ada2735007fc53dL).toBeTruthy + } + + it("should implement bitCount") { + expect(JLong.bitCount(0L)).toEqual(0) + expect(JLong.bitCount(35763829229342837L)).toEqual(26) + expect(JLong.bitCount(-350003829229342837L)).toEqual(32) + } + + it("should provide `compareTo`") { + def compare(x: Long, y: Long): Int = + new JLong(x).compareTo(new JLong(y)) + + expect(compare(0L, 5L)).toBeLessThan(0) + expect(compare(10L, 9L)).toBeGreaterThan(0) + expect(compare(-2L, -1L)).toBeLessThan(0) + expect(compare(3L, 3L)).toEqual(0) + } + + it("should be a Comparable") { + def compare(x: Any, y: Any): Int = + x.asInstanceOf[Comparable[Any]].compareTo(y) + + expect(compare(0L, 5L)).toBeLessThan(0) + expect(compare(10L, 9L)).toBeGreaterThan(0) + expect(compare(-2L, -1L)).toBeLessThan(0) + expect(compare(3L, 3L)).toEqual(0) + } + + it("should parse strings") { + def test(s: String, v: Long): Unit = { + expect(JLong.parseLong(s)).toEqual(v) + expect(JLong.valueOf(s).longValue()).toEqual(v) + expect(new JLong(s).longValue()).toEqual(v) + } + + test("0", 0L) + test("5", 5L) + test("127", 127L) + test("-100", -100L) + test("30000", 30000L) + test("-90000", -90000L) + test("4", 4L) + test("-4", -4L) + test("4000000000", 4000000000L) + test("-18014398509482040", -18014398509482040L) + } + + it("should reject invalid strings when parsing") { + def test(s: String): Unit = + expect(() => JLong.parseLong(s)).toThrow + + test("abc") + test("asdf") + test("") + } + + it("should parse strings in base 16") { + def test(s: String, v: Long): Unit = { + expect(JLong.parseLong(s, 16)).toEqual(v) + expect(JLong.valueOf(s, 16).longValue()).toEqual(v) + } + + test("0", 0x0L) + test("5", 0x5L) + test("ff", 0xffL) + test("-24", -0x24L) + test("30000", 0x30000L) + test("-90000", -0x90000L) + } + + it("should implement toString") { + expect(Int.MaxValue.toLong.toString).toEqual("2147483647") + expect((-50L).toString).toEqual("-50") + expect((-1000000000L).toString).toEqual("-1000000000") + expect((Int.MaxValue.toLong+1L).toString).toEqual("2147483648") + expect(Int.MinValue.toLong.toString).toEqual("-2147483648") + } + + it("should implement toBinaryString") { + expect(JLong.toBinaryString( 0L)).toEqual("0") + expect(JLong.toBinaryString( -1L)).toEqual("1111111111111111111111111111111111111111111111111111111111111111") + expect(JLong.toBinaryString( 456324454L)).toEqual("11011001100101111010101100110") + expect(JLong.toBinaryString( -456324454L)).toEqual("1111111111111111111111111111111111100100110011010000101010011010") + expect(JLong.toBinaryString( 98765432158845L)).toEqual("10110011101001110011110011111111111101001111101") + expect(JLong.toBinaryString(-49575304457780L)).toEqual("1111111111111111110100101110100101011001100101101001000111001100") + expect(JLong.toBinaryString(Long.MinValue )).toEqual("1000000000000000000000000000000000000000000000000000000000000000") + expect(JLong.toBinaryString(Long.MaxValue )).toEqual("111111111111111111111111111111111111111111111111111111111111111") + } + + it("should implement toHexString") { + expect(JLong.toHexString( 0L)).toEqual("0") + expect(JLong.toHexString( -1L)).toEqual("ffffffffffffffff") + expect(JLong.toHexString( 456324454L)).toEqual("1b32f566") + expect(JLong.toHexString( -456324454L)).toEqual("ffffffffe4cd0a9a") + expect(JLong.toHexString( 98765432158845L)).toEqual("59d39e7ffa7d") + expect(JLong.toHexString(-49575304457780L)).toEqual("ffffd2e9599691cc") + expect(JLong.toHexString(Long.MinValue )).toEqual("8000000000000000") + expect(JLong.toHexString(Long.MaxValue )).toEqual("7fffffffffffffff") + } + + it("should implement toOctalString") { + expect(JLong.toOctalString( 0L)).toEqual("0") + expect(JLong.toOctalString( -1L)).toEqual("1777777777777777777777") + expect(JLong.toOctalString( 456324454L)).toEqual("3314572546") + expect(JLong.toOctalString( -456324454L)).toEqual("1777777777774463205232") + expect(JLong.toOctalString( 98765432158845L)).toEqual("2635163637775175") + expect(JLong.toOctalString(-49575304457780L)).toEqual("1777776456453145510714") + expect(JLong.toOctalString(Long.MinValue )).toEqual("1000000000000000000000") + expect(JLong.toOctalString(Long.MaxValue )).toEqual("777777777777777777777") + } + + it("should correctly compute trailing zeros") { + expect(JLong.numberOfTrailingZeros(0xff10000000000000L)).toEqual(52) + expect(JLong.numberOfTrailingZeros(0xff20000000000000L)).toEqual(53) + expect(JLong.numberOfTrailingZeros(0xff40000000000000L)).toEqual(54) + expect(JLong.numberOfTrailingZeros(0xff80000000000000L)).toEqual(55) + + expect(JLong.numberOfTrailingZeros(0x0000010000000000L)).toEqual(40) + expect(JLong.numberOfTrailingZeros(0x0000020000000000L)).toEqual(41) + expect(JLong.numberOfTrailingZeros(0x0000040000000000L)).toEqual(42) + expect(JLong.numberOfTrailingZeros(0x0000080000000000L)).toEqual(43) + + expect(JLong.numberOfTrailingZeros(0x0000000000010000L)).toEqual(16) + expect(JLong.numberOfTrailingZeros(0x0000000000020000L)).toEqual(17) + expect(JLong.numberOfTrailingZeros(0x0000000000040000L)).toEqual(18) + expect(JLong.numberOfTrailingZeros(0x0000000000080000L)).toEqual(19) + } + + } +} diff --git a/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/MathTest.scala b/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/MathTest.scala new file mode 100644 index 0000000..a3c887a --- /dev/null +++ b/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/MathTest.scala @@ -0,0 +1,142 @@ +/* __ *\ +** ________ ___ / / ___ __ ____ Scala.js Test Suite ** +** / __/ __// _ | / / / _ | __ / // __/ (c) 2013, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \ http://scala-js.org/ ** +** /____/\___/_/ |_/____/_/ | |__/ /____/ ** +** |/____/ ** +\* */ +package scala.scalajs.testsuite.javalib + +import scala.scalajs.js +import org.scalajs.jasminetest.JasmineTest +import java.lang.Math + +object MathTest extends JasmineTest { + + describe("java.lang.Math") { + + it("should respond to `cbrt`") { + expect(1 / Math.cbrt(-0.0) < 0).toBeTruthy + expect(Math.cbrt(27.0)).toEqual(3.0) + expect(Math.cbrt(1000000.0)).toEqual(100.0) + expect(Math.cbrt(1000000000.0)).toEqual(1000.0) + expect(Math.cbrt(-1.0E24)).toEqual(-100000000.0) + expect(Math.cbrt(-65890311319.0E24)).toEqual(-4039.0E8) + } + + it("should respond to `log1p`") { + expect(Math.log1p(-2.0).isNaN).toBeTruthy + expect(Math.log1p(js.Number.NaN.toDouble).isNaN).toBeTruthy + expect(Math.log1p(0.0)).toEqual(0.0) + } + + it("should respond to `log10`") { + expect(Math.log10(-230.0).isNaN).toBeTruthy + expect(Math.log10(js.Number.NaN.toDouble).isNaN).toBeTruthy + } + + it("should respond to `signum` for Double") { + expect(Math.signum(234394.2198273)).toEqual(1.0) + expect(Math.signum(-124937498.58)).toEqual(-1.0) + + expect(Math.signum(+0.0)).toEqual(0.0) + expect(1 / Math.signum(+0.0) > 0).toBeTruthy + + expect(Math.signum(-0.0)).toEqual(-0.0) + expect(1 / Math.signum(-0.0) < 0).toBeTruthy + + expect(Math.signum(js.Number.NaN.toDouble).isNaN).toBeTruthy + } + + it("should respond to `signum` for Float") { + expect(Math.signum(234394.2198273f)).toEqual(1.0f) + expect(Math.signum(-124937498.58f)).toEqual(-1.0f) + + expect(Math.signum(+0.0f)).toEqual(0.0f) + expect(1 / Math.signum(+0.0f) > 0).toBeTruthy + + expect(Math.signum(-0.0f)).toEqual(-0.0f) + expect(1 / Math.signum(-0.0f) < 0).toBeTruthy + + expect(Math.signum(js.Number.NaN.toFloat).isNaN).toBeTruthy + } + + it("should respond to `nextUp` for Double") { + expect(Math.nextUp(Double.PositiveInfinity)).toEqual(Double.PositiveInfinity) + expect(Math.nextUp(Double.NegativeInfinity)).toEqual(-Double.MaxValue) + expect(Math.nextUp(Double.MaxValue)).toEqual(Double.PositiveInfinity) + expect(Math.nextUp(-Double.MaxValue)).toEqual(-1.7976931348623155e+308) + expect(Math.nextUp(-Double.MinValue)).toEqual(Double.PositiveInfinity) + expect(Math.nextUp(0.0)).toEqual(Double.MinValue) + expect(Math.nextUp(-0.0)).toEqual(Double.MinValue) + expect(Math.nextUp(9007199254740991.0)).toEqual(9007199254740992.0) + expect(Math.nextUp(9007199254740992.0)).toEqual(9007199254740994.0) + expect(Math.nextUp(1.0)).toEqual(1 + 2.2204460492503130808472633361816E-16) + } + + it("should respond to `nextAfter` for Double") { + expect(Math.nextAfter(1.0, js.Number.NaN.toDouble).isNaN).toBeTruthy + expect(Math.nextAfter(js.Number.NaN.toDouble, 1.0).isNaN).toBeTruthy + expect(Math.nextAfter(0.0, 0.0)).toEqual(0.0) + expect(Math.nextAfter(0.0, -0.0)).toEqual(-0.0) + expect(Math.nextAfter(-0.0, 0.0)).toEqual(0.0) + expect(Math.nextAfter(-0.0, -0.0)).toEqual(-0.0) + expect(Math.nextAfter(Double.MinValue, Double.NegativeInfinity)).toEqual(Double.NegativeInfinity) + expect(Math.nextAfter(-Double.MinValue, Double.PositiveInfinity)).toEqual(Double.PositiveInfinity) + expect(Math.nextAfter(Double.PositiveInfinity, Double.NegativeInfinity)).toEqual(Double.MaxValue) + expect(Math.nextAfter(Double.NegativeInfinity, Double.PositiveInfinity)).toEqual(-Double.MaxValue) + expect(Math.nextAfter(Double.MaxValue, Double.PositiveInfinity)).toEqual(Double.PositiveInfinity) + expect(Math.nextAfter(-Double.MaxValue, Double.NegativeInfinity)).toEqual(Double.NegativeInfinity) + expect(Math.nextAfter(1.0, 1.0)).toEqual(1.0) + } + + it("should respond to `ulp` for Double") { + expect(Math.ulp(3.4)).toEqual(4.440892098500626E-16) + expect(Math.ulp(3.423E109)).toEqual(4.1718496795330275E93) + expect(Math.ulp(0.0)).toEqual(Double.MinValue) + } + + it("should respond to `hypot`") { + expect(Math.hypot(0.0, 0.0)).toBeCloseTo(0.0) + expect(Math.hypot(3.0, 4.0)).toBeCloseTo(5.0) + expect(Math.hypot(3.0, js.Number.NaN.toDouble).isNaN).toBeTruthy + expect(Math.hypot(Double.NegativeInfinity, 4.0)).toEqual(Double.PositiveInfinity) + } + + it("should respond to `expm1`") { + expect(1 / Math.expm1(-0.0) < 0).toBeTruthy + expect(Math.expm1(-0.0)).toBeCloseTo(0.0) + expect(Math.expm1(3.0)).toBeCloseTo(19.085536923187668) + expect(Math.expm1(15.0)).toBeCloseTo(3269016.3724721107) + expect(Math.expm1(1.8E10)).toEqual(Double.PositiveInfinity) + expect(Math.expm1(Double.PositiveInfinity)).toEqual(Double.PositiveInfinity) + expect(Math.expm1(Double.NegativeInfinity)).toBeCloseTo(-1.0) + expect(Math.expm1(4.9E-324)).toBeCloseTo(4.9E-324) + } + + it("should respond to `sinh`") { + expect(Math.sinh(-1234.56)).toEqual(Double.NegativeInfinity) + expect(Math.sinh(1234.56)).toEqual(Double.PositiveInfinity) + expect(Math.sinh(0.0)).toBeCloseTo(0.0) + expect(Math.sinh(Double.PositiveInfinity)).toEqual(Double.PositiveInfinity) + } + + it("should respond to `cosh`") { + expect(Math.cosh(-1234.56)).toEqual(Double.PositiveInfinity) + expect(Math.cosh(1234.56)).toEqual(Double.PositiveInfinity) + expect(Math.cosh(-0.0)).toBeCloseTo(1.0) + expect(Math.cosh(Double.PositiveInfinity)).toEqual(Double.PositiveInfinity) + } + + it("should respond to `tanh`") { + expect(Math.tanh(-1234.56)).toBeCloseTo(-1.0) + expect(Math.tanh(-120.56)).toBeCloseTo(-1.0) + expect(Math.tanh(1234.56)).toBeCloseTo(1.0) + expect(Math.tanh(0.0)).toBeCloseTo(0.0) + expect(Math.tanh(Double.PositiveInfinity)).toBeCloseTo(1.0) + expect(Math.tanh(Double.NegativeInfinity)).toBeCloseTo(-1.0) + } + + } + +} diff --git a/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/MockByteArrayOutputStream.scala b/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/MockByteArrayOutputStream.scala new file mode 100644 index 0000000..3356a85 --- /dev/null +++ b/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/MockByteArrayOutputStream.scala @@ -0,0 +1,47 @@ +package scala.scalajs.testsuite.javalib + +import java.io._ + +/** A ByteArrayOutputStream that exposes various hooks for testing purposes. */ +class MockByteArrayOutputStream extends ByteArrayOutputStream { + private var _flushed: Boolean = true + private var _closed: Boolean = false + + var throwing: Boolean = false + + def flushed: Boolean = _flushed + def closed: Boolean = _closed + + private def maybeThrow(): Unit = { + if (throwing) + throw new IOException("MockByteArrayOutputStream throws") + } + + private def writeOp[A](op: => A): A = { + maybeThrow() + _flushed = false + op + } + + override def flush(): Unit = { + maybeThrow() + super.flush() + _flushed = true + } + + override def close(): Unit = { + maybeThrow() + super.close() + _closed = true + } + + override def write(c: Int): Unit = + writeOp(super.write(c)) + + override def write(b: Array[Byte]): Unit = + writeOp(super.write(b)) + + override def write(b: Array[Byte], off: Int, len: Int): Unit = + writeOp(super.write(b, off, len)) + +} diff --git a/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/ObjectTest.scala b/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/ObjectTest.scala new file mode 100644 index 0000000..3584454 --- /dev/null +++ b/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/ObjectTest.scala @@ -0,0 +1,72 @@ +/* __ *\ +** ________ ___ / / ___ __ ____ Scala.js Test Suite ** +** / __/ __// _ | / / / _ | __ / // __/ (c) 2013, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \ http://scala-js.org/ ** +** /____/\___/_/ |_/____/_/ | |__/ /____/ ** +** |/____/ ** +\* */ +package scala.scalajs.testsuite.javalib + +import org.scalajs.jasminetest.JasmineTest + +import scala.scalajs.js + +object ObjectTest extends JasmineTest { + + describe("java.lang.Object") { + + it("should provide `equals`") { + case class xy(x: Int, y: Int) + + val l = List(xy(1, 2), xy(2, 1)) + val xy12 = xy(1, 2) + + expect(l.contains(xy12)).toBeTruthy + expect(l.exists(_ == xy12)).toBeTruthy // the workaround + } + + it("everything but null should be an Object") { + expect((() : Any).isInstanceOf[Object]).toBeTruthy + expect((true : Any).isInstanceOf[Object]).toBeTruthy + expect(('a' : Any).isInstanceOf[Object]).toBeTruthy + expect((1.toByte : Any).isInstanceOf[Object]).toBeTruthy + expect((658.toShort : Any).isInstanceOf[Object]).toBeTruthy + expect((60000 : Any).isInstanceOf[Object]).toBeTruthy + expect((12345678910112L: Any).isInstanceOf[Object]).toBeTruthy + expect((6.5f : Any).isInstanceOf[Object]).toBeTruthy + expect((12.4 : Any).isInstanceOf[Object]).toBeTruthy + expect((new Object : Any).isInstanceOf[Object]).toBeTruthy + expect(("hello" : Any).isInstanceOf[Object]).toBeTruthy + expect((List(1) : Any).isInstanceOf[Object]).toBeTruthy + expect((Array(1) : Any).isInstanceOf[Object]).toBeTruthy + expect((Array(Nil) : Any).isInstanceOf[Object]).toBeTruthy + expect((new js.Object : Any).isInstanceOf[Object]).toBeTruthy + expect((js.Array(5) : Any).isInstanceOf[Object]).toBeTruthy + } + + it("null should not be an Object") { + expect((null: Any).isInstanceOf[Object]).toBeFalsy + } + + it("everything should cast to Object successfully, including null") { + expect(() => (() : Any).asInstanceOf[Object]).not.toThrow + expect(() => (true : Any).asInstanceOf[Object]).not.toThrow + expect(() => ('a' : Any).asInstanceOf[Object]).not.toThrow + expect(() => (1.toByte : Any).asInstanceOf[Object]).not.toThrow + expect(() => (658.toShort : Any).asInstanceOf[Object]).not.toThrow + expect(() => (60000 : Any).asInstanceOf[Object]).not.toThrow + expect(() => (12345678910112L: Any).asInstanceOf[Object]).not.toThrow + expect(() => (6.5f : Any).asInstanceOf[Object]).not.toThrow + expect(() => (12.4 : Any).asInstanceOf[Object]).not.toThrow + expect(() => (new Object : Any).asInstanceOf[Object]).not.toThrow + expect(() => ("hello" : Any).asInstanceOf[Object]).not.toThrow + expect(() => (List(1) : Any).asInstanceOf[Object]).not.toThrow + expect(() => (Array(1) : Any).asInstanceOf[Object]).not.toThrow + expect(() => (Array(Nil) : Any).asInstanceOf[Object]).not.toThrow + expect(() => (new js.Object : Any).asInstanceOf[Object]).not.toThrow + expect(() => (js.Array(5) : Any).asInstanceOf[Object]).not.toThrow + expect(() => (null : Any).asInstanceOf[Object]).not.toThrow + } + + } +} diff --git a/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/OutputStreamWriterTest.scala b/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/OutputStreamWriterTest.scala new file mode 100644 index 0000000..7987a4c --- /dev/null +++ b/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/OutputStreamWriterTest.scala @@ -0,0 +1,134 @@ +/* __ *\ +** ________ ___ / / ___ __ ____ Scala.js Test Suite ** +** / __/ __// _ | / / / _ | __ / // __/ (c) 2013, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \ http://scala-js.org/ ** +** /____/\___/_/ |_/____/_/ | |__/ /____/ ** +** |/____/ ** +\* */ +package scala.scalajs.testsuite.javalib + +import java.io._ + +import scala.scalajs.js +import js.JSConverters._ +import org.scalajs.jasminetest.JasmineTest + +object OutputStreamWriterTest extends JasmineTest { + private def newOSWriter(): (OutputStreamWriter, MockByteArrayOutputStream) = { + val bos = new MockByteArrayOutputStream + val osw = new OutputStreamWriter(bos) + (osw, bos) + } + + describe("java.io.OutputStreamWriter") { + it("flush") { + val (osw, bos) = newOSWriter() + bos.write(1) + osw.write("ABC") + expect(bos.flushed).toBeFalsy + osw.flush() + expect(bos.flushed).toBeTruthy + } + + it("close") { + val (osw, bos) = newOSWriter() + bos.write(1) + osw.write("ABC") + expect(bos.flushed).toBeFalsy + + osw.close() + expect(bos.flushed).toBeTruthy + expect(bos.closed).toBeTruthy + + // can double-close without error + osw.close() + + // when closed, other operations cause error + expect(() => osw.write('A')).toThrow + expect(() => osw.write("never printed")).toThrow + expect(() => osw.write(Array('a', 'b'))).toThrow + expect(() => osw.append("hello", 1, 3)).toThrow + expect(() => osw.flush()).toThrow + + // at the end of it all, bos is still what it was when it was closed + expect(bos.toByteArray().toJSArray).toEqual(js.Array(1, 65, 66, 67)) + } + + def testW(body: OutputStreamWriter => Unit, + expected: js.Array[Int], alreadyFlushed: Boolean = false): Unit = { + val (osw, bos) = newOSWriter() + body(osw) + if (!alreadyFlushed) { + expect(bos.size).toEqual(0) // write() methods should buffer + osw.flush() + } + expect(bos.flushed).toBeTruthy + expect(bos.toByteArray.toJSArray).toEqual(expected.map(_.toByte)) + } + + it("write(), ASCII repertoire") { + // Pure ASCII + testW(_.write('\n'), js.Array('\n')) + testW(_.write("hello\n"), js.Array('h', 'e', 'l', 'l', 'o', '\n')) + testW(_.write("hello\nworld", 3, 4), js.Array('l', 'o', '\n', 'w')) + testW(_.write(Array('A', '\n')), js.Array('A', '\n')) + testW(_.write(Array('A', 'B', '\n', 'C'), 1, 2), js.Array('B', '\n')) + } + + it("write(), Unicode repertoire without surrogates") { + testW(_.write('é'), js.Array(0xc3, 0xa9)) + testW(_.write("こんにちは"), js.Array( + 0xe3, 0x81, 0x93, 0xe3, 0x82, 0x93, 0xe3, 0x81, 0xab, 0xe3, 0x81, 0xa1, 0xe3, 0x81, 0xaf)) + testW(_.write("Καλημέρα", 3, 4), js.Array( + 0xce, 0xb7, 0xce, 0xbc, 0xce, 0xad, 0xcf, 0x81)) + } + + it("write(), surrogate pairs") { + testW(_.write("\ud83d\udca9"), js.Array(0xf0, 0x9f, 0x92, 0xa9)) + testW(_.write("ab\ud83d\udca9cd", 1, 3), js.Array('b', 0xf0, 0x9f, 0x92, 0xa9)) + } + + it("write(), surrogate pairs spread across multiple writes") { + testW({ osw => osw.write('\ud83d'); osw.write('\udca9') }, + js.Array(0xf0, 0x9f, 0x92, 0xa9)) + + testW({ osw => osw.write('\ud83d'); osw.flush(); osw.write('\udca9') }, + js.Array(0xf0, 0x9f, 0x92, 0xa9)) + + testW({ osw => osw.write("ab\ud83d"); osw.write('\udca9') }, + js.Array('a', 'b', 0xf0, 0x9f, 0x92, 0xa9)) + + testW({ osw => osw.write("ab\ud83d"); osw.write("\udca9cd") }, + js.Array('a', 'b', 0xf0, 0x9f, 0x92, 0xa9, 'c', 'd')) + + testW({ osw => osw.write("ab\ud83dzz", 1, 2); osw.write("ww\udca9cd", 2, 2) }, + js.Array('b', 0xf0, 0x9f, 0x92, 0xa9, 'c')) + } + + it("write(), malformed surrogates") { + testW(_.write("\ud83da"), js.Array('?', 'a')) + testW(_.write("\udca9"), js.Array('?')) + } + + it("write(), malformed surrogates spread across multiple writes") { + testW({ osw => osw.write('\ud83d'); osw.write('a') }, + js.Array('?', 'a')) + + testW({ osw => osw.write("ab\ud83d"); osw.write("\ud83d") }, + js.Array('a', 'b', '?')) + + testW({ osw => osw.write("ab\ud83d"); osw.write("\ud83dc") }, + js.Array('a', 'b', '?', '?', 'c')) + } + + it("write(), malformed surrogates at end of input") { + testW({ osw => osw.write('\ud83d'); osw.close() }, + js.Array('?'), alreadyFlushed = true) + + testW({ osw => osw.write("ab\ud83d"); osw.close() }, + js.Array('a', 'b', '?'), alreadyFlushed = true) + } + + } + +} diff --git a/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/PrintStreamTest.scala b/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/PrintStreamTest.scala new file mode 100644 index 0000000..01c872b --- /dev/null +++ b/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/PrintStreamTest.scala @@ -0,0 +1,296 @@ +/* __ *\ +** ________ ___ / / ___ __ ____ Scala.js Test Suite ** +** / __/ __// _ | / / / _ | __ / // __/ (c) 2013, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \ http://scala-js.org/ ** +** /____/\___/_/ |_/____/_/ | |__/ /____/ ** +** |/____/ ** +\* */ +package scala.scalajs.testsuite.javalib + +import java.io._ + +import scala.scalajs.js +import js.JSConverters._ +import org.scalajs.jasminetest.JasmineTest + +object PrintStreamTest extends JasmineTest { + private def newPrintStream( + autoFlush: Boolean = false): (MockPrintStream, MockByteArrayOutputStream) = { + val bos = new MockByteArrayOutputStream + val ps = new MockPrintStream(bos, autoFlush) + (ps, bos) + } + + describe("java.io.PrintStream") { + it("flush") { + val (ps, bos) = newPrintStream() + ps.print("hello") + expect(bos.flushed).toBeFalsy + ps.flush() + expect(bos.flushed).toBeTruthy + } + + it("close") { + val (ps, bos) = newPrintStream() + ps.write(Array[Byte](1)) + expect(bos.flushed).toBeFalsy + + ps.close() + expect(bos.flushed).toBeTruthy + expect(bos.closed).toBeTruthy + expect(ps.checkError()).toBeFalsy + + // can double-close without error + ps.close() + expect(ps.checkError()).toBeFalsy + ps.clearError() + + // when closed, other operations cause error + def expectCausesError(body: => Unit): Unit = { + body + expect(ps.checkError()).toBeTruthy + ps.clearError() + } + expectCausesError(ps.print("never printed")) + expectCausesError(ps.write(Array[Byte]('a', 'b'))) + expectCausesError(ps.append("hello", 1, 3)) + expectCausesError(ps.flush()) + + // at the end of it all, bos is still what it was when it was closed + expect(bos.toByteArray.toJSArray).toEqual(js.Array(1)) + } + + it("write, pass the bytes through") { + def test(body: PrintStream => Unit, expected: js.Array[Int], + testFlushed: Boolean = false): Unit = { + val (ps, bos) = newPrintStream(autoFlush = true) + body(ps) + if (testFlushed) + expect(bos.flushed).toBeTruthy + expect(ps.checkError()).toBeFalsy + expect(bos.toByteArray.toJSArray).toEqual(expected.map(_.toByte)) + } + + test(_.write('a'), js.Array('a')) + test(_.write('\n'), js.Array('\n'), testFlushed = true) + test(_.write(Array[Byte]('A', '\n')), + js.Array('A', '\n'), testFlushed = true) + test(_.write(Array[Byte]('A', 'B', '\n', 'C'), 1, 2), + js.Array('B', '\n'), testFlushed = true) + + test(_.write('é'.toByte), js.Array('é')) + test(_.write(Array[Byte]('é'.toByte, 'à'.toByte)), js.Array('é', 'à')) + } + + it("print") { + def test(body: PrintStream => Unit, expected: String, + testFlushed: Boolean = false): Unit = { + val (ps, bos) = newPrintStream(autoFlush = true) + body(ps) + if (testFlushed) + expect(bos.flushed).toBeTruthy + expect(ps.checkError()).toBeFalsy + expect(bos.toString()).toBe(expected) + } + + test(_.print(true), "true") + test(_.print('Z'), "Z") + test(_.print('\n'), "\n", testFlushed = true) + test(_.print(5), "5") + test(_.print(1234567891011L), "1234567891011") + test(_.print(1.5f), "1.5") + test(_.print(Math.PI), "3.141592653589793") + test(_.print(Array('A', '\n')), "A\n", testFlushed = true) + test(_.print("hello\n"), "hello\n", testFlushed = true) + test(_.print(null: String), "null") + test(_.print((1, 2)), "(1,2)") + test(_.print(null: AnyRef), "null") + } + + it("print encodes in UTF-8") { + def test(body: PrintStream => Unit, expected: js.Array[Int]): Unit = { + val (ps, bos) = newPrintStream(autoFlush = false) + body(ps) + expect(ps.checkError()).toBeFalsy + expect(bos.toByteArray.toJSArray).toEqual(expected.map(_.toByte)) + } + + test(_.print('é'), js.Array(0xc3, 0xa9)) + test(_.print("こんにちは"), js.Array( + 0xe3, 0x81, 0x93, 0xe3, 0x82, 0x93, 0xe3, 0x81, 0xab, 0xe3, 0x81, 0xa1, 0xe3, 0x81, 0xaf)) + test(_.print("ημέρ"), js.Array( + 0xce, 0xb7, 0xce, 0xbc, 0xce, 0xad, 0xcf, 0x81)) + + test(_.print("\ud83d\udca9"), js.Array(0xf0, 0x9f, 0x92, 0xa9)) + test(_.print("b\ud83d\udca9c"), js.Array('b', 0xf0, 0x9f, 0x92, 0xa9, 'c')) + + test({ osw => osw.print("ab\ud83d"); osw.print('\udca9') }, + js.Array('a', 'b', 0xf0, 0x9f, 0x92, 0xa9)) + + test({ osw => osw.print("ab\ud83d"); osw.print("\udca9cd") }, + js.Array('a', 'b', 0xf0, 0x9f, 0x92, 0xa9, 'c', 'd')) + + // Start of malformed sequences + + test(_.print("\ud83da"), js.Array('?', 'a')) + test(_.print("\udca9"), js.Array('?')) + + test({ osw => osw.print('\ud83d'); osw.print('a') }, + js.Array('?', 'a')) + + test({ osw => osw.print("ab\ud83d"); osw.print("\ud83d") }, + js.Array('a', 'b', '?')) + + test({ osw => osw.print("ab\ud83d"); osw.print("\ud83dc") }, + js.Array('a', 'b', '?', '?', 'c')) + + test({ osw => osw.print('\ud83d'); osw.close() }, + js.Array('?')) + + test({ osw => osw.print("ab\ud83d"); osw.close() }, + js.Array('a', 'b', '?')) + } + + for (autoFlush <- Seq(true, false)) { + val title = + if (autoFlush) "println forwards and flushes when autoFlush is true" + else "println forwards, does not flush when autoFlush is false" + it(title) { + def test(body: PrintStream => Unit, expected: String): Unit = { + val (ps, bos) = newPrintStream(autoFlush = autoFlush) + body(ps) + if (autoFlush) expect(bos.flushed).toBeTruthy + else expect(bos.flushed).toBeFalsy + expect(ps.checkError()).toBeFalsy + expect(bos.toString()).toBe(expected) + } + + test(_.println(), "\n") + test(_.println(true), "true\n") + test(_.println('Z'), "Z\n") + test(_.println('\n'), "\n\n") + test(_.println(5), "5\n") + test(_.println(1234567891011L), "1234567891011\n") + test(_.println(1.5f), "1.5\n") + test(_.println(Math.PI), "3.141592653589793\n") + test(_.println(Array('A', '\n')), "A\n\n") + test(_.println("hello\n"), "hello\n\n") + test(_.println(null: String), "null\n") + test(_.println((1, 2)), "(1,2)\n") + test(_.println(null: AnyRef), "null\n") + } + } + + for (autoFlush <- Seq(true, false)) { + val title = + if (autoFlush) "printf/format, which flushes when autoFlush is true" + else "printf/format, does not flush when autoFlush is false" + it(title) { + def test(body: PrintStream => Unit, expected: String): Unit = { + val (ps, bos) = newPrintStream(autoFlush = autoFlush) + body(ps) + if (autoFlush) expect(bos.flushed).toBeTruthy + else expect(bos.flushed).toBeFalsy + expect(ps.checkError()).toBeFalsy + expect(bos.toString()).toBe(expected) + } + + test(_.printf("%04d", Int.box(5)), "0005") + test(_.format("%.5f", Double.box(Math.PI)), "3.14159") + } + } + + it("append") { + def test(body: PrintStream => Unit, expected: String, + testFlushed: Boolean = false): Unit = { + val (ps, bos) = newPrintStream(autoFlush = true) + body(ps) + if (testFlushed) + expect(bos.flushed).toBeTruthy + expect(ps.checkError()).toBeFalsy + expect(bos.toString()).toBe(expected) + } + + test(_.append("hello\n"), "hello\n", testFlushed = true) + test(_.append(null: CharSequence), "null") + test(_.append("hello\nworld", 3, 6), "lo\n", testFlushed = true) + test(_.append(null: CharSequence, 1, 2), "u") + test(_.append('A'), "A") + test(_.append('\n'), "\n", testFlushed = true) + } + + it("traps all IOException and updates checkError") { + def test(body: PrintStream => Unit): Unit = { + val (ps, bos) = newPrintStream() + bos.throwing = true + body(ps) + expect(ps.checkError()).toBeTruthy + } + + test(_.flush()) + test(_.close()) + + test(_.write('Z')) + test(_.write(Array[Byte]('A', 'B'))) + test(_.write(Array[Byte]('A', 'B'), 1, 1)) + + test(_.print(true)) + test(_.print('Z')) + test(_.print('\n')) + test(_.print(5)) + test(_.print(1234567891011L)) + test(_.print(1.5f)) + test(_.print(Math.PI)) + test(_.print(Array('A', '\n'))) + test(_.print("hello\n")) + test(_.print(null: String)) + test(_.print((1, 2))) + test(_.print(null: AnyRef)) + + test(_.println()) + test(_.println(true)) + test(_.println('Z')) + test(_.println('\n')) + test(_.println(5)) + test(_.println(1234567891011L)) + test(_.println(1.5f)) + test(_.println(Math.PI)) + test(_.println(Array('A', '\n'))) + test(_.println("hello\n")) + test(_.println(null: String)) + test(_.println((1, 2))) + test(_.println(null: AnyRef)) + + test(_.append("hello\n")) + test(_.append(null: CharSequence)) + test(_.append("hello\nworld", 3, 6)) + test(_.append(null: CharSequence, 1, 2)) + test(_.append('A')) + test(_.append('\n')) + } + + it("write short-circuits pending high surrogates in print") { + val (ps, bos) = newPrintStream() + ps.print('A') + expect(bos.toByteArray.toJSArray).toEqual(js.Array[Byte]('A')) + ps.print('\ud83d') + expect(bos.toByteArray.toJSArray).toEqual(js.Array[Byte]('A')) + ps.flush() + expect(bos.toByteArray.toJSArray).toEqual(js.Array[Byte]('A')) + ps.write('Z') + expect(bos.toByteArray.toJSArray).toEqual(js.Array[Byte]('A', 'Z')) + ps.print('\udca9') + expect(bos.toByteArray.toJSArray).toEqual(js.Array[Byte]( + 'A', 'Z', -16, -97, -110, -87)) + } + } + + /** A PrintStream that exposes various hooks for testing purposes. */ + private class MockPrintStream(out: OutputStream, + autoFlush: Boolean) extends PrintStream(out, autoFlush) { + def this(out: OutputStream) = this(out, false) + + override def clearError(): Unit = super.clearError() + } + +} diff --git a/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/PrintWriterTest.scala b/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/PrintWriterTest.scala new file mode 100644 index 0000000..b2b3309 --- /dev/null +++ b/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/PrintWriterTest.scala @@ -0,0 +1,287 @@ +/* __ *\ +** ________ ___ / / ___ __ ____ Scala.js Test Suite ** +** / __/ __// _ | / / / _ | __ / // __/ (c) 2013, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \ http://scala-js.org/ ** +** /____/\___/_/ |_/____/_/ | |__/ /____/ ** +** |/____/ ** +\* */ +package scala.scalajs.testsuite.javalib + +import scala.language.implicitConversions + +import java.io._ + +import scala.scalajs.js +import org.scalajs.jasminetest.JasmineTest + +object PrintWriterTest extends JasmineTest { + private def newPrintWriter( + autoFlush: Boolean = false): (MockPrintWriter, MockStringWriter) = { + val sw = new MockStringWriter + val pw = new MockPrintWriter(sw, autoFlush) + (pw, sw) + } + + describe("java.io.PrintWriter") { + it("flush") { + val (pw, sw) = newPrintWriter() + pw.print("hello") + expect(sw.flushed).toBeFalsy + pw.flush() + expect(sw.flushed).toBeTruthy + } + + it("close") { + val (pw, sw) = newPrintWriter() + pw.write("begin") + expect(sw.flushed).toBeFalsy + + pw.close() + expect(sw.flushed).toBeTruthy + expect(sw.closed).toBeTruthy + expect(pw.checkError()).toBeFalsy + + // can double-close without error + pw.close() + expect(pw.checkError()).toBeFalsy + pw.clearError() + + // when closed, other operations cause error + def expectCausesError(body: => Unit): Unit = { + body + expect(pw.checkError()).toBeTruthy + pw.clearError() + } + expectCausesError(pw.print("never printed")) + expectCausesError(pw.write(Array('a', 'b'))) + expectCausesError(pw.append("hello", 1, 3)) + expectCausesError(pw.flush()) + + // at the end of it all, sw is still what it was when it was closed + expect(sw.toString()).toBe("begin") + } + + it("write, does not flush even with \\n") { + def test(body: PrintWriter => Unit, expected: String): Unit = { + val (pw, sw) = newPrintWriter(autoFlush = true) + body(pw) + expect(sw.flushed).toBeFalsy + expect(pw.checkError()).toBeFalsy + expect(sw.toString()).toBe(expected) + } + + test(_.write('\n'), "\n") + test(_.write("hello\n"), "hello\n") + test(_.write("hello\nworld", 3, 3), "lo\n") + test(_.write(Array('A', '\n')), "A\n") + test(_.write(Array('A', 'B', '\n', 'C'), 1, 2), "B\n") + } + + it("print, does not flush even with \\n") { + def test(body: PrintWriter => Unit, expected: String): Unit = { + val (pw, sw) = newPrintWriter(autoFlush = true) + body(pw) + expect(sw.flushed).toBeFalsy + expect(pw.checkError()).toBeFalsy + expect(sw.toString()).toBe(expected) + } + + test(_.print(true), "true") + test(_.print('Z'), "Z") + test(_.print('\n'), "\n") + test(_.print(5), "5") + test(_.print(1234567891011L), "1234567891011") + test(_.print(1.5f), "1.5") + test(_.print(Math.PI), "3.141592653589793") + test(_.print(Array('A', '\n')), "A\n") + test(_.print("hello\n"), "hello\n") + test(_.print(null: String), "null") + test(_.print((1, 2)), "(1,2)") + test(_.print(null: AnyRef), "null") + } + + for (autoFlush <- Seq(true, false)) { + val title = + if (autoFlush) "println forwards and flushes when autoFlush is true" + else "println forwards, does not flush when autoFlush is false" + it(title) { + def test(body: PrintWriter => Unit, expected: String): Unit = { + val (pw, sw) = newPrintWriter(autoFlush = autoFlush) + body(pw) + if (autoFlush) expect(sw.flushed).toBeTruthy + else expect(sw.flushed).toBeFalsy + expect(pw.checkError()).toBeFalsy + expect(sw.toString()).toBe(expected) + } + + test(_.println(), "\n") + test(_.println(true), "true\n") + test(_.println('Z'), "Z\n") + test(_.println('\n'), "\n\n") + test(_.println(5), "5\n") + test(_.println(1234567891011L), "1234567891011\n") + test(_.println(1.5f), "1.5\n") + test(_.println(Math.PI), "3.141592653589793\n") + test(_.println(Array('A', '\n')), "A\n\n") + test(_.println("hello\n"), "hello\n\n") + test(_.println(null: String), "null\n") + test(_.println((1, 2)), "(1,2)\n") + test(_.println(null: AnyRef), "null\n") + } + } + + for (autoFlush <- Seq(true, false)) { + val title = + if (autoFlush) "printf/format, which flushes when autoFlush is true" + else "printf/format, does not flush when autoFlush is false" + it(title) { + def test(body: PrintWriter => Unit, expected: String): Unit = { + val (pw, sw) = newPrintWriter(autoFlush = autoFlush) + body(pw) + if (autoFlush) expect(sw.flushed).toBeTruthy + else expect(sw.flushed).toBeFalsy + expect(pw.checkError()).toBeFalsy + expect(sw.toString()).toBe(expected) + } + + test(_.printf("%04d", Int.box(5)), "0005") + test(_.format("%.5f", Double.box(Math.PI)), "3.14159") + } + } + + it("append, does not flush even with \\n") { + def test(body: PrintWriter => Unit, expected: String): Unit = { + val (pw, sw) = newPrintWriter(autoFlush = true) + body(pw) + expect(sw.flushed).toBeFalsy + expect(pw.checkError()).toBeFalsy + expect(sw.toString()).toBe(expected) + } + + test(_.append("hello\n"), "hello\n") + test(_.append(null: CharSequence), "null") + test(_.append("hello\nworld", 3, 6), "lo\n") + test(_.append(null: CharSequence, 1, 2), "u") + test(_.append('A'), "A") + test(_.append('\n'), "\n") + } + + it("traps all IOException and updates checkError") { + def test(body: PrintWriter => Unit): Unit = { + val (pw, sw) = newPrintWriter() + sw.throwing = true + body(pw) + expect(pw.checkError()).toBeTruthy + } + + test(_.flush()) + test(_.close()) + + test(_.write('Z')) + test(_.write("booh")) + test(_.write("booh", 1, 1)) + test(_.write(Array('A', 'B'))) + test(_.write(Array('A', 'B'), 1, 1)) + + test(_.print(true)) + test(_.print('Z')) + test(_.print('\n')) + test(_.print(5)) + test(_.print(1234567891011L)) + test(_.print(1.5f)) + test(_.print(Math.PI)) + test(_.print(Array('A', '\n'))) + test(_.print("hello\n")) + test(_.print(null: String)) + test(_.print((1, 2))) + test(_.print(null: AnyRef)) + + test(_.println()) + test(_.println(true)) + test(_.println('Z')) + test(_.println('\n')) + test(_.println(5)) + test(_.println(1234567891011L)) + test(_.println(1.5f)) + test(_.println(Math.PI)) + test(_.println(Array('A', '\n'))) + test(_.println("hello\n")) + test(_.println(null: String)) + test(_.println((1, 2))) + test(_.println(null: AnyRef)) + + test(_.append("hello\n")) + test(_.append(null: CharSequence)) + test(_.append("hello\nworld", 3, 6)) + test(_.append(null: CharSequence, 1, 2)) + test(_.append('A')) + test(_.append('\n')) + } + } + + /** A PrintWriter that exposes various hooks for testing purposes. */ + private class MockPrintWriter(out: Writer, + autoFlush: Boolean) extends PrintWriter(out, autoFlush) { + def this(out: Writer) = this(out, false) + + override def clearError(): Unit = super.clearError() + } + + /** A StringWriter that exposes various hooks for testing purposes. */ + private class MockStringWriter extends StringWriter { + private var _flushed: Boolean = true + private var _closed: Boolean = false + + var throwing: Boolean = false + + def flushed: Boolean = _flushed + def closed: Boolean = _closed + + private def maybeThrow(): Unit = { + if (throwing) + throw new IOException("MockStringWriter throws") + } + + private def writeOp[A](op: => A): A = { + maybeThrow() + _flushed = false + op + } + + override def flush(): Unit = { + maybeThrow() + super.flush() + _flushed = true + } + + override def close(): Unit = { + maybeThrow() + super.close() + _closed = true + } + + override def append(c: Char): StringWriter = + writeOp(super.append(c)) + + override def append(csq: CharSequence): StringWriter = + writeOp(super.append(csq)) + + override def append(csq: CharSequence, start: Int, end: Int): StringWriter = + writeOp(super.append(csq, start, end)) + + override def write(c: Int): Unit = + writeOp(super.write(c)) + + override def write(cbuf: Array[Char]): Unit = + writeOp(super.write(cbuf)) + + override def write(cbuf: Array[Char], off: Int, len: Int): Unit = + writeOp(super.write(cbuf, off, len)) + + override def write(str: String): Unit = + writeOp(super.write(str)) + + override def write(str: String, off: Int, len: Int): Unit = + writeOp(super.write(str, off, len)) + } +} diff --git a/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/RandomTest.scala b/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/RandomTest.scala new file mode 100644 index 0000000..1794d4a --- /dev/null +++ b/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/RandomTest.scala @@ -0,0 +1,225 @@ +/* __ *\ +** ________ ___ / / ___ __ ____ Scala.js Test Suite ** +** / __/ __// _ | / / / _ | __ / // __/ (c) 2013, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \ http://scala-js.org/ ** +** /____/\___/_/ |_/____/_/ | |__/ /____/ ** +** |/____/ ** +\* */ +package scala.scalajs.testsuite.javalib + +import org.scalajs.jasminetest.JasmineTest + +import java.util.Random + +import scala.scalajs.js +import scala.scalajs.js.JSConverters._ + +object RandomTest extends JasmineTest { + + describe("java.util.Random") { + + it("should produce bits according to spec with seed=10") { + val random = new HackRandom(10) + + expect(random.next(10)).toBe(747) + expect(random.next(1)).toBe(0) + expect(random.next(6)).toBe(16) + expect(random.next(20)).toBe(432970) + expect(random.next(32)).toBe(254270492) + } + + it("should produce bits according to spec with seed=-5") { + val random = new HackRandom(-5) + + expect(random.next(10)).toBe(275) + expect(random.next(1)).toBe(0) + expect(random.next(6)).toBe(21) + expect(random.next(20)).toBe(360349) + expect(random.next(32)).toBe(1635930704) + } + + it("should produce bits according to spec with seed=MaxLong") { + val random = new HackRandom(Long.MaxValue) + + expect(random.next(10)).toBe(275) + expect(random.next(1)).toBe(0) + expect(random.next(6)).toBe(0) + expect(random.next(20)).toBe(574655) + expect(random.next(32)).toBe(-1451336087) + } + + it("should produce bits according to spec with seed=MinInt") { + val random = new HackRandom(Int.MinValue) + + expect(random.next(10)).toBe(388) + expect(random.next(1)).toBe(0) + expect(random.next(6)).toBe(25) + expect(random.next(20)).toBe(352095) + expect(random.next(32)).toBe(-2140124682) + } + + it("should allow resetting the seed") { + val random = new HackRandom(11) + expect(random.next(10)).toBe(747) + expect(random.next(1)).toBe(1) + expect(random.next(6)).toBe(27) + + random.setSeed(11) + expect(random.next(10)).toBe(747) + expect(random.next(1)).toBe(1) + expect(random.next(6)).toBe(27) + } + + it("should reset nextNextGaussian when setting the seed") { + val random = new Random(-1) + expect(random.nextGaussian()).toBe(1.7853314409882288) + random.setSeed(-1) + expect(random.nextGaussian()).toBe(1.7853314409882288) + } + + it("should correctly implement nextDouble") { + val random = new Random(-45) + expect(random.nextDouble()).toBe(0.27288421395636253) + expect(random.nextDouble()).toBe(0.5523165360074201) + expect(random.nextDouble()).toBe(0.5689979434708298) + expect(random.nextDouble()).toBe(0.9961166166874871) + expect(random.nextDouble()).toBe(0.5368984665202684) + expect(random.nextDouble()).toBe(0.19849067496547423) + expect(random.nextDouble()).toBe(0.6021019223595357) + expect(random.nextDouble()).toBe(0.06132131151816378) + expect(random.nextDouble()).toBe(0.7303867762743866) + expect(random.nextDouble()).toBe(0.7426529384056163) + } + + it("should correctly implement nextBoolean") { + val random = new Random(4782934) + expect(random.nextBoolean()).toBe(false) + expect(random.nextBoolean()).toBe(true) + expect(random.nextBoolean()).toBe(true) + expect(random.nextBoolean()).toBe(false) + expect(random.nextBoolean()).toBe(false) + expect(random.nextBoolean()).toBe(false) + expect(random.nextBoolean()).toBe(true) + expect(random.nextBoolean()).toBe(false) + } + + it("should correctly implement nextInt") { + val random = new Random(-84638) + expect(random.nextInt()).toBe(-1217585344) + expect(random.nextInt()).toBe(1665699216) + expect(random.nextInt()).toBe(382013296) + expect(random.nextInt()).toBe(1604432482) + expect(random.nextInt()).toBe(-1689010196) + expect(random.nextInt()).toBe(1743354032) + expect(random.nextInt()).toBe(454046816) + expect(random.nextInt()).toBe(922172344) + expect(random.nextInt()).toBe(-1890515287) + expect(random.nextInt()).toBe(1397525728) + } + + it("should correctly implement nextInt(n)") { + val random = new Random(7) + expect(random.nextInt(76543)).toBe(32736) + expect(() => random.nextInt(0)).toThrow + expect(random.nextInt(45)).toBe(29) + expect(random.nextInt(945)).toBe(60) + expect(random.nextInt(35694839)).toBe(20678044) + expect(random.nextInt(35699)).toBe(23932) + expect(random.nextInt(3699)).toBe(2278) + expect(random.nextInt(10)).toBe(8) + } + + it("should correctly implement nextInt(n) for powers of 2") { + val random = new Random(-56938) + + expect(random.nextInt(32)).toBe(8) + expect(random.nextInt(8)).toBe(3) + expect(random.nextInt(128)).toBe(3) + expect(random.nextInt(4096)).toBe(1950) + expect(random.nextInt(8192)).toBe(3706) + expect(random.nextInt(8192)).toBe(4308) + expect(random.nextInt(8192)).toBe(3235) + expect(random.nextInt(8192)).toBe(7077) + expect(random.nextInt(8192)).toBe(2392) + expect(random.nextInt(32)).toBe(31) + } + + it("should correctly implement nextLong") { + val random = new Random(205620432625028L) + expect(random.nextLong()).toBe(3710537363280377478L) + expect(random.nextLong()).toBe(4121778334981170700L) + expect(random.nextLong()).toBe(289540773990891960L) + expect(random.nextLong()).toBe(307008980197674441L) + expect(random.nextLong()).toBe(7527069864796025013L) + expect(random.nextLong()).toBe(-4563192874520002144L) + expect(random.nextLong()).toBe(7619507045427546529L) + expect(random.nextLong()).toBe(-7888117030898487184L) + expect(random.nextLong()).toBe(-3499168703537933266L) + expect(random.nextLong()).toBe(-1998975913933474L) + } + + it("should correctly implement nextFloat") { + val random = new Random(-3920005825473L) + expect(random.nextFloat()).toBeCloseTo(0.059591234, 7) + expect(random.nextFloat()).toBeCloseTo(0.7007871, 7) + expect(random.nextFloat()).toBeCloseTo(0.39173192, 7) + expect(random.nextFloat()).toBeCloseTo(0.0647918, 7) + expect(random.nextFloat()).toBeCloseTo(0.9029677, 7) + expect(random.nextFloat()).toBeCloseTo(0.18226051, 7) + expect(random.nextFloat()).toBeCloseTo(0.94444054, 7) + expect(random.nextFloat()).toBeCloseTo(0.008844078, 7) + expect(random.nextFloat()).toBeCloseTo(0.08891684, 7) + expect(random.nextFloat()).toBeCloseTo(0.06482434, 7) + } + + it("should correctly implement nextBytes") { + val random = new Random(7399572013373333L) + + def test(exps: Int*) = { + val exp = js.Array(exps.map(_.toByte): _*) + val buf = new Array[Byte](exp.length) + random.nextBytes(buf) + expect(buf.toJSArray).toEqual(exp) + } + + test(62, 89, 68, -91, 10, 0, 85) + test(-89, -76, 88, 121, -25, 47, 58, -8, 78, 20, -77, 84, -3, + -33, 58, -9, 11, 57, -118, 40, -74, -86, 78, 123, 58) + test(-77, 112, -116) + test() + test(-84, -96, 108) + test(57, -106, 42, -100, -47, -84, 67, -48, 45) + } + + it("should correctly implement nextGaussian") { + val random = new Random(2446004) + expect(random.nextGaussian()).toBe(-0.5043346938630431) + expect(random.nextGaussian()).toBe(-0.3250983270156675) + expect(random.nextGaussian()).toBe(-0.23799457294994966) + expect(random.nextGaussian()).toBe(0.4164610631507695) + expect(random.nextGaussian()).toBe(0.22086348814760687) + expect(random.nextGaussian()).toBe(-0.706833209972521) + expect(random.nextGaussian()).toBe(0.6730758289772553) + expect(random.nextGaussian()).toBe(0.2797393696191283) + expect(random.nextGaussian()).toBe(-0.2979099632667685) + expect(random.nextGaussian()).toBe(0.37443415981434314) + expect(random.nextGaussian()).toBe(0.9584801742918951) + expect(random.nextGaussian()).toBe(1.1762179112229345) + expect(random.nextGaussian()).toBe(0.8736960092848826) + expect(random.nextGaussian()).toBe(0.12301554931271008) + expect(random.nextGaussian()).toBe(-0.6052081187207353) + expect(random.nextGaussian()).toBe(-0.2015925608755316) + expect(random.nextGaussian()).toBe(-1.0071216119742104) + expect(random.nextGaussian()).toBe(0.6734222041441913) + expect(random.nextGaussian()).toBe(0.3990565555091522) + expect(random.nextGaussian()).toBe(2.0051627385915154) + } + + } + + /** Helper class to access next */ + class HackRandom(seed: Long) extends Random(seed) { + override def next(bits: Int): Int = super.next(bits) + } + +} diff --git a/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/ReadersTest.scala b/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/ReadersTest.scala new file mode 100644 index 0000000..f970141 --- /dev/null +++ b/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/ReadersTest.scala @@ -0,0 +1,244 @@ +/* __ *\ +** ________ ___ / / ___ __ ____ Scala.js Test Suite ** +** / __/ __// _ | / / / _ | __ / // __/ (c) 2013, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \ http://scala-js.org/ ** +** /____/\___/_/ |_/____/_/ | |__/ /____/ ** +** |/____/ ** +\* */ +package scala.scalajs.testsuite.javalib + +import scala.annotation.tailrec + +import java.io._ + +import scala.scalajs.js +import scala.scalajs.js.JSConverters._ + +import org.scalajs.jasminetest.JasmineTest + +/** Tests for our implementation of java.io._ reader classes */ +object ReadersTest extends JasmineTest { + + describe("java.io.StringReader") { + val str = "asdf" + def newReader = new StringReader(str) + + it("should provide read()") { + val r = newReader + + for (c <- str) { + expect(r.read().toChar).toEqual(c) + } + + expect(r.read()).toEqual(-1) + } + + it("should provide read(buf: Array[Char], off: Int, len: Int)") { + val r = newReader + val buf = new Array[Char](10) + + expect(r.read(buf, 2, 8)).toBe(4) + expect(buf.map(_.toInt).toJSArray).toEqual( + js.Array[Int](0,0,'a','s','d','f',0,0,0,0)) + } + + it("should provide read(java.nio.CharBuffer)") { + val r = newReader + val buf0 = java.nio.CharBuffer.allocate(25) + buf0.position(3) + val buf = buf0.slice() + buf.position(4) + buf.limit(14) + + expect(r.read(buf)).toBe(4) + expect(buf.position()).toBe(8) + buf.flip() + expect(buf.toString().map(_.toInt).toJSArray).toEqual( + js.Array[Int](0, 0, 0, 0, 'a', 's', 'd', 'f')) + } + + it("should provide ready") { + val r = newReader + + for (c <- str) { + expect(r.ready()).toBeTruthy + expect(r.read().toChar).toEqual(c) + } + + expect(r.ready()).toBeFalsy + expect(r.read()).toEqual(-1) + } + + it("should provide mark/reset") { + val r = newReader + r.mark(str.length) + + for (c <- str) { + expect(r.read().toChar).toEqual(c) + } + expect(r.read()).toEqual(-1) + + r.reset() + + for (c <- str) { + expect(r.read().toChar).toEqual(c) + } + expect(r.read()).toEqual(-1) + } + + it("should provide skip") { + val r = newReader + + expect(r.read()).toEqual('a') + expect(r.skip(2L).toInt).toBe(2) + + expect(r.read()).toEqual('f') + expect(r.read()).toEqual(-1) + } + + it("should provide close") { + val r = newReader + + r.close() + expect(() => r.read()).toThrow + } + + it("should support marking") { + expect(newReader.markSupported).toBeTruthy + } + } + + describe("java.io.BufferedReader") { + val str = "line1\nline2\r\n\nline4\rline5" + def newReader = new BufferedReader(new StringReader(str), 3) + + it("should provide read()") { + val r = newReader + + for (c <- str) { + expect(r.read().toChar).toEqual(c) + } + expect(r.read()).toEqual(-1) + } + + it("should provide read(cbuf)") { + var read = 0 + val r = newReader + val buf = new Array[Char](15) + + // twice to force filling internal buffer + for (_ <- 0 to 1) { + val len = r.read(buf) + expect(len).toBeGreaterThan(0) + + for (i <- 0 until len) + expect(buf(i)).toEqual(str.charAt(i+read)) + + read += len + } + } + + it("should provide read(cbuf, off, len)") { + var read = 0 + val r = newReader + val buf = new Array[Char](15) + + // twice to force filling internal buffer + for (_ <- 0 to 1) { + val len = r.read(buf, 1, 10) + expect(len).toBeGreaterThan(0) + expect(len).toBeLessThan(11) + + for (i <- 0 until len) + expect(buf(i+1)).toEqual(str.charAt(i+read)) + + read += len + } + } + + it("should provide mark/reset") { + val r = newReader + expect(r.read()).toEqual('l') + + // force moving and resizing buffer + r.mark(10) + + for (i <- 0 until 10) { + expect(r.read()).toEqual(str.charAt(i+1)) + } + + r.reset() + + for (i <- 1 until str.length) { + expect(r.read()).toEqual(str.charAt(i)) + } + } + + it("should provide readLine") { + val r = newReader + + expect(r.readLine()).toEqual("line1") + expect(r.readLine()).toEqual("line2") + expect(r.readLine()).toEqual("") + expect(r.readLine()).toEqual("line4") + expect(r.readLine()).toEqual("line5") + expect(r.readLine()).toEqual(null) + } + + it("should readLine on an empty stream") { + val r = new BufferedReader(new StringReader("")) + + expect(r.readLine()).toEqual(null) + } + + it("should readline with empty lines only") { + val r = new BufferedReader(new StringReader("\n\r\n\r\r\n"), 1) + + for (_ <- 1 to 4) + expect(r.readLine()).toEqual("") + + expect(r.readLine()).toEqual(null) + } + + it("should support marking") { + expect(newReader.markSupported).toBeTruthy + } + } + + describe("java.io.InputStreamReader") { + + it("should read UTF8") { + + val buf = Array[Byte](72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, + 46, -29, -127, -109, -29, -126, -109, -29, -127, -85, -29, -127, -95, + -29, -127, -81, -26, -105, -91, -26, -100, -84, -24, -86, -98, -29, + -126, -110, -24, -86, -83, -29, -126, -127, -29, -127, -66, -29, -127, + -103, -29, -127, -117, -29, -128, -126) + + val r = new InputStreamReader(new ByteArrayInputStream(buf)) + + def expectRead(str: String) = { + val buf = new Array[Char](str.length) + @tailrec + def readAll(readSoFar: Int): Int = { + if (readSoFar == buf.length) readSoFar + else { + val newlyRead = r.read(buf, readSoFar, buf.length - readSoFar) + if (newlyRead == -1) readSoFar + else readAll(readSoFar + newlyRead) + } + } + expect(readAll(0)).toBe(str.length) + expect(new String(buf)).toEqual(str) + } + + expectRead("Hello World.") + expectRead("こんにちは") + expectRead("日本語を読めますか。") + expect(r.read()).toBe(-1) + + } + + } + +} diff --git a/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/ReferenceTest.scala b/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/ReferenceTest.scala new file mode 100644 index 0000000..cf0fd0c --- /dev/null +++ b/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/ReferenceTest.scala @@ -0,0 +1,28 @@ +/* __ *\ +** ________ ___ / / ___ __ ____ Scala.js Test Suite ** +** / __/ __// _ | / / / _ | __ / // __/ (c) 2013, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \ http://scala-js.org/ ** +** /____/\___/_/ |_/____/_/ | |__/ /____/ ** +** |/____/ ** +\* */ +package scala.scalajs.testsuite.javalib + +import org.scalajs.jasminetest.JasmineTest + +object ReferenceTest extends JasmineTest { + + describe("java.land.ref.Reference") { + + it("Should have all the normal operations") { + val s = "string" + val ref = new java.lang.ref.WeakReference(s) + expect(ref.get).toEqual(s) + expect(ref.enqueue).toEqual(false) + expect(ref.isEnqueued).toEqual(false) + ref.clear + // can't use `expect` because it tries to be clever and .toString things, + // which makes it blow up when you pass in null + assert(ref.get == null) + } + } +} diff --git a/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/RegexTest.scala b/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/RegexTest.scala new file mode 100644 index 0000000..a27584a --- /dev/null +++ b/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/RegexTest.scala @@ -0,0 +1,397 @@ +/* __ *\ +** ________ ___ / / ___ __ ____ Scala.js Test Suite ** +** / __/ __// _ | / / / _ | __ / // __/ (c) 2013, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \ http://scala-js.org/ ** +** /____/\___/_/ |_/____/_/ | |__/ /____/ ** +** |/____/ ** +\* */ +package scala.scalajs.testsuite.javalib + +import scala.scalajs.js +import scala.scalajs.js.JSConverters._ + +import org.scalajs.jasminetest.JasmineTest + +import java.util.regex.Pattern + +object RegexTest extends JasmineTest { + + describe("java.util.regex.Pattern") { + + it("should respond to `matches`") { + expect(Pattern.matches("[Scal]*\\.js", "Scala.js")).toBeTruthy + expect(Pattern.matches(".[cal]*\\.j.", "Scala.js")).toBeTruthy + expect(Pattern.matches(".*\\.js", "Scala.js")).toBeTruthy + expect(Pattern.matches("S[a-z]*", "Scala.js")).toBeFalsy + } + + it("should respond to `matches` with flags") { + matches("scala.js", "Scala.js") + matches("SCALA.JS", "Scala.js") + matches("waz*up", "WAZZZZZZZZZZZUP") + + def matches(regex: String, input: String): Unit = { + val result = Pattern.compile(regex, Pattern.CASE_INSENSITIVE).matcher(input) + expect(result.matches()).toBeTruthy + } + } + + it("should respond to `split`") { + val result = Pattern.compile("[aj]").split("Scala.js") + val expected = js.Array("Sc", "l", ".", "s") + expect(result.length).toEqual(4) + expect(result.toJSArray).toEqual(expected) + + // Tests from JavaDoc + split("boo:and:foo", ":", Array("boo", "and", "foo")) + split("boo:and:foo", "o", Array("b", "", ":and:f")) + + // Splitting the empty string must return 1 element - #987 + split("", "a", Array("")) + split("", "\\*", Array("")) + split("", "\n", Array("")) + split("", "", Array("")) + + // Should remove leading empty match under some conditions - #1171 + // These tests are "measured" on the JVM since the spec is unclear + split("abc", "(?=a)", Array("abc")) + split("abc", "(?=b)", Array("a", "bc")) + split("abc", "(?=a)|b", Array("", "a", "c")) + split("abc", "", Array("", "a", "b", "c")) + split("abc", "(?=a)|(?=b)", Array("", "a", "bc")) + split("abc", "(?=a)|(?=a)", Array("abc")) + split("abc", "(?=a|b)", Array("", "a", "bc")) + split("abc", "(?=a|d)", Array("abc")) + split("abc", "^d*", Array("abc")) + split("abc", "d*", Array("", "a", "b", "c")) + split("a", "", Array("", "a")) + split("a", "^d*", Array("a")) + split("a", "d*", Array("", "a")) + split("a", "(?=a)", Array("a")) + split("ab", "a", Array("", "b")) + + def split(input: String, regex: String, expected: Array[String]): Unit = { + val result = Pattern.compile(regex).split(input) + expect(result.toJSArray).toEqual(expected.toJSArray) + } + } + + it("should respond to `split` with limit") { + // Tests from JavaDoc + splitWithLimit("boo:and:foo", ":", 2, Array("boo", "and:foo")) + splitWithLimit("boo:and:foo", ":", 5, Array("boo", "and", "foo")) + splitWithLimit("boo:and:foo", ":", -2, Array("boo", "and", "foo")) + splitWithLimit("boo:and:foo", "o", 5, Array("b", "", ":and:f", "", "")) + splitWithLimit("boo:and:foo", "o", -2, Array("b", "", ":and:f", "", "")) + splitWithLimit("boo:and:foo", "o", 0, Array("b", "", ":and:f")) + + // Splitting the empty string must return 1 element - #987 + splitWithLimit("", "a", 0, Array("")) + splitWithLimit("", "\\*", 5, Array("")) + splitWithLimit("", "\n", -2, Array("")) + splitWithLimit("", "", 1, Array("")) + + // Should remove leading empty match under some conditions - #1171 + splitWithLimit("abc", "", 2, Array("", "abc")) + splitWithLimit("abc", "(?=a)", 2, Array("abc")) + splitWithLimit("ab", "a", 1, Array("ab")) + + def splitWithLimit(input: String, regex: String, limit: Int, expected: Array[String]): Unit = { + val result = Pattern.compile(regex).split(input, limit) + expect(result.toJSArray).toEqual(expected.toJSArray) + } + } + + it("should respond to `flags`") { + val pattern0 = Pattern.compile("a") + val pattern1 = Pattern.compile("a", 0) + val flags2 = Pattern.CASE_INSENSITIVE | Pattern.DOTALL + val pattern2 = Pattern.compile("a", flags2) + + expect(pattern0.flags).toEqual(0) + expect(pattern1.flags).toEqual(0) + expect(pattern2.flags).toEqual(flags2) + } + + it("should respond to `pattern` and `toString`") { + def checkPatternAndToString(regex: String): Unit = { + val pattern0 = Pattern.compile(regex) + expect(pattern0.pattern).toEqual(regex) + expect(pattern0.toString).toEqual(regex) + + val pattern1 = Pattern.compile(regex, Pattern.CASE_INSENSITIVE) + expect(pattern1.pattern).toEqual(regex) + expect(pattern1.toString).toEqual(regex) + } + + checkPatternAndToString("a*b+c") + checkPatternAndToString("\\S[(a1]a.js") + } + + it("should respond to `quote`") { + val splitWithQuote = Pattern.compile(Pattern.quote("$1&$2")).split("Scala$1&$2.js") + val splitNoQuote = Pattern.compile("$1&$2").split("Scala$1&$2.js") + expect(splitWithQuote.mkString).toEqual("Scala.js") + expect(splitNoQuote.mkString).toEqual("Scala$1&$2.js") + } + + } + + describe("java.util.regex.Matcher") { + + it("should respond to `find`") { + val matcher = Pattern.compile("a").matcher("Scala.js") + + expect(matcher.find()).toBeTruthy + expect(matcher.find()).toBeTruthy + expect(matcher.find()).toBeFalsy + expect(matcher.find(4)).toBeTruthy + expect(matcher.find()).toBeFalsy + } + + it("should respond to `start`, `end`, `group`, and `toMatchResult`") { + val matcher = Pattern.compile("\\s(([A-Za-z]{5}(hum)?).js)\\s").matcher("Write Scala.js everyday!") + + def checkGroup0(start: Int, end: Int, group: String) = + checkGroup(start, 5, end, 15, group, " Scala.js ") + + def checkGroup1(start: Int, end: Int, group: String) = + checkGroup(start, 6, end, 14, group, "Scala.js") + + def checkGroup2(start: Int, end: Int, group: String) = + checkGroup(start, 6, end, 11, group, "Scala") + + def checkGroup3(start: Int, end: Int, group: String) = + checkGroup(start, -1, end, -1, group, null) + + def checkGroup(start: Int, startExpected: Int, end: Int, endExpected: Int, + group: String, groupExpected: String): Unit = { + expect(start).toEqual(startExpected) + expect(end).toEqual(endExpected) + expect(group).toEqual(groupExpected) + } + + expect(matcher.find()).toBeTruthy + expect(matcher.groupCount).toEqual(3) + checkGroup0(matcher.start, matcher.end, matcher.group) + checkGroup0(matcher.start(0), matcher.end(0), matcher.group(0)) + checkGroup1(matcher.start(1), matcher.end(1), matcher.group(1)) + checkGroup2(matcher.start(2), matcher.end(2), matcher.group(2)) + checkGroup3(matcher.start(3), matcher.end(3), matcher.group(3)) + + val matchResult = matcher.toMatchResult + expect(matchResult.groupCount).toEqual(3) + checkGroup0(matchResult.start, matchResult.end, matchResult.group) + checkGroup0(matchResult.start(0), matchResult.end(0), matchResult.group(0)) + checkGroup1(matchResult.start(1), matchResult.end(1), matchResult.group(1)) + checkGroup2(matchResult.start(2), matchResult.end(2), matchResult.group(2)) + checkGroup3(matchResult.start(3), matchResult.end(3), matchResult.group(3)) + } + + it("should respond to `matches`") { + val matcher0 = Pattern.compile("S[a-z]+").matcher("Scala") + val matcher1 = Pattern.compile("S[a-z]+").matcher("Scala.js") + + expect(matcher0.matches()).toBeTruthy + expect(matcher1.matches()).toBeFalsy + } + + it("should respond to `reset`") { + val matcher = Pattern.compile("S[a-z]+").matcher("Scalable") + + expect(matcher.find()).toBeTruthy + expect(matcher.find()).toBeFalsy + matcher.reset() + expect(matcher.find()).toBeTruthy + } + + it("should respond to `reset(String)`") { + val matcher = Pattern.compile("S[a-z]+").matcher("Scalable") + + expect(matcher.matches()).toBeTruthy + matcher.reset("Scala.js") + expect(matcher.matches()).toBeFalsy + } + + it("should respond to `usePattern`") { + val patternNoDots = Pattern.compile("S[a-z]+") + val patternWithDots = Pattern.compile("S[a-z.]+") + + val matcher0 = patternNoDots.matcher("Scala.js") + expect(matcher0.matches()).toBeFalsy + matcher0.usePattern(patternWithDots) + expect(matcher0.matches()).toBeTruthy + + val matcher1 = patternWithDots.matcher("Scala.js") + expect(matcher1.matches()).toBeTruthy + matcher1.usePattern(patternNoDots) + expect(matcher1.matches()).toBeFalsy + } + + it("should respond to `lookingAt`") { + val matcher0 = Pattern.compile("S[a-z]+").matcher("Scala") + val matcher1 = Pattern.compile("S[a-z]+").matcher("Scala.js") + val matcher2 = Pattern.compile("[a-z]+").matcher("Scala.js") + + expect(matcher0.lookingAt()).toBeTruthy + expect(matcher1.lookingAt()).toBeTruthy + expect(matcher2.lookingAt()).toBeFalsy + + val matcher3 = Pattern.compile("S[a-z]+").matcher("Scala.js") + expect(matcher3.find()).toBeTruthy + expect(matcher3.lookingAt()).toBeTruthy + } + + it("should respond to `hitEnd`") { + val matcher0 = Pattern.compile("S[a-z]*").matcher("Scala.js") + expect(matcher0.find()).toBeTruthy + expect(matcher0.hitEnd).toBeFalsy + expect(matcher0.find()).toBeFalsy + expect(matcher0.hitEnd).toBeTruthy + + val matcher1 = Pattern.compile("[A-Za-z]+").matcher("Scala.js") + expect(matcher1.find()).toBeTruthy + expect(matcher1.hitEnd).toBeFalsy + expect(matcher1.group).toBe("Scala") + expect(matcher1.find()).toBeTruthy + expect(matcher1.hitEnd).toBeTruthy + expect(matcher1.group).toBe("js") + expect(matcher1.lookingAt()).toBeTruthy + expect(matcher1.group).toBe("Scala") + expect(matcher1.hitEnd).toBeFalsy + } + + it("should respond to `region`") { + val matcher0 = Pattern.compile("S[a-z]+").matcher("A Scalable Solution") + + val region0to3 = matcher0.region(0, 3) + expect(region0to3.regionStart).toBe(0) + expect(region0to3.regionEnd).toBe(3) + expect(region0to3.find()).toBeFalsy + + val region0to15 = matcher0.region(0, 15) + expect(region0to15.regionStart).toBe(0) + expect(region0to15.regionEnd).toBe(15) + expect(region0to15.find()).toBeTruthy + expect(region0to15.group).toEqual("Scalable") + + val region2to7 = region0to15.region(2, 7) + expect(region2to7.regionStart).toBe(2) + expect(region2to7.regionEnd).toBe(7) + expect(region2to7.find()).toBeTruthy + expect(region2to7.group).toEqual("Scala") + + val region5toEnd = matcher0.region(5, matcher0.regionEnd) + expect(region5toEnd.regionStart).toBe(5) + expect(region5toEnd.regionEnd).toBe(19) + expect(region5toEnd.find()).toBeTruthy + expect(region5toEnd.group).toEqual("Solution") + + val matcher1 = Pattern.compile("0[xX][A-Fa-f0-9]{3}$").matcher("In CSS, 0xc4fe is not a color") + + val region5to13 = matcher1.region(5, 13) + expect(region5to13.regionStart).toBe(5) + expect(region5to13.regionEnd).toBe(13) + expect(region5to13.find()).toBeTruthy + expect(region5to13.group).toEqual("0xc4f") + + val region5to20 = matcher1.region(5, 20) + expect(region5to20.regionStart).toBe(5) + expect(region5to20.regionEnd).toBe(20) + expect(region5to20.find()).toBeFalsy + } + + it("should respond to `appendReplacement` and `appendTail`") { + // From the JavaDoc + val matcher = Pattern.compile("cat").matcher("one cat two cats in the yard") + val sb = new StringBuffer + + while (matcher.find()) { + matcher.appendReplacement(sb, "dog") + } + matcher.appendTail(sb) + + expect(sb.toString).toBe("one dog two dogs in the yard") + } + + it("should respond to `replaceAll`") { + // From the JavaDoc + val matcher = Pattern.compile("a*b").matcher("aabfooaabfooabfoob") + expect(matcher.replaceAll("-")).toBe("-foo-foo-foo-") + } + + it("should respond to `replaceFirst`") { + // From the JavaDoc + val matcher = Pattern.compile("dog").matcher("zzzdogzzzdogzzz") + expect(matcher.replaceFirst("cat")).toBe("zzzcatzzzdogzzz") + } + + it("should throw exception if match accessors are called before `find`") { + def checkInvalidAccess(block: => Unit): Unit = { + val exception: Throwable = try { + block + throw new Error("No exception thrown") + } catch { + case e: Throwable => e + } + + expect(exception.getClass.getName).toBe("java.lang.IllegalStateException") + expect(exception.getMessage).toBe("No match available") + } + + val matcher = Pattern.compile("(Sc([a-z]*))").matcher("Scala.js") + + checkInvalidAccess { matcher.start } + checkInvalidAccess { matcher.end } + checkInvalidAccess { matcher.group } + checkInvalidAccess { matcher.group(42) } + + val matchResult = matcher.toMatchResult + + checkInvalidAccess { matchResult.start } + checkInvalidAccess { matchResult.end } + checkInvalidAccess { matchResult.group } + checkInvalidAccess { matchResult.group(42) } + } + + it("should correctly handle zero-length matches") { + val pat = Pattern.compile("a*?") + val mat = pat.matcher("aaaaa") + for (i <- 0 to 5) { + expect(mat.find()).toBeTruthy + expect(mat.start).toEqual(i) + expect(mat.end).toEqual(i) + } + + // Make sure we don't suddenly re-match + for (i <- 0 to 5) { + expect(mat.find()).toBeFalsy + } + } + + it("should support in-pattern flags - #997") { + val p0 = Pattern.compile("(?i)abc") + + expect(p0.flags() & Pattern.CASE_INSENSITIVE).not.toBe(0) + + val m0 = p0.matcher("abcABC") + + expect(m0.find()).toBeTruthy + expect(m0.group()).toEqual("abc") + expect(m0.find()).toBeTruthy + expect(m0.group()).toEqual("ABC") + expect(m0.find()).toBeFalsy + + val p1 = Pattern.compile("(?-i)abc", Pattern.CASE_INSENSITIVE) + + expect(p1.flags() & Pattern.CASE_INSENSITIVE).toBe(0) + + val m1 = p1.matcher("abcABC") + + expect(m1.find()).toBeTruthy + expect(m1.group()).toEqual("abc") + expect(m1.find()).toBeFalsy + } + + } +} diff --git a/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/ShortTest.scala b/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/ShortTest.scala new file mode 100644 index 0000000..e59f2e9 --- /dev/null +++ b/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/ShortTest.scala @@ -0,0 +1,66 @@ +/* __ *\ +** ________ ___ / / ___ __ ____ Scala.js Test Suite ** +** / __/ __// _ | / / / _ | __ / // __/ (c) 2013, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \ http://scala-js.org/ ** +** /____/\___/_/ |_/____/_/ | |__/ /____/ ** +** |/____/ ** +\* */ +package scala.scalajs.testsuite.javalib + +import java.lang.{Short => JShort} + +import org.scalajs.jasminetest.JasmineTest + +/** + * tests the implementation of the java standard library Short + */ +object ShortTest extends JasmineTest { + + describe("java.lang.Short") { + + it("should provide `compareTo`") { + def compare(x: Short, y: Short): Int = + new JShort(x).compareTo(new JShort(y)) + + expect(compare(0.toShort, 5.toShort)).toBeLessThan(0) + expect(compare(10.toShort, 9.toShort)).toBeGreaterThan(0) + expect(compare(-2.toShort, -1.toShort)).toBeLessThan(0) + expect(compare(3.toShort, 3.toShort)).toEqual(0) + } + + it("should be a Comparable") { + def compare(x: Any, y: Any): Int = + x.asInstanceOf[Comparable[Any]].compareTo(y) + + expect(compare(0.toShort, 5.toShort)).toBeLessThan(0) + expect(compare(10.toShort, 9.toShort)).toBeGreaterThan(0) + expect(compare(-2.toShort, -1.toShort)).toBeLessThan(0) + expect(compare(3.toShort, 3.toShort)).toEqual(0) + } + + it("should parse strings") { + def test(s: String, v: Short): Unit = { + expect(JShort.parseShort(s)).toEqual(v) + expect(JShort.valueOf(s).shortValue()).toEqual(v) + expect(new JShort(s).shortValue()).toEqual(v) + } + + test("0", 0) + test("5", 5) + test("127", 127) + test("-100", -100) + test("30000", 30000) + } + + it("should reject invalid strings when parsing") { + def test(s: String): Unit = + expect(() => JShort.parseShort(s)).toThrow + + test("abc") + test("") + test("60000") // out of range + test("-90000") // out of range + } + + } +} diff --git a/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/StackTraceElementTest.scala b/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/StackTraceElementTest.scala new file mode 100644 index 0000000..b02a889 --- /dev/null +++ b/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/StackTraceElementTest.scala @@ -0,0 +1,29 @@ +/* __ *\ +** ________ ___ / / ___ __ ____ Scala.js Test Suite ** +** / __/ __// _ | / / / _ | __ / // __/ (c) 2013, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \ http://scala-js.org/ ** +** /____/\___/_/ |_/____/_/ | |__/ /____/ ** +** |/____/ ** +\* */ +package scala.scalajs.testsuite.javalib + +import scala.scalajs.js + +import org.scalajs.jasminetest.JasmineTest + +object StackTraceElementTest extends JasmineTest { + + describe("java.lang.StackTraceElement") { + it("should use the magic columnNumber field in its toString") { + val st = new StackTraceElement("MyClass", "myMethod", "myFile.scala", 1) + st.asInstanceOf[js.Dynamic].columnNumber = 5 + expect(st.toString).toEqual("MyClass.myMethod(myFile.scala:1:5)") + } + + it("should leave toString unmodified without magic columnNumber") { + val st = new StackTraceElement("MyClass", "myMethod", "myFile.scala", 1) + expect(st.toString).toEqual("MyClass.myMethod(myFile.scala:1)") + } + } + +} diff --git a/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/StreamsTest.scala b/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/StreamsTest.scala new file mode 100644 index 0000000..6975614 --- /dev/null +++ b/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/StreamsTest.scala @@ -0,0 +1,308 @@ +/* __ *\ +** ________ ___ / / ___ __ ____ Scala.js Test Suite ** +** / __/ __// _ | / / / _ | __ / // __/ (c) 2013, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \ http://scala-js.org/ ** +** /____/\___/_/ |_/____/_/ | |__/ /____/ ** +** |/____/ ** +\* */ +package scala.scalajs.testsuite.javalib + +import java.io._ + +import scala.language.implicitConversions + +import scala.scalajs.js + +import org.scalajs.jasminetest.JasmineTest + +/** Tests for our implementation of java.io._ stream classes */ +object StreamsTest extends JasmineTest with CommonStreamsTests { + + // Need to define this again, otherwise conversion on function + // triggers for Seqs + override implicit def traversable2array[T]( + a: TraversableOnce[T]): js.Array[T] = super.traversable2array(a) + + describe("java.io.InputStream") { + + class DummyInputStream(val length: Int) extends InputStream { + private var i: Int = 0 + def read(): Int = if (i < length) { i += 1; i } else -1 + } + + it("should provide a default implementation of `read` to an array") { + val stream = new DummyInputStream(200) + + val buf = new Array[Byte](50) + + // Should read first 50 bytes (next: 51) + expect(stream.read(buf)).toBe(50) + expect(buf).toEqual((1 to 50)) + + // Should read another 20 (next: 71) + expect(stream.read(buf, 10, 20)).toBe(20) + expect(buf).toEqual((1 to 10) ++ (51 to 70) ++ (31 to 50)) + + // Test some Exception conditions + expect(() => stream.read(null, 0, 10)).toThrow + expect(() => stream.read(buf, -1, 10)).toThrow + expect(() => stream.read(buf, 0, -1)).toThrow + expect(() => stream.read(buf, 10, 100)).toThrow + // Buffer should be unmodified + expect(buf).toEqual((1 to 10) ++ (51 to 70) ++ (31 to 50)) + + // Should read nothing (next: 71) + expect(stream.read(buf, 10, 0)).toBe(0) + expect(buf).toEqual((1 to 10) ++ (51 to 70) ++ (31 to 50)) + + // Skip 40 bytes (next: 111) + expect(stream.skip(40)).toBe(40) + + // Read 50 bytes, should wrap (next: 161) + expect(stream.read(buf)).toBe(50) + expect(buf).toEqual((111 to 127) ++ (-128 to -96)) + + // Read 45 bytes, should read 40 (next: EOF) + expect(stream.read(buf, 5, 45)).toBe(40) + expect(buf).toEqual((111 to 115) ++ (-95 to -56) ++ (-100 to -96)) + + // Read 50 bytes, should read nothing + expect(stream.read(buf)).toBe(-1) + expect(stream.read(buf, 0, 0)).toBe(0) + expect(buf).toEqual((111 to 115) ++ (-95 to -56) ++ (-100 to -96)) + } + + it("should provide a default implementation of `skip`") { + val stream = new DummyInputStream(10) + + expect(stream.skip(5)).toBe(5) + expect(stream.read()).toBe(6) + expect(stream.skip(1)).toBe(1) + expect(stream.read()).toBe(8) + expect(stream.skip(-5)).toBe(0) + expect(stream.read()).toBe(9) + expect(stream.skip(0)).toBe(0) + expect(stream.read()).toBe(10) + expect(stream.skip(10)).toBe(0) + } + + } + + describe("java.io.ByteArrayInputStream") { + byteArrayInputStreamLikeTests(seq => + new ByteArrayInputStream(seq.map(_.toByte).toArray)) + } + + describe("java.io.ByteArrayOutputStream") { + + it("should support simple write(x: Int)") { + val out = new ByteArrayOutputStream() + + for (i <- 0 to 9) + out.write(i) + + expect(out.toByteArray).toEqual(js.Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)) + } + + it("should support simple write(x: Array[Byte])") { + val out = new ByteArrayOutputStream() + val arr = Array[Byte](0, 1, 2, 3, 4, 5) + + out.write(arr, 1, 4) + out.write(arr) + + expect(out.toByteArray).toEqual(js.Array(1, 2, 3, 4, 0, 1, 2, 3, 4, 5)) + } + + it("should support write(x: Array[Byte]) with buffer resize") { + val out = new ByteArrayOutputStream(16) + val arr = Array[Byte](0, 1, 2, 3, 4, 5, 6, 7, 8, 9) + + out.write(arr) + out.write(arr) + + expect(out.toByteArray).toEqual(arr ++ arr) + } + + it("should support toString (with UTF8)") { + val buf = Array[Byte](72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, + 46, -29, -127, -109, -29, -126, -109, -29, -127, -85, -29, -127, -95, + -29, -127, -81, -26, -105, -91, -26, -100, -84, -24, -86, -98, -29, + -126, -110, -24, -86, -83, -29, -126, -127, -29, -127, -66, -29, -127, + -103, -29, -127, -117, -29, -128, -126) + + val out = new ByteArrayOutputStream() + out.write(buf) + + expect(out.toString).toEqual("Hello World.こんにちは日本語を読めますか。") + } + + it("should support reset()") { + val out = new ByteArrayOutputStream() + for (i <- 0 to 9) out.write(i) + out.reset() + for (i <- 0 to 9) out.write(i) + + expect(out.toByteArray).toEqual(js.Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)) + } + + } + +} + +/** tests also used by typedarray.ArrayBufferInputStreamTests */ +trait CommonStreamsTests extends JasmineTest { + + implicit def traversable2array[T](a: TraversableOnce[T]): js.Array[T] = { + import js.JSConverters._ + a.toJSArray + } + + implicit def array2array[T](a: Array[T]): js.Array[T] = { + import js.JSConverters._ + a.toJSArray + } + + def byteArrayInputStreamLikeTests(mkStream: Seq[Int] => InputStream): Unit = { + val length = 50 + def newStream = mkStream(1 to length) + + it("should provide `read()`") { + val stream = newStream + + for (i <- 1 to length) + expect(stream.read()).toBe(i) + + for (_ <- 1 to 5) + expect(stream.read()).toBe(-1) + } + + it("should provide `read(buf)`") { + val stream = newStream + val buf = new Array[Byte](10) + + expect(stream.read(buf)).toBe(10) + expect(buf).toEqual(1 to 10) + + expect(stream.skip(35)).toBe(35) + + expect(stream.read(buf)).toBe(5) + expect(buf).toEqual((46 to 50) ++ (6 to 10)) + + expect(stream.read(buf)).toBe(-1) + expect(stream.read()).toBe(-1) + } + + it("should provide full-argument `read`") { + val stream = newStream + val buf = new Array[Byte](20) + + expect(stream.read(buf, 10, 5)).toBe(5) + expect(buf).toEqual(Seq.fill(10)(0) ++ (1 to 5) ++ Seq.fill(5)(0)) + + expect(stream.read(buf, 0, 20)).toBe(20) + expect(buf).toEqual(6 to 25) + + expect(stream.read(buf, 10, 0)).toBe(0) + expect(buf).toEqual(6 to 25) + + expect(() => stream.read(buf, -1, 0)).toThrow + expect(() => stream.read(buf, 0, -1)).toThrow + expect(() => stream.read(buf, 100, 0)).toThrow + expect(() => stream.read(buf, 10, 100)).toThrow + expect(buf).toEqual(6 to 25) + + expect(stream.skip(20)).toBe(20) + + expect(stream.read(buf, 0, 10)).toBe(5) + expect(buf).toEqual((46 to 50) ++ (11 to 25)) + + expect(stream.read(buf, 0, 10)).toBe(-1) + expect(stream.read(buf, 0, 0)).toBe(0) + expect(buf).toEqual((46 to 50) ++ (11 to 25)) + + } + + it("should provide `available`") { + val stream = newStream + + def mySkip(n: Int) = for (_ <- 1 to n) expect(stream.read()).not.toBe(-1) + def check(n: Int) = expect(stream.available).toBe(n) + + check(50) + mySkip(5) + check(45) + expect(stream.skip(10)).toBe(10) + check(35) + mySkip(30) + check(5) + expect(stream.skip(20)).toBe(5) + check(0) + } + + it("should provide `skip`") { + val stream = newStream + + expect(stream.skip(7)).toBe(7) + + for (i <- 8 to 32) + expect(stream.read()).toBe(i) + + expect(stream.skip(0)).toBe(0) + expect(stream.read()).toBe(33) + expect(stream.skip(-4)).toBe(0) + expect(stream.read()).toBe(34) + + expect(stream.skip(30)).toBe(16) + expect(stream.skip(30)).toBe(0) + } + + it("should return true from `markSupported`") { + expect(newStream.markSupported).toBe(true) + } + + it("should provide no-op `close`") { + val stream = newStream + + for (i <- 1 to length) { + stream.close() + expect(stream.read()).toBe(i) + } + } + + it("should provide `mark`/`reset`") { + val stream = newStream + + def read(range: Range) = for (i <- range) expect(stream.read()).toBe(i) + + read(1 to 10) + stream.reset() // mark must be 0 at creation + read(1 to 5) + stream.mark(length) + read(6 to 22) + stream.reset() + read(6 to 20) + stream.reset() + read(6 to 25) + stream.reset() + expect(stream.skip(40)).toBe(40) + stream.mark(length) + read(46 to 50) + stream.reset() + read(46 to 50) + stream.mark(length) + expect(stream.read()).toBe(-1) + stream.reset() + expect(stream.read()).toBe(-1) + } + + it("should return positive integers when calling read") { + val stream = mkStream(Seq(-1, -2, -3)) + expect(stream.read()).toBe(255) + expect(stream.read()).toBe(254) + expect(stream.read()).toBe(253) + expect(stream.read()).toBe(-1) + } + } + +} diff --git a/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/StringBufferTest.scala b/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/StringBufferTest.scala new file mode 100644 index 0000000..a034ce6 --- /dev/null +++ b/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/StringBufferTest.scala @@ -0,0 +1,219 @@ +/* __ *\ +** ________ ___ / / ___ __ ____ Scala.js Test Suite ** +** / __/ __// _ | / / / _ | __ / // __/ (c) 2013, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \ http://scala-js.org/ ** +** /____/\___/_/ |_/____/_/ | |__/ /____/ ** +** |/____/ ** +\* */ +package scala.scalajs.testsuite.javalib + +import scala.reflect.{classTag, ClassTag} +import scala.scalajs.js +import org.scalajs.jasminetest.JasmineTest + +object StringBufferTest extends JasmineTest { + + def shouldThrow[T : ClassTag](fn: => Unit) = + try { + fn + expect("exception").toBe("thrown") + } catch { + case e: T => + case x: Throwable => expect(x.toString).toBe(classTag[T].runtimeClass.getSimpleName) + } + + describe("java.lang.StringBuffer") { + + def newBuf = new java.lang.StringBuffer + def initBuf(str: String) = new java.lang.StringBuffer(str) + + it("should respond to `append`") { + expect(newBuf.append("asdf").toString).toEqual("asdf") + expect(newBuf.append(null: AnyRef).toString).toEqual("null") + expect(newBuf.append(null: String).toString).toEqual("null") + expect(newBuf.append(null: CharSequence,0,2).toString).toEqual("nu") + expect(newBuf.append(js.undefined).toString).toEqual("undefined") + expect(newBuf.append(true).toString).toEqual("true") + expect(newBuf.append('a').toString).toEqual("a") + expect(newBuf.append(Array('a','b','c','d')).toString).toEqual("abcd") + expect(newBuf.append(Array('a','b','c','d'), 1, 2).toString).toEqual("bc") + expect(newBuf.append(4.toByte).toString).toEqual("4") + expect(newBuf.append(304.toShort).toString).toEqual("304") + expect(newBuf.append(100000).toString).toEqual("100000") + expect(newBuf.append(2.5f).toString).toEqual("2.5") + expect(newBuf.append(3.5).toString).toEqual("3.5") + } + + it("should respond to `insert`") { + expect(newBuf.insert(0, "asdf").toString).toEqual("asdf") + expect(newBuf.insert(0, null: AnyRef).toString).toEqual("null") + expect(newBuf.insert(0, null: String).toString).toEqual("null") + expect(newBuf.insert(0, null: CharSequence,0,2).toString).toEqual("nu") + expect(newBuf.insert(0, js.undefined).toString).toEqual("undefined") + expect(newBuf.insert(0, true).toString).toEqual("true") + expect(newBuf.insert(0, 'a').toString).toEqual("a") + expect(newBuf.insert(0, Array('a','b','c','d')).toString).toEqual("abcd") + expect(newBuf.insert(0, Array('a','b','c','d'), 1, 2).toString).toEqual("bc") + expect(newBuf.insert(0, 4.toByte).toString).toEqual("4") + expect(newBuf.insert(0, 304.toShort).toString).toEqual("304") + expect(newBuf.insert(0, 100000).toString).toEqual("100000") + expect(newBuf.insert(0, 2.5f).toString).toEqual("2.5") + expect(newBuf.insert(0, 3.5).toString).toEqual("3.5") + + expect(initBuf("adef").insert(1, "bc")).toEqual("abcdef") + expect(initBuf("abcd").insert(4, "ef")).toEqual("abcdef") + expect(initBuf("adef").insert(1, Array('b','c'))).toEqual("abcdef") + expect(initBuf("adef").insert(1, initBuf("bc"))).toEqual("abcdef") + expect(initBuf("abef").insert(2, Array('a','b','c','d','e'), 2, 2)).toEqual("abcdef") + expect(initBuf("abef").insert(2, initBuf("abcde"), 2, 4)).toEqual("abcdef") + + shouldThrow[StringIndexOutOfBoundsException](initBuf("abcd").insert(5, "whatever")) + shouldThrow[StringIndexOutOfBoundsException](initBuf("abcd").insert(-1, "whatever")) + } + + it("should respond to `deleteCharAt`") { + expect(initBuf("0123").deleteCharAt(1).toString).toEqual("023") + expect(initBuf("0123").deleteCharAt(0).toString).toEqual("123") + expect(initBuf("0123").deleteCharAt(3).toString).toEqual("012") + shouldThrow[StringIndexOutOfBoundsException](initBuf("0123").deleteCharAt(-1)) + shouldThrow[StringIndexOutOfBoundsException](initBuf("0123").deleteCharAt(4)) + } + + it("should respond to `replace`") { + expect(initBuf("0123").replace(1,3,"bc").toString).toEqual("0bc3") + expect(initBuf("0123").replace(0,4,"abcd").toString).toEqual("abcd") + expect(initBuf("0123").replace(0,10,"abcd").toString).toEqual("abcd") + expect(initBuf("0123").replace(3,10,"defg").toString).toEqual("012defg") + expect(initBuf("0123").replace(0,1,"xxxx").toString).toEqual("xxxx123") + expect(initBuf("0123").replace(1,1,"xxxx").toString).toEqual("0xxxx123") + + shouldThrow[StringIndexOutOfBoundsException](initBuf("0123").replace(-1,3,"x")) + shouldThrow[StringIndexOutOfBoundsException](initBuf("0123").replace(4,5,"x")) + } + + it("should respond to `setCharAt`") { + val buf = newBuf + buf.append("foobar") + + buf.setCharAt(2, 'x') + expect(buf.toString).toEqual("foxbar") + + buf.setCharAt(5, 'h') + expect(buf.toString).toEqual("foxbah") + + expect(() => buf.setCharAt(-1, 'h')).toThrow + expect(() => buf.setCharAt(6, 'h')).toThrow + } + + it("should properly setLength") { + val buf = newBuf + buf.append("foobar") + + expect(() => buf.setLength(-3)).toThrow + + expect({ buf.setLength(3); buf.toString }).toEqual("foo") + expect({ buf.setLength(6); buf.toString }).toEqual("foo\u0000\u0000\u0000") + } + + } + + describe("java.lang.StringBuilder") { + + def newBuilder = new java.lang.StringBuilder + def initBuilder(str: String) = new java.lang.StringBuilder(str) + + it("should respond to `append`") { + expect(newBuilder.append("asdf").toString).toEqual("asdf") + expect(newBuilder.append(null: AnyRef).toString).toEqual("null") + expect(newBuilder.append(null: String).toString).toEqual("null") + expect(newBuilder.append(null: CharSequence,0,2).toString).toEqual("nu") + expect(newBuilder.append(js.undefined).toString).toEqual("undefined") + expect(newBuilder.append(true).toString).toEqual("true") + expect(newBuilder.append('a').toString).toEqual("a") + expect(newBuilder.append(Array('a','b','c','d')).toString).toEqual("abcd") + expect(newBuilder.append(Array('a','b','c','d'), 1, 2).toString).toEqual("bc") + expect(newBuilder.append(4.toByte).toString).toEqual("4") + expect(newBuilder.append(304.toShort).toString).toEqual("304") + expect(newBuilder.append(100000).toString).toEqual("100000") + expect(newBuilder.append(2.5f).toString).toEqual("2.5") + expect(newBuilder.append(3.5).toString).toEqual("3.5") + } + + it("should respond to `insert`") { + expect(newBuilder.insert(0, "asdf").toString).toEqual("asdf") + expect(newBuilder.insert(0, null: AnyRef).toString).toEqual("null") + expect(newBuilder.insert(0, null: String).toString).toEqual("null") + expect(newBuilder.insert(0, null: CharSequence,0,2).toString).toEqual("nu") + expect(newBuilder.insert(0, js.undefined).toString).toEqual("undefined") + expect(newBuilder.insert(0, true).toString).toEqual("true") + expect(newBuilder.insert(0, 'a').toString).toEqual("a") + expect(newBuilder.insert(0, Array('a','b','c','d')).toString).toEqual("abcd") + expect(newBuilder.insert(0, Array('a','b','c','d'), 1, 2).toString).toEqual("bc") + expect(newBuilder.insert(0, 4.toByte).toString).toEqual("4") + expect(newBuilder.insert(0, 304.toShort).toString).toEqual("304") + expect(newBuilder.insert(0, 100000).toString).toEqual("100000") + expect(newBuilder.insert(0, 2.5f).toString).toEqual("2.5") + expect(newBuilder.insert(0, 3.5).toString).toEqual("3.5") + + expect(initBuilder("adef").insert(1, "bc")).toEqual("abcdef") + expect(initBuilder("abcd").insert(4, "ef")).toEqual("abcdef") + expect(initBuilder("adef").insert(1, Array('b','c'))).toEqual("abcdef") + expect(initBuilder("adef").insert(1, initBuilder("bc"))).toEqual("abcdef") + expect(initBuilder("abef").insert(2, Array('a','b','c','d','e'), 2, 2)).toEqual("abcdef") + expect(initBuilder("abef").insert(2, initBuilder("abcde"), 2, 4)).toEqual("abcdef") + + shouldThrow[StringIndexOutOfBoundsException](initBuilder("abcd").insert(5, "whatever")) + shouldThrow[StringIndexOutOfBoundsException](initBuilder("abcd").insert(-1, "whatever")) + } + + it("should allow string interpolation to survive `null` and `undefined`") { + expect(s"${null}").toEqual("null") + expect(s"${js.undefined}").toEqual("undefined") + } + + it("should respond to `deleteCharAt`") { + expect(initBuilder("0123").deleteCharAt(1).toString).toEqual("023") + expect(initBuilder("0123").deleteCharAt(0).toString).toEqual("123") + expect(initBuilder("0123").deleteCharAt(3).toString).toEqual("012") + shouldThrow[StringIndexOutOfBoundsException](initBuilder("0123").deleteCharAt(-1)) + shouldThrow[StringIndexOutOfBoundsException](initBuilder("0123").deleteCharAt(4)) + } + + it("should respond to `replace`") { + expect(initBuilder("0123").replace(1,3,"bc").toString).toEqual("0bc3") + expect(initBuilder("0123").replace(0,4,"abcd").toString).toEqual("abcd") + expect(initBuilder("0123").replace(0,10,"abcd").toString).toEqual("abcd") + expect(initBuilder("0123").replace(3,10,"defg").toString).toEqual("012defg") + expect(initBuilder("0123").replace(0,1,"xxxx").toString).toEqual("xxxx123") + expect(initBuilder("0123").replace(1,1,"xxxx").toString).toEqual("0xxxx123") + + shouldThrow[StringIndexOutOfBoundsException](initBuilder("0123").replace(-1,3,"x")) + shouldThrow[StringIndexOutOfBoundsException](initBuilder("0123").replace(4,5,"x")) + } + + it("should respond to `setCharAt`") { + val b = newBuilder + b.append("foobar") + + b.setCharAt(2, 'x') + expect(b.toString).toEqual("foxbar") + + b.setCharAt(5, 'h') + expect(b.toString).toEqual("foxbah") + + expect(() => b.setCharAt(-1, 'h')).toThrow + expect(() => b.setCharAt(6, 'h')).toThrow + } + + it("should properly setLength") { + val b = newBuilder + b.append("foobar") + + expect(() => b.setLength(-3)).toThrow + + expect({ b.setLength(3); b.toString }).toEqual("foo") + expect({ b.setLength(6); b.toString }).toEqual("foo\u0000\u0000\u0000") + } + + } +} diff --git a/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/StringTest.scala b/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/StringTest.scala new file mode 100644 index 0000000..a49e521 --- /dev/null +++ b/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/StringTest.scala @@ -0,0 +1,237 @@ +/* __ *\ +** ________ ___ / / ___ __ ____ Scala.js Test Suite ** +** / __/ __// _ | / / / _ | __ / // __/ (c) 2013, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \ http://scala-js.org/ ** +** /____/\___/_/ |_/____/_/ | |__/ /____/ ** +** |/____/ ** +\* */ +package scala.scalajs.testsuite.javalib + +import scala.scalajs.js +import scala.scalajs.js.JSConverters._ + +import org.scalajs.jasminetest.JasmineTest + +object StringTest extends JasmineTest { + + describe("java.lang.String") { + + it("should respond to `length`") { + expect("Scala.js".length).toEqual(8) + expect("".length).toEqual(0) + } + + it("should respond to `intern`") { + val s = "Scala.js" + expect(s.intern).toEqual(s) + } + + it("should respond to `equals`") { + expect("Scala.js".equals("Scala.js")).toBeTruthy + expect("Scala.js".equals("Java")).toBeFalsy + } + + it("should respond to `equalsIgnoreCase`") { + expect("Scala.JS".equalsIgnoreCase("Scala.js")).toBeTruthy + expect("åløb".equalsIgnoreCase("ÅLØb")).toBeTruthy + expect("Scala.js".equalsIgnoreCase("Java")).toBeFalsy + expect("Scala.js".equalsIgnoreCase(null)).toBeFalsy + } + + it("should respond to `compareTo`") { + expect("Scala.js".compareTo("Scala")).toBeGreaterThan(0) + expect("Scala.js".compareTo("Scala.js")).toBe(0) + expect("Scala.js".compareTo("banana")).toBeLessThan(0) + } + + it("should respond to `compareToIgnoreCase`") { + expect("Scala.JS".compareToIgnoreCase("Scala.js")).toBe(0) + expect("Scala.JS".compareToIgnoreCase("scala")).toBeGreaterThan(0) + expect("åløb".compareToIgnoreCase("ÅLØB")).toBe(0) + expect("Java".compareToIgnoreCase("Scala")).toBeLessThan(0) + } + + it("should respond to `isEmpty`") { + expect("Scala.js".isEmpty).toBeFalsy + expect("".isEmpty).toBeTruthy + } + + it("should respond to `contains`") { + expect("Scala.js".contains("Scala")).toBeTruthy + expect("Scala.js".contains("Scala.js")).toBeTruthy + expect("ananas".contains("na")).toBeTruthy + expect("Scala.js".contains("scala")).toBeFalsy + } + + it("should respond to `startWith`") { + expect("Scala.js".startsWith("Scala")).toBeTruthy + expect("Scala.js".startsWith("Scala.js")).toBeTruthy + expect("Scala.js".startsWith("scala")).toBeFalsy + expect("ananas".startsWith("an")).toBeTruthy + } + + it("should respond to `endsWith`") { + expect("Scala.js".endsWith("js")).toBeTruthy + expect("Scala.js".endsWith("Scala.js")).toBeTruthy + expect("Scala.js".endsWith("JS")).toBeFalsy + expect("banana".endsWith("na")).toBeTruthy + } + + it("should respond to `indexOf(String)`") { + expect("Scala.js".indexOf("js")).toBe(6) + expect("Scala.js".indexOf("Scala.js")).toBe(0) + expect("ananas".indexOf("na")).toBe(1) + expect("Scala.js".indexOf("Java")).toBe(-1) + } + + it("should respond to `indexOf(int)`") { + expect("abc\uD834\uDF06def\uD834\uDF06def".indexOf(0x61)).toEqual(0) + expect("abc\uD834\uDF06def\uD834\uDF06def".indexOf(0x1D306)).toEqual(3) + expect("abc\uD834\uDF06def\uD834\uDF06def".indexOf(0xD834)).toEqual(3) + expect("abc\uD834\uDF06def\uD834\uDF06def".indexOf(0xDF06)).toEqual(4) + expect("abc\uD834\uDF06def\uD834\uDF06def".indexOf(0x64)).toEqual(5) + } + + it("should respond to `lastIndexOf(String)`") { + expect("Scala.js".lastIndexOf("Scala.js")).toBe(0) + expect("ananas".lastIndexOf("na")).toBe(3) + expect("Scala.js".lastIndexOf("Java")).toBe(-1) + } + + it("should respond to `lastIndexOf(int)`") { + expect("abc\uD834\uDF06def\uD834\uDF06def".lastIndexOf(0x61)).toEqual(0) + expect("abc\uD834\uDF06def\uD834\uDF06def".lastIndexOf(0x1D306)).toEqual(8) + expect("abc\uD834\uDF06def\uD834\uDF06def".lastIndexOf(0xD834)).toEqual(8) + expect("abc\uD834\uDF06def\uD834\uDF06def".lastIndexOf(0xDF06)).toEqual(9) + expect("abc\uD834\uDF06def\uD834\uDF06def".lastIndexOf(0x64)).toEqual(10) + } + + it("should respond to `toUpperCase`") { + expect("Scala.js".toUpperCase()).toBe("SCALA.JS") + } + + it("should respond to `toLowerCase`") { + expect("Scala.js".toLowerCase()).toBe("scala.js") + } + + it("should respond to `charAt`") { + expect("Scala.js".charAt(5)).toBe('.') + expect("Scala.js".charAt(6)).not.toBe('.') + } + + when("compliant-asinstanceofs"). + it("charAt() should throw with out-of-bound indices") { + // Type Strings both as CharSequence and String. One will invoke the + // helper, the other directly the method on RuntimeString. + expect(() => ("Scala.js": CharSequence).charAt(-3)).toThrow + expect(() => ("Scala.js": CharSequence).charAt(20)).toThrow + expect(() => "Scala.js".charAt(-3)).toThrow + expect(() => "Scala.js".charAt(20)).toThrow + } + + it("should respond to `codePointAt`") { + // String that starts with a BMP symbol + expect("abc\uD834\uDF06def".codePointAt(0)).toEqual(0x61) + expect("abc\uD834\uDF06def".codePointAt(3)).toEqual(0x1D306) + expect("abc\uD834\uDF06def".codePointAt(4)).toEqual(0xDF06) + expect("abc\uD834\uDF06def".codePointAt(5)).toEqual(0x64) + + // String that starts with an astral symbol + expect("\uD834\uDF06def".codePointAt(0)).toEqual(0x1D306) + expect("\uD834\uDF06def".codePointAt(1)).toEqual(0xDF06) + + // Lone high surrogates + expect("\uD834abc".codePointAt(0)).toEqual(0xD834) + + // Lone low surrogates + expect("\uDF06abc".codePointAt(0)).toEqual(0xDF06) + } + + it("should respond to `subSequence`") { + expect("Scala.js".subSequence(0, 5)).toBe("Scala") + expect("Scala.js".subSequence(6, 8)).toBe("js") + expect("Scala.js".subSequence(3, 5)).toBe("la") + expect("Scala.js".subSequence(3, 3)).toBe("") + } + + it("should respond to `replace`") { + expect("Scala.js".replace(".js", "")).toBe("Scala") + expect("Scala.js".replace("JS", "")).toBe("Scala.js") + expect("aa".replace('a', 'b')).toBe("bb") // #25 + } + + it("should respond to `matches`") { + expect("Scala.js".matches(".*js")).toBeTruthy + expect("Scala.js".matches(".*JS")).toBeFalsy + } + + it("should respond to `split`") { + expect("Scala.js".split("a").toJSArray).toEqual(js.Array("Sc", "l", ".js")) + expect("asdf".split("").toJSArray).toEqual(js.Array("","a","s","d","f")) + expect("asdf".split("", -1).toJSArray).toEqual(js.Array("","a","s","d","f", "")) + } + + it("should respond to `split` with char as argument") { + expect("Scala.js".split('.').toJSArray).toEqual(js.Array("Scala","js")) + for (i <- 0 to 32) { + val c = i.toChar + expect(s"blah${c}blah${c}blah${c}blah".split(c).toJSArray).toEqual( + js.Array("blah", "blah", "blah", "blah")) + } + } + + it("should respond to `toCharArray`") { + expect("Scala.js".toCharArray()(5)).toEqual('.') + } + + it("should respond to `hashCode`") { + expect("a`jkxzcbfaslkjfbkj,289oinkasdf".hashCode()).toEqual(-1395193631) + expect("-34".hashCode()).toEqual(44878) + expect("".hashCode()).toEqual(0) + } + + it("should respond to `getChars`") { + val trg = new Array[Char](10) + "asdf_foo".getChars(2, 6, trg, 3) + val exp = Array(0,0,0,'d','f','_','f',0,0,0) + + for ((i,e) <- trg zip exp) { + expect(i).toEqual(e) + } + } + + + it("should respond to `concat`") { + expect("asdf".concat("fdsa")).toEqual("asdffdsa") + } + + it("should respond to constructors") { + val charArray = + Array('a', 'b', 'c', 'd', '\uD834', '\uDF06', 'e', 'f', 'g', 'h', 'i') + val codePointArray = + Array(65, 0x1D306, 67, 68, 0xD834, 69, 72, 0xDF06) + + expect(new String()).toEqual("") + expect(new String(charArray)).toEqual("abcd\uD834\uDF06efghi") + expect(new String(charArray, 3, 5)).toEqual("d\uD834\uDF06ef") + expect(new String(codePointArray, 1, 5)).toEqual("\uD834\uDF06CD\uD834E") + expect(new String("foo")).toEqual("foo") + expect(new String(new StringBuffer("buffer-foo"))).toEqual("buffer-foo") + expect(new String(new java.lang.StringBuilder("builder-foo")) + ).toEqual("builder-foo") + } + + it("should provide `format`") { + expect(String.format("%d", new Integer(5))).toEqual("5") + expect(String.format("%05d", new Integer(5))).toEqual("00005") + expect(String.format("%0#5x", new Integer(5))).toEqual("0x005") + expect(String.format("%#5x", new Integer(5))).toEqual(" 0x5") + expect(String.format("%#5X", new Integer(5))).toEqual(" 0X5") + expect(String.format("%5d", new Integer(-10))).toEqual(" -10") + expect(String.format("%05d", new Integer(-10))).toEqual("-0010") + expect(String.format("%x", new Integer(-3))).toEqual("fffffffd") + expect(String.format("%x", new java.lang.Byte(-4.toByte))).toEqual("fffffffc") + } + + } +} diff --git a/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/SystemTest.scala b/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/SystemTest.scala new file mode 100644 index 0000000..4435e00 --- /dev/null +++ b/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/SystemTest.scala @@ -0,0 +1,118 @@ +/* __ *\ +** ________ ___ / / ___ __ ____ Scala.js Test Suite ** +** / __/ __// _ | / / / _ | __ / // __/ (c) 2013, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \ http://scala-js.org/ ** +** /____/\___/_/ |_/____/_/ | |__/ /____/ ** +** |/____/ ** +\* */ +package scala.scalajs.testsuite.javalib + +import language.implicitConversions + +import scala.scalajs.js +import scala.scalajs.js.JSConverters._ + +import org.scalajs.jasminetest.JasmineTest + +object SystemTest extends JasmineTest { + + // Just in here, we allow ourselves to do this + implicit def array2jsArray[T](arr: Array[T]): js.Array[T] = arr.toJSArray + + describe("java.lang.System") { + + it("should respond to `arraycopy`") { + val object0 = Array[Any]("[", "b", "c", "d", "e", "f", "]") + val object1 = Array[Any](() => true, 1, "2", '3', 4.0, true, object0) + + System.arraycopy(object1, 1, object0, 1, 5) + expect(object0.mkString).toEqual("[1234true]") + + val string0 = Array("a", "b", "c", "d", "e", "f") + val string1 = Array("1", "2", "3", "4") + + System.arraycopy(string1, 0, string0, 3, 3) + expect(string0.mkString).toEqual("abc123") + + val ab01Chars = Array("ab".toCharArray, "01".toCharArray) + val chars = new Array[Array[Char]](32) + System.arraycopy(ab01Chars, 0, chars, 0, 2) + for (i <- Seq(0, 2, 4, 8, 16)) { + System.arraycopy(chars, i / 4, chars, i, i) + } + + expect(chars.filter(_ == null).length).toEqual(12) + expect(chars.filter(_ != null).map(_.mkString).mkString). + toEqual("ab01ab0101ab01ab0101ab0101ab01ab0101ab01") + } + + it("should respond to `arraycopy` with range overlaps for the same array") { + val array = new Array[Int](10) + + for (i <- 1 to 6) { + array(i) = i + } + + expect(array).toEqual(Array(0, 1, 2, 3, 4, 5, 6, 0, 0, 0)) + System.arraycopy(array, 0, array, 3, 7) + expect(array).toEqual(Array(0, 1, 2, 0, 1, 2, 3, 4, 5, 6)) + + System.arraycopy(array, 0, array, 1, 0) + expect(array).toEqual(Array(0, 1, 2, 0, 1, 2, 3, 4, 5, 6)) + + System.arraycopy(array, 0, array, 1, 9) + expect(array).toEqual(Array(0, 0, 1, 2, 0, 1, 2, 3, 4, 5)) + + System.arraycopy(array, 1, array, 0, 9) + expect(array).toEqual(Array(0, 1, 2, 0, 1, 2, 3, 4, 5, 5)) + + System.arraycopy(array, 0, array, 0, 10) + expect(array).toEqual(Array(0, 1, 2, 0, 1, 2, 3, 4, 5, 5)) + + val reversed = array.reverse + System.arraycopy(reversed, 5, array, 5, 5) + expect(array).toEqual(Array(0, 1, 2, 0, 1, 1, 0, 2, 1, 0)) + } + + it("should provide identityHashCode") { + /* This test is more restrictive than the spec, but we know our + * implementation will always pass the test. + */ + class HasIDHashCode + + val x1 = new HasIDHashCode + val x2 = new HasIDHashCode + val x1FirstHash = x1.hashCode() + expect(x1.hashCode()).toEqual(x1FirstHash) + expect(x1.hashCode()).not.toEqual(x2.hashCode()) + expect(x1.hashCode()).toEqual(x1FirstHash) + + expect(System.identityHashCode(x1)).toEqual(x1FirstHash) + expect(System.identityHashCode(x2)).toEqual(x2.hashCode()) + } + + it("identityHashCode should by-pass .hashCode()") { + val list1 = List(1, 3, 5) + val list2 = List(1, 3, 5) + expect(list1 == list2).toBeTruthy + expect(list1.hashCode()).toEqual(list2.hashCode()) + expect(System.identityHashCode(list1)).not.toEqual(System.identityHashCode(list2)) + } + + it("identityHashCode(null)") { + expect(System.identityHashCode(null)).toEqual(0) + } + + it("identityHashCode of values implemented as JS primitives") { + expect(System.identityHashCode("foo")).toEqual("foo".hashCode()) + expect(System.identityHashCode("")).toEqual("".hashCode()) + + expect(System.identityHashCode(false)).toEqual(false.hashCode()) + expect(System.identityHashCode(true)).toEqual(true.hashCode()) + + expect(System.identityHashCode(5)).toEqual(5.hashCode()) + expect(System.identityHashCode(789456)).toEqual(789456.hashCode()) + } + + } +} diff --git a/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/ThrowablesTest.scala b/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/ThrowablesTest.scala new file mode 100644 index 0000000..7e53975 --- /dev/null +++ b/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/ThrowablesTest.scala @@ -0,0 +1,90 @@ +/* __ *\ +** ________ ___ / / ___ __ ____ Scala.js Test Suite ** +** / __/ __// _ | / / / _ | __ / // __/ (c) 2013, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \ http://scala-js.org/ ** +** /____/\___/_/ |_/____/_/ | |__/ /____/ ** +** |/____/ ** +\* */ +package scala.scalajs.testsuite.javalib + +import org.scalajs.jasminetest.JasmineTest + +object ThrowablesTest extends JasmineTest { + describe("java.lang.Throwables, java.util.Throwables") { + + it("should define all java.lang and java.util Errors/Exceptions") { + new ArithmeticException() + new ArrayIndexOutOfBoundsException() + new ArrayStoreException() + new ClassCastException() + new ClassNotFoundException() + new CloneNotSupportedException() + // Needs an instance of java.lang.Enum. + // import scala.language.existentials + // new EnumConstantNotPresentException(null.asInstanceOf[Class[_ <: Enum[T] forSome { type T <: Enum[T] }]], null) + new Exception() + new IllegalAccessException() + new IllegalArgumentException() + new IllegalMonitorStateException() + new IllegalStateException() + new IllegalThreadStateException() + new IndexOutOfBoundsException() + new InstantiationException() + new InterruptedException() + new NegativeArraySizeException() + new NoSuchFieldException() + new NoSuchMethodException() + new NullPointerException() + new NumberFormatException() + new RuntimeException() + new SecurityException() + new StringIndexOutOfBoundsException() + new TypeNotPresentException(null, null) + new UnsupportedOperationException() + new AbstractMethodError() + new AssertionError() + new ClassCircularityError() + new ClassFormatError() + new Error() + new ExceptionInInitializerError() + new IllegalAccessError() + new IncompatibleClassChangeError() + new InstantiationError() + new InternalError() + new LinkageError() + new NoClassDefFoundError() + new NoSuchFieldError() + new NoSuchMethodError() + new OutOfMemoryError() + new StackOverflowError() + new UnknownError() + new UnsatisfiedLinkError() + new UnsupportedClassVersionError() + new VerifyError() + new VirtualMachineError() {} + + import java.util._ + new ServiceConfigurationError("") + new ConcurrentModificationException() + new DuplicateFormatFlagsException("") + new EmptyStackException() + new FormatFlagsConversionMismatchException("", '\u0000') + new FormatterClosedException() + new IllegalFormatCodePointException(0) + new IllegalFormatConversionException('\u0000', new Object().getClass) + new IllegalFormatFlagsException("") + new IllegalFormatPrecisionException(0) + new IllegalFormatWidthException(0) + new InputMismatchException() + // Needs java.io.IOException. + // new InvalidPropertiesFormatException("") + new MissingFormatArgumentException("") + new MissingFormatWidthException("") + new MissingResourceException(null, null, null) + new NoSuchElementException() + new TooManyListenersException() + new UnknownFormatConversionException("") + new UnknownFormatFlagsException("") + } + } +} diff --git a/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/TimeUnitTest.scala b/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/TimeUnitTest.scala new file mode 100644 index 0000000..79cd8a8 --- /dev/null +++ b/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/TimeUnitTest.scala @@ -0,0 +1,135 @@ +/* __ *\ +** ________ ___ / / ___ __ ____ Scala.js Test Suite ** +** / __/ __// _ | / / / _ | __ / // __/ (c) 2013, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \ http://scala-js.org/ ** +** /____/\___/_/ |_/____/_/ | |__/ /____/ ** +** |/____/ ** +\* */ +package scala.scalajs.testsuite.javalib + +import scala.scalajs.js +import org.scalajs.jasminetest.JasmineTest +import java.util.concurrent.TimeUnit + +object TimeUnitTest extends JasmineTest { + + describe("java.util.concurrent.TimeUnit") { + + it("should respond to `toNanos`") { + expect(TimeUnit.NANOSECONDS.toNanos(42L)).toEqual(42L) + expect(TimeUnit.MICROSECONDS.toNanos(42L)).toEqual(42000L) + expect(TimeUnit.MILLISECONDS.toNanos(42L)).toEqual(42000000L) + expect(TimeUnit.SECONDS.toNanos(42L)).toEqual(42000000000L) + expect(TimeUnit.MINUTES.toNanos(42L)).toEqual(2520000000000L) + expect(TimeUnit.HOURS.toNanos(42L)).toEqual(151200000000000L) + expect(TimeUnit.DAYS.toNanos(42L)).toEqual(3628800000000000L) + } + + it("should respond to `toMicros`") { + expect(TimeUnit.NANOSECONDS.toMicros(42L)).toEqual(0L) + expect(TimeUnit.NANOSECONDS.toMicros(42123L)).toEqual(42L) + expect(TimeUnit.MICROSECONDS.toMicros(42L)).toEqual(42L) + expect(TimeUnit.MILLISECONDS.toMicros(42L)).toEqual(42000L) + expect(TimeUnit.SECONDS.toMicros(42L)).toEqual(42000000L) + expect(TimeUnit.MINUTES.toMicros(42L)).toEqual(2520000000L) + expect(TimeUnit.HOURS.toMicros(42L)).toEqual(151200000000L) + expect(TimeUnit.DAYS.toMicros(42L)).toEqual(3628800000000L) + } + + it("should respond to `toMillis`") { + expect(TimeUnit.NANOSECONDS.toMillis(42L)).toEqual(0L) + expect(TimeUnit.NANOSECONDS.toMillis(42000123L)).toEqual(42L) + expect(TimeUnit.MICROSECONDS.toMillis(42L)).toEqual(0L) + expect(TimeUnit.MICROSECONDS.toMillis(42123L)).toEqual(42L) + expect(TimeUnit.MILLISECONDS.toMillis(42L)).toEqual(42L) + expect(TimeUnit.SECONDS.toMillis(42L)).toEqual(42000L) + expect(TimeUnit.MINUTES.toMillis(42L)).toEqual(2520000L) + expect(TimeUnit.HOURS.toMillis(42L)).toEqual(151200000L) + expect(TimeUnit.DAYS.toMillis(42L)).toEqual(3628800000L) + } + + it("should respond to `toSeconds`") { + expect(TimeUnit.NANOSECONDS.toSeconds(42L)).toEqual(0L) + expect(TimeUnit.NANOSECONDS.toSeconds(42000000123L)).toEqual(42L) + expect(TimeUnit.MICROSECONDS.toSeconds(42L)).toEqual(0L) + expect(TimeUnit.MICROSECONDS.toSeconds(42000123L)).toEqual(42L) + expect(TimeUnit.MILLISECONDS.toSeconds(42L)).toEqual(0L) + expect(TimeUnit.MILLISECONDS.toSeconds(42123L)).toEqual(42L) + expect(TimeUnit.SECONDS.toSeconds(42L)).toEqual(42L) + expect(TimeUnit.MINUTES.toSeconds(42L)).toEqual(2520L) + expect(TimeUnit.HOURS.toSeconds(42L)).toEqual(151200L) + expect(TimeUnit.DAYS.toSeconds(42L)).toEqual(3628800L) + } + + it("should respond to `toMinutes`") { + expect(TimeUnit.NANOSECONDS.toMinutes(42L)).toEqual(0L) + expect(TimeUnit.NANOSECONDS.toMinutes(2520000007380L)).toEqual(42L) + expect(TimeUnit.MICROSECONDS.toMinutes(42L)).toEqual(0L) + expect(TimeUnit.MICROSECONDS.toMinutes(2520007380L)).toEqual(42L) + expect(TimeUnit.MILLISECONDS.toMinutes(42L)).toEqual(0L) + expect(TimeUnit.MILLISECONDS.toMinutes(2520738L)).toEqual(42L) + expect(TimeUnit.SECONDS.toMinutes(42L)).toEqual(0L) + expect(TimeUnit.SECONDS.toMinutes(2520L)).toEqual(42L) + expect(TimeUnit.MINUTES.toMinutes(42L)).toEqual(42L) + expect(TimeUnit.HOURS.toMinutes(42L)).toEqual(2520L) + expect(TimeUnit.DAYS.toMinutes(42L)).toEqual(60480L) + } + + it("should respond to `toHours`") { + expect(TimeUnit.NANOSECONDS.toHours(42L)).toEqual(0L) + expect(TimeUnit.NANOSECONDS.toHours(151200000442800L)).toEqual(42L) + expect(TimeUnit.MICROSECONDS.toHours(42L)).toEqual(0L) + expect(TimeUnit.MICROSECONDS.toHours(151200442800L)).toEqual(42L) + expect(TimeUnit.MILLISECONDS.toHours(42L)).toEqual(0L) + expect(TimeUnit.MILLISECONDS.toHours(151244280L)).toEqual(42L) + expect(TimeUnit.SECONDS.toHours(42L)).toEqual(0L) + expect(TimeUnit.SECONDS.toHours(151200L)).toEqual(42L) + expect(TimeUnit.MINUTES.toHours(42L)).toEqual(0L) + expect(TimeUnit.MINUTES.toHours(2520L)).toEqual(42L) + expect(TimeUnit.HOURS.toHours(42L)).toEqual(42L) + expect(TimeUnit.DAYS.toHours(42L)).toEqual(1008L) + } + + it("should respond to `toDays`") { + expect(TimeUnit.NANOSECONDS.toDays(42L)).toEqual(0L) + expect(TimeUnit.NANOSECONDS.toDays(3628800010627200L)).toEqual(42L) + expect(TimeUnit.MICROSECONDS.toDays(42L)).toEqual(0L) + expect(TimeUnit.MICROSECONDS.toDays(3628810627200L)).toEqual(42L) + expect(TimeUnit.MILLISECONDS.toDays(42L)).toEqual(0L) + expect(TimeUnit.MILLISECONDS.toDays(3629862720L)).toEqual(42L) + expect(TimeUnit.SECONDS.toDays(42L)).toEqual(0L) + expect(TimeUnit.SECONDS.toDays(3628800L)).toEqual(42L) + expect(TimeUnit.MINUTES.toDays(42L)).toEqual(0L) + expect(TimeUnit.MINUTES.toDays(60480L)).toEqual(42L) + expect(TimeUnit.HOURS.toDays(42L)).toEqual(1L) + expect(TimeUnit.HOURS.toDays(1008L)).toEqual(42L) + expect(TimeUnit.DAYS.toDays(42L)).toEqual(42L) + } + + it("should respond to `values`") { + val values = TimeUnit.values() + expect(values.length).toEqual(7) + expectTimeUnit(values(0), TimeUnit.NANOSECONDS) + expectTimeUnit(values(1), TimeUnit.MICROSECONDS) + expectTimeUnit(values(2), TimeUnit.MILLISECONDS) + expectTimeUnit(values(3), TimeUnit.SECONDS) + expectTimeUnit(values(4), TimeUnit.MINUTES) + expectTimeUnit(values(5), TimeUnit.HOURS) + expectTimeUnit(values(6), TimeUnit.DAYS) + } + + it("should respond to `valueOf`") { + expectTimeUnit(TimeUnit.valueOf("NANOSECONDS"), TimeUnit.NANOSECONDS) + expectTimeUnit(TimeUnit.valueOf("MICROSECONDS"), TimeUnit.MICROSECONDS) + expectTimeUnit(TimeUnit.valueOf("MILLISECONDS"), TimeUnit.MILLISECONDS) + expectTimeUnit(TimeUnit.valueOf("SECONDS"), TimeUnit.SECONDS) + expectTimeUnit(TimeUnit.valueOf("MINUTES"), TimeUnit.MINUTES) + expectTimeUnit(TimeUnit.valueOf("HOURS"), TimeUnit.HOURS) + expectTimeUnit(TimeUnit.valueOf("DAYS"), TimeUnit.DAYS) + } + + } + + def expectTimeUnit(actual: TimeUnit, expected: TimeUnit): Unit = + expect(actual.asInstanceOf[js.Any]).toEqual(expected.asInstanceOf[js.Any]) +} diff --git a/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/URITest.scala b/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/URITest.scala new file mode 100644 index 0000000..65a049f --- /dev/null +++ b/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/URITest.scala @@ -0,0 +1,312 @@ +/* __ *\ +** ________ ___ / / ___ __ ____ Scala.js Test Suite ** +** / __/ __// _ | / / / _ | __ / // __/ (c) 2013, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \ http://scala-js.org/ ** +** /____/\___/_/ |_/____/_/ | |__/ /____/ ** +** |/____/ ** +\* */ +package scala.scalajs.testsuite.javalib + +import scala.scalajs.js +import org.scalajs.jasminetest.JasmineTest + +import java.net.URI + +object URITest extends JasmineTest { + + def expectURI(uri: URI, isAbsolute: Boolean, isOpaque: Boolean)( + authority: String = null, fragment: String = null, + host: String = null, path: String = null, port: Int = -1, + query: String = null, scheme: String = null, userInfo: String = null, + schemeSpecificPart: String = null)(rawAuthority: String = authority, + rawFragment: String = fragment, rawPath: String = path, + rawQuery: String = query, rawUserInfo: String = userInfo, + rawSchemeSpecificPart: String = schemeSpecificPart): Unit = { + + expect(uri.getAuthority()).toBe(authority) + expect(uri.getFragment()).toBe(fragment) + expect(uri.getHost()).toBe(host) + expect(uri.getPath()).toBe(path) + expect(uri.getPort()).toBe(port) + expect(uri.getQuery()).toBe(query) + expect(uri.getRawAuthority()).toBe(rawAuthority) + expect(uri.getRawFragment()).toBe(rawFragment) + expect(uri.getRawPath()).toBe(rawPath) + expect(uri.getRawQuery()).toBe(rawQuery) + expect(uri.getRawSchemeSpecificPart()).toBe(rawSchemeSpecificPart) + expect(uri.getRawUserInfo()).toBe(rawUserInfo) + expect(uri.getScheme()).toBe(scheme) + expect(uri.getSchemeSpecificPart()).toBe(schemeSpecificPart) + expect(uri.getUserInfo()).toBe(userInfo) + expect(uri.isAbsolute()).toBe(isAbsolute) + expect(uri.isOpaque()).toBe(isOpaque) + + } + + describe("java.net.URI") { + + it("should parse vanilla absolute URIs") { + expectURI(new URI("http://java.sun.com/j2se/1.3/"), true, false)( + scheme = "http", + host = "java.sun.com", + path = "/j2se/1.3/", + authority = "java.sun.com", + schemeSpecificPart = "//java.sun.com/j2se/1.3/")() + } + + it("should parse absolute URIs with IPv6") { + val uri = new URI("http://hans@[ffff::0:128.4.5.3]:345/~hans/") + expectURI(uri, true, false)( + scheme = "http", + host = "[ffff::0:128.4.5.3]", + userInfo = "hans", + port = 345, + path = "/~hans/", + authority = "hans@[ffff::0:128.4.5.3]:345", + schemeSpecificPart = "//hans@[ffff::0:128.4.5.3]:345/~hans/" + )() + } + + it("should parse absolute URIs without authority") { + expectURI(new URI("file:/~/calendar"), true, false)( + scheme = "file", + path = "/~/calendar", + schemeSpecificPart = "/~/calendar")() + } + + it("should parse absolute URIs with empty authority") { + expectURI(new URI("file:///~/calendar"), true, false)( + authority = "", + scheme = "file", + path = "/~/calendar", + schemeSpecificPart = "///~/calendar")() + } + + it("should parse opaque URIs") { + expectURI(new URI("mailto:java-net@java.sun.com"), true, true)( + scheme = "mailto", + schemeSpecificPart = "java-net@java.sun.com")() + + expectURI(new URI("news:comp.lang.java"), true, true)( + scheme = "news", + schemeSpecificPart = "comp.lang.java")() + + expectURI(new URI("urn:isbn:096139210x"), true, true)( + scheme = "urn", + schemeSpecificPart = "isbn:096139210x")() + } + + it("should parse relative URIs") { + expectURI(new URI("docs/guide/collections/designfaq.html#28"), false, false)( + path = "docs/guide/collections/designfaq.html", + fragment = "28", + schemeSpecificPart = "docs/guide/collections/designfaq.html#28" + )() + expectURI(new URI("../../../demo/jfc/SwingSet2/src/SwingSet2.java"), false, false)( + path = "../../../demo/jfc/SwingSet2/src/SwingSet2.java", + schemeSpecificPart = "../../../demo/jfc/SwingSet2/src/SwingSet2.java" + )() + } + + it("should parse relative URIs with IPv4") { + expectURI(new URI("//123.5.6.3:45/bar"), false, false)( + authority = "123.5.6.3:45", + host = "123.5.6.3", + port = 45, + path = "/bar", + schemeSpecificPart = "//123.5.6.3:45/bar" + )() + } + + it("should parse relative URIs with registry-based authority") { + expectURI(new URI("//foo:bar"), false, false)( + authority = "foo:bar", + schemeSpecificPart = "//foo:bar" + )() + } + + it("should parse relative URIs with escapes") { + expectURI(new URI("//ma%5dx:secret@example.com:8000/foo"), false, false)( + authority = "ma]x:secret@example.com:8000", + userInfo = "ma]x:secret", + host = "example.com", + port = 8000, + path = "/foo", + schemeSpecificPart = "//ma]x:secret@example.com:8000/foo")( + rawUserInfo = "ma%5dx:secret", + rawAuthority = "ma%5dx:secret@example.com:8000", + rawSchemeSpecificPart = "//ma%5dx:secret@example.com:8000/foo") + } + + it("should parse relative URIs with fragment only") { + expectURI(new URI("#foo"), false, false)( + fragment = "foo", + path = "", + schemeSpecificPart = "#foo" + )() + } + + it("should provide compareTo") { + val x = new URI("http://example.com/asdf%6a") + val y = new URI("http://example.com/asdf%6A") + val z = new URI("http://example.com/asdfj") + val rel = new URI("/foo/bar") + + expect(x.compareTo(y)).toBeGreaterThan(0) + expect(x.compareTo(z)).toBeLessThan(0) + expect(y.compareTo(z)).toBeLessThan(0) + expect(x.compareTo(x)).toBe(0) + expect(y.compareTo(y)).toBe(0) + expect(z.compareTo(z)).toBe(0) + expect(x.compareTo(rel)).toBeGreaterThan(0) + expect(y.compareTo(rel)).toBeGreaterThan(0) + expect(z.compareTo(rel)).toBeGreaterThan(0) + expect(rel.compareTo(rel)).toBe(0) + } + + it("should provide equals") { + val x = new URI("http://example.com/asdf%6a") + val y = new URI("http://example.com/asdf%6A") + val z = new URI("http://example.com/asdfj") + + expect(x == y).toBeTruthy + expect(x == z).toBeFalsy + expect(y == z).toBeFalsy + expect(x == x).toBeTruthy + expect(y == y).toBeTruthy + expect(z == z).toBeTruthy + + expect(new URI("foo:helloWorld%6b%6C") == new URI("foo:helloWorld%6C%6b")) + } + + it("should provide normalize") { + expectURI(new URI("http://example.com/../asef/../../").normalize, true, false)( + scheme = "http", + host = "example.com", + authority = "example.com", + path = "/../../", + schemeSpecificPart = "//example.com/../../")() + expectURI(new URI("http://example.com/../as/./ef/foo/../../").normalize, true, false)( + scheme = "http", + host = "example.com", + authority = "example.com", + path = "/../as/", + schemeSpecificPart = "//example.com/../as/")() + expectURI(new URI("bar/../fo:o/./bar").normalize, false, false)( + path = "./fo:o/bar", + schemeSpecificPart = "./fo:o/bar")() + expectURI(new URI("bar/..//fo:o//./bar").normalize, false, false)( + path = "./fo:o/bar", + schemeSpecificPart = "./fo:o/bar")() + + val x = new URI("http://www.example.com/foo/bar") + expect(x.normalize eq x).toBeTruthy + } + + it("should provide resolve - JavaDoc examples") { + val base = "http://java.sun.com/j2se/1.3/" + val relative1 = "docs/guide/collections/designfaq.html#28" + val resolved1 = + "http://java.sun.com/j2se/1.3/docs/guide/collections/designfaq.html#28" + val relative2 = "../../../demo/jfc/SwingSet2/src/SwingSet2.java" + val resolved2 = + "http://java.sun.com/j2se/1.3/demo/jfc/SwingSet2/src/SwingSet2.java" + + expect(new URI(base).resolve(relative1).toString).toEqual(resolved1) + expect(new URI(resolved1).resolve(relative2).toString).toEqual(resolved2) + } + + it("should provide resolve - RFC2396 examples") { + val base = new URI("http://a/b/c/d;p?q") + def resTest(ref: String, trg: String) = + expect(base.resolve(ref).toString).toEqual(trg) + + // Normal examples + resTest("g:h" , "g:h") + resTest("g" , "http://a/b/c/g") + resTest("./g" , "http://a/b/c/g") + resTest("g/" , "http://a/b/c/g/") + resTest("/g" , "http://a/g") + resTest("//g" , "http://g") + resTest("?y" , "http://a/b/c/?y") + resTest("g?y" , "http://a/b/c/g?y") + resTest("#s" , "http://a/b/c/d;p?q#s") + resTest("g#s" , "http://a/b/c/g#s") + resTest("g?y#s" , "http://a/b/c/g?y#s") + resTest(";x" , "http://a/b/c/;x") + resTest("g;x" , "http://a/b/c/g;x") + resTest("g;x?y#s", "http://a/b/c/g;x?y#s") + resTest("." , "http://a/b/c/") + resTest("./" , "http://a/b/c/") + resTest(".." , "http://a/b/") + resTest("../" , "http://a/b/") + resTest("../g" , "http://a/b/g") + resTest("../.." , "http://a/") + resTest("../../" , "http://a/") + resTest("../../g", "http://a/g") + + // Abnormal examples + resTest("../../../g" , "http://a/../g") + resTest("../../../../g", "http://a/../../g") + resTest("/./g" , "http://a/./g") + resTest("/../g" , "http://a/../g") + resTest("g." , "http://a/b/c/g.") + resTest(".g" , "http://a/b/c/.g") + resTest("g.." , "http://a/b/c/g..") + resTest("..g" , "http://a/b/c/..g") + resTest("./../g" , "http://a/b/g") + resTest("./g/." , "http://a/b/c/g/") + resTest("g/./h" , "http://a/b/c/g/h") + resTest("g/../h" , "http://a/b/c/h") + resTest("g;x=1/./y" , "http://a/b/c/g;x=1/y") + resTest("g;x=1/../y" , "http://a/b/c/y") + resTest("g?y/./x" , "http://a/b/c/g?y/./x") + resTest("g?y/../x" , "http://a/b/c/g?y/../x") + resTest("g#s/./x" , "http://a/b/c/g#s/./x") + resTest("g#s/../x" , "http://a/b/c/g#s/../x") + resTest("http:g" , "http:g") + } + + it("should provide normalize - examples derived from RFC relativize") { + expectURI(new URI("http://a/b/c/..").normalize, true, false)( + scheme = "http", + host = "a", + authority = "a", + path = "/b/", + schemeSpecificPart = "//a/b/")() + + expectURI(new URI("http://a/b/c/.").normalize, true, false)( + scheme = "http", + host = "a", + authority = "a", + path = "/b/c/", + schemeSpecificPart = "//a/b/c/")() + } + + it("should provide relativize") { + val x = new URI("http://f%4Aoo@asdf/a") + val y = new URI("http://fJoo@asdf/a/b/") + val z = new URI("http://f%4aoo@asdf/a/b/") + expect(x.relativize(y) eq y).toBeTruthy + expect(x.relativize(z).toString()).toEqual("b/") + + def relTest(base: String, trg: String, exp: String) = + expect(new URI(base).relativize(new URI(trg)).toString()).toEqual(exp) + + relTest("http://a.ch/a", "http://a.ch/a/b", "b") + relTest("http://a.ch/a/", "http://a.ch/a/b", "b") + relTest("https://a.ch/a", "http://a.ch/a/b", "http://a.ch/a/b") + relTest("/a/b/c", "/a/b/c/d/e", "d/e") + relTest("/a/b/c/", "/a/b/c/d/e", "d/e") + relTest("/a/b/c/", "/a/b/c/foo:e/d", "foo:e/d") // see bug JDK-7037120 + relTest("../a/b", "../a/b/c", "c") + } + + it("should provide hashCode") { + expect(new URI("http://example.com/asdf%6a").hashCode).toEqual( + new URI("http://example.com/asdf%6A").hashCode) + } + + } + +} diff --git a/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/UUIDTest.scala b/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/UUIDTest.scala new file mode 100644 index 0000000..b22fe02 --- /dev/null +++ b/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/UUIDTest.scala @@ -0,0 +1,180 @@ +/* __ *\ +** ________ ___ / / ___ __ ____ Scala.js Test Suite ** +** / __/ __// _ | / / / _ | __ / // __/ (c) 2013, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \ http://scala-js.org/ ** +** /____/\___/_/ |_/____/_/ | |__/ /____/ ** +** |/____/ ** +\* */ +package scala.scalajs.testsuite.javalib + +import scala.scalajs.js +import org.scalajs.jasminetest.JasmineTest + +import java.util.UUID + +object UUIDTest extends JasmineTest { + describe("java.util.UUID") { + it("constructor") { + val uuid = new UUID(0xf81d4fae7dec11d0L, 0xa76500a0c91e6bf6L) + expect(uuid.getMostSignificantBits() == 0xf81d4fae7dec11d0L).toBeTruthy + expect(uuid.getLeastSignificantBits() == 0xa76500a0c91e6bf6L).toBeTruthy + expect(uuid.variant()).toEqual(2) + expect(uuid.version()).toEqual(1) + expect(uuid.timestamp() == 0x1d07decf81d4faeL).toBeTruthy + expect(uuid.clockSequence()).toEqual(0x2765) + expect(uuid.node() == 0xA0C91E6BF6L).toBeTruthy + } + + it("getLeastSignificantBits") { + expect(new UUID(0L, 0L).getLeastSignificantBits() == 0L).toBeTruthy + expect(new UUID(0L, Long.MinValue).getLeastSignificantBits() == Long.MinValue).toBeTruthy + expect(new UUID(0L, Long.MaxValue).getLeastSignificantBits() == Long.MaxValue).toBeTruthy + } + + it("getMostSignificantBits") { + expect(new UUID(0L, 0L).getMostSignificantBits() == 0L).toBeTruthy + expect(new UUID(Long.MinValue, 0L).getMostSignificantBits() == Long.MinValue).toBeTruthy + expect(new UUID(Long.MaxValue, 0L).getMostSignificantBits() == Long.MaxValue).toBeTruthy + } + + it("version") { + expect(new UUID(0L, 0L).version()).toEqual(0) + expect(new UUID(0x0000000000001000L, 0L).version()).toEqual(1) + expect(new UUID(0x00000000000f2f00L, 0L).version()).toEqual(2) + } + + it("variant") { + expect(new UUID(0L, 0L).variant()).toEqual(0) + expect(new UUID(0L, 0x7000000000000000L).variant()).toEqual(0) + expect(new UUID(0L, 0x3ff0000000000000L).variant()).toEqual(0) + expect(new UUID(0L, 0x1ff0000000000000L).variant()).toEqual(0) + + expect(new UUID(0L, 0x8000000000000000L).variant()).toEqual(2) + expect(new UUID(0L, 0xb000000000000000L).variant()).toEqual(2) + expect(new UUID(0L, 0xaff0000000000000L).variant()).toEqual(2) + expect(new UUID(0L, 0x9ff0000000000000L).variant()).toEqual(2) + + expect(new UUID(0L, 0xc000000000000000L).variant()).toEqual(6) + expect(new UUID(0L, 0xdf00000000000000L).variant()).toEqual(6) + } + + it("timestamp") { + expect(new UUID(0x0000000000001000L, + 0x8000000000000000L).timestamp() == 0L).toBeTruthy + expect(new UUID(0x7777777755551333L, + 0x8000000000000000L).timestamp() == 0x333555577777777L).toBeTruthy + + expect(() => new UUID(0x0000000000000000L, 0x8000000000000000L).timestamp()).toThrow + expect(() => new UUID(0x0000000000002000L, 0x8000000000000000L).timestamp()).toThrow + } + + it("clockSequence") { + expect(new UUID(0x0000000000001000L, 0x8000000000000000L).clockSequence()).toEqual(0) + expect(new UUID(0x0000000000001000L, 0x8fff000000000000L).clockSequence()).toEqual(0x0fff) + expect(new UUID(0x0000000000001000L, 0xBfff000000000000L).clockSequence()).toEqual(0x3fff) + + expect(() => new UUID(0x0000000000000000L, 0x8000000000000000L).clockSequence()).toThrow + expect(() => new UUID(0x0000000000002000L, 0x8000000000000000L).clockSequence()).toThrow + } + + it("node") { + expect(new UUID(0x0000000000001000L, 0x8000000000000000L).node() == 0L).toBeTruthy + expect(new UUID(0x0000000000001000L, 0x8000ffffffffffffL).node() == 0xffffffffffffL).toBeTruthy + + expect(() => new UUID(0x0000000000000000L, 0x8000000000000000L).node()).toThrow + expect(() => new UUID(0x0000000000002000L, 0x8000000000000000L).node()).toThrow + } + + it("compareTo") { + val uuid0101 = new UUID(1L, 1L) + val uuid0111 = new UUID(1L, 0x100000001L) + val uuid1000 = new UUID(0x100000000L, 0L) + + expect(uuid0101.compareTo(uuid0101)).toEqual(0) + expect(uuid0111.compareTo(uuid0111)).toEqual(0) + expect(uuid1000.compareTo(uuid1000)).toEqual(0) + + expect(uuid0101.compareTo(uuid0111)).toBeLessThan(0) + expect(uuid0101.compareTo(uuid1000)).toBeLessThan(0) + expect(uuid0111.compareTo(uuid1000)).toBeLessThan(0) + + expect(uuid0111.compareTo(uuid0101)).toBeGreaterThan(0) + expect(uuid1000.compareTo(uuid0101)).toBeGreaterThan(0) + expect(uuid1000.compareTo(uuid0111)).toBeGreaterThan(0) + } + + it("hashCode") { + expect(new UUID(0L, 0L).hashCode()).toEqual(0) + expect(new UUID(123L, 123L).hashCode()).toEqual(new UUID(123L, 123L).hashCode()) + } + + it("equals") { + val uuid1 = new UUID(0L, 0L) + expect(uuid1.equals(uuid1)).toBeTruthy + expect(uuid1.equals(null)).toBeFalsy + expect(uuid1.equals("something else")).toBeFalsy + + val uuid2 = new UUID(0L, 0L) + expect(uuid1.equals(uuid2)).toBeTruthy + + val uuid3 = new UUID(0xf81d4fae7dec11d0L, 0xa76500a0c91e6bf6L) + val uuid4 = new UUID(0xf81d4fae7dec11d0L, 0xa76500a0c91e6bf6L) + expect(uuid3.equals(uuid4)).toBeTruthy + expect(uuid3.equals(uuid1)).toBeFalsy + + expect(uuid3.equals(new UUID(0x781d4fae7dec11d0L, 0xa76500a0c91e6bf6L))).toBeFalsy + expect(uuid3.equals(new UUID(0xf81d4fae7dec11d1L, 0xa76500a0c91e6bf6L))).toBeFalsy + expect(uuid3.equals(new UUID(0xf81d4fae7dec11d0L, 0xa76530a0c91e6bf6L))).toBeFalsy + expect(uuid3.equals(new UUID(0xf81d4fae7dec11d0L, 0xa76500a0c91e6cf6L))).toBeFalsy + } + + it("toString") { + expect(new UUID(0xf81d4fae7dec11d0L, + 0xa76500a0c91e6bf6L).toString()).toEqual("f81d4fae-7dec-11d0-a765-00a0c91e6bf6") + expect(new UUID(0x0000000000001000L, + 0x8000000000000000L).toString()).toEqual("00000000-0000-1000-8000-000000000000") + } + + it("randomUUID") { + val uuid = UUID.randomUUID() + expect(uuid.variant()).toEqual(2) + expect(uuid.version()).toEqual(4) + } + + it("fromString") { + val uuid1 = UUID.fromString("f81d4fae-7dec-11d0-a765-00a0c91e6bf6") + expect(uuid1.equals(new UUID(0xf81d4fae7dec11d0L, 0xa76500a0c91e6bf6L))).toBeTruthy + expect(uuid1.getMostSignificantBits() == 0xf81d4fae7dec11d0L).toBeTruthy + expect(uuid1.getLeastSignificantBits() == 0xa76500a0c91e6bf6L).toBeTruthy + expect(uuid1.variant()).toEqual(2) + expect(uuid1.version()).toEqual(1) + expect(uuid1.timestamp() == 130742845922168750L).toBeTruthy + expect(uuid1.clockSequence()).toEqual(10085) + expect(uuid1.node() == 690568981494L).toBeTruthy + + val uuid2 = UUID.fromString("00000000-0000-1000-8000-000000000000") + expect(uuid2.equals(new UUID(0x0000000000001000L, 0x8000000000000000L))) + expect(uuid2.getMostSignificantBits() == 0x0000000000001000L).toBeTruthy + expect(uuid2.getLeastSignificantBits() == 0x8000000000000000L).toBeTruthy + expect(uuid2.variant()).toEqual(2) + expect(uuid2.version()).toEqual(1) + expect(uuid2.timestamp() == 0L).toBeTruthy + expect(uuid2.clockSequence()).toEqual(0) + expect(uuid2.node() == 0L).toBeTruthy + + expect(() => UUID.fromString(null)).toThrow + expect(() => UUID.fromString("")).toThrow + expect(() => UUID.fromString("f81d4fae_7dec-11d0-a765-00a0c91e6bf6")).toThrow + expect(() => UUID.fromString("f81d4fae-7dec_11d0-a765-00a0c91e6bf6")).toThrow + expect(() => UUID.fromString("f81d4fae-7dec-11d0_a765-00a0c91e6bf6")).toThrow + expect(() => UUID.fromString("f81d4fae-7dec-11d0-a765_00a0c91e6bf6")).toThrow + expect(() => UUID.fromString("-7dec-11d0-a765-00a0c91e6bf6")).toThrow + expect(() => UUID.fromString("f81d4fae--11d0-a765-00a0c91e6bf6")).toThrow + expect(() => UUID.fromString("f81d4fae-7dec--a765-00a0c91e6bf6")).toThrow + expect(() => UUID.fromString("f81d4fae-7dec-11d0--00a0c91e6bf6")).toThrow + expect(() => UUID.fromString("f81d4fae-7dec-11d0-a765-")).toThrow + expect(() => UUID.fromString("f81d4fae-7dec-11d0-a765")).toThrow + expect(() => UUID.fromString("f81d4fae-7dZc-11d0-a765-00a0c91e6bf6")).toThrow + } + } +} |