Skip to content

Commit 25ff661

Browse files
committed
Fix and test std::array<T, 0>
1 parent a48bfa7 commit 25ff661

2 files changed

Lines changed: 55 additions & 79 deletions

File tree

include/vide/types/array.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace vide { // ------------------------------------------------------------
1111
template <class Archive, class T, std::size_t N>
1212
inline void VIDE_FUNCTION_NAME_SERIALIZE(Archive& ar, std::array<T, N>& array) {
1313
if constexpr (Archive::template supports_binary<T>) {
14-
ar(binary_data(array.data(), sizeof(array)));
14+
ar(binary_data(array.data(), N*sizeof(T)));
1515
} else {
1616
for (auto& i : array)
1717
ar(i);

unittests/array.hpp

Lines changed: 54 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,71 @@
1-
/*
2-
Copyright (c) 2013-2022, Randolph Voorhies, Shane Grant
3-
All rights reserved.
1+
//
42

5-
Redistribution and use in source and binary forms, with or without
6-
modification, are permitted provided that the following conditions are met:
7-
* Redistributions of source code must retain the above copyright
8-
notice, this list of conditions and the following disclaimer.
9-
* Redistributions in binary form must reproduce the above copyright
10-
notice, this list of conditions and the following disclaimer in the
11-
documentation and/or other materials provided with the distribution.
12-
* Neither the name of the copyright holder nor the
13-
names of its contributors may be used to endorse or promote products
14-
derived from this software without specific prior written permission.
3+
#pragma once
154

16-
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17-
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18-
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19-
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
20-
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21-
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22-
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23-
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24-
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25-
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26-
*/
27-
#ifndef VIDE_TEST_ARRAY_H_
28-
#define VIDE_TEST_ARRAY_H_
295
#include "common.hpp"
306

31-
template <class IArchive, class OArchive> inline
32-
void test_array()
33-
{
34-
std::random_device rd;
35-
std::mt19937 gen(rd());
7+
template <typename IArchive, typename OArchive, size_t N>
8+
inline void aux_test_array() {
9+
std::random_device rd;
10+
std::mt19937 gen(rd());
3611

37-
for(int ii=0; ii<100; ++ii)
38-
{
39-
std::array<int, 100> o_podarray;
40-
for(auto & elem : o_podarray)
41-
elem = random_value<int>(gen);
12+
for (size_t ii = 0; ii < N; ++ii) {
13+
std::array<int, N> o_podarray;
14+
for (auto& elem : o_podarray)
15+
elem = random_value<int>(gen);
4216

43-
std::array<StructInternalSerialize, 100> o_iserarray;
44-
for(auto & elem : o_iserarray)
45-
elem = StructInternalSerialize( random_value<int>(gen), random_value<int>(gen) );
17+
std::array<StructInternalSerialize, N> o_iserarray;
18+
for (auto& elem : o_iserarray)
19+
elem = StructInternalSerialize(random_value<int>(gen), random_value<int>(gen));
4620

47-
std::array<StructInternalSplit, 100> o_isplarray;
48-
for(auto & elem : o_isplarray)
49-
elem = StructInternalSplit( random_value<int>(gen), random_value<int>(gen) );
21+
std::array<StructInternalSplit, N> o_isplarray;
22+
for (auto& elem : o_isplarray)
23+
elem = StructInternalSplit(random_value<int>(gen), random_value<int>(gen));
5024

51-
std::array<StructExternalSerialize, 100> o_eserarray;
52-
for(auto & elem : o_eserarray)
53-
elem = StructExternalSerialize( random_value<int>(gen), random_value<int>(gen) );
25+
std::array<StructExternalSerialize, N> o_eserarray;
26+
for (auto& elem : o_eserarray)
27+
elem = StructExternalSerialize(random_value<int>(gen), random_value<int>(gen));
5428

55-
std::array<StructExternalSplit, 100> o_esplarray;
56-
for(auto & elem : o_esplarray)
57-
elem = StructExternalSplit( random_value<int>(gen), random_value<int>(gen) );
29+
std::array<StructExternalSplit, N> o_esplarray;
30+
for (auto& elem : o_esplarray)
31+
elem = StructExternalSplit(random_value<int>(gen), random_value<int>(gen));
5832

59-
std::ostringstream os;
60-
{
61-
OArchive oar(os);
33+
std::ostringstream os; {
34+
OArchive oar(os);
6235

63-
oar(o_podarray);
64-
oar(o_iserarray);
65-
oar(o_isplarray);
66-
oar(o_eserarray);
67-
oar(o_esplarray);
68-
}
36+
oar(o_podarray);
37+
oar(o_iserarray);
38+
oar(o_isplarray);
39+
oar(o_eserarray);
40+
oar(o_esplarray);
41+
}
6942

70-
std::array<int, 100> i_podarray;
71-
std::array<StructInternalSerialize, 100> i_iserarray;
72-
std::array<StructInternalSplit, 100> i_isplarray;
73-
std::array<StructExternalSerialize, 100> i_eserarray;
74-
std::array<StructExternalSplit, 100> i_esplarray;
43+
std::array<int, N> i_podarray;
44+
std::array<StructInternalSerialize, N> i_iserarray;
45+
std::array<StructInternalSplit, N> i_isplarray;
46+
std::array<StructExternalSerialize, N> i_eserarray;
47+
std::array<StructExternalSplit, N> i_esplarray;
7548

76-
std::istringstream is(os.str());
77-
{
78-
IArchive iar(is);
49+
std::istringstream is(os.str()); {
50+
IArchive iar(is);
7951

80-
iar(i_podarray);
81-
iar(i_iserarray);
82-
iar(i_isplarray);
83-
iar(i_eserarray);
84-
iar(i_esplarray);
85-
}
52+
iar(i_podarray);
53+
iar(i_iserarray);
54+
iar(i_isplarray);
55+
iar(i_eserarray);
56+
iar(i_esplarray);
57+
}
8658

87-
check_collection( i_podarray, o_podarray );
88-
check_collection( i_iserarray, o_iserarray );
89-
check_collection( i_isplarray, o_isplarray );
90-
check_collection( i_eserarray, o_eserarray );
91-
check_collection( i_esplarray, o_esplarray );
92-
}
59+
check_collection(i_podarray, o_podarray);
60+
check_collection(i_iserarray, o_iserarray);
61+
check_collection(i_isplarray, o_isplarray);
62+
check_collection(i_eserarray, o_eserarray);
63+
check_collection(i_esplarray, o_esplarray);
64+
}
9365
}
9466

95-
#endif // VIDE_TEST_ARRAY_H_
67+
template <typename IArchive, typename OArchive>
68+
inline void test_array() {
69+
aux_test_array<IArchive, OArchive, 0>();
70+
aux_test_array<IArchive, OArchive, 100>();
71+
}

0 commit comments

Comments
 (0)