Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.DS_Store
tmp
29 changes: 29 additions & 0 deletions bin/run_tests.R
Original file line number Diff line number Diff line change
@@ -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)
12 changes: 12 additions & 0 deletions bin/run_tests.bat
Original file line number Diff line number Diff line change
@@ -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
11 changes: 5 additions & 6 deletions config.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
{
"slug": "r",
"language": "R",
"repository": "https://github.com/exercism/xr",
"active": false,
"repository": "https://github.com/morphatic/xr",
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The repository needs to be exercism/xr, as that is the canonical repo that the x-api uses to serve problems from.

"active": true,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't activate the track until there are at least 10 exercises.

"problems": [

"hello-world",
"hello-variables",
"operate"
],
"deprecated": [

],
"ignored": [

],
"foregone": [

]
}
3 changes: 3 additions & 0 deletions hello-variables/example.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
say.hello.to <- function(name) {
return(paste("Hello, ", name, "!", sep=""))
}
14 changes: 14 additions & 0 deletions hello-variables/hello-variables_test.R
Original file line number Diff line number Diff line change
@@ -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!")
}
3 changes: 3 additions & 0 deletions hello-world/example.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
say.hello <- function() {
return("Hello, World!")
}
14 changes: 14 additions & 0 deletions hello-world/hello-world_test.R
Original file line number Diff line number Diff line change
@@ -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!")
}
22 changes: 22 additions & 0 deletions operate/example.R
Original file line number Diff line number Diff line change
@@ -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 ) }
105 changes: 105 additions & 0 deletions operate/operate_test.R
Original file line number Diff line number Diff line change
@@ -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 ) )

}