diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..d4eb940a --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.DS_Store +tmp diff --git a/bin/run_tests.R b/bin/run_tests.R new file mode 100755 index 00000000..59f987b3 --- /dev/null +++ b/bin/run_tests.R @@ -0,0 +1,29 @@ +#!/usr/bin/env Rscript +# run_tests -- run R tests for exercism.io +# the following were useful in writing this script: +# http://mazamascience.com/WorkingWithData/?p=888 for writing command line scripts +# http://www.johnmyleswhite.com/notebook/2010/08/17/unit-testing-in-r-the-bare-minimum/ for using RUnit + + +# function to determine if a package is installed +# will install it if necessary and load it +# otherwise stops the script with an error message +# source: http://stackoverflow.com/q/9341635 +install.if.necessary <- function(package.name) { + if ( !require(package.name, character.only=TRUE) ) { + install.packages(package.name, repos="http://cran.cnr.Berkeley.edu/") + if ( !require(package.name, character.only=TRUE) ) stop( paste( package.name, " not found") ) + } +} + +# make sure RUnit is installed and loaded +install.if.necessary("RUnit") + +# define a test suite; looks for files named "^.*_test.R" in current directory +test.suite <- defineTestSuite("exercism", dirs = getwd(), testFileRegex = "^.*_test\\.R" ) + +# run the tests +test.result <- runTestSuite(test.suite) + +# output the result +printTextProtocol(test.result) \ No newline at end of file diff --git a/bin/run_tests.bat b/bin/run_tests.bat new file mode 100644 index 00000000..6327f21a --- /dev/null +++ b/bin/run_tests.bat @@ -0,0 +1,12 @@ +@echo OFF + +REM The location of the registry key for the current version of R +set R_PATH_KEY=HKLM\Software\R-core\R +set R_PATH_VAL=InstallPath + +REM Get the path where R is installed +set R_PATH= +for /f "tokens=2,*" %%a in ('reg query %R_PATH_KEY% /v %R_PATH_VAL% ^| findstr %R_PATH_VAL%') do (set R_PATH=%%b) + +REM Run run_tests.R +Rscript "%R_PATH%"\bin\run_tests.R \ No newline at end of file diff --git a/config.json b/config.json index ef79bff9..36a75bd0 100644 --- a/config.json +++ b/config.json @@ -1,18 +1,17 @@ { "slug": "r", "language": "R", - "repository": "https://github.com/exercism/xr", - "active": false, + "repository": "https://github.com/morphatic/xr", + "active": true, "problems": [ - + "hello-world", + "hello-variables", + "operate" ], "deprecated": [ - ], "ignored": [ - ], "foregone": [ - ] } diff --git a/hello-variables/example.R b/hello-variables/example.R new file mode 100644 index 00000000..b3bb3145 --- /dev/null +++ b/hello-variables/example.R @@ -0,0 +1,3 @@ +say.hello.to <- function(name) { + return(paste("Hello, ", name, "!", sep="")) +} \ No newline at end of file diff --git a/hello-variables/hello-variables_test.R b/hello-variables/hello-variables_test.R new file mode 100644 index 00000000..15b2f4ee --- /dev/null +++ b/hello-variables/hello-variables_test.R @@ -0,0 +1,14 @@ +test.hello.variables <- function() { + + # default filename is example.R + filename <- "example.R" + + # check for hello-world.R + if (file.exists("hello-variables.R")) filename <- "hello-variables.R" + + # load the code to be tested + source(filename) + + # test the output + checkEquals(say.hello.to("Bob"), "Hello, Bob!") +} \ No newline at end of file diff --git a/hello-world/example.R b/hello-world/example.R new file mode 100644 index 00000000..eddc2723 --- /dev/null +++ b/hello-world/example.R @@ -0,0 +1,3 @@ +say.hello <- function() { + return("Hello, World!") +} \ No newline at end of file diff --git a/hello-world/hello-world_test.R b/hello-world/hello-world_test.R new file mode 100644 index 00000000..736509ec --- /dev/null +++ b/hello-world/hello-world_test.R @@ -0,0 +1,14 @@ +test.hello.world <- function() { + + # default filename is example.R + filename <- "example.R" + + # check for hello-world.R + if (file.exists("hello-world.R")) filename <- "hello-world.R" + + # load the code to be tested + source(filename) + + # test the output + checkEquals(say.hello(), "Hello, World!") +} \ No newline at end of file diff --git a/operate/example.R b/operate/example.R new file mode 100644 index 00000000..0d5dff3d --- /dev/null +++ b/operate/example.R @@ -0,0 +1,22 @@ +# Arithmetic Operators +add <- function( a, b ) { return( a + b ) } +subtract <- function( a, b ) { return( a - b ) } +multiply <- function( a, b ) { return( a * b ) } +divide <- function( a, b ) { return( a / b ) } +remainder <- function( a, b ) { return( a %% b ) } + +# Comparison Operators +equals <- function( a, b ) { return( a == b ) } +isIdentical <- function( a, b ) { return( identical( a, b ) ) } +notEquals <- function( a, b ) { return( a != b ) } +notIdentical <- function( a, b ) { return( ! identical( a, b ) ) } +lessThan <- function( a, b ) { return( a < b ) } +lessOrEqual <- function( a, b ) { return( a <= b ) } +greaterThan <- function( a, b ) { return( a > b ) } +greaterOrEqual <- function( a, b ) { return( a >= b ) } + +# Logical Operators +logicalAnd <- function( a, b ) { return( a & b ) } +logicalOr <- function( a, b ) { return( a | b ) } +logicalXor <- function( a, b ) { return( ( a & !b ) | ( !a & b ) ) } +logicalNot <- function( a ) { return( ! a ) } diff --git a/operate/operate_test.R b/operate/operate_test.R new file mode 100644 index 00000000..cb53396e --- /dev/null +++ b/operate/operate_test.R @@ -0,0 +1,105 @@ +test.hello.world <- function() { + + # default filename is example.R + filename <- "example.R" + + # check for hello-world.R + if (file.exists("operate.R")) filename <- "operate.R" + + # load the code to be tested + source(filename) + + # test Arithmetic Operators + checkEquals( add( 2, 2 ), 4 ) + checkEquals( add( 7, 3 ), 10 ) + checkEquals( add( 123, 456 ), 579 ) + + checkEquals( subtract( 2, 2 ), 0 ) + checkEquals( subtract( 7, 3 ), 4 ) + checkEquals( subtract( 123, 456 ), -333 ) + + checkEquals( multiply( 2, 2 ), 4 ) + checkEquals( multiply( 7, 3 ), 21 ) + checkEquals( multiply( 123, 456 ), 56088 ) + + checkEquals( divide( 2, 2 ), 1 ) + checkEquals( divide( 7, 3 ), 7/3 ) + checkEquals( divide( 123, 456 ), 123/456 ) + + checkEquals( remainder( 2, 2 ), 0 ) + checkEquals( remainder( 7, 3 ), 1 ) + checkEquals( remainder( 123, 456 ), 123 ) + + # test Comparison Operators + checkTrue( equals( 3 , 3 ) ) + checkTrue( equals( 3 , '3' ) ) + checkTrue( equals( 'Bob', 'Bob' ) ) + checkTrue( ! equals( 3 , 4 ) ) + checkTrue( ! equals( 3 , 'three' ) ) + + checkTrue( isIdentical( 3 , 3 ) ) + checkTrue( ! isIdentical( 3 , '3' ) ) + checkTrue( isIdentical( 'Bob', 'Bob' ) ) + checkTrue( ! isIdentical( 3 , 4 ) ) + checkTrue( ! isIdentical( 3 , 'three' ) ) + + checkTrue( ! notEquals( 3 , 3 ) ) + checkTrue( ! notEquals( 3 , '3' ) ) + checkTrue( ! notEquals( 'Bob', 'Bob' ) ) + checkTrue( notEquals( 3 , 4 ) ) + checkTrue( notEquals( 3 , 'three' ) ) + + checkTrue( ! notIdentical( 3 , 3 ) ) + checkTrue( notIdentical( 3 , '3' ) ) + checkTrue( ! notIdentical( 'Bob', 'Bob' ) ) + checkTrue( notIdentical( 3 , 4 ) ) + checkTrue( notIdentical( 3 , 'three' ) ) + + checkTrue( ! lessThan( 3 , 3 ) ) + checkTrue( lessThan( 3 , 4 ) ) + checkTrue( ! lessThan( 4 , 3 ) ) + checkTrue( ! lessThan( 'a', 'a' ) ) + checkTrue( lessThan( 'a', 'b' ) ) + checkTrue( ! lessThan( 'b', 'a' ) ) + + checkTrue( lessOrEqual( 3 , 3 ) ) + checkTrue( lessOrEqual( 3 , 4 ) ) + checkTrue( ! lessOrEqual( 4 , 3 ) ) + checkTrue( lessOrEqual( 'a', 'a' ) ) + checkTrue( lessOrEqual( 'a', 'b' ) ) + checkTrue( ! lessOrEqual( 'b', 'a' ) ) + + checkTrue( ! greaterThan( 3 , 3 ) ) + checkTrue( ! greaterThan( 3 , 4 ) ) + checkTrue( greaterThan( 4 , 3 ) ) + checkTrue( ! greaterThan( 'a', 'a' ) ) + checkTrue( ! greaterThan( 'a', 'b' ) ) + checkTrue( greaterThan( 'b', 'a' ) ) + + checkTrue( greaterOrEqual( 3 , 3 ) ) + checkTrue( ! greaterOrEqual( 3 , 4 ) ) + checkTrue( greaterOrEqual( 4 , 3 ) ) + checkTrue( greaterOrEqual( 'a', 'a' ) ) + checkTrue( ! greaterOrEqual( 'a', 'b' ) ) + checkTrue( greaterOrEqual( 'b', 'a' ) ) + + # test Logical Operators + checkTrue( logicalAnd( TRUE, TRUE ) ) + checkTrue( ! logicalAnd( TRUE, FALSE ) ) + checkTrue( ! logicalAnd( FALSE, TRUE ) ) + checkTrue( ! logicalAnd( FALSE, FALSE ) ) + + checkTrue( logicalOr( TRUE, TRUE ) ) + checkTrue( logicalOr( TRUE, FALSE ) ) + checkTrue( logicalOr( FALSE, TRUE ) ) + checkTrue( ! logicalOr( FALSE, FALSE ) ) + + checkTrue( ! logicalXor( TRUE, TRUE ) ) + checkTrue( logicalXor( TRUE, FALSE ) ) + checkTrue( logicalXor( FALSE, TRUE ) ) + checkTrue( ! logicalXor( FALSE, FALSE ) ) + + checkTrue( ! logicalNot( TRUE ) ) + checkTrue( logicalNot( FALSE ) ) + +} \ No newline at end of file