Page Menu
Home
HEPForge
Search
Configure Global Search
Log In
Files
F19244474
test_ArchiveIO.cc
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Award Token
Flag For Later
Size
13 KB
Referenced Files
None
Subscribers
None
test_ArchiveIO.cc
View Options
#include
<cassert>
#include
<sstream>
#include
"UnitTest++.h"
#include
"test_utils.hh"
#include
"npstat/stat/HistoND.hh"
#include
"npstat/stat/DistributionsND.hh"
#include
"npstat/stat/InMemoryNtuple.hh"
#include
"npstat/stat/NtupleBuffer.hh"
#include
"geners/Record.hh"
#include
"geners/Reference.hh"
#include
"geners/StringArchive.hh"
#include
"geners/BinaryFileArchive.hh"
#include
"geners/MultiFileArchive.hh"
#include
"geners/arrayIO.hh"
#include
"geners/ColumnBuffer.hh"
#define DIM 5
#define NPOINTS 50
using
namespace
npstat
;
using
namespace
std
;
using
namespace
gs
;
namespace
{
class
Persistent2
{
public
:
inline
Persistent2
(
int
n
=
0
)
:
n_
(
n
)
{}
inline
bool
operator
==
(
const
Persistent2
&
r
)
const
{
return
n_
==
r
.
n_
;}
inline
bool
operator
!=
(
const
Persistent2
&
r
)
const
{
return
!
(
*
this
==
r
);}
inline
Persistent2
&
operator
+=
(
const
int
&
r
)
{
n_
+=
r
;
return
*
this
;
}
inline
gs
::
ClassId
classId
()
const
{
return
gs
::
ClassId
(
*
this
);}
inline
bool
write
(
std
::
ostream
&
os
)
const
{
gs
::
write_pod
(
os
,
n_
);
return
!
os
.
bad
()
&&
!
os
.
fail
();
}
static
inline
const
char
*
classname
()
{
return
"npstat::test::Persistent2"
;}
static
inline
unsigned
version
()
{
return
1
;}
static
inline
Persistent2
*
read
(
const
gs
::
ClassId
&
id
,
std
::
istream
&
in
)
{
assert
(
id
==
gs
::
ClassId
::
makeId
<
Persistent2
>
());
int
n
;
gs
::
read_pod
(
in
,
&
n
);
if
(
in
.
bad
()
||
in
.
fail
())
return
0
;
else
return
new
Persistent2
(
n
);
}
private
:
int
n_
;
};
void
test_archive
(
AbsArchive
&
ar
,
const
bool
readStuff
=
true
)
{
typedef
CPP11_array
<
Persistent2
,
DIM
>
Point
;
typedef
CPP11_array
<
double
,
DIM
>
Point2
;
CHECK
(
ar
.
isOpen
());
if
(
readStuff
)
CHECK
(
ar
.
isReadable
());
CHECK
(
ar
.
isWritable
());
// Create a table of objects
std
::
vector
<
Point
>
data
;
Point
location
;
for
(
unsigned
i
=
0
;
i
<
NPOINTS
;
++
i
)
{
for
(
unsigned
idim
=
0
;
idim
<
DIM
;
++
idim
)
location
[
idim
]
=
Persistent2
(
static_cast
<
int
>
(
test_rng
()
*
1000000
));
data
.
push_back
(
location
);
}
CHECK
(
data
[
0
]
!=
data
[
1
]
||
data
[
0
]
!=
data
[
2
]);
// Write out the table of objects
ar
<<
Record
(
data
,
"name1"
,
"category1"
);
for
(
unsigned
i
=
0
;
i
<
NPOINTS
;
++
i
)
{
for
(
unsigned
idim
=
0
;
idim
<
DIM
;
++
idim
)
location
[
idim
]
=
Persistent2
(
static_cast
<
int
>
(
test_rng
()
*
1000000
));
data
.
push_back
(
location
);
}
ar
<<
Record
(
data
,
"name1"
,
"category1"
);
// Create a table of PODs
std
::
vector
<
Point2
>
data2
;
Point2
datum
;
for
(
unsigned
i
=
0
;
i
<
NPOINTS
;
++
i
)
{
for
(
unsigned
idim
=
0
;
idim
<
DIM
;
++
idim
)
datum
[
idim
]
=
test_rng
();
data2
.
push_back
(
datum
);
}
// Write out the table of PODs
ar
<<
Record
(
data2
,
"name2"
,
"category2"
);
// Write out an ntuple
NtupleBuffer
<
double
>
ntbuf1
(
NPOINTS
,
4
,
true
);
NtupleBuffer
<
double
>
ntbuf2
(
NPOINTS
,
4
,
false
);
InMemoryNtuple
<
double
>
ntuple
(
ntupleColumns
(
"a"
,
"b"
,
"c"
,
"duh"
));
double
ntbuf
[
4
];
for
(
unsigned
i
=
0
;
i
<
NPOINTS
;
++
i
)
{
for
(
unsigned
idim
=
0
;
idim
<
4
;
++
idim
)
ntbuf
[
idim
]
=
test_rng
();
ntuple
.
fill
(
ntbuf
,
4
);
for
(
unsigned
idim
=
0
;
idim
<
4
;
++
idim
)
ntbuf
[
idim
]
=
test_rng
();
ntbuf1
.
fill
(
ntbuf
,
4
);
for
(
unsigned
idim
=
0
;
idim
<
4
;
++
idim
)
ntbuf
[
idim
]
=
test_rng
();
ntbuf2
.
fill
(
ntbuf
,
4
);
}
ar
<<
Record
(
ntuple
,
"test ntuple"
,
"histos"
);
ar
<<
Record
(
ntbuf1
,
"nt buf"
,
"histos"
);
ar
<<
Record
(
ntbuf2
,
"nt buf"
,
"histos"
);
// Try an ntuple record with non-POD contents
NtupleBuffer
<
HistoND
<
double
>
>
ntbuf3
(
NPOINTS
,
4
,
true
);
NtupleBuffer
<
HistoND
<
double
>
>
ntbuf4
(
NPOINTS
,
4
,
false
);
std
::
vector
<
HistoND
<
double
>
>
histos
;
for
(
unsigned
i
=
0
;
i
<
4
;
++
i
)
{
std
::
vector
<
HistoAxis
>
axes
(
1
,
HistoAxis
(
10
,
0.0
,
i
+
2.0
));
histos
.
push_back
(
HistoND
<
double
>
(
axes
));
}
for
(
unsigned
ip
=
0
;
ip
<
NPOINTS
/
2
;
++
ip
)
{
for
(
unsigned
ihis
=
0
;
ihis
<
4
;
++
ihis
)
{
histos
[
ihis
].
clearBinContents
();
for
(
unsigned
n
=
0
;
n
<
1000
;
++
n
)
histos
[
ihis
].
fill
(
test_rng
()
*
(
ihis
+
2.0
),
1.0
);
}
ntbuf3
.
fill
(
&
histos
[
0
],
4
);
ntbuf4
.
fill
(
&
histos
[
0
],
4
);
}
ar
<<
Record
(
ntbuf3
,
"nt buf 3"
,
"histos"
);
ar
<<
Record
(
ntbuf4
,
"nt buf 4"
,
"histos"
);
// Write out an object
double
locd
[
DIM
],
scale
[
DIM
];
for
(
unsigned
idim
=
0
;
idim
<
DIM
;
++
idim
)
{
locd
[
idim
]
=
idim
-
3.0
;
scale
[
idim
]
=
(
idim
+
1.0
)
*
2.0
;
}
UniformND
uni
(
locd
,
scale
,
DIM
);
ar
<<
Record
(
uni
,
"uniform distro"
,
"category1"
);
// Write out a POD
unsigned
mynumber
=
12345
;
ar
<<
Record
(
mynumber
,
"my number"
,
"who cares"
);
// Read back the table of PODs and check for identity
// with what was written
if
(
readStuff
)
{
std
::
vector
<
Point2
>
read2
;
Reference
<
std
::
vector
<
Point2
>
>
(
ar
,
"name2"
,
"category2"
).
restore
(
0
,
&
read2
);
CHECK
(
data2
==
read2
);
}
// Write out a container
std
::
vector
<
HistoAxis
>
axes
;
for
(
unsigned
idim
=
0
;
idim
<
DIM
;
++
idim
)
{
char
tmp
[
32
];
sprintf
(
tmp
,
"axis %u"
,
idim
);
axes
.
push_back
(
HistoAxis
(
idim
+
1
,
0.0
,
idim
+
1.0
,
tmp
));
}
HistoND
<
float
>
histo
(
axes
,
"Test histo"
,
"Events"
);
HistoND
<
Persistent2
>
histo2
(
axes
,
"Test histo 2"
,
"Events"
);
for
(
unsigned
i
=
0
;
i
<
10000
;
++
i
)
{
for
(
unsigned
idim
=
0
;
idim
<
DIM
;
++
idim
)
locd
[
idim
]
=
1.01
*
test_rng
()
*
(
idim
+
1.0
);
histo
.
fill
(
locd
,
DIM
,
1.f
);
histo2
.
fill
(
locd
,
DIM
,
1
);
}
ar
<<
Record
(
histo
,
"dummy histo"
,
"histos"
);
ar
<<
Record
(
histo2
,
"dummy histo 2"
,
"histos"
);
// Read back a pod
if
(
readStuff
)
{
Reference
<
unsigned
>
ref4
(
ar
,
"my number"
,
"who cares"
);
unsigned
dummy
=
0
;
ref4
.
restore
(
0
,
&
dummy
);
CHECK_EQUAL
(
mynumber
,
dummy
);
CPP11_auto_ptr
<
unsigned
>
uns2
=
Reference
<
unsigned
>
(
ar
,
"my number"
,
"who cares"
).
get
(
0
);
CHECK_EQUAL
(
mynumber
,
*
uns2
);
}
// Write out a vector of PODs
std
::
vector
<
double
>
vecd
;
for
(
unsigned
i
=
0
;
i
<
NPOINTS
;
++
i
)
vecd
.
push_back
(
test_rng
());
ar
<<
Record
(
vecd
,
"my vector"
,
0
);
// Write out a vector of objects
std
::
vector
<
Persistent2
>
veco
;
for
(
unsigned
i
=
0
;
i
<
NPOINTS
;
++
i
)
veco
.
push_back
(
Persistent2
(
static_cast
<
int
>
(
test_rng
()
*
1000000
)));
ar
<<
Record
(
veco
,
"my vector 2"
,
"duh"
);
// Write out a largish histogram
axes
.
clear
();
for
(
unsigned
i
=
0
;
i
<
4
;
++
i
)
axes
.
push_back
(
HistoAxis
((
i
+
1U
)
*
10
,
0.0
,
i
+
2.0
));
HistoND
<
double
>
bigh
(
axes
,
"Big histo"
,
"Some Data"
);
for
(
unsigned
i
=
0
;
i
<
10000
;
++
i
)
bigh
.
fill
(
test_rng
()
*
3
,
test_rng
()
*
4
,
test_rng
()
*
4
,
test_rng
()
*
4
,
1.0
);
ar
<<
Record
(
bigh
,
"big histo"
,
"who knows"
);
// Read back the table of objects and check for identity
// with what was written
if
(
readStuff
)
{
Reference
<
NtupleBuffer
<
HistoND
<
double
>
>
>
bufref3
(
ar
,
"nt buf 3"
,
"histos"
);
CPP11_auto_ptr
<
NtupleBuffer
<
HistoND
<
double
>
>
>
pb3
=
bufref3
.
get
(
0
);
CHECK
(
*
pb3
==
ntbuf3
);
Reference
<
NtupleBuffer
<
HistoND
<
double
>
>
>
bufref4
(
ar
,
"nt buf 4"
,
"histos"
);
NtupleBuffer
<
HistoND
<
double
>
>
b4
(
NPOINTS
,
4
,
false
);
bufref4
.
restore
(
0
,
&
b4
);
CHECK
(
ntbuf4
==
b4
);
CPP11_auto_ptr
<
InMemoryNtuple
<
double
>
>
nt1
=
Reference
<
InMemoryNtuple
<
double
>
>
(
ar
,
"test ntuple"
,
"histos"
).
get
(
0
);
CHECK
(
*
nt1
==
ntuple
);
Reference
<
NtupleBuffer
<
double
>
>
bufref
(
ar
,
"nt buf"
,
"histos"
);
CHECK
(
bufref
.
size
()
==
2
);
CPP11_auto_ptr
<
NtupleBuffer
<
double
>
>
buf3
=
bufref
.
get
(
0
);
CHECK
(
*
buf3
==
ntbuf1
);
buf3
=
bufref
.
get
(
1
);
CHECK
(
*
buf3
==
ntbuf2
);
Reference
<
std
::
vector
<
Point
>
>
ref
(
ar
,
"name1"
,
"category1"
);
CHECK
(
ref
.
size
()
==
2
);
std
::
vector
<
Point
>
read
;
ref
.
restore
(
0
,
&
read
);
CHECK
(
data
!=
read
);
ref
.
restore
(
1
,
&
read
);
CHECK
(
data
==
read
);
CPP11_auto_ptr
<
std
::
vector
<
Point
>
>
tmp
=
ref
.
get
(
1
);
CHECK
(
data
==
*
tmp
);
// Read back the distribution
CPP11_auto_ptr
<
UniformND
>
restored
=
Reference
<
UniformND
>
(
ar
,
"uniform distro"
,
"category1"
).
get
(
0
);
CHECK
(
*
restored
==
uni
);
// Read back a container
CPP11_auto_ptr
<
HistoND
<
float
>
>
h1
=
Reference
<
HistoND
<
float
>
>
(
ar
,
"dummy histo"
,
"histos"
).
get
(
0
);
CHECK
(
*
h1
==
histo
);
CPP11_auto_ptr
<
HistoND
<
Persistent2
>
>
h2
=
Reference
<
HistoND
<
Persistent2
>
>
(
ar
,
"dummy histo 2"
,
"histos"
).
get
(
0
);
CHECK
(
*
h2
==
histo2
);
// Read back the vector of PODs
std
::
vector
<
double
>
haha
;
Reference
<
std
::
vector
<
double
>
>
(
ar
,
"my vector"
,
0
).
restore
(
0
,
&
haha
);
CHECK
(
haha
==
vecd
);
// Read back the vector of objects
CPP11_auto_ptr
<
std
::
vector
<
Persistent2
>
>
rv
=
Reference
<
std
::
vector
<
Persistent2
>
>
(
ar
,
"my vector 2"
,
"duh"
).
get
(
0
);
CHECK
(
*
rv
==
veco
);
// Read back the largish histo
CPP11_auto_ptr
<
HistoND
<
double
>
>
hbig
=
Reference
<
HistoND
<
double
>
>
(
ar
,
"big histo"
,
"who knows"
).
get
(
0
);
CHECK
(
*
hbig
==
bigh
);
}
}
TEST
(
ArchiveIO_string
)
{
StringArchive
ar
(
"Test Archive"
);
test_archive
(
ar
);
const
std
::
string
&
st
(
ar
.
str
());
CHECK_EQUAL
(
st
.
size
(),
ar
.
dataSize
());
std
::
ostringstream
os
;
gs
::
write_item
(
os
,
ar
);
std
::
istringstream
is
(
os
.
str
());
gs
::
ClassId
myid
(
is
,
1
);
CPP11_auto_ptr
<
StringArchive
>
ar2
(
StringArchive
::
read
(
myid
,
is
));
CHECK
(
ar2
.
get
());
CHECK
(
ar
==
*
ar2
);
}
TEST
(
ArchiveIO_binary
)
{
BinaryFileArchive
ar
(
"archive"
,
"w+:cat=i"
,
"binary file archive test"
);
test_archive
(
ar
);
ar
.
flush
();
BinaryFileArchive
ar2
(
"archive"
,
"r"
);
const
unsigned
long
long
nItems
=
ar
.
size
();
CHECK_EQUAL
(
nItems
,
ar2
.
size
());
const
unsigned
long
long
offset
=
1ULL
;
for
(
unsigned
long
long
i
=
offset
;
i
<
nItems
+
offset
;
++
i
)
CHECK
(
*
ar
.
catalogEntry
(
i
)
==
*
ar2
.
catalogEntry
(
i
));
BinaryFileArchive
ar3
(
"archive"
,
"w:cat=i"
,
"binary file archive test"
);
test_archive
(
ar3
,
false
);
ar3
.
flush
();
BinaryFileArchive
ar4
(
"archive"
,
"r"
);
CHECK_EQUAL
(
nItems
,
ar4
.
size
());
for
(
unsigned
long
long
i
=
offset
;
i
<
nItems
+
offset
;
++
i
)
CHECK
(
*
ar
.
catalogEntry
(
i
)
==
*
ar4
.
catalogEntry
(
i
));
}
TEST
(
ArchiveIO_multifile
)
{
MultiFileArchive
ar
(
"archive_m"
,
"w+:cat=i"
,
"multifile archive test"
,
0
);
test_archive
(
ar
);
ar
.
flush
();
MultiFileArchive
ar2
(
"archive_m"
,
"r"
);
const
unsigned
long
long
nItems
=
ar
.
size
();
CHECK_EQUAL
(
nItems
,
ar2
.
size
());
const
unsigned
long
long
offset
=
1ULL
;
for
(
unsigned
long
long
i
=
offset
;
i
<
nItems
+
offset
;
++
i
)
CHECK
(
*
ar
.
catalogEntry
(
i
)
==
*
ar2
.
catalogEntry
(
i
));
MultiFileArchive
ar3
(
"archive_m"
,
"w:cat=i"
,
"multifile archive test"
,
0
);
test_archive
(
ar3
,
false
);
ar3
.
flush
();
MultiFileArchive
ar4
(
"archive_m"
,
"r"
);
CHECK_EQUAL
(
nItems
,
ar4
.
size
());
for
(
unsigned
long
long
i
=
offset
;
i
<
nItems
+
offset
;
++
i
)
CHECK
(
*
ar
.
catalogEntry
(
i
)
==
*
ar4
.
catalogEntry
(
i
));
}
TEST
(
ArchiveIO_compression_bzip2
)
{
BinaryFileArchive
ar
(
"archive_bzip2"
,
"w+:z=b"
,
"bzip2 compressed archive"
);
test_archive
(
ar
);
ar
.
flush
();
}
TEST
(
ArchiveIO_compression_zlib
)
{
BinaryFileArchive
ar
(
"archive_zlib"
,
"w+:z=z"
,
"zlib compressed archive"
);
test_archive
(
ar
);
ar
.
flush
();
}
TEST
(
ArchiveIO_compression_options
)
{
BinaryFileArchive
ar
(
"archive_zlib"
,
"w+:z=z:cl=9:cb=4096:cm=2048"
,
"zlib compression options"
);
test_archive
(
ar
);
ar
.
flush
();
CHECK
(
ar
.
compressionMode
()
==
CStringStream
::
ZLIB
);
CHECK_EQUAL
(
4096U
,
ar
.
compressionBufferSize
());
CHECK_EQUAL
(
2048U
,
ar
.
minSizeToCompress
());
CHECK_EQUAL
(
9
,
ar
.
compressionLevel
());
}
}
File Metadata
Details
Attached
Mime Type
text/x-c
Expires
Tue, Sep 30, 4:42 AM (1 d, 17 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
6560097
Default Alt Text
test_ArchiveIO.cc (13 KB)
Attached To
Mode
rNPSTATSVN npstatsvn
Attached
Detach File
Event Timeline
Log In to Comment