From ad3fe3d536b591014dc0e9d6dc07a11918abdbd4 Mon Sep 17 00:00:00 2001 From: Rodrigo Graciano Date: Fri, 15 Feb 2019 23:43:15 -0500 Subject: [PATCH 1/4] BAEL-2648 --- .../src/main/com/baeldung/range/CharRange.kt | 13 +++++++++ .../src/main/com/baeldung/range/Filter.kt | 18 ++++++++++++ .../src/main/com/baeldung/range/FirstLast.kt | 8 ++++++ .../com/baeldung/range/OtherRangeFunctions.kt | 14 ++++++++++ .../src/main/com/baeldung/range/Range.kt | 28 +++++++++++++++++++ .../main/com/baeldung/range/ReverseRange.kt | 14 ++++++++++ .../src/main/com/baeldung/range/Step.kt | 15 ++++++++++ .../src/main/com/baeldung/range/UntilRange.kt | 8 ++++++ 8 files changed, 118 insertions(+) create mode 100644 core-kotlin-2/src/main/com/baeldung/range/CharRange.kt create mode 100644 core-kotlin-2/src/main/com/baeldung/range/Filter.kt create mode 100644 core-kotlin-2/src/main/com/baeldung/range/FirstLast.kt create mode 100644 core-kotlin-2/src/main/com/baeldung/range/OtherRangeFunctions.kt create mode 100644 core-kotlin-2/src/main/com/baeldung/range/Range.kt create mode 100644 core-kotlin-2/src/main/com/baeldung/range/ReverseRange.kt create mode 100644 core-kotlin-2/src/main/com/baeldung/range/Step.kt create mode 100644 core-kotlin-2/src/main/com/baeldung/range/UntilRange.kt diff --git a/core-kotlin-2/src/main/com/baeldung/range/CharRange.kt b/core-kotlin-2/src/main/com/baeldung/range/CharRange.kt new file mode 100644 index 0000000000..3151674d61 --- /dev/null +++ b/core-kotlin-2/src/main/com/baeldung/range/CharRange.kt @@ -0,0 +1,13 @@ +package com.baeldung.range + +fun main(args: Array) { + + for (ch in 'a'..'f') { + print(ch) + } + println() + + for (ch in 'f' downTo 'a') { + print(ch) + } +} \ No newline at end of file diff --git a/core-kotlin-2/src/main/com/baeldung/range/Filter.kt b/core-kotlin-2/src/main/com/baeldung/range/Filter.kt new file mode 100644 index 0000000000..0e611b14cf --- /dev/null +++ b/core-kotlin-2/src/main/com/baeldung/range/Filter.kt @@ -0,0 +1,18 @@ +package com.baeldung.range + +fun main(args: Array) { + 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) + +} \ No newline at end of file diff --git a/core-kotlin-2/src/main/com/baeldung/range/FirstLast.kt b/core-kotlin-2/src/main/com/baeldung/range/FirstLast.kt new file mode 100644 index 0000000000..b82f5a8b9b --- /dev/null +++ b/core-kotlin-2/src/main/com/baeldung/range/FirstLast.kt @@ -0,0 +1,8 @@ +package com.baeldung.range + +fun main(args: Array) { + + println((1..9).first) + println((1..9 step 2).step) + println((3..9).reversed().last) +} \ No newline at end of file diff --git a/core-kotlin-2/src/main/com/baeldung/range/OtherRangeFunctions.kt b/core-kotlin-2/src/main/com/baeldung/range/OtherRangeFunctions.kt new file mode 100644 index 0000000000..19dcab89b2 --- /dev/null +++ b/core-kotlin-2/src/main/com/baeldung/range/OtherRangeFunctions.kt @@ -0,0 +1,14 @@ +package com.baeldung.range + +fun main(args: Array) { + + 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()) +} \ No newline at end of file diff --git a/core-kotlin-2/src/main/com/baeldung/range/Range.kt b/core-kotlin-2/src/main/com/baeldung/range/Range.kt new file mode 100644 index 0000000000..c313181599 --- /dev/null +++ b/core-kotlin-2/src/main/com/baeldung/range/Range.kt @@ -0,0 +1,28 @@ +package com.baeldung.range + +fun main(args: Array) { + + 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) + } +} diff --git a/core-kotlin-2/src/main/com/baeldung/range/ReverseRange.kt b/core-kotlin-2/src/main/com/baeldung/range/ReverseRange.kt new file mode 100644 index 0000000000..875cf62200 --- /dev/null +++ b/core-kotlin-2/src/main/com/baeldung/range/ReverseRange.kt @@ -0,0 +1,14 @@ +package com.baeldung.range + +fun main(args: Array) { + + (1..9).reversed().forEach { + print(it) + } + + println() + + (1..9).reversed().step(3).forEach { + print(it) + } +} \ No newline at end of file diff --git a/core-kotlin-2/src/main/com/baeldung/range/Step.kt b/core-kotlin-2/src/main/com/baeldung/range/Step.kt new file mode 100644 index 0000000000..b9c5d48588 --- /dev/null +++ b/core-kotlin-2/src/main/com/baeldung/range/Step.kt @@ -0,0 +1,15 @@ +package com.baeldung.range + +fun main(args: Array) { + + for(i in 1..9 step 2){ + print(i) + } + + println() + + for (i in 9 downTo 1 step 2){ + print(i) + } + +} \ No newline at end of file diff --git a/core-kotlin-2/src/main/com/baeldung/range/UntilRange.kt b/core-kotlin-2/src/main/com/baeldung/range/UntilRange.kt new file mode 100644 index 0000000000..2c116a286f --- /dev/null +++ b/core-kotlin-2/src/main/com/baeldung/range/UntilRange.kt @@ -0,0 +1,8 @@ +package com.baeldung.range + +fun main(args: Array) { + + for (i in 1 until 9) { + print(i) + } +} \ No newline at end of file From bad8753fd17cad0f3474914435fc81ddba4f9e76 Mon Sep 17 00:00:00 2001 From: Rodrigo Graciano Date: Mon, 18 Feb 2019 22:41:01 -0500 Subject: [PATCH 2/4] BAEL-2648 --- core-kotlin-2/pom.xml | 64 ++++++++++++++++++- .../com/baeldung/range/CharRange.kt | 0 .../{ => kotlin}/com/baeldung/range/Filter.kt | 0 .../com/baeldung/range/FirstLast.kt | 0 .../com/baeldung/range/OtherRangeFunctions.kt | 0 .../{ => kotlin}/com/baeldung/range/Range.kt | 0 .../com/baeldung/range/ReverseRange.kt | 0 .../{ => kotlin}/com/baeldung/range/Step.kt | 0 .../com/baeldung/range/UntilRange.kt | 0 .../com/baeldung/range/CharRangeTest.kt | 17 +++++ .../kotlin/com/baeldung/range/FilterTest.kt | 24 +++++++ .../com/baeldung/range/FirstLastTest.kt | 22 +++++++ .../baeldung/range/OtherRangeFunctionsTest.kt | 40 ++++++++++++ .../kotlin/com/baeldung/range/RangeTest.kt | 22 +++++++ .../com/baeldung/range/ReverseRangeTest.kt | 12 ++++ .../kotlin/com/baeldung/range/StepTest.kt | 17 +++++ .../com/baeldung/range/UntilRangeTest.kt | 12 ++++ 17 files changed, 229 insertions(+), 1 deletion(-) rename core-kotlin-2/src/main/{ => kotlin}/com/baeldung/range/CharRange.kt (100%) rename core-kotlin-2/src/main/{ => kotlin}/com/baeldung/range/Filter.kt (100%) rename core-kotlin-2/src/main/{ => kotlin}/com/baeldung/range/FirstLast.kt (100%) rename core-kotlin-2/src/main/{ => kotlin}/com/baeldung/range/OtherRangeFunctions.kt (100%) rename core-kotlin-2/src/main/{ => kotlin}/com/baeldung/range/Range.kt (100%) rename core-kotlin-2/src/main/{ => kotlin}/com/baeldung/range/ReverseRange.kt (100%) rename core-kotlin-2/src/main/{ => kotlin}/com/baeldung/range/Step.kt (100%) rename core-kotlin-2/src/main/{ => kotlin}/com/baeldung/range/UntilRange.kt (100%) create mode 100644 core-kotlin-2/src/test/kotlin/com/baeldung/range/CharRangeTest.kt create mode 100644 core-kotlin-2/src/test/kotlin/com/baeldung/range/FilterTest.kt create mode 100644 core-kotlin-2/src/test/kotlin/com/baeldung/range/FirstLastTest.kt create mode 100644 core-kotlin-2/src/test/kotlin/com/baeldung/range/OtherRangeFunctionsTest.kt create mode 100644 core-kotlin-2/src/test/kotlin/com/baeldung/range/RangeTest.kt create mode 100644 core-kotlin-2/src/test/kotlin/com/baeldung/range/ReverseRangeTest.kt create mode 100644 core-kotlin-2/src/test/kotlin/com/baeldung/range/StepTest.kt create mode 100644 core-kotlin-2/src/test/kotlin/com/baeldung/range/UntilRangeTest.kt diff --git a/core-kotlin-2/pom.xml b/core-kotlin-2/pom.xml index 81df3cee81..e329611593 100644 --- a/core-kotlin-2/pom.xml +++ b/core-kotlin-2/pom.xml @@ -13,4 +13,66 @@ ../parent-kotlin - + + + org.jetbrains.kotlin + kotlin-stdlib-jdk8 + ${kotlin.version} + + + org.junit.platform + junit-platform-runner + ${junit.platform.version} + test + + + org.assertj + assertj-core + ${assertj.version} + test + + + org.jetbrains.kotlin + kotlin-test + ${kotlin.version} + test + + + + + + + org.jetbrains.kotlin + kotlin-maven-plugin + ${kotlin.version} + + + compile + compile + + compile + + + + test-compile + test-compile + + test-compile + + + + + 1.8 + + + + + + + 1.2.71 + 1.1.1 + 5.2.0 + 3.10.0 + + + \ No newline at end of file diff --git a/core-kotlin-2/src/main/com/baeldung/range/CharRange.kt b/core-kotlin-2/src/main/kotlin/com/baeldung/range/CharRange.kt similarity index 100% rename from core-kotlin-2/src/main/com/baeldung/range/CharRange.kt rename to core-kotlin-2/src/main/kotlin/com/baeldung/range/CharRange.kt diff --git a/core-kotlin-2/src/main/com/baeldung/range/Filter.kt b/core-kotlin-2/src/main/kotlin/com/baeldung/range/Filter.kt similarity index 100% rename from core-kotlin-2/src/main/com/baeldung/range/Filter.kt rename to core-kotlin-2/src/main/kotlin/com/baeldung/range/Filter.kt diff --git a/core-kotlin-2/src/main/com/baeldung/range/FirstLast.kt b/core-kotlin-2/src/main/kotlin/com/baeldung/range/FirstLast.kt similarity index 100% rename from core-kotlin-2/src/main/com/baeldung/range/FirstLast.kt rename to core-kotlin-2/src/main/kotlin/com/baeldung/range/FirstLast.kt diff --git a/core-kotlin-2/src/main/com/baeldung/range/OtherRangeFunctions.kt b/core-kotlin-2/src/main/kotlin/com/baeldung/range/OtherRangeFunctions.kt similarity index 100% rename from core-kotlin-2/src/main/com/baeldung/range/OtherRangeFunctions.kt rename to core-kotlin-2/src/main/kotlin/com/baeldung/range/OtherRangeFunctions.kt diff --git a/core-kotlin-2/src/main/com/baeldung/range/Range.kt b/core-kotlin-2/src/main/kotlin/com/baeldung/range/Range.kt similarity index 100% rename from core-kotlin-2/src/main/com/baeldung/range/Range.kt rename to core-kotlin-2/src/main/kotlin/com/baeldung/range/Range.kt diff --git a/core-kotlin-2/src/main/com/baeldung/range/ReverseRange.kt b/core-kotlin-2/src/main/kotlin/com/baeldung/range/ReverseRange.kt similarity index 100% rename from core-kotlin-2/src/main/com/baeldung/range/ReverseRange.kt rename to core-kotlin-2/src/main/kotlin/com/baeldung/range/ReverseRange.kt diff --git a/core-kotlin-2/src/main/com/baeldung/range/Step.kt b/core-kotlin-2/src/main/kotlin/com/baeldung/range/Step.kt similarity index 100% rename from core-kotlin-2/src/main/com/baeldung/range/Step.kt rename to core-kotlin-2/src/main/kotlin/com/baeldung/range/Step.kt diff --git a/core-kotlin-2/src/main/com/baeldung/range/UntilRange.kt b/core-kotlin-2/src/main/kotlin/com/baeldung/range/UntilRange.kt similarity index 100% rename from core-kotlin-2/src/main/com/baeldung/range/UntilRange.kt rename to core-kotlin-2/src/main/kotlin/com/baeldung/range/UntilRange.kt diff --git a/core-kotlin-2/src/test/kotlin/com/baeldung/range/CharRangeTest.kt b/core-kotlin-2/src/test/kotlin/com/baeldung/range/CharRangeTest.kt new file mode 100644 index 0000000000..0e23f508b6 --- /dev/null +++ b/core-kotlin-2/src/test/kotlin/com/baeldung/range/CharRangeTest.kt @@ -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()) + } +} \ No newline at end of file diff --git a/core-kotlin-2/src/test/kotlin/com/baeldung/range/FilterTest.kt b/core-kotlin-2/src/test/kotlin/com/baeldung/range/FilterTest.kt new file mode 100644 index 0000000000..d0e2df8860 --- /dev/null +++ b/core-kotlin-2/src/test/kotlin/com/baeldung/range/FilterTest.kt @@ -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 }) + } +} \ No newline at end of file diff --git a/core-kotlin-2/src/test/kotlin/com/baeldung/range/FirstLastTest.kt b/core-kotlin-2/src/test/kotlin/com/baeldung/range/FirstLastTest.kt new file mode 100644 index 0000000000..ca797e9c9b --- /dev/null +++ b/core-kotlin-2/src/test/kotlin/com/baeldung/range/FirstLastTest.kt @@ -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) + } +} \ No newline at end of file diff --git a/core-kotlin-2/src/test/kotlin/com/baeldung/range/OtherRangeFunctionsTest.kt b/core-kotlin-2/src/test/kotlin/com/baeldung/range/OtherRangeFunctionsTest.kt new file mode 100644 index 0000000000..d2d36bbfae --- /dev/null +++ b/core-kotlin-2/src/test/kotlin/com/baeldung/range/OtherRangeFunctionsTest.kt @@ -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()) + } +} \ No newline at end of file diff --git a/core-kotlin-2/src/test/kotlin/com/baeldung/range/RangeTest.kt b/core-kotlin-2/src/test/kotlin/com/baeldung/range/RangeTest.kt new file mode 100644 index 0000000000..48fa483924 --- /dev/null +++ b/core-kotlin-2/src/test/kotlin/com/baeldung/range/RangeTest.kt @@ -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())) + } +} \ No newline at end of file diff --git a/core-kotlin-2/src/test/kotlin/com/baeldung/range/ReverseRangeTest.kt b/core-kotlin-2/src/test/kotlin/com/baeldung/range/ReverseRangeTest.kt new file mode 100644 index 0000000000..7e1c7badb7 --- /dev/null +++ b/core-kotlin-2/src/test/kotlin/com/baeldung/range/ReverseRangeTest.kt @@ -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()) + } +} \ No newline at end of file diff --git a/core-kotlin-2/src/test/kotlin/com/baeldung/range/StepTest.kt b/core-kotlin-2/src/test/kotlin/com/baeldung/range/StepTest.kt new file mode 100644 index 0000000000..4570ceeb0a --- /dev/null +++ b/core-kotlin-2/src/test/kotlin/com/baeldung/range/StepTest.kt @@ -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()) + } +} \ No newline at end of file diff --git a/core-kotlin-2/src/test/kotlin/com/baeldung/range/UntilRangeTest.kt b/core-kotlin-2/src/test/kotlin/com/baeldung/range/UntilRangeTest.kt new file mode 100644 index 0000000000..f941c7f1e6 --- /dev/null +++ b/core-kotlin-2/src/test/kotlin/com/baeldung/range/UntilRangeTest.kt @@ -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()) + } +} \ No newline at end of file From b98294aaed0bf6cae91937b7f282c01f8b0e54b8 Mon Sep 17 00:00:00 2001 From: Rodrigo Graciano Date: Tue, 19 Feb 2019 23:01:34 -0500 Subject: [PATCH 3/4] BAEL-2648 --- .../main/kotlin/com/baeldung/range/Color.kt | 21 +++++++++++++++++++ .../kotlin/com/baeldung/range/ColorTest.kt | 20 ++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 core-kotlin-2/src/main/kotlin/com/baeldung/range/Color.kt create mode 100644 core-kotlin-2/src/test/kotlin/com/baeldung/range/ColorTest.kt diff --git a/core-kotlin-2/src/main/kotlin/com/baeldung/range/Color.kt b/core-kotlin-2/src/main/kotlin/com/baeldung/range/Color.kt new file mode 100644 index 0000000000..ef7adf06b5 --- /dev/null +++ b/core-kotlin-2/src/main/kotlin/com/baeldung/range/Color.kt @@ -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) { + + 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)) +} \ No newline at end of file diff --git a/core-kotlin-2/src/test/kotlin/com/baeldung/range/ColorTest.kt b/core-kotlin-2/src/test/kotlin/com/baeldung/range/ColorTest.kt new file mode 100644 index 0000000000..4ac3270fcc --- /dev/null +++ b/core-kotlin-2/src/test/kotlin/com/baeldung/range/ColorTest.kt @@ -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) } + } +} \ No newline at end of file From da2f95bfa39dc24a8a12750e28497618c67eb41d Mon Sep 17 00:00:00 2001 From: Rodrigo Graciano Date: Sun, 10 Mar 2019 20:34:19 -0400 Subject: [PATCH 4/4] BAEL-2760 --- .../kotlin/com/baeldung/range/CustomColor.kt | 56 +++++++++++++++++++ .../com/baeldung/range/CustomColorTest.kt | 41 ++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 core-kotlin-2/src/main/kotlin/com/baeldung/range/CustomColor.kt create mode 100644 core-kotlin-2/src/test/kotlin/com/baeldung/range/CustomColorTest.kt diff --git a/core-kotlin-2/src/main/kotlin/com/baeldung/range/CustomColor.kt b/core-kotlin-2/src/main/kotlin/com/baeldung/range/CustomColor.kt new file mode 100644 index 0000000000..b4fed13b18 --- /dev/null +++ b/core-kotlin-2/src/main/kotlin/com/baeldung/range/CustomColor.kt @@ -0,0 +1,56 @@ +package com.baeldung.range + +import java.lang.IllegalStateException + +class CustomColor(val rgb: Int): Comparable { + + override fun compareTo(other: CustomColor): Int { + return this.rgb.compareTo(other.rgb) + } + + operator fun rangeTo(that: CustomColor) = ColorRange(this,that) + + operator fun inc(): CustomColor { + return CustomColor(rgb + 1) + } + + init { + if(rgb < 0x000000 || rgb > 0xFFFFFF){ + throw IllegalStateException("RGB must be between 0 and 16777215") + } + } + + override fun toString(): String { + return "CustomColor(rgb=$rgb)" + } +} +class ColorRange(override val start: CustomColor, + override val endInclusive: CustomColor) : ClosedRange, Iterable{ + + override fun iterator(): Iterator { + return ColorIterator(start, endInclusive) + } +} + +class ColorIterator(val start: CustomColor, val endInclusive: CustomColor) : Iterator { + + var initValue = start + + override fun hasNext(): Boolean { + return initValue <= endInclusive + } + + override fun next(): CustomColor { + return initValue++ + } +} + +fun main(args: Array) { + val a = CustomColor(0xABCDEF) + val b = CustomColor(-1) + val c = CustomColor(0xABCDFF) + + for(color in a..c){ + println(color) + } +} \ No newline at end of file diff --git a/core-kotlin-2/src/test/kotlin/com/baeldung/range/CustomColorTest.kt b/core-kotlin-2/src/test/kotlin/com/baeldung/range/CustomColorTest.kt new file mode 100644 index 0000000000..8c8795ac42 --- /dev/null +++ b/core-kotlin-2/src/test/kotlin/com/baeldung/range/CustomColorTest.kt @@ -0,0 +1,41 @@ +package com.baeldung.range + +import org.junit.Test +import java.lang.IllegalStateException +import kotlin.test.assertFailsWith +import kotlin.test.assertTrue + +class CustomColorTest { + + @Test + fun testInvalidConstructor(){ + assertFailsWith(IllegalStateException::class){ + CustomColor(-1) + } + } + + @Test + fun assertHas10Colors(){ + assertTrue { + val a = CustomColor(1) + val b = CustomColor(10) + val range = a..b + for(cc in range){ + println(cc) + } + range.toList().size == 10 + } + } + + @Test + fun assertContains0xCCCCCC(){ + assertTrue { + val a = CustomColor(0xBBBBBB) + val b = CustomColor(0xDDDDDD) + val range = a..b + range.contains(CustomColor(0xCCCCCC)) + } + } + +} +