27e6d59ba299ec5b9282f2596a8f14c8aace635b
[epclust.git] / epclust / tests / testthat / test.de_serialize.R
1 context("de_serialize")
2
3 data_bin_file <<- "/tmp/epclust_test.bin"
4 unlink(data_bin_file)
5
6 test_that("serialization + getDataInFile retrieve original data / from matrix",
7 {
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
14 serialize(data_ascii, data_bin_file, 500, ",", nbytes, endian)
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))
17 {
18 data_lines = getDataInFile(indices, data_bin_file, nbytes, endian)
19 expect_equal(data_lines, data_ascii[indices,], tolerance=1e-6)
20 }
21 unlink(data_bin_file)
22
23 #...in several calls (last call complete, next call NULL)
24 for (i in 1:20)
25 serialize(data_ascii[((i-1)*10+1):(i*10),], data_bin_file, 20, ",", nbytes, endian)
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))
28 {
29 data_lines = getDataInFile(indices, data_bin_file, nbytes, endian)
30 expect_equal(data_lines, data_ascii[indices,], tolerance=1e-6)
31 }
32 unlink(data_bin_file)
33 })
34
35 test_that("serialization + getDataInFile retrieve original data / from connection",
36 {
37 #dataset 300 lignes / 50 columns
38 data_csv = system.file("testdata","de_serialize.csv",package="epclust")
39 nbytes = 8
40 endian = "big"
41 data_ascii = as.matrix(read.csv(test_series, sep=";", header=FALSE)) #for ref
42
43 #Simulate serialization in one single call
44 serialize(data_csv, data_bin_file, 350, ";", nbytes, endian)
45 expect_equal(file.info(data_bin_file)$size, 300*50*8+8)
46 for (indices in list(c(1,3,5), 3:13, c(5,20,50), c(75,130:135), 196:200))
47 {
48 #HACK: as.matrix(as.data.frame( )) required to match (ref) data structure
49 data_lines = as.matrix(as.data.frame( getDataInFile(indices,data_bin_file,nbytes,endian) ))
50 expect_equal(data_lines, data_ascii[indices,])
51 }
52 unlink(data_bin_file)
53
54 #...in several calls / chunks of 29 --> 29*10 + 10, incomplete last
55 data_con = file(data_csv, "r")
56 serialize(data_con, data_bin_file, 29, ";", nbytes, endian)
57 expect_equal(file.info(data_bin_file)$size, 300*50*8+8)
58 for (indices in list(c(1,3,5), 3:13, c(5,20,50), c(75,130:135), 196:200))
59 {
60 data_lines = as.matrix(as.data.frame( getDataInFile(indices,data_bin_file,nbytes,endian) ))
61 expect_equal(data_lines, data_ascii[indices,])
62 }
63 unlink(data_bin_file)
64 #close(data_con) --> done in serialize()
65 })