From d178c4ff0d8936c59ad040bc6600f6c3d6c308ab Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Tue, 2 Nov 2021 10:50:38 +0300 Subject: [PATCH] Fix number comparison bug in value --- CHANGELOG.md | 1 + build.gradle.kts | 2 +- dataforge-context/build.gradle.kts | 4 +-- .../space/kscience/dataforge/data/Goal.kt | 2 ++ .../space/kscience/dataforge/io/IOPlugin.kt | 9 +++--- .../space/kscience/dataforge/values/Value.kt | 19 +++++------ .../kscience/dataforge/values/exoticValues.kt | 4 +-- .../kscience/dataforge/meta/SchemeTest.kt | 15 +++++++++ .../dataforge/meta/SpecificationTest.kt | 2 ++ .../dataforge/values/ValueEqualityTest.kt | 32 +++++++++++++++++++ .../dataforge/workspace/WorkspaceBuilder.kt | 12 ++++--- settings.gradle.kts | 2 +- 12 files changed, 80 insertions(+), 24 deletions(-) create mode 100644 dataforge-meta/src/commonTest/kotlin/space/kscience/dataforge/values/ValueEqualityTest.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 14f6ed73..c2e63dc1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ ### Fixed - MutableMetaImpl attachment and checks - Listeners in observable meta are replaced by lists +- JS number comparison bug. ### Security ## [0.5.0] diff --git a/build.gradle.kts b/build.gradle.kts index 42505cb9..97c9b3f8 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -4,7 +4,7 @@ plugins { allprojects { group = "space.kscience" - version = "0.5.2-dev-1" + version = "0.5.2-dev-3" repositories{ mavenCentral() } diff --git a/dataforge-context/build.gradle.kts b/dataforge-context/build.gradle.kts index d090e9de..4d476c50 100644 --- a/dataforge-context/build.gradle.kts +++ b/dataforge-context/build.gradle.kts @@ -11,7 +11,7 @@ kscience { kotlin { sourceSets { - val commonMain by getting{ + val commonMain by getting { dependencies { api(project(":dataforge-meta")) } @@ -30,6 +30,6 @@ kotlin { } } -readme{ +readme { maturity = ru.mipt.npm.gradle.Maturity.DEVELOPMENT } \ No newline at end of file diff --git a/dataforge-data/src/commonMain/kotlin/space/kscience/dataforge/data/Goal.kt b/dataforge-data/src/commonMain/kotlin/space/kscience/dataforge/data/Goal.kt index b7ddea96..678711c1 100644 --- a/dataforge-data/src/commonMain/kotlin/space/kscience/dataforge/data/Goal.kt +++ b/dataforge-data/src/commonMain/kotlin/space/kscience/dataforge/data/Goal.kt @@ -1,6 +1,7 @@ package space.kscience.dataforge.data import kotlinx.coroutines.* +import space.kscience.dataforge.misc.DFExperimental import kotlin.coroutines.CoroutineContext import kotlin.coroutines.EmptyCoroutineContext @@ -66,6 +67,7 @@ public open class LazyGoal( * If [GoalExecutionRestriction] is present in the [coroutineScope] context, the call could produce a error a warning * depending on the settings. */ + @OptIn(DFExperimental::class) override fun async(coroutineScope: CoroutineScope): Deferred { val log = coroutineScope.coroutineContext[GoalLogger] // Check if context restricts goal computation diff --git a/dataforge-io/src/commonMain/kotlin/space/kscience/dataforge/io/IOPlugin.kt b/dataforge-io/src/commonMain/kotlin/space/kscience/dataforge/io/IOPlugin.kt index 66281f19..daf5ba41 100644 --- a/dataforge-io/src/commonMain/kotlin/space/kscience/dataforge/io/IOPlugin.kt +++ b/dataforge-io/src/commonMain/kotlin/space/kscience/dataforge/io/IOPlugin.kt @@ -10,7 +10,6 @@ import space.kscience.dataforge.meta.Meta import space.kscience.dataforge.meta.get import space.kscience.dataforge.meta.string import space.kscience.dataforge.names.Name -import space.kscience.dataforge.names.asName import kotlin.reflect.KClass public class IOPlugin(meta: Meta) : AbstractPlugin(meta) { @@ -74,11 +73,13 @@ public class IOPlugin(meta: Meta) : AbstractPlugin(meta) { } } +internal val ioContext = Context("IO") { + plugin(IOPlugin) +} + public val Context.io: IOPlugin get() = if (this == Global) { - Global.buildContext("IO".asName()) { - plugin(IOPlugin) - }.fetch(IOPlugin) + ioContext.fetch(IOPlugin) } else { fetch(IOPlugin) } \ No newline at end of file diff --git a/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/values/Value.kt b/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/values/Value.kt index 707c86f6..93ea1994 100644 --- a/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/values/Value.kt +++ b/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/values/Value.kt @@ -140,14 +140,17 @@ public class NumberValue(public val number: Number) : Value { val otherNumber = other.numberOrNull ?: return false + if(number == otherNumber) return true + + //Do not change the order of comparison. On JS number is the instance of all types return when (numberOrNull) { - is Short -> number.toShort() == otherNumber.toShort() - is Long -> number.toLong() == otherNumber.toLong() - is Byte -> number.toByte() == otherNumber.toByte() - is Int -> number.toInt() == otherNumber.toInt() - is Float -> number.toFloat() == otherNumber.toFloat() is Double -> number.toDouble() == otherNumber.toDouble() - else -> number.toString() == otherNumber.toString() + is Float -> number.toFloat() == otherNumber.toFloat() + is Long -> number.toLong() == otherNumber.toLong() + is Short -> number.toShort() == otherNumber.toShort() + is Int -> number.toInt() == otherNumber.toInt() + is Byte -> number.toByte() == otherNumber.toByte() + else -> false } } @@ -188,9 +191,7 @@ public class ListValue(override val list: List) : Value, Iterable return list == other.list } - override fun hashCode(): Int { - return list.hashCode() - } + override fun hashCode(): Int = list.hashCode() public companion object { public val EMPTY: ListValue = ListValue(emptyList()) diff --git a/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/values/exoticValues.kt b/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/values/exoticValues.kt index d3fc66b5..218ef5ad 100644 --- a/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/values/exoticValues.kt +++ b/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/values/exoticValues.kt @@ -36,9 +36,7 @@ public class DoubleArrayValue(override val value: DoubleArray) : Value, Iterable } } - override fun hashCode(): Int { - return value.contentHashCode() - } + override fun hashCode(): Int = value.contentHashCode() override fun toString(): String = list.joinToString(prefix = "[", postfix = "]") diff --git a/dataforge-meta/src/commonTest/kotlin/space/kscience/dataforge/meta/SchemeTest.kt b/dataforge-meta/src/commonTest/kotlin/space/kscience/dataforge/meta/SchemeTest.kt index 87948ec9..ddf81780 100644 --- a/dataforge-meta/src/commonTest/kotlin/space/kscience/dataforge/meta/SchemeTest.kt +++ b/dataforge-meta/src/commonTest/kotlin/space/kscience/dataforge/meta/SchemeTest.kt @@ -1,8 +1,11 @@ package space.kscience.dataforge.meta import space.kscience.dataforge.misc.DFExperimental +import space.kscience.dataforge.values.ListValue +import space.kscience.dataforge.values.Value import kotlin.test.Test import kotlin.test.assertEquals +import kotlin.test.assertNotNull @DFExperimental class SchemeTest { @@ -33,4 +36,16 @@ class SchemeTest { scheme.a = 2 assertEquals(2, flag) } + + @Test + fun testListSubscription(){ + val scheme = TestScheme.empty() + var value: Value? = null + scheme.v = ListValue(0.0,0.0,0.0) + scheme.useProperty(TestScheme::v){ + value = it + } + scheme.v = ListValue(1.0, 2.0, 3.0) + assertNotNull(value) + } } \ No newline at end of file diff --git a/dataforge-meta/src/commonTest/kotlin/space/kscience/dataforge/meta/SpecificationTest.kt b/dataforge-meta/src/commonTest/kotlin/space/kscience/dataforge/meta/SpecificationTest.kt index 3a61363c..8d4d3537 100644 --- a/dataforge-meta/src/commonTest/kotlin/space/kscience/dataforge/meta/SpecificationTest.kt +++ b/dataforge-meta/src/commonTest/kotlin/space/kscience/dataforge/meta/SpecificationTest.kt @@ -9,6 +9,8 @@ internal class TestScheme : Scheme() { var a by int() var b by string() + var v by value() + companion object : SchemeSpec(::TestScheme) } diff --git a/dataforge-meta/src/commonTest/kotlin/space/kscience/dataforge/values/ValueEqualityTest.kt b/dataforge-meta/src/commonTest/kotlin/space/kscience/dataforge/values/ValueEqualityTest.kt new file mode 100644 index 00000000..cfc61e01 --- /dev/null +++ b/dataforge-meta/src/commonTest/kotlin/space/kscience/dataforge/values/ValueEqualityTest.kt @@ -0,0 +1,32 @@ +package space.kscience.dataforge.values + +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertNotEquals + +class ValueEqualityTest { + @Test + fun numberValueNotEquals(){ + val a = 0.33.asValue() + val b = 0.34.asValue() + + println(a.number == b.number) + + assertNotEquals(a,b) + } + + @Test + fun arrayEqualsList() { + val v1 = doubleArrayOf(1.0, 2.0, 3.0).asValue() + val v2 = listOf(1, 2, 3).map { it.asValue() }.asValue() + assertEquals(v1, v2) + } + + @Test + fun notEquals() { + val v1 = doubleArrayOf(1.0, 2.0, 3.0).asValue() + val v2 = listOf(1, 2, 6).map { it.asValue() }.asValue() + assertNotEquals(v1, v2) + } + +} \ No newline at end of file diff --git a/dataforge-workspace/src/commonMain/kotlin/space/kscience/dataforge/workspace/WorkspaceBuilder.kt b/dataforge-workspace/src/commonMain/kotlin/space/kscience/dataforge/workspace/WorkspaceBuilder.kt index 2d5715de..ec078020 100644 --- a/dataforge-workspace/src/commonMain/kotlin/space/kscience/dataforge/workspace/WorkspaceBuilder.kt +++ b/dataforge-workspace/src/commonMain/kotlin/space/kscience/dataforge/workspace/WorkspaceBuilder.kt @@ -18,7 +18,7 @@ import space.kscience.dataforge.names.asName import kotlin.properties.PropertyDelegateProvider import kotlin.properties.ReadOnlyProperty -public data class TaskReference(public val taskName: Name, public val task: Task): DataSelector{ +public data class TaskReference(public val taskName: Name, public val task: Task) : DataSelector { @Suppress("UNCHECKED_CAST") override suspend fun select(workspace: Workspace, meta: Meta): DataSet { @@ -35,7 +35,6 @@ public interface TaskContainer { public fun registerTask(taskName: Name, task: Task<*>) } - public inline fun TaskContainer.registerTask( name: String, noinline descriptorBuilder: MetaDescriptorBuilder.() -> Unit = {}, @@ -43,15 +42,20 @@ public inline fun TaskContainer.registerTask( ): Unit = registerTask(Name.parse(name), Task(MetaDescriptor(descriptorBuilder), builder)) public inline fun TaskContainer.task( - noinline descriptorBuilder: MetaDescriptorBuilder.() -> Unit = {}, + descriptor: MetaDescriptor, noinline builder: suspend TaskResultBuilder.() -> Unit, ): PropertyDelegateProvider>> = PropertyDelegateProvider { _, property -> val taskName = Name.parse(property.name) - val task = Task(MetaDescriptor(descriptorBuilder), builder) + val task = Task(descriptor, builder) registerTask(taskName, task) ReadOnlyProperty { _, _ -> TaskReference(taskName, task) } } +public inline fun TaskContainer.task( + noinline descriptorBuilder: MetaDescriptorBuilder.() -> Unit = {}, + noinline builder: suspend TaskResultBuilder.() -> Unit, +): PropertyDelegateProvider>> = + task(MetaDescriptor(descriptorBuilder), builder) public class WorkspaceBuilder(private val parentContext: Context = Global) : TaskContainer { private var context: Context? = null diff --git a/settings.gradle.kts b/settings.gradle.kts index 26f6fcc4..fcf4a710 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -5,7 +5,7 @@ pluginManagement { gradlePluginPortal() } - val toolsVersion = "0.10.4" + val toolsVersion = "0.10.5" plugins { id("ru.mipt.npm.gradle.project") version toolsVersion