Rearrange stdlib unit tests across packages.

Rearrange JS stdlib unit tests.
This commit is contained in:
Ilya Gorbunov
2016-06-17 00:13:05 +03:00
parent d266f546f4
commit c5a208f3eb
33 changed files with 57 additions and 80 deletions
@@ -0,0 +1,35 @@
package test.numbers
import kotlin.test.*
import org.junit.Test as test
// TODO: Run these tests during compiler test only (JVM & JS)
class BitwiseOperationsTest {
@test fun orForInt() {
assertEquals(3, 2 or 1)
}
@test fun andForInt() {
assertEquals(0, 1 and 0)
}
@test fun xorForInt() {
assertEquals(1, 2 xor 3)
}
@test fun shlForInt() {
assertEquals(4, 1 shl 2)
}
@test fun shrForInt() {
assertEquals(1, 2 shr 1)
}
@test fun ushrForInt() {
assertEquals(2147483647, -1 ushr 1)
}
@test fun invForInt() {
assertEquals(0, (-1).inv())
}
}
@@ -1,123 +0,0 @@
package numbers
import org.junit.Test
import org.junit.Test as test
import kotlin.test.*
class CoercionTest {
fun usage() {
val n = 1
// infix usage
val n1 = n.coerceAtLeast(2)
// function usage
val n2 = n.coerceAtLeast(2)
// infix with range
val n3 = n.coerceIn(2..5)
}
@Test
fun coercionsInt() {
expect(5) { 5.coerceAtLeast(1) }
expect(5) { 1.coerceAtLeast(5) }
expect(1) { 5.coerceAtMost(1) }
expect(1) { 1.coerceAtMost(5) }
for (value in 0..10) {
expect(value) { value.coerceIn(null, null) }
val min = 2
val max = 5
val range = min..max
expect(value.coerceAtLeast(min)) { value.coerceIn(min, null) }
expect(value.coerceAtMost(max)) { value.coerceIn(null, max) }
expect(value.coerceAtLeast(min).coerceAtMost(max)) { value.coerceIn(min, max) }
expect(value.coerceAtMost(max).coerceAtLeast(min)) { value.coerceIn(range) }
assertTrue((value.coerceIn(range)) in range)
}
assertFails { 1.coerceIn(1, 0) }
assertFails { 1.coerceIn(1..0) }
}
@Test
fun coercionsLong() {
expect(5L) { 5L.coerceAtLeast(1L) }
expect(5L) { 1L.coerceAtLeast(5L) }
expect(1L) { 5L.coerceAtMost(1L) }
expect(1L) { 1L.coerceAtMost(5L) }
for (value in 0L..10L) {
expect(value) { value.coerceIn(null, null) }
val min = 2L
val max = 5L
val range = min..max
expect(value.coerceAtLeast(min)) { value.coerceIn(min, null) }
expect(value.coerceAtMost(max)) { value.coerceIn(null, max) }
expect(value.coerceAtLeast(min).coerceAtMost(max)) { value.coerceIn(min, max) }
expect(value.coerceAtMost(max).coerceAtLeast(min)) { value.coerceIn(range) }
assertTrue((value.coerceIn(range)) in range)
}
assertFails { 1L.coerceIn(1L, 0L) }
assertFails { 1L.coerceIn(1L..0L) }
}
@Test
@Suppress("DEPRECATION_ERROR")
fun coercionsDouble() {
expect(5.0) { 5.0.coerceAtLeast(1.0) }
expect(5.0) { 1.0.coerceAtLeast(5.0) }
expect(1.0) { 5.0.coerceAtMost(1.0) }
expect(1.0) { 1.0.coerceAtMost(5.0) }
for (value in (0..10).map { it.toDouble() }) {
expect(value) { value.coerceIn(null, null) }
val min = 2.0
val max = 5.0
val range = min..max
expect(value.coerceAtLeast(min)) { value.coerceIn(min, null) }
expect(value.coerceAtMost(max)) { value.coerceIn(null, max) }
expect(value.coerceAtLeast(min).coerceAtMost(max)) { value.coerceIn(min, max) }
expect(value.coerceAtMost(max).coerceAtLeast(min)) { value.coerceIn(range) }
assertTrue((value.coerceIn(range)) in range)
}
assertFails { 1.0.coerceIn(1.0, 0.0) }
assertFails { 1.0.coerceIn(1.0..0.0) }
}
@Test
fun coercionsComparable() {
val v = (0..10).map { ComparableNumber(it) }
expect(5) { v[5].coerceAtLeast(v[1]).value }
expect(5) { v[1].coerceAtLeast(v[5]).value }
expect(v[5]) { v[5].coerceAtLeast(ComparableNumber(5)) }
expect(1) { v[5].coerceAtMost(v[1]).value }
expect(1) { v[1].coerceAtMost(v[5]).value }
expect(v[1]) { v[1].coerceAtMost(ComparableNumber(1)) }
for (value in v) {
expect(value) { value.coerceIn(null, null) }
val min = v[2]
val max = v[5]
val range = min..max
expect(value.coerceAtLeast(min)) { value.coerceIn(min, null) }
expect(value.coerceAtMost(max)) { value.coerceIn(null, max) }
expect(value.coerceAtLeast(min).coerceAtMost(max)) { value.coerceIn(min, max) }
expect(value.coerceAtMost(max).coerceAtLeast(min)) { value.coerceIn(range) }
assertTrue((value.coerceIn(range)) in range)
}
assertFails { v[1].coerceIn(v[1], v[0]) }
assertFails { v[1].coerceIn(v[1]..v[0]) }
}
}
private class ComparableNumber(val value: Int) : Comparable<ComparableNumber> {
override fun compareTo(other: ComparableNumber): Int = this.value - other.value
override fun toString(): String = "CV$value"
}
@@ -0,0 +1,85 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package test.numbers
import kotlin.test.*
import org.junit.Test as test
class CompanionIntrinsicObjectsJVMTest {
@test fun intTest() {
val i = Int
assertEquals(java.lang.Integer.MAX_VALUE, Int.MAX_VALUE)
assertEquals(java.lang.Integer.MIN_VALUE, Int.MIN_VALUE)
}
@test fun doubleTest() {
val d = Double
assertEquals(java.lang.Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY)
assertEquals(java.lang.Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY)
assertEquals(java.lang.Double.NaN, Double.NaN)
assertEquals(java.lang.Double.MAX_VALUE, Double.MAX_VALUE)
assertEquals(java.lang.Double.MIN_VALUE, Double.MIN_VALUE)
}
@test fun floatTest() {
val f = Float
assertEquals(java.lang.Float.POSITIVE_INFINITY, Float.POSITIVE_INFINITY)
assertEquals(java.lang.Float.NEGATIVE_INFINITY, Float.NEGATIVE_INFINITY)
assertEquals(java.lang.Float.NaN, Float.NaN)
assertEquals(java.lang.Float.MAX_VALUE, Float.MAX_VALUE)
assertEquals(java.lang.Float.MIN_VALUE, Float.MIN_VALUE)
}
@test fun longTest() {
val l = Long
assertEquals(java.lang.Long.MAX_VALUE, Long.MAX_VALUE)
assertEquals(java.lang.Long.MIN_VALUE, Long.MIN_VALUE)
}
@test fun shortTest() {
val s = Short
assertEquals(java.lang.Short.MAX_VALUE, Short.MAX_VALUE)
assertEquals(java.lang.Short.MIN_VALUE, Short.MIN_VALUE)
}
@test fun byteTest() {
val b = Byte
assertEquals(java.lang.Byte.MAX_VALUE, Byte.MAX_VALUE)
assertEquals(java.lang.Byte.MIN_VALUE, Byte.MIN_VALUE)
}
@test fun charTest() {
val ch = Char
assertEquals(ch, Char)
}
@test fun stringTest() {
val s = String
assertEquals(s, String)
}
}
@@ -0,0 +1,39 @@
package test.numbers
import java.math.BigInteger
import java.math.BigDecimal
import kotlin.test.*
import org.junit.Test as test
class MathTest {
@test fun testBigInteger() {
val a = BigInteger("2")
val b = BigInteger("3")
assertEquals(BigInteger("5"), a + b)
assertEquals(BigInteger("-1"), a - b)
assertEquals(BigInteger("6"), a * b)
assertEquals(BigInteger("0"), a / b)
assertEquals(BigInteger("-2"), -a)
assertEquals(BigInteger("1"), -a % b)
assertEquals(BigInteger("-2"), (-a).remainder(b))
}
@test fun testBigDecimal() {
val a = BigDecimal("2")
val b = BigDecimal("3")
assertEquals(BigDecimal("5"), a + b)
assertEquals(BigDecimal("-1"), a - b)
assertEquals(BigDecimal("6"), a * b)
assertEquals(BigDecimal("2"), BigDecimal("4") / a)
assertEquals(BigDecimal("-2"), -a)
assertEquals(BigDecimal("-2"), -a % b)
}
}
fun main(args: Array<String>) {
MathTest().testBigInteger()
MathTest().testBigDecimal()
}
@@ -1,4 +1,4 @@
package numbers
package test.numbers
import java.math.BigDecimal
import org.junit.Test as test
+1 -1
View File
@@ -1,4 +1,4 @@
package numbers
package test.numbers
import org.junit.Test as test
import kotlin.test.*