From 4810df7fb979fb8920f8636ccb80a59eff2286c6 Mon Sep 17 00:00:00 2001 From: Waldir Leoncio Date: Tue, 3 Mar 2020 13:51:04 +0100 Subject: [PATCH] Added translated function + unit tests --- R/strcmp.R | 28 ++++++++++++++++++++ man/strcmp.Rd | 19 +++++++++++++ tests/testthat/test-convertedBaseFunctions.R | 17 ++++++++++++ 3 files changed, 64 insertions(+) create mode 100644 R/strcmp.R create mode 100644 man/strcmp.Rd diff --git a/R/strcmp.R b/R/strcmp.R new file mode 100644 index 0000000..c64cbce --- /dev/null +++ b/R/strcmp.R @@ -0,0 +1,28 @@ +#' @title Compare two character elements +#' @description Logical test if two character elements are identical +#' @param s1 first character element (string, vector or matrix) +#' @param s2 second character element (string, vector or matrix) +#' @return a logical element of the same type as the input +#' @export +strcmp <- function(s1, s2) { + if (length(s1) == 1 & length(s2) == 1) { + # Both are scalars, comparison is straightforward + return(identical(s1, s2)) + } else if (length(s1) == 1 & length(s2) > 1) { + # s1 is a scalar and s2 is a vector or a matrix + checks <- sapply(s2, function(s) s1 %in% s) + if (is(s2, "matrix")) checks <- matrix(checks, nrow(s2)) + } else if (length(s1) > 1 & length(s2) == 1) { + # s1 is a vector/matrix, s2 is a scalar + checks <- sapply(s1, function(s) s2 %in% s) + if (is(s1, "matrix")) checks <- matrix(checks, nrow(s1)) + } else { + # s1 and s2 are vectors/matrices + if (identical(dim(s1), dim(s2))) { + checks <- as.matrix(s4 == s5) + } else { + stop("Inputs must be the same size or either one can be a scalar.") + } + } + return(checks) +} \ No newline at end of file diff --git a/man/strcmp.Rd b/man/strcmp.Rd new file mode 100644 index 0000000..b832fa9 --- /dev/null +++ b/man/strcmp.Rd @@ -0,0 +1,19 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/strcmp.R +\name{strcmp} +\alias{strcmp} +\title{Compare two character elements} +\usage{ +strcmp(s1, s2) +} +\arguments{ +\item{s1}{first character element (string, vector or matrix)} + +\item{s2}{second character element (string, vector or matrix)} +} +\value{ +a logical element of the same type as the input +} +\description{ +Logical test if two character elements are identical +} diff --git a/tests/testthat/test-convertedBaseFunctions.R b/tests/testthat/test-convertedBaseFunctions.R index 5683637..b94ab16 100644 --- a/tests/testthat/test-convertedBaseFunctions.R +++ b/tests/testthat/test-convertedBaseFunctions.R @@ -111,4 +111,21 @@ test_that("isfield works as on Matlab", { object = isfield(S, c("x", "y", "z", "title", "error")), expected = c(TRUE, TRUE, FALSE, TRUE, FALSE) ) +}) + +test_that("strcmp works as expected", { + yes <- 'Yes' + no <- 'No' + ja <- 'Yes' + expect_false(strcmp(yes, no)) + expect_true(strcmp(yes, ja)) + s1 <- 'upon' + s2 <- matrix(c('Once', 'upon', 'a', 'time'), 2, byrow=TRUE) + s3 <- c('Once', 'upon', 'a', 'time') + s4 <- matrix(c("A", "bc", "def", "G"), 2, byrow=TRUE) + s5 <- matrix(c("B", "c", "def", "G"), 2, byrow=TRUE) + expect_equal(strcmp(s1, s2), matrix(c(FALSE, FALSE, TRUE, FALSE), 2)) + expect_equivalent(strcmp(s1, s3), c(FALSE, TRUE, FALSE, FALSE)) + expect_error(strcmp(s2, s3)) + expect_equal(strcmp(s4, s5), matrix(c(FALSE, TRUE, FALSE, TRUE), 2)) }) \ No newline at end of file