[Rprotobuf-commits] r629 - in pkg: . inst/unitTests src

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Mon Dec 30 18:51:28 CET 2013


Author: murray
Date: 2013-12-30 18:51:28 +0100 (Mon, 30 Dec 2013)
New Revision: 629

Added:
   pkg/inst/unitTests/runit.extremevalues.R
Modified:
   pkg/ChangeLog
   pkg/src/Rcppsupport.h
   pkg/src/extractors.cpp
Log:
Finish correcting our uint handling and add a unit test verifying we
can store 2^32 -1 properly in repeated or optional uint32 protobuf fields.



Modified: pkg/ChangeLog
===================================================================
--- pkg/ChangeLog	2013-12-30 17:20:59 UTC (rev 628)
+++ pkg/ChangeLog	2013-12-30 17:51:28 UTC (rev 629)
@@ -1,3 +1,11 @@
+2013-12-30  Murray Stokely  <mstokely at google.com>
+
+	* inst/unitTests/runit.extremevalues.R (test.uint32): Add test
+	  verifying that we can store 2^32 - 1 properly in repeated or
+	  optional protobuf fields.
+	* src/extractors.cpp (rprotobuf): Correct handling of uint32 for
+	  repeated fields.
+	
 2013-12-28  Murray Stokely  <mstokely at google.com>
 
 	* src/extractors.cpp (rprotobuf): Correct handling of uint32 for

Added: pkg/inst/unitTests/runit.extremevalues.R
===================================================================
--- pkg/inst/unitTests/runit.extremevalues.R	                        (rev 0)
+++ pkg/inst/unitTests/runit.extremevalues.R	2013-12-30 17:51:28 UTC (rev 629)
@@ -0,0 +1,32 @@
+# Copyright 2013 Google Inc.
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+
+test.uint32 <- function() {
+    if (!exists("protobuf_unittest.TestAllTypes",
+                "RProtoBuf:DescriptorPool")) {
+        unittest.proto.file <- system.file("unitTests", "data",
+                                           "unittest.proto",
+                                           package="RProtoBuf")
+        readProtoFiles(file=unittest.proto.file)
+    }
+
+    foo <- new(protobuf_unittest.TestAllTypes)
+    foo$optional_uint32 <- 2^32 - 1
+    foo$repeated_uint32 <- c(foo$optional_uint32, foo$optional_uint32)
+    checkEquals(as.character(foo$optional_uint32),
+                "4294967295")
+    checkEquals(foo$optional_uint32,
+                foo$repeated_uint32[[1]])

Modified: pkg/src/Rcppsupport.h
===================================================================
--- pkg/src/Rcppsupport.h	2013-12-30 17:20:59 UTC (rev 628)
+++ pkg/src/Rcppsupport.h	2013-12-30 17:51:28 UTC (rev 629)
@@ -78,6 +78,29 @@
     const GPB::FieldDescriptor* field ;
 };
 
+// TODO(mstokely): Rcpp doesn't handle uint32 properly as of 2013/12
+// See https://r-forge.r-project.org/tracker/index.php?func=detail&aid=1360&group_id=155&atid=637
+class UInt32RepeatedFieldImporter {
+public:
+    // Represent as doubles, since R doesn't have uint32s.
+    typedef double r_import_type;
+    UInt32RepeatedFieldImporter(const GPB::Reflection* ref_ ,
+				const GPB::Message& message_,
+				const GPB::FieldDescriptor* field_):
+            ref(ref_), message(message_), field(field_){}
+    inline int size() const {
+        return ref->FieldSize( message, field ) ;
+    }
+    inline double get(int i) const {
+	return double(ref->GetRepeatedUInt32(message, field, i));
+    }
+  private:
+    const GPB::Reflection* ref ;
+    const GPB::Message& message ;
+    const GPB::FieldDescriptor* field ;
+};
+
+
 template <typename T> class RepeatedFieldImporter{} ;
 
 #undef GENERATE__FIELD__IMPORTER__DECL

Modified: pkg/src/extractors.cpp
===================================================================
--- pkg/src/extractors.cpp	2013-12-30 17:20:59 UTC (rev 628)
+++ pkg/src/extractors.cpp	2013-12-30 17:51:28 UTC (rev 629)
@@ -97,14 +97,17 @@
 			return Rcpp::wrap( RepeatedFieldImporter<DATATYPE>(ref, *message, fieldDesc) ) ; \
 
 			HANDLE_REPEATED_FIELD(CPPTYPE_INT32, GPB::int32) ;
-			// TODO(mstokely): Rcpp doesn't handle uint32 properly as of 2013/12
-			// See https://r-forge.r-project.org/tracker/index.php?func=detail&aid=1360&group_id=155&atid=637
-    		HANDLE_REPEATED_FIELD(CPPTYPE_UINT32, GPB::uint32) ;
     		HANDLE_REPEATED_FIELD(CPPTYPE_DOUBLE, double) ;
     		HANDLE_REPEATED_FIELD(CPPTYPE_FLOAT, float) ;
     		HANDLE_REPEATED_FIELD(CPPTYPE_BOOL, bool) ;
     		HANDLE_REPEATED_FIELD(CPPTYPE_ENUM, enum_field ) ;
     		HANDLE_REPEATED_FIELD(CPPTYPE_MESSAGE, message_field ) ;
+			// TODO(mstokely): Rcpp doesn't handle uint32 properly as of 2013/12
+			// See https://r-forge.r-project.org/tracker/index.php?func=detail&aid=1360&group_id=155&atid=637
+			case CPPTYPE_UINT32:
+				return Rcpp::wrap(
+						UInt32RepeatedFieldImporter(ref, *message,
+													fieldDesc));
 #ifdef RCPP_HAS_LONG_LONG_TYPES
             // We can't handle these the same way, because Rcpp::wrap silently
             // casts int64s to doubles which may cause us to lose precision.



More information about the Rprotobuf-commits mailing list