Jens Geyer | ae0b22c | 2014-09-04 23:04:21 +0200 | [diff] [blame] | 1 | /* |
| 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 Geyer | 4d223c6 | 2014-09-05 22:31:39 +0200 | [diff] [blame] | 21 | #include "gen-cpp/EnumTest_types.h" |
Jens Geyer | ae0b22c | 2014-09-04 23:04:21 +0200 | [diff] [blame] | 22 | |
Vivek Jain | 655b9b6 | 2016-09-07 10:34:25 -0700 | [diff] [blame] | 23 | std::ostream& operator <<(std::ostream& os, const MyEnumWithCustomOstream::type& val) |
| 24 | { |
| 25 | os << "{" << (int)val << ":CUSTOM!" << "}"; |
| 26 | return os; |
| 27 | } |
| 28 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 29 | BOOST_AUTO_TEST_SUITE(EnumTest) |
Jens Geyer | ae0b22c | 2014-09-04 23:04:21 +0200 | [diff] [blame] | 30 | |
Vivek Jain | 655b9b6 | 2016-09-07 10:34:25 -0700 | [diff] [blame] | 31 | BOOST_AUTO_TEST_CASE(test_enum_value) { |
Jens Geyer | ae0b22c | 2014-09-04 23:04:21 +0200 | [diff] [blame] | 32 | // Check that all the enum values match what we expect |
name | b3e5ebe | 2014-09-12 11:15:24 +0200 | [diff] [blame] | 33 | BOOST_CHECK_EQUAL(MyEnum1::ME1_0, 0); |
| 34 | BOOST_CHECK_EQUAL(MyEnum1::ME1_1, 1); |
| 35 | BOOST_CHECK_EQUAL(MyEnum1::ME1_2, 2); |
| 36 | BOOST_CHECK_EQUAL(MyEnum1::ME1_3, 3); |
| 37 | BOOST_CHECK_EQUAL(MyEnum1::ME1_5, 5); |
| 38 | BOOST_CHECK_EQUAL(MyEnum1::ME1_6, 6); |
Jens Geyer | ae0b22c | 2014-09-04 23:04:21 +0200 | [diff] [blame] | 39 | |
name | b3e5ebe | 2014-09-12 11:15:24 +0200 | [diff] [blame] | 40 | BOOST_CHECK_EQUAL(MyEnum2::ME2_0, 0); |
| 41 | BOOST_CHECK_EQUAL(MyEnum2::ME2_1, 1); |
| 42 | BOOST_CHECK_EQUAL(MyEnum2::ME2_2, 2); |
Jens Geyer | ae0b22c | 2014-09-04 23:04:21 +0200 | [diff] [blame] | 43 | |
name | b3e5ebe | 2014-09-12 11:15:24 +0200 | [diff] [blame] | 44 | BOOST_CHECK_EQUAL(MyEnum3::ME3_0, 0); |
| 45 | BOOST_CHECK_EQUAL(MyEnum3::ME3_1, 1); |
| 46 | BOOST_CHECK_EQUAL(MyEnum3::ME3_N2, -2); |
| 47 | BOOST_CHECK_EQUAL(MyEnum3::ME3_N1, -1); |
| 48 | BOOST_CHECK_EQUAL(MyEnum3::ME3_D0, 0); |
| 49 | BOOST_CHECK_EQUAL(MyEnum3::ME3_D1, 1); |
| 50 | BOOST_CHECK_EQUAL(MyEnum3::ME3_9, 9); |
| 51 | BOOST_CHECK_EQUAL(MyEnum3::ME3_10, 10); |
Jens Geyer | ae0b22c | 2014-09-04 23:04:21 +0200 | [diff] [blame] | 52 | |
name | b3e5ebe | 2014-09-12 11:15:24 +0200 | [diff] [blame] | 53 | BOOST_CHECK_EQUAL(MyEnum4::ME4_A, 0x7ffffffd); |
| 54 | BOOST_CHECK_EQUAL(MyEnum4::ME4_B, 0x7ffffffe); |
| 55 | BOOST_CHECK_EQUAL(MyEnum4::ME4_C, 0x7fffffff); |
Vivek Jain | 655b9b6 | 2016-09-07 10:34:25 -0700 | [diff] [blame] | 56 | |
| 57 | BOOST_CHECK_EQUAL(MyEnum5::e1, 0); |
| 58 | BOOST_CHECK_EQUAL(MyEnum5::e2, 42); |
Jens Geyer | ae0b22c | 2014-09-04 23:04:21 +0200 | [diff] [blame] | 59 | } |
| 60 | |
Vivek Jain | 655b9b6 | 2016-09-07 10:34:25 -0700 | [diff] [blame] | 61 | template <class _T> |
| 62 | std::string EnumToString(_T e) |
| 63 | { |
| 64 | std::stringstream ss; |
| 65 | ss << e; |
| 66 | return ss.str(); |
| 67 | } |
| 68 | |
| 69 | |
| 70 | BOOST_AUTO_TEST_CASE(test_enum_ostream) |
| 71 | { |
| 72 | BOOST_CHECK_EQUAL(EnumToString(MyEnum1::ME1_0), "ME1_0"); |
| 73 | BOOST_CHECK_EQUAL(EnumToString(MyEnum5::e2), "e2"); |
| 74 | BOOST_CHECK_EQUAL(EnumToString(MyEnum3::ME3_N1), "ME3_N1"); |
| 75 | BOOST_CHECK_EQUAL(EnumToString(MyEnumWithCustomOstream::CustoM2), "{2:CUSTOM!}"); |
| 76 | |
| 77 | // some invalid or unknown value |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 78 | auto uut = (MyEnum5::type)44; |
Vivek Jain | 655b9b6 | 2016-09-07 10:34:25 -0700 | [diff] [blame] | 79 | BOOST_CHECK_EQUAL(EnumToString(uut), "44"); |
| 80 | } |
| 81 | |
| 82 | BOOST_AUTO_TEST_CASE(test_enum_constant) |
| 83 | { |
Jens Geyer | ae0b22c | 2014-09-04 23:04:21 +0200 | [diff] [blame] | 84 | MyStruct ms; |
| 85 | BOOST_CHECK_EQUAL(ms.me2_2, 2); |
| 86 | BOOST_CHECK_EQUAL(ms.me3_n2, -2); |
| 87 | BOOST_CHECK_EQUAL(ms.me3_d1, 1); |
| 88 | } |
| 89 | |
| 90 | BOOST_AUTO_TEST_SUITE_END() |