-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathStructTest.cpp
More file actions
123 lines (109 loc) · 5.48 KB
/
StructTest.cpp
File metadata and controls
123 lines (109 loc) · 5.48 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/**
* Copyright (c) 2018 HERE Europe B.V.
* See the LICENSE file in the root of this project for license details.
*/
#include "test_structures.hpp"
#include <flatdata/flatdata.h>
#include "catch_amalgamated.hpp"
using namespace flatdata;
using namespace test_structures;
TEST_CASE( "Filling data in struct", "[Struct]" )
{
Struct< AStruct > data;
( *data ).value = 10;
REQUIRE( ( *data ).value == uint64_t( 10 ) );
}
TEST_CASE( "Struct is copyable", "[Struct]" )
{
Struct< AStruct > data;
( *data ).value = 10;
REQUIRE( ( *data ).value == uint64_t( 10 ) );
// try to copy and make sure that they are not implicitly sharing storage
auto copy = data;
REQUIRE( ( *copy ).value == uint64_t( 10 ) );
( *copy ).value = 11;
REQUIRE( ( *copy ).value == uint64_t( 11 ) );
REQUIRE( ( *data ).value == uint64_t( 10 ) );
}
TEST_CASE( "Invalid values are handled", "[Struct]" )
{
Struct< TestInvalidValue > data;
TestInvalidValueMutator writer = *data;
TestInvalidValue reader = *data;
writer.invalid_zero = 10;
REQUIRE( *writer.invalid_zero == 10 );
REQUIRE( static_cast< bool >( writer.invalid_zero ) == true );
REQUIRE( static_cast< boost::optional< int8_t > >( writer.invalid_zero )
== boost::optional< int8_t >( 10 ) );
REQUIRE( static_cast< std::optional< int8_t > >( writer.invalid_zero )
== std::optional< int8_t >( 10 ) );
REQUIRE( *reader.invalid_zero == 10 );
REQUIRE( static_cast< bool >( reader.invalid_zero ) == true );
REQUIRE( static_cast< boost::optional< int8_t > >( reader.invalid_zero )
== boost::optional< int8_t >( 10 ) );
REQUIRE( static_cast< std::optional< int8_t > >( reader.invalid_zero )
== std::optional< int8_t >( 10 ) );
writer.invalid_zero = 0;
REQUIRE( *writer.invalid_zero == 0 );
REQUIRE( static_cast< bool >( writer.invalid_zero ) == false );
REQUIRE( static_cast< boost::optional< int8_t > >( writer.invalid_zero ) == boost::none );
REQUIRE( static_cast< std::optional< int8_t > >( writer.invalid_zero ) == std::nullopt );
REQUIRE( *reader.invalid_zero == 0 );
REQUIRE( static_cast< bool >( reader.invalid_zero ) == false );
REQUIRE( static_cast< boost::optional< int8_t > >( reader.invalid_zero ) == boost::none );
REQUIRE( static_cast< std::optional< int8_t > >( reader.invalid_zero ) == std::nullopt );
writer.invalid_min_int = 10;
REQUIRE( *writer.invalid_min_int == 10 );
REQUIRE( static_cast< bool >( writer.invalid_min_int ) == true );
REQUIRE( static_cast< boost::optional< int8_t > >( writer.invalid_min_int )
== boost::optional< int8_t >( 10 ) );
REQUIRE( static_cast< std::optional< int8_t > >( writer.invalid_min_int )
== std::optional< int8_t >( 10 ) );
REQUIRE( *reader.invalid_min_int == 10 );
REQUIRE( static_cast< bool >( reader.invalid_min_int ) == true );
REQUIRE( static_cast< boost::optional< int8_t > >( reader.invalid_min_int )
== boost::optional< int8_t >( 10 ) );
REQUIRE( static_cast< std::optional< int8_t > >( reader.invalid_min_int )
== std::optional< int8_t >( 10 ) );
writer.invalid_min_int = -128;
REQUIRE( *writer.invalid_min_int == -128 );
REQUIRE( static_cast< bool >( writer.invalid_min_int ) == false );
REQUIRE( static_cast< boost::optional< int8_t > >( writer.invalid_min_int ) == boost::none );
REQUIRE( static_cast< std::optional< int8_t > >( writer.invalid_min_int ) == std::nullopt );
REQUIRE( *reader.invalid_min_int == -128 );
REQUIRE( static_cast< bool >( reader.invalid_min_int ) == false );
REQUIRE( static_cast< boost::optional< int8_t > >( reader.invalid_min_int ) == boost::none );
REQUIRE( static_cast< std::optional< int8_t > >( reader.invalid_min_int ) == std::nullopt );
writer.invalid_max_int = 10;
REQUIRE( *writer.invalid_max_int == 10 );
REQUIRE( static_cast< bool >( writer.invalid_max_int ) == true );
REQUIRE( static_cast< boost::optional< int8_t > >( writer.invalid_max_int )
== boost::optional< int8_t >( 10 ) );
REQUIRE( static_cast< std::optional< int8_t > >( writer.invalid_max_int )
== std::optional< int8_t >( 10 ) );
REQUIRE( *reader.invalid_max_int == 10 );
REQUIRE( static_cast< bool >( reader.invalid_max_int ) == true );
REQUIRE( static_cast< boost::optional< int8_t > >( reader.invalid_max_int )
== boost::optional< int8_t >( 10 ) );
REQUIRE( static_cast< std::optional< int8_t > >( reader.invalid_max_int )
== std::optional< int8_t >( 10 ) );
writer.invalid_max_int = 127;
REQUIRE( *writer.invalid_max_int == 127 );
REQUIRE( static_cast< bool >( writer.invalid_max_int ) == false );
REQUIRE( static_cast< boost::optional< int8_t > >( writer.invalid_max_int ) == boost::none );
REQUIRE( static_cast< std::optional< int8_t > >( writer.invalid_max_int ) == std::nullopt );
REQUIRE( *reader.invalid_max_int == 127 );
REQUIRE( static_cast< bool >( reader.invalid_max_int ) == false );
REQUIRE( static_cast< boost::optional< int8_t > >( reader.invalid_max_int ) == boost::none );
REQUIRE( static_cast< std::optional< int8_t > >( reader.invalid_max_int ) == std::nullopt );
}
TEST_CASE( "Invalid values can be converted to string", "[Struct]" )
{
Struct< TestInvalidValue > data;
constexpr auto EXPECTED = R"(TestInvalidValue {
invalid_zero : 0,
invalid_min_int : 0,
invalid_max_int : 0,
})";
REQUIRE( ( *data ).to_string( ) == EXPECTED );
}