GSOC Week 6
Unit tests for libcpp_test file
This week, I wrote unit tests for the libcpp_test file, which currently has all easy cases wrapped except for functions using map & other nested data structures, overloaded methods, attributes of LibCppTest class containing raw pointers, deepcopy/copy methods and class methods for operator overloading.
For writing tests I have used the testthat, a very popular unit testing package in R. It has functions for easily testing the expected function behaviour and also displays the test results in an interactive manner.
Test Structure
In general, all the R test files can be found in the directory Rtests inside the main autowrap repository. All the files, which need to be tested are kept in the test_files, a sub-directory of Rtests. Please note before testing any R file, a corresponding python binding (or package) for it must exist in Rtests.
Also, these dependency R packages need to be installed : reticulate
, R6
, purrr
, plotrix
& varhandle
.
Tests are organized hierarchically, a file contains a set of tests and each test further comprises of expectations, where an expectation describes the expected result of a function.
Below, we can see the structure of R test file for libcpp_test. We have two tests, one to test the constructors and the other for testing the functions of LibCppTest.
context("Testing R code generated for libcpp_test")
# load the file to test
source("./test_files/py_libcpp_test.R")
test_that("Test Copy constructors",{
# create new Int, make copy (shallow)
int_wrp <- Int$new(1)
expect_identical(int_wrp$i_,1L)
int_wrpcpy <- int_wrp
expect_identical(int_wrpcpy$i_,1L)
...
}
test_that("Testing LibCppTest Processes",{
# creating a new object of LibCppTest
t <- LibCppTest$new()
expect_equivalent(t$twist(list("hi",2)),list(2L,"hi"))
# process
li = list(1)
li2 <- t$process(li)
expect_equivalent(li,li2)
expect_equivalent(li,list(1L,42L))
...
}
Running the tests
Currently, we can run all the test files at once and obtain the results interactively on the command line by executing the function testthat::test_dir()
passing the relative/absolute path of Rtests directory.
A screenshot of test results:
Going Forward
I would try to wrap the cases associated with nested data structures and write additional tests.