-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathEnumTest.cpp
More file actions
98 lines (88 loc) · 3.06 KB
/
EnumTest.cpp
File metadata and controls
98 lines (88 loc) · 3.06 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
/**
* 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( "EnumerationValues", "[Enum]" )
{
REQUIRE( static_cast< unsigned >( Enum1::VALUE_1 ) == 0u );
REQUIRE( static_cast< unsigned >( Enum1::VALUE_2 ) == 3u );
REQUIRE( static_cast< unsigned >( Enum1::VALUE_3 ) == 4u );
REQUIRE( static_cast< unsigned >( Enum1::VALUE_4 ) == 1u );
REQUIRE( static_cast< unsigned >( Enum1::VALUE_5 ) == 2u );
}
TEST_CASE( "Enum to string", "[Enum]" )
{
REQUIRE( to_string( Enum1::VALUE_1 ) == std::string( "Enum1::VALUE_1" ) );
REQUIRE( to_string( Enum1::VALUE_2 ) == std::string( "Enum1::VALUE_2" ) );
REQUIRE( to_string( Enum1::VALUE_3 ) == std::string( "Enum1::VALUE_3" ) );
REQUIRE( to_string( Enum1::VALUE_4 ) == std::string( "Enum1::VALUE_4" ) );
REQUIRE( to_string( Enum1::VALUE_5 ) == std::string( "Enum1::VALUE_5" ) );
REQUIRE( to_string( static_cast< Enum1 >( 66 ) ) == std::string( "Unknown value of Enum1" ) );
}
TEST_CASE( "Struct with enum", "[Enum]" )
{
Vector< StructWithEnum > v( 1 );
StructWithEnumMutator x = v[ 0 ];
REQUIRE( x.size_in_bytes( ) == size_t( 4 ) );
x.a = 0x789ab;
x.b = Enum1::VALUE_1;
x.c = Enum1::VALUE_3;
const uint8_t* data = x.data( );
StructWithEnum reader{ data };
REQUIRE( uint32_t( reader.a ) == 0x789abu );
Enum1 b = reader.b;
REQUIRE( b == Enum1::VALUE_1 );
Enum1 c = reader.c;
REQUIRE( c == Enum1::VALUE_3 );
REQUIRE( reader.to_string( ) == R"(StructWithEnum {
a : 493995,
b : Enum1::VALUE_1,
c : Enum1::VALUE_3,
})" );
}
TEST_CASE( "Struct with signed enum", "[Enum]" )
{
// we test that reading/writing signed enums of different sizes work
// e.g. full storage width / reduced storage width
// min / max, -1, etc
Vector< StructWithSignedEnum > v( 1 );
StructWithSignedEnumMutator x = v[ 0 ];
REQUIRE( x.size_in_bytes( ) == size_t( 1 ) );
const uint8_t* data = x.data( );
StructWithSignedEnum reader{ data };
{
x.a = SignedEnum1::VALUE_MINUS_ONE;
SignedEnum1 result = reader.a;
REQUIRE( result == SignedEnum1::VALUE_MINUS_ONE );
}
{
x.a = SignedEnum1::VALUE_MINUS_ONE;
SignedEnum1 result = reader.a;
REQUIRE( result == SignedEnum1::VALUE_MINUS_ONE );
}
{
x.a = SignedEnum1::VALUE_INT4_MAX;
SignedEnum1 result = reader.a;
REQUIRE( result == SignedEnum1::VALUE_INT4_MAX );
}
{
x.a = SignedEnum1::VALUE_INT4_MIN;
SignedEnum1 result = reader.a;
REQUIRE( result == SignedEnum1::VALUE_INT4_MIN );
}
{
x.a = SignedEnum1::VALUE_INT3_MAX;
SignedEnum1 result = reader.a;
REQUIRE( result == SignedEnum1::VALUE_INT3_MAX );
}
{
x.a = SignedEnum1::VALUE_INT3_MIN;
SignedEnum1 result = reader.a;
REQUIRE( result == SignedEnum1::VALUE_INT3_MIN );
}
}