forked from AliceO2Group/AliceO2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_FlattenRestore.cxx
More file actions
74 lines (68 loc) · 3.24 KB
/
test_FlattenRestore.cxx
File metadata and controls
74 lines (68 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
// All rights not expressly granted are reserved.
//
// This software is distributed under the terms of the GNU General Public
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
// @file test_FlattenRestore.cxx
// @author Matthias Richter
// @since 2020-04-05
// @brief Test program flatten/restore tools
#define BOOST_TEST_MODULE Algorithm FlattenRestore test
#define BOOST_TEST_MAIN
#define BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp>
#include "../include/Algorithm/FlattenRestore.h"
#include <vector>
#include <algorithm>
#include <cstdlib>
namespace flatten = o2::algorithm::flatten;
namespace o2::test
{
struct DataAccess {
size_t count = 0;
char* chars = nullptr;
int* ints = nullptr;
float* floats = nullptr;
};
} // namespace o2::test
BOOST_AUTO_TEST_CASE(test_flattenrestore)
{
unsigned int seed = 1;
o2::test::DataAccess access{static_cast<size_t>(rand_r(&seed) % 32)};
std::vector<char> chars(access.count);
std::generate(chars.begin(), chars.end(), [&seed]() { return rand_r(&seed) % 256; });
std::vector<int> ints(access.count);
std::generate(ints.begin(), ints.end(), [&seed]() { return rand_r(&seed) % 256; });
std::vector<float> floats(access.count);
std::generate(floats.begin(), floats.end(), [&seed]() { return rand_r(&seed) % 256; });
access.chars = chars.data();
access.ints = ints.data();
access.floats = floats.data();
std::vector<char> raw(flatten::value_size(access.chars, access.ints, access.floats) * access.count);
char* wrtptr = raw.data();
auto copied = flatten::copy_to(wrtptr, access.count, access.chars, access.ints, access.floats);
BOOST_CHECK(copied == raw.size());
char* checkptr = raw.data();
BOOST_CHECK(memcmp(checkptr, chars.data(), chars.size() * sizeof(decltype(chars)::value_type)) == 0);
checkptr += flatten::calc_size(nullptr, chars.size(), chars.data());
BOOST_CHECK(memcmp(checkptr, ints.data(), ints.size() * sizeof(decltype(ints)::value_type)) == 0);
checkptr += flatten::calc_size(nullptr, ints.size(), ints.data());
BOOST_CHECK(memcmp(checkptr, floats.data(), floats.size() * sizeof(decltype(floats)::value_type)) == 0);
checkptr += flatten::calc_size(nullptr, floats.size(), floats.data());
o2::test::DataAccess target{access.count, nullptr, nullptr, nullptr};
char* readptr = raw.data();
auto readsize = flatten::set_from(readptr, target.count, target.chars, target.ints, target.floats);
BOOST_CHECK(readsize == copied);
checkptr = raw.data();
BOOST_CHECK(reinterpret_cast<decltype(target.chars)>(checkptr) == target.chars);
checkptr += flatten::calc_size(nullptr, chars.size(), chars.data());
BOOST_CHECK(reinterpret_cast<decltype(target.ints)>(checkptr) == target.ints);
checkptr += flatten::calc_size(nullptr, ints.size(), ints.data());
BOOST_CHECK(reinterpret_cast<decltype(target.floats)>(checkptr) == target.floats);
checkptr += flatten::calc_size(nullptr, floats.size(), floats.data());
}