blob: 388abb7e9efa0ceca85f5813d35e6c9081cfe0d3 [file] [log] [blame]
Jens Geyerae0b22c2014-09-04 23:04:21 +02001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19#define BOOST_TEST_MODULE EnumTest
20#include <boost/test/unit_test.hpp>
Jens Geyer4d223c62014-09-05 22:31:39 +020021#include "gen-cpp/EnumTest_types.h"
Jens Geyerae0b22c2014-09-04 23:04:21 +020022
Vivek Jain655b9b62016-09-07 10:34:25 -070023std::ostream& operator <<(std::ostream& os, const MyEnumWithCustomOstream::type& val)
24{
25 os << "{" << (int)val << ":CUSTOM!" << "}";
26 return os;
27}
28
soroshsabz6a61dfa2019-07-02 04:43:54 +043029std::string to_string(const MyEnumWithCustomOstream::type& val)
30{
31 std::ostringstream os;
32 os << val;
33 return os.str();
34}
35
Konrad Grochowski16a23a62014-11-13 15:33:38 +010036BOOST_AUTO_TEST_SUITE(EnumTest)
Jens Geyerae0b22c2014-09-04 23:04:21 +020037
Vivek Jain655b9b62016-09-07 10:34:25 -070038BOOST_AUTO_TEST_CASE(test_enum_value) {
Jens Geyerae0b22c2014-09-04 23:04:21 +020039 // Check that all the enum values match what we expect
nameb3e5ebe2014-09-12 11:15:24 +020040 BOOST_CHECK_EQUAL(MyEnum1::ME1_0, 0);
41 BOOST_CHECK_EQUAL(MyEnum1::ME1_1, 1);
42 BOOST_CHECK_EQUAL(MyEnum1::ME1_2, 2);
43 BOOST_CHECK_EQUAL(MyEnum1::ME1_3, 3);
44 BOOST_CHECK_EQUAL(MyEnum1::ME1_5, 5);
45 BOOST_CHECK_EQUAL(MyEnum1::ME1_6, 6);
Jens Geyerae0b22c2014-09-04 23:04:21 +020046
nameb3e5ebe2014-09-12 11:15:24 +020047 BOOST_CHECK_EQUAL(MyEnum2::ME2_0, 0);
48 BOOST_CHECK_EQUAL(MyEnum2::ME2_1, 1);
49 BOOST_CHECK_EQUAL(MyEnum2::ME2_2, 2);
Jens Geyerae0b22c2014-09-04 23:04:21 +020050
nameb3e5ebe2014-09-12 11:15:24 +020051 BOOST_CHECK_EQUAL(MyEnum3::ME3_0, 0);
52 BOOST_CHECK_EQUAL(MyEnum3::ME3_1, 1);
53 BOOST_CHECK_EQUAL(MyEnum3::ME3_N2, -2);
54 BOOST_CHECK_EQUAL(MyEnum3::ME3_N1, -1);
55 BOOST_CHECK_EQUAL(MyEnum3::ME3_D0, 0);
56 BOOST_CHECK_EQUAL(MyEnum3::ME3_D1, 1);
57 BOOST_CHECK_EQUAL(MyEnum3::ME3_9, 9);
58 BOOST_CHECK_EQUAL(MyEnum3::ME3_10, 10);
Jens Geyerae0b22c2014-09-04 23:04:21 +020059
nameb3e5ebe2014-09-12 11:15:24 +020060 BOOST_CHECK_EQUAL(MyEnum4::ME4_A, 0x7ffffffd);
61 BOOST_CHECK_EQUAL(MyEnum4::ME4_B, 0x7ffffffe);
62 BOOST_CHECK_EQUAL(MyEnum4::ME4_C, 0x7fffffff);
Vivek Jain655b9b62016-09-07 10:34:25 -070063
64 BOOST_CHECK_EQUAL(MyEnum5::e1, 0);
65 BOOST_CHECK_EQUAL(MyEnum5::e2, 42);
Jens Geyerae0b22c2014-09-04 23:04:21 +020066}
67
Vivek Jain655b9b62016-09-07 10:34:25 -070068template <class _T>
69std::string EnumToString(_T e)
70{
71 std::stringstream ss;
72 ss << e;
73 return ss.str();
74}
75
Vivek Jain655b9b62016-09-07 10:34:25 -070076BOOST_AUTO_TEST_CASE(test_enum_ostream)
77{
78 BOOST_CHECK_EQUAL(EnumToString(MyEnum1::ME1_0), "ME1_0");
79 BOOST_CHECK_EQUAL(EnumToString(MyEnum5::e2), "e2");
80 BOOST_CHECK_EQUAL(EnumToString(MyEnum3::ME3_N1), "ME3_N1");
81 BOOST_CHECK_EQUAL(EnumToString(MyEnumWithCustomOstream::CustoM2), "{2:CUSTOM!}");
82
83 // some invalid or unknown value
soroshsabz6a61dfa2019-07-02 04:43:54 +043084 auto uut = static_cast<MyEnum5::type>(44);
Vivek Jain655b9b62016-09-07 10:34:25 -070085 BOOST_CHECK_EQUAL(EnumToString(uut), "44");
86}
87
soroshsabz6a61dfa2019-07-02 04:43:54 +043088BOOST_AUTO_TEST_CASE(test_enum_to_string)
89{
90 BOOST_CHECK_EQUAL(::to_string(MyEnum1::ME1_0), "ME1_0");
91 BOOST_CHECK_EQUAL(::to_string(MyEnum5::e2), "e2");
92 BOOST_CHECK_EQUAL(::to_string(MyEnum3::ME3_N1), "ME3_N1");
93 BOOST_CHECK_EQUAL(::to_string(MyEnumWithCustomOstream::CustoM2), "{2:CUSTOM!}");
94
95 // some invalid or unknown value
96 auto uut = static_cast<MyEnum5::type>(44);
97 BOOST_CHECK_EQUAL(::to_string(uut), "44");
98}
99
Vivek Jain655b9b62016-09-07 10:34:25 -0700100BOOST_AUTO_TEST_CASE(test_enum_constant)
101{
Jens Geyerae0b22c2014-09-04 23:04:21 +0200102 MyStruct ms;
103 BOOST_CHECK_EQUAL(ms.me2_2, 2);
104 BOOST_CHECK_EQUAL(ms.me3_n2, -2);
105 BOOST_CHECK_EQUAL(ms.me3_d1, 1);
106}
107
108BOOST_AUTO_TEST_SUITE_END()