[Rprotobuf-commits] r597 - pkg/inst/unitTests

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Dec 27 02:30:04 CET 2013


Author: murray
Date: 2013-12-27 02:30:03 +0100 (Fri, 27 Dec 2013)
New Revision: 597

Added:
   pkg/inst/unitTests/runit.int32.R
Log:
Add a unit test of setting int32 fields with character vectors and
make sure we correctly raise exceptions in the appropriate cases
rather than crashing R since Rcpp::as<int> can't handle string types.



Added: pkg/inst/unitTests/runit.int32.R
===================================================================
--- pkg/inst/unitTests/runit.int32.R	                        (rev 0)
+++ pkg/inst/unitTests/runit.int32.R	2013-12-27 01:30:03 UTC (rev 597)
@@ -0,0 +1,49 @@
+# 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.int32 <- function() {
+    if (!exists("protobuf_unittest.TestAllTypes",
+                "RProtoBuf:DescriptorPool")) {
+        unittest.proto.file <- system.file("unitTests", "data",
+                                           "unittest.proto",
+                                           package="RProtoBuf")
+        readProtoFiles(file=unittest.proto.file)
+    }
+
+    a <- new(protobuf_unittest.TestAllTypes)
+    a$repeated_int32 <- 1
+    # Verify we can set character strings
+    a$repeated_int32 <- c("9007", "9008")
+    checkEquals(length(a$repeated_int32), 2)
+    # Verify we can't set any garbage string to a repeated int32.
+    checkException(a$repeated_int32 <-c("invalid", "invalid"))
+    checkException(a$repeated_int32 <-c("33-"))
+
+    a$optional_int32 <- 1
+    a$optional_int32 <- "2"
+    checkEquals(a$optional_int32, 2)
+    # Verify we can't set any garbage string to an optional int32.
+    checkException(a$optional_int32 <- "invalid")
+
+    a$optional_uint32 <- 10000
+    a$optional_uint32 <- "200000"
+    checkEquals(a$optional_uint32, 200000)
+    # Verify we can't set any garbage string to an optional uint32.
+    checkException(a$optional_uint32 <- "invalid")
+
+    a$repeated_uint32 <- c("9007", "9008")
+    checkEquals(length(a$repeated_uint32), 2)
+}



More information about the Rprotobuf-commits mailing list