Fix package, ok for R CMD check - ongoing debug for main function
[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{
4bcfdbee
BA
5 data_bin_file = "/tmp/epclust_test_m.bin"
6 unlink(data_bin_file)
7
56857861
BA
8 #dataset 200 lignes / 30 columns
9 data_ascii = matrix(runif(200*30,-10,10),ncol=30)
10 nbytes = 4 #lead to a precision of 1e-7 / 1e-8
11 endian = "little"
12
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 {
56857861
BA
18 data_lines = getDataInFile(indices, data_bin_file, nbytes, endian)
19 expect_equal(data_lines, data_ascii[indices,], tolerance=1e-6)
62deb424 20 }
56857861
BA
21 unlink(data_bin_file)
22
23 #...in several calls (last call complete, next call NULL)
24 for (i in 1:20)
4bcfdbee 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 {
56857861
BA
29 data_lines = getDataInFile(indices, data_bin_file, nbytes, endian)
30 expect_equal(data_lines, data_ascii[indices,], tolerance=1e-6)
62deb424 31 }
56857861
BA
32 unlink(data_bin_file)
33})
34
35test_that("serialization + getDataInFile retrieve original data / from connection",
36{
4bcfdbee
BA
37 data_bin_file = "/tmp/epclust_test_c.bin"
38 unlink(data_bin_file)
39
56857861
BA
40 #dataset 300 lignes / 50 columns
41 data_csv = system.file("testdata","de_serialize.csv",package="epclust")
42 nbytes = 8
43 endian = "big"
4bcfdbee 44 data_ascii = as.matrix(read.csv(data_csv, sep=";", header=FALSE)) #for ref
56857861
BA
45
46 #Simulate serialization in one single call
4bcfdbee 47 binarize(data_csv, data_bin_file, 350, ";", nbytes, endian)
56857861
BA
48 expect_equal(file.info(data_bin_file)$size, 300*50*8+8)
49 for (indices in list(c(1,3,5), 3:13, c(5,20,50), c(75,130:135), 196:200))
62deb424 50 {
56857861
BA
51 #HACK: as.matrix(as.data.frame( )) required to match (ref) data structure
52 data_lines = as.matrix(as.data.frame( getDataInFile(indices,data_bin_file,nbytes,endian) ))
53 expect_equal(data_lines, data_ascii[indices,])
62deb424 54 }
56857861
BA
55 unlink(data_bin_file)
56
57 #...in several calls / chunks of 29 --> 29*10 + 10, incomplete last
58 data_con = file(data_csv, "r")
4bcfdbee 59 binarize(data_con, data_bin_file, 29, ";", nbytes, endian)
56857861
BA
60 expect_equal(file.info(data_bin_file)$size, 300*50*8+8)
61 for (indices in list(c(1,3,5), 3:13, c(5,20,50), c(75,130:135), 196:200))
62deb424 62 {
56857861
BA
63 data_lines = as.matrix(as.data.frame( getDataInFile(indices,data_bin_file,nbytes,endian) ))
64 expect_equal(data_lines, data_ascii[indices,])
62deb424 65 }
56857861 66 unlink(data_bin_file)
4bcfdbee 67 #close(data_con) --> done in binarize()
56857861 68})