'update'
[epclust.git] / epclust / tests / testthat / test-de_serialize.R
CommitLineData
56857861 1context("de_serialize")
62deb424 2
56857861 3test_that("serialization + getDataInFile retrieve original data / from matrix",
62deb424 4{
282342ba 5 data_bin_file <- ".epclust_test_m.bin"
4bcfdbee
BA
6 unlink(data_bin_file)
7
a52836b2 8 # Dataset 200 cols / 30 rows
282342ba
BA
9 data_ascii <- matrix(runif(200*30,-10,10),nrow=30)
10 nbytes <- 4 #lead to a precision of 1e-7 / 1e-8
11 endian <- "little"
56857861 12
a52836b2 13 # Simulate serialization in one single call
4bcfdbee 14 binarize(data_ascii, data_bin_file, 500, ",", nbytes, endian)
56857861
BA
15 expect_equal(file.info(data_bin_file)$size, length(data_ascii)*nbytes+8)
16 for (indices in list(c(1,3,5), 3:13, c(5,20,50), c(75,130:135), 196:200))
62deb424 17 {
282342ba 18 data_lines <- getDataInFile(indices, data_bin_file, nbytes, endian)
37c82bba 19 expect_equal(data_lines, data_ascii[,indices], tolerance=1e-6)
62deb424 20 }
56857861
BA
21 unlink(data_bin_file)
22
a52836b2 23 # Serialization in several calls (last call complete, next call NULL)
56857861 24 for (i in 1:20)
0486fbad 25 binarize(data_ascii[,((i-1)*10+1):(i*10)], data_bin_file, 20, ",", nbytes, endian)
56857861
BA
26 expect_equal(file.info(data_bin_file)$size, length(data_ascii)*nbytes+8)
27 for (indices in list(c(1,3,5), 3:13, c(5,20,50), c(75,130:135), 196:200))
62deb424 28 {
282342ba 29 data_lines <- getDataInFile(indices, data_bin_file, nbytes, endian)
37c82bba 30 expect_equal(data_lines, data_ascii[,indices], tolerance=1e-6)
62deb424 31 }
56857861
BA
32 unlink(data_bin_file)
33})
34
492cd9e7
BA
35test_that("serialization + transform + getDataInFile retrieve original transforms",
36{
282342ba 37 data_bin_file <- ".epclust_test_t.bin"
492cd9e7
BA
38 unlink(data_bin_file)
39
a52836b2 40 # Dataset 200 cols / 30 rows
282342ba
BA
41 data_ascii <- matrix(runif(200*30,-10,10),nrow=30)
42 nbytes <- 8
43 endian <- "little"
492cd9e7
BA
44
45 binarize(data_ascii, data_bin_file, 500, ",", nbytes, endian)
46 # Serialize transformation (just compute range) into a new binary file
282342ba 47 trans_bin_file <- ".epclust_test_t_trans.bin"
492cd9e7 48 unlink(trans_bin_file)
282342ba 49 getSeries <- function(inds) getDataInFile(inds, data_bin_file, nbytes, endian)
37c82bba 50 binarizeTransform(getSeries, function(series) apply(series, 2, range),
492cd9e7
BA
51 trans_bin_file, 250, nbytes, endian)
52 unlink(data_bin_file)
0486fbad 53 expect_equal(file.info(trans_bin_file)$size, 2*ncol(data_ascii)*nbytes+8)
492cd9e7
BA
54 for (indices in list(c(1,3,5), 3:13, c(5,20,50), c(75,130:135), 196:200))
55 {
282342ba 56 trans_cols <- getDataInFile(indices, trans_bin_file, nbytes, endian)
0486fbad 57 expect_equal(trans_cols, apply(data_ascii[,indices],2,range), tolerance=1e-6)
492cd9e7
BA
58 }
59 unlink(trans_bin_file)
60})
61
56857861
BA
62test_that("serialization + getDataInFile retrieve original data / from connection",
63{
282342ba 64 data_bin_file <- ".epclust_test_c.bin"
4bcfdbee
BA
65 unlink(data_bin_file)
66
a52836b2 67 # Dataset 300 cols / 50 rows
282342ba
BA
68 data_csv <- system.file("testdata","de_serialize.csv",package="epclust")
69 nbytes <- 8
70 endian <- "big"
71 data_ascii <- unname( t( as.matrix(read.table(data_csv,sep=";",header=FALSE)) ) ) #ref
56857861 72
a52836b2 73 # Simulate serialization in one single call
4bcfdbee 74 binarize(data_csv, data_bin_file, 350, ";", nbytes, endian)
56857861
BA
75 expect_equal(file.info(data_bin_file)$size, 300*50*8+8)
76 for (indices in list(c(1,3,5), 3:13, c(5,20,50), c(75,130:135), 196:200))
62deb424 77 {
282342ba 78 data_cols <- getDataInFile(indices,data_bin_file,nbytes,endian)
37c82bba 79 expect_equal(data_cols, data_ascii[,indices])
62deb424 80 }
56857861
BA
81 unlink(data_bin_file)
82
a52836b2 83 # Serialization in several calls / chunks of 29 --> 29*10 + 10, incomplete last
282342ba 84 data_con <- file(data_csv, "r")
4bcfdbee 85 binarize(data_con, data_bin_file, 29, ";", nbytes, endian)
56857861
BA
86 expect_equal(file.info(data_bin_file)$size, 300*50*8+8)
87 for (indices in list(c(1,3,5), 3:13, c(5,20,50), c(75,130:135), 196:200))
62deb424 88 {
282342ba 89 data_cols <- getDataInFile(indices,data_bin_file,nbytes,endian)
37c82bba 90 expect_equal(data_cols, data_ascii[,indices])
62deb424 91 }
56857861 92 unlink(data_bin_file)
4bcfdbee 93 #close(data_con) --> done in binarize()
56857861 94})