You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@daffodil.apache.org by GitBox <gi...@apache.org> on 2021/11/08 14:15:34 UTC

[GitHub] [daffodil] stevedlawrence commented on a change in pull request #678: long to double function - for preserving binary data floating point p…

stevedlawrence commented on a change in pull request #678:
URL: https://github.com/apache/daffodil/pull/678#discussion_r744740877



##########
File path: daffodil-runtime1/src/main/scala/org/apache/daffodil/dpath/DFDLXFunctions.scala
##########
@@ -18,7 +18,7 @@
 package org.apache.daffodil.dpath
 
 import java.math.{BigInteger => JBigInt}
-
+import java.lang.Double

Review comment:
       The convention when important java numeric types is to import it with an added `J` to differentiate between Java and Scala types, i..e
   ```scala
   import java.lang.{Double => JDouble}
   ...
   JDouble.longBitsToDouble()
   ```

##########
File path: daffodil-runtime1/src/main/scala/org/apache/daffodil/dpath/DFDLXFunctions.scala
##########
@@ -269,3 +271,18 @@ case class DFDLXLookAhead(recipes: List[CompiledDPath])
     }
   }
 }
+
+
+case class DFDLXdoubleFromRawLong(recipes: CompiledDPath, argType: NodeInfo.Kind )
+  extends FNOneArg (recipes, argType) {

Review comment:
       There shouldn't be a space between `FNOneArg` and the open parenthesis.

##########
File path: daffodil-core/src/main/scala/org/apache/daffodil/dpath/Expression.scala
##########
@@ -1687,6 +1690,16 @@ case class FunctionCallExpression(functionQNameString: String, expressions: List
 
       //End typeValueCalc related functions
 
+      case (RefQName(_, "doubleFromRawLong", DFDLX), args) => {
+        FNOneArgExpr(functionQNameString, functionQName, args,
+           NodeInfo.Double, NodeInfo.Long, DFDLXdoubleFromRawLong(_, _))

Review comment:
       `double` should be capitalized the class name, i.e. `DFDLXDoubleFromRawLong`, same with `DFDLXDoubleToRawLong`

##########
File path: daffodil-runtime1/src/main/scala/org/apache/daffodil/dpath/DFDLXFunctions.scala
##########
@@ -269,3 +271,18 @@ case class DFDLXLookAhead(recipes: List[CompiledDPath])
     }
   }
 }
+
+
+case class DFDLXdoubleFromRawLong(recipes: CompiledDPath, argType: NodeInfo.Kind )
+  extends FNOneArg (recipes, argType) {
+  override def computeValue(a: DataValuePrimitive, dstate: DState): DataValueDouble = {

Review comment:
       `a` isn't  great parameter name. Something like `value` or `arg` is a bit more clear.

##########
File path: daffodil-test/src/test/resources/org/apache/daffodil/extensions/doubleToRawLong/doubleToRawLong.tdml
##########
@@ -0,0 +1,65 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  Licensed to the Apache Software Foundation (ASF) under one or more
+  contributor license agreements.  See the NOTICE file distributed with
+  this work for additional information regarding copyright ownership.
+  The ASF licenses this file to You 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.
+-->
+
+<tdml:testSuite xmlns:ex="http://example.com" xmlns="http://example.com"
+  xmlns:tdml="http://www.ibm.com/xmlns/dfdl/testData" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+  xmlns:dfdl="http://www.ogf.org/dfdl/dfdl-1.0/" xmlns:xs="http://www.w3.org/2001/XMLSchema"
+  xmlns:dfdlx="http://www.ogf.org/dfdl/dfdl-1.0/extensions"
+  xmlns:fn="http://www.w3.org/2005/xpath-functions"  >
+
+  <tdml:defineSchema name="doubleToRawLong.dfdl.xsd">
+
+    <xs:include schemaLocation="org/apache/daffodil/xsd/DFDLGeneralFormat.dfdl.xsd"/>
+    <dfdl:format ref="ex:GeneralFormat"
+      lengthUnits="bytes" lengthKind="explicit" length="8"
+      dfdlx:emptyElementParsePolicy="treatAsEmpty"
+      representation="binary"
+
+      />
+
+   <xs:element name="root">
+     <xs:complexType>
+       <xs:sequence>
+       <xs:element type="xs:double" name="DoubleValue"  dfdl:length="8"   />
+        <xs:element type="xs:long" name="LongValue" dfdl:inputValueCalc="{ dfdlx:doubleToRawLong(../ex:DoubleValue) }" />
+       </xs:sequence>
+     </xs:complexType>
+   </xs:element>
+
+  </tdml:defineSchema>
+  <tdml:parserTestCase name="doubleToRawLong"
+    root="root" model="doubleToRawLong.dfdl.xsd" description="Extensions - doubleToRawLong">
+    <tdml:document>
+   <tdml:documentPart type="byte">
+     5DC8 225C AF53 6E1F
+    </tdml:documentPart>
+    </tdml:document>
+
+    <tdml:infoset>
+      <tdml:dfdlInfoset xmlns:xs="http://www.w3.org/2001/XMLSchema"
+        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
+        <root>
+        <DoubleValue>5.886014640420331E143</DoubleValue>
+        <LongValue>6757689022343245343</LongValue>
+        </root>
+      </tdml:dfdlInfoset>
+    </tdml:infoset>
+  </tdml:parserTestCase>
+
+</tdml:testSuite>

Review comment:
       The indentation is inconsisent in this file. Suggest fixing it up or running it through a formatter.

##########
File path: daffodil-test/src/test/resources/org/apache/daffodil/extensions/doubleFromRawLong/doubleFromRawLong.tdml
##########
@@ -0,0 +1,65 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  Licensed to the Apache Software Foundation (ASF) under one or more
+  contributor license agreements.  See the NOTICE file distributed with
+  this work for additional information regarding copyright ownership.
+  The ASF licenses this file to You 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.
+-->
+
+<tdml:testSuite xmlns:ex="http://example.com" xmlns="http://example.com"
+  xmlns:tdml="http://www.ibm.com/xmlns/dfdl/testData" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+  xmlns:dfdl="http://www.ogf.org/dfdl/dfdl-1.0/" xmlns:xs="http://www.w3.org/2001/XMLSchema"
+  xmlns:dfdlx="http://www.ogf.org/dfdl/dfdl-1.0/extensions"
+  xmlns:fn="http://www.w3.org/2005/xpath-functions"  >
+
+  <tdml:defineSchema name="doubleFromRawLong.dfdl.xsd">
+
+    <xs:include schemaLocation="org/apache/daffodil/xsd/DFDLGeneralFormat.dfdl.xsd"/>
+    <dfdl:format ref="ex:GeneralFormat"
+      lengthUnits="bytes" lengthKind="explicit" length="8"

Review comment:
       In general, it's a bad idea to have the default value include an explicit length. Even though it works for this test, that rarely makes sense. Instead, I would recommend you remove `lengthKind="explicit"` and `length="8"` from this. The lengthKind value from GeneralFormat will be `implicit`, which is a sane default. And then you'll need to add `dfdl:lengthkind="explicit"` to the `LongValue` element. 
   
   You also shouldn't need `dfdlx:emptyElementParsePolicy="treatAsEmpty"` for this tests. I would suggest removing that.
   
   Same goes for your other tests.

##########
File path: daffodil-runtime1/src/main/scala/org/apache/daffodil/dpath/DFDLXFunctions.scala
##########
@@ -269,3 +271,18 @@ case class DFDLXLookAhead(recipes: List[CompiledDPath])
     }
   }
 }
+
+
+case class DFDLXdoubleFromRawLong(recipes: CompiledDPath, argType: NodeInfo.Kind )
+  extends FNOneArg (recipes, argType) {
+  override def computeValue(a: DataValuePrimitive, dstate: DState): DataValueDouble = {
+    Double.longBitsToDouble(a.getLong);

Review comment:
       No need for the semi colon in scala.

##########
File path: daffodil-test/src/test/scala/org/apache/daffodil/extensions/TestdoubleToRawLong.scala
##########
@@ -0,0 +1,42 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.daffodil.extensions
+
+import org.junit.Test
+import org.junit.Test
+import org.apache.daffodil.tdml.Runner
+import org.junit.AfterClass
+
+object TestdoubleToRawLong {
+  val testDir = "/org/apache/daffodil/extensions/doubleToRawLong/"
+
+  val runner = Runner(testDir, "doubleToRawLong.tdml", validateTDMLFile = false)
+
+
+  @AfterClass def shutDown: Unit = {
+    runner.reset
+
+  }
+
+}
+
+class TestdoubleToRawLong {
+  import TestdoubleToRawLong._
+  @Test def doubleToRawLong(): Unit = { runner.runOneTest("doubleToRawLong") }
+
+}

Review comment:
       Since each .tdml/.scala file only has a single test, I would suggest combining them into a single file. There's not much benefit in separating them, and just adds extra test suites/files to manage. In fact, it might even be better to just include them in an existing .tdml/.scala file. `Functions.tdml` and `TestDFDLExpressions.scala` might be a reasonable place to put these tests.

##########
File path: daffodil-core/src/main/scala/org/apache/daffodil/dpath/Expression.scala
##########
@@ -1663,6 +1663,9 @@ case class FunctionCallExpression(functionQNameString: String, expressions: List
           NodeInfo.Boolean, NodeInfo.String, DFDLContainsDFDLEntities(_, _))
       }
 
+
+
+

Review comment:
       In the future, avoid adding unrelated whitespace.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@daffodil.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org