throw away old code, prepare tests
[epclust.git] / epclust / tests / testthat / test.de_serialize.R
diff --git a/epclust/tests/testthat/test.de_serialize.R b/epclust/tests/testthat/test.de_serialize.R
new file mode 100644 (file)
index 0000000..41c57e4
--- /dev/null
@@ -0,0 +1,52 @@
+
+#data: matrix of double or connection
+serialize = function(data, file, type, nb_per_chunk)
+{
+       bin_data = file(file, "ab")
+       #write data length on first call
+       nbytes = ifelse(type=="double",8,4)
+       first_write = FALSE
+       if (file.info(file)$size == 0)
+       {
+               #number of items always on 8 bytes
+               writeBin(0L, bin_data, size=8) #,endian="little")
+               first_write = TRUE
+       }
+       if (is.matrix(data))
+       {
+               writeBin(t(data), bin_data, size=nbytes)
+               data_length = ncol(data)
+       }
+       else #if (is(data, "connection"))
+       {
+               if (first_write)
+               {
+                       data_line = scan(data, double(), sep=",", nlines=1)
+                       writeBin(data_line, bin_data, size=nbytes)
+                       data_length = length(data_line)
+               }
+               repeat
+               {
+                       data_chunk = scan(data, double(), sep=",", nlines=nb_per_chunk)
+                       if (length(data_chunk)==0)
+                               break
+                       writeBin(data_chunk, bin_data, size=nbytes)
+               }
+       }
+       if (first_write)
+       {
+               #ecrire file_size-1 / (nbytes*nbWritten) en 0 dans bin_data ! ignored == file_size
+               ignored = seek(bin_data, 0)
+               writeBin(data_length, bin_data, size=8)
+       }
+       close(bin_data)
+}
+
+#TODO: read in binary file, always same structure
+getDataFromFile(indices, file, type)
+{
+       bin_data = file(file, "rb")
+       nbytes = ifelse(type=="double",8,4)
+       data_length = readBin(bin_data,"double",1,nbytes) #,endian="little")
+       t(sapply(indices, function(i) readBin(bin_data,"double",n=data_length,size=nbytes)))
+}