Merge pull request #6339 from rodrigolgraciano/master

BAEL-2648
This commit is contained in:
Loredana Crusoveanu 2019-03-27 22:44:13 +02:00 committed by GitHub
commit 87e20275a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 388 additions and 1 deletions

View File

@ -13,4 +13,66 @@
<relativePath>../parent-kotlin</relativePath>
</parent>
</project>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
<version>${junit.platform.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-test</artifactId>
<version>${kotlin.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<configuration>
<jvmTarget>1.8</jvmTarget>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<kotlin.version>1.2.71</kotlin.version>
<junit.platform.version>1.1.1</junit.platform.version>
<junit.vintage.version>5.2.0</junit.vintage.version>
<assertj.version>3.10.0</assertj.version>
</properties>
</project>

View File

@ -0,0 +1,13 @@
package com.baeldung.range
fun main(args: Array<String>) {
for (ch in 'a'..'f') {
print(ch)
}
println()
for (ch in 'f' downTo 'a') {
print(ch)
}
}

View File

@ -0,0 +1,21 @@
package com.baeldung.range
enum class Color(val rgb: Int) {
BLUE(0x0000FF),
GREEN(0x008000),
RED(0xFF0000),
MAGENTA(0xFF00FF),
YELLOW(0xFFFF00);
}
fun main(args: Array<String>) {
println(Color.values().toList());
val red = Color.RED
val yellow = Color.YELLOW
val range = red..yellow
println(range.contains(Color.MAGENTA))
println(range.contains(Color.BLUE))
println(range.contains(Color.GREEN))
}

View File

@ -0,0 +1,18 @@
package com.baeldung.range
fun main(args: Array<String>) {
val r = 1..10
//Apply filter
val f = r.filter { it -> it % 2 == 0 }
println(f)
//Map
val m = r.map { it -> it * it }
println(m)
//Reduce
val rdc = r.reduce { a, b -> a + b }
println(rdc)
}

View File

@ -0,0 +1,8 @@
package com.baeldung.range
fun main(args: Array<String>) {
println((1..9).first)
println((1..9 step 2).step)
println((3..9).reversed().last)
}

View File

@ -0,0 +1,14 @@
package com.baeldung.range
fun main(args: Array<String>) {
val r = 1..20
println(r.min())
println(r.max())
println(r.sum())
println(r.average())
println(r.count())
val repeated = listOf(1, 1, 2, 4, 4, 6, 10)
println(repeated.distinct())
}

View File

@ -0,0 +1,28 @@
package com.baeldung.range
fun main(args: Array<String>) {
for (i in 1..9) {
print(i)
}
println()
for (i in 9 downTo 1) {
print(i)
}
println()
for (i in 1.rangeTo(9)) {
print(i)
}
println()
for (i in 9.downTo(1)) {
print(i)
}
println()
for (i in 1 until 9) {
print(i)
}
}

View File

@ -0,0 +1,14 @@
package com.baeldung.range
fun main(args: Array<String>) {
(1..9).reversed().forEach {
print(it)
}
println()
(1..9).reversed().step(3).forEach {
print(it)
}
}

View File

@ -0,0 +1,15 @@
package com.baeldung.range
fun main(args: Array<String>) {
for(i in 1..9 step 2){
print(i)
}
println()
for (i in 9 downTo 1 step 2){
print(i)
}
}

View File

@ -0,0 +1,8 @@
package com.baeldung.range
fun main(args: Array<String>) {
for (i in 1 until 9) {
print(i)
}
}

View File

@ -0,0 +1,17 @@
package com.baeldung.range
import org.junit.Test
import kotlin.test.assertEquals
class CharRangeTest {
@Test
fun testCharRange() {
assertEquals(listOf('a', 'b', 'c'), ('a'..'c').toList())
}
@Test
fun testCharDownRange() {
assertEquals(listOf('c', 'b', 'a'), ('c'.downTo('a')).toList())
}
}

View File

@ -0,0 +1,20 @@
package com.baeldung.range
import org.junit.Test
import kotlin.test.assertFalse
import kotlin.test.assertTrue
class ColorTest {
@Test
fun testEnumRange() {
println(Color.values().toList());
val red = Color.RED
val yellow = Color.YELLOW
val range = red..yellow
assertTrue { range.contains(Color.MAGENTA) }
assertFalse { range.contains(Color.BLUE) }
}
}

View File

@ -0,0 +1,24 @@
package com.baeldung.range
import org.junit.Test
import kotlin.test.assertEquals
class FilterTest {
val r = 1..10
@Test
fun filterTest() {
assertEquals(listOf(2, 4, 6, 8, 10), r.filter { it -> it % 2 == 0 }.toList())
}
@Test
fun mapTest() {
assertEquals(listOf(1, 4, 9, 16, 25, 36, 49, 64, 81, 100), r.map { it -> it * it }.toList())
}
@Test
fun reduceTest() {
assertEquals(55, r.reduce { a, b -> a + b })
}
}

View File

@ -0,0 +1,22 @@
package com.baeldung.range
import org.junit.Test
import kotlin.test.assertEquals
class FirstLastTest {
@Test
fun testFirst() {
assertEquals(1, (1..9).first)
}
@Test
fun testLast() {
assertEquals(9, (1..9).last)
}
@Test
fun testStep() {
assertEquals(2, (1..9 step 2).step)
}
}

View File

@ -0,0 +1,40 @@
package com.baeldung.range
import org.junit.Test
import kotlin.test.assertEquals
class OtherRangeFunctionsTest {
val r = 1..20
val repeated = listOf(1, 1, 2, 4, 4, 6, 10)
@Test
fun testMin() {
assertEquals(1, r.min())
}
@Test
fun testMax() {
assertEquals(20, r.max())
}
@Test
fun testSum() {
assertEquals(210, r.sum())
}
@Test
fun testAverage() {
assertEquals(10.5, r.average())
}
@Test
fun testCount() {
assertEquals(20, r.count())
}
@Test
fun testDistinct() {
assertEquals(listOf(1, 2, 4, 6, 10), repeated.distinct())
}
}

View File

@ -0,0 +1,22 @@
package com.baeldung.range
import org.junit.Test
import kotlin.test.assertEquals
class RangeTest {
@Test
fun testRange() {
assertEquals(listOf(1,2,3), (1.rangeTo(3).toList()))
}
@Test
fun testDownTo(){
assertEquals(listOf(3,2,1), (3.downTo(1).toList()))
}
@Test
fun testUntil(){
assertEquals(listOf(1,2), (1.until(3).toList()))
}
}

View File

@ -0,0 +1,12 @@
package com.baeldung.range
import org.junit.Test
import kotlin.test.assertEquals
class ReverseRangeTest {
@Test
fun reversedTest() {
assertEquals(listOf(9, 6, 3), (1..9).reversed().step(3).toList())
}
}

View File

@ -0,0 +1,17 @@
package com.baeldung.range
import org.junit.Test
import kotlin.test.assertEquals
class StepTest {
@Test
fun testStep() {
assertEquals(listOf(1, 3, 5, 7, 9), (1..9 step 2).toList())
}
@Test
fun testStepDown() {
assertEquals(listOf(9, 7, 5, 3, 1), (9 downTo 1 step 2).toList())
}
}

View File

@ -0,0 +1,12 @@
package com.baeldung.range
import org.junit.Test
import kotlin.test.assertEquals
class UntilRangeTest {
@Test
fun testUntil() {
assertEquals(listOf(1, 2, 3, 4), (1 until 5).toList())
}
}