Last active
April 30, 2020 06:35
-
-
Save MichaelChirico/58327b30aa865fd8bb234b7b98fa6df9 to your computer and use it in GitHub Desktop.
Regression tests for writeObject & writeType refactor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| old_env = new.env() | |
| URL = 'https://raw.githubusercontent.com/apache/spark/master/R/pkg/R/serialize.R' | |
| source(URL, old_env) | |
| new_env = new.env() | |
| # run from ./R/pkg on the write_type_object_s3 branch | |
| source('R/serialize.R', new_env) | |
| # missing: jobj | |
| test_objects = list( | |
| 1L, 1, as.raw(0x01), # integer, numeric, raw | |
| 'a', TRUE, # character, logical | |
| list2env( | |
| list(int = 1L, num = 4) # environment (different types) | |
| ), | |
| list2env(list(a = 'b')), # length-1 environment | |
| list('a', TRUE, 0L), # list | |
| as.list(1:5), # integer array | |
| as.list(letters), # character array | |
| 'abcdefg', '', # length>1 character, length-0 character | |
| .Date(0), .POSIXct(0), # Date, POSIXct | |
| list(), list(1L), # empty list, length-1 list | |
| list(1:2), # length-2 list (so x[[1]] is an array) | |
| list2env(list( | |
| a = list(1:2), | |
| b = list( | |
| list(NULL), 'c' # complicated env with nesting | |
| ), | |
| c = c('a', 'b') | |
| )), | |
| NA, NA_real_, # NA logical, NA numeric | |
| NULL # NULL | |
| ) | |
| for (ii in seq_along(test_objects)) { | |
| old_env$conn = rawConnection(raw(), 'wb') | |
| eval(quote(writeObject(conn, test_objects[[ii]])), old_env) | |
| oldBin = rawConnectionValue(old_env$conn) | |
| close(old_env$conn) | |
| new_env$conn = rawConnection(raw(), 'wb') | |
| eval(quote(writeObject(test_objects[[ii]], conn)), new_env) | |
| newBin = rawConnectionValue(new_env$conn) | |
| close(new_env$conn) | |
| if (!identical(oldBin, newBin)) { | |
| stop('Failed at object ', ii, | |
| '\n\noldBin:\n', format(oldBin), | |
| '\nnewBin:\n', format(newBin)) | |
| } | |
| } | |
| # ** "EMPIRICAL" TESTS" ** | |
| # add this in writeObject S3 method: | |
| # TEST_FILE = 'pkg/writeObject.inputs' | |
| # repro_env = new.env() | |
| # if (file.exists(TEST_FILE)) { | |
| # load(TEST_FILE, envir = repro_env) | |
| # repro_env$obj_count = repro_env$obj_count + 1L | |
| # } else repro_env$obj_count = 1L | |
| # repro_env[[sprintf('object_%d', repro_env$obj_count)]] = object | |
| # save(list = ls(repro_env), envir = repro_env, file = TEST_FILE) | |
| # then run tests & every time writeObject is called, its input will be | |
| # added to the writeObject.inputs file; here, we test that all of these | |
| # inputs produce the same output as the old version | |
| test_env = new.env() | |
| load('writeObject.inputs', envir = test_env) | |
| # mock function | |
| isValidJobj = function(...) TRUE | |
| for (nm in ls(test_env)) { | |
| # internal class in refactor only | |
| if (class(get(nm, test_env)) == 'ArrayList') next | |
| old_env$conn = rawConnection(raw(), 'wb') | |
| eval(quote(writeObject(conn, test_env[[nm]])), old_env) | |
| oldBin = rawConnectionValue(old_env$conn) | |
| close(old_env$conn) | |
| new_env$conn = rawConnection(raw(), 'wb') | |
| eval(quote(writeObject(test_env[[nm]], conn)), new_env) | |
| newBin = rawConnectionValue(new_env$conn) | |
| close(new_env$conn) | |
| if (!identical(oldBin, newBin)) { | |
| stop('Failed at object ', nm, | |
| '\n\noldBin:\n', format(oldBin), | |
| '\nnewBin:\n', format(newBin)) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment