test_attribute.cpp
Go to the documentation of this file.
1 #include <gtest/gtest.h>
2 
6 
7 using namespace std::string_literals;
8 using namespace lanelet;
9 
10 TEST(Attribute, string) { // NOLINT
11  Attribute attr("bla");
12  EXPECT_EQ("bla"s, attr.value());
13 }
14 
15 TEST(Attribute, getSet) { // NOLINT
16  Attribute attr("bla");
17  attr.setValue("bla2");
18  EXPECT_EQ("bla2"s, attr.value());
19 }
20 
21 TEST(Attribute, boolConversion) { // NOLINT
22  Attribute attr("yes");
23  EXPECT_TRUE(*attr.asBool());
24  EXPECT_TRUE(*attr.asBool());
25  attr.setValue("no");
26  EXPECT_FALSE(*attr.asBool());
27  EXPECT_FALSE(*attr.asBool());
28 
29  // other possible values
30  EXPECT_TRUE(*Attribute("true").asBool());
31  EXPECT_TRUE(*Attribute("1").asBool());
32  EXPECT_FALSE(*Attribute("false").asBool());
33  EXPECT_FALSE(*Attribute("0").asBool());
34 }
35 
36 TEST(Attribute, intConversion) { // NOLINT
37  Attribute attr(0);
38  EXPECT_EQ(0., *attr.asInt());
39  EXPECT_EQ(0., *attr.asInt()); // twice to test the cache
40  EXPECT_FALSE(Attribute("0a").asInt());
41  EXPECT_FALSE(Attribute(0.1).asInt());
42 }
43 
44 TEST(Attribute, idConversion) { // NOLINT
45  Attribute attrMax(std::to_string(std::numeric_limits<Id>::max()));
46  EXPECT_EQ(std::numeric_limits<Id>::max(), *attrMax.asId());
47 
48  Attribute attrMin(std::to_string(std::numeric_limits<Id>::min()));
49  EXPECT_EQ(std::numeric_limits<Id>::min(), *attrMin.asId());
50 
51  EXPECT_FALSE(Attribute("0a").asId());
52  EXPECT_FALSE(Attribute("0.1").asId());
53  EXPECT_FALSE(Attribute(" 0").asId());
54 }
55 
56 TEST(Attribute, doubleConversion) { // NOLINT
57  EXPECT_DOUBLE_EQ(0.1, *Attribute(0.1).asDouble());
58  EXPECT_FALSE(Attribute("0a").asId());
59 }
60 
61 TEST(Attribute, velocityConversion) { // NOLINT
62  using namespace units;
63  using namespace units::literals;
64  EXPECT_DOUBLE_EQ(0.1, KmHQuantity(*Attribute("0.1").asVelocity()).value());
65  EXPECT_DOUBLE_EQ(0.1, Attribute("0.1 mps").asVelocity()->value());
66  EXPECT_DOUBLE_EQ(0.1, Attribute("0.1mps").asVelocity()->value());
67  EXPECT_DOUBLE_EQ(0.1, KmHQuantity(*Attribute("0.1 kmh").asVelocity()).value());
68  EXPECT_DOUBLE_EQ(0.1, KmHQuantity(*Attribute("0.1 km/h").asVelocity()).value());
69  EXPECT_DOUBLE_EQ(0.1, MPHQuantity(*Attribute("0.1 m/h").asVelocity()).value());
70  EXPECT_DOUBLE_EQ(0.1, KmHQuantity(*Attribute(0.1_kmh).asVelocity()).value());
71  EXPECT_DOUBLE_EQ(0.1, Velocity(*Attribute(0.1_mps).asVelocity()).value());
72  EXPECT_FALSE(Attribute("0a").asVelocity());
73 }
74 
75 TEST(Attribute, caching) { // NOLINT
76  Attribute attr("0.1");
77  EXPECT_DOUBLE_EQ(*attr.asDouble(), 0.1);
78  EXPECT_FALSE(attr.asId());
79 }
80 
81 TEST(Attribute, as) { // NOLINT
82  Attribute attr("0");
83  EXPECT_EQ(0, *attr.as<double>());
84  EXPECT_EQ(0, *attr.as<Optional<double>>());
85  EXPECT_EQ(0, *attr.as<Optional<int>>());
86  EXPECT_EQ("0", *attr.as<std::string>());
87  EXPECT_EQ("0", *attr.as<Optional<std::string>>());
88  auto opt = attr.as<const char*>();
89  EXPECT_EQ(std::string("0"), *opt);
90  opt = attr.as<Optional<const char*>>();
91  EXPECT_EQ(std::string("0"), *opt);
92  EXPECT_EQ(0, *attr.as<Id>());
93 }
94 
95 TEST(AttributeMap, enumAccess) { // NOLINT
96  AttributeMap attr;
97  attr[AttributeName::Type] = "test";
98  EXPECT_EQ(attr[AttributeName::Type], "test");
99 }
100 
101 TEST(AttributeMap, normalAccess) { // NOLINT
102  AttributeMap attr;
103  attr[AttributeNamesString::Type] = "test";
104  EXPECT_EQ(attr[AttributeName::Type], "test");
105 }
Optional< bool > asBool() const
interpret this attribute as bool value
Definition: Attribute.cpp:61
A hybrid map is just like a normal map with keys as string, but elements can also be accessed using a...
Definition: HybridMap.h:67
boost::units::quantity< MPH > MPHQuantity
Definition: Units.h:24
int64_t Id
Definition: Forward.h:198
const std::string & value() const
gets the value of this attribute
Definition: Attribute.h:96
TEST(Attribute, string)
boost::optional< T > Optional
Definition: Optional.h:7
void setValue(const std::string &value)
set the value of this attribute
Definition: Attribute.cpp:149
An attribute represents one value of a tag of a lanelet primitive.
Definition: Attribute.h:32
Optional< ValueOfT< T > > as() const
templated version. Works ony for the as.. above
boost::units::quantity< KmH > KmHQuantity
Definition: Units.h:23
Optional< double > asDouble() const
interpret this attribute as double value
Definition: Attribute.cpp:81
Optional< Id > asId() const
interpret this attribute as an id
Definition: Attribute.cpp:95
units::MPSQuantity Velocity
Definition: Forward.h:210
Optional< int > asInt() const
interpret this attribute as an int
Definition: Attribute.cpp:109


lanelet2_core
Author(s): Fabian Poggenhans
autogenerated on Tue Jun 6 2023 02:23:32