THRIFT-2513 clean up enum value assignment
Patch: Dave Watson
This closes #88
Summary:
Clean up how enum values are handled if an integer value is not
explicitly specified in the thrift file.
For example, the following used to be a compile error, but
works now:
enum MyEnum {
SOMEVALUE
}
struct MyStruct {
1: MyEnum e = SOMEVALUE
}
This change also cleans up some of the error handling with out-of-range
values. Previously thrift simply issued a warning for enum values that
didn't fit in an i32, but serialized them as i32 anyway. Now
out-of-range enum values result in a compile failure.
Test Plan:
Included a new unit test to verify the assignment of enum values. I
also verified that g++ makes the same enum value assignments when
compiling these enums as C++ code.
diff --git a/lib/cpp/test/EnumTest.cpp b/lib/cpp/test/EnumTest.cpp
new file mode 100644
index 0000000..21bf819
--- /dev/null
+++ b/lib/cpp/test/EnumTest.cpp
@@ -0,0 +1,59 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+#define BOOST_TEST_MODULE EnumTest
+#include <boost/test/unit_test.hpp>
+#include "thrift/test/gen-cpp/EnumTest_types.h"
+
+BOOST_AUTO_TEST_SUITE( EnumTest )
+
+BOOST_AUTO_TEST_CASE( test_enum ) {
+ // Check that all the enum values match what we expect
+ BOOST_CHECK_EQUAL(ME1_0, 0);
+ BOOST_CHECK_EQUAL(ME1_1, 1);
+ BOOST_CHECK_EQUAL(ME1_2, 2);
+ BOOST_CHECK_EQUAL(ME1_3, 3);
+ BOOST_CHECK_EQUAL(ME1_5, 5);
+ BOOST_CHECK_EQUAL(ME1_6, 6);
+
+ BOOST_CHECK_EQUAL(ME2_0, 0);
+ BOOST_CHECK_EQUAL(ME2_1, 1);
+ BOOST_CHECK_EQUAL(ME2_2, 2);
+
+ BOOST_CHECK_EQUAL(ME3_0, 0);
+ BOOST_CHECK_EQUAL(ME3_1, 1);
+ BOOST_CHECK_EQUAL(ME3_N2, -2);
+ BOOST_CHECK_EQUAL(ME3_N1, -1);
+ BOOST_CHECK_EQUAL(ME3_D0, 0);
+ BOOST_CHECK_EQUAL(ME3_D1, 1);
+ BOOST_CHECK_EQUAL(ME3_9, 9);
+ BOOST_CHECK_EQUAL(ME3_10, 10);
+
+ BOOST_CHECK_EQUAL(ME4_A, 0x7ffffffd);
+ BOOST_CHECK_EQUAL(ME4_B, 0x7ffffffe);
+ BOOST_CHECK_EQUAL(ME4_C, 0x7fffffff);
+}
+
+BOOST_AUTO_TEST_CASE( test_enum_constant ) {
+ MyStruct ms;
+ BOOST_CHECK_EQUAL(ms.me2_2, 2);
+ BOOST_CHECK_EQUAL(ms.me3_n2, -2);
+ BOOST_CHECK_EQUAL(ms.me3_d1, 1);
+}
+
+BOOST_AUTO_TEST_SUITE_END()
diff --git a/lib/cpp/test/Makefile.am b/lib/cpp/test/Makefile.am
index c1fad3e..e68e66b 100755
--- a/lib/cpp/test/Makefile.am
+++ b/lib/cpp/test/Makefile.am
@@ -20,6 +20,8 @@
noinst_LTLIBRARIES = libtestgencpp.la libprocessortest.la
nodist_libtestgencpp_la_SOURCES = \
gen-cpp/DebugProtoTest_types.cpp \
+ gen-cpp/EnumTest_types.cpp \
+ gen-cpp/EnumTest_types.h \
gen-cpp/OptionalRequiredTest_types.cpp \
gen-cpp/DebugProtoTest_types.cpp \
gen-cpp/ThriftTest_types.cpp \
@@ -65,7 +67,8 @@
TFileTransportTest \
UnitTests \
link_test \
- OpenSSLManualInitTest
+ OpenSSLManualInitTest \
+ EnumTest
# disable these test ... too strong
# processor_test
# concurrency_test
@@ -112,6 +115,13 @@
-l:libboost_unit_test_framework.a \
-lz
+EnumTest_SOURCES = \
+ EnumTest.cpp
+
+EnumTest_LDADD = \
+ libtestgencpp.la \
+ $(BOOST_ROOT_PATH)/lib/libboost_unit_test_framework.a
+
TFileTransportTest_SOURCES = \
TFileTransportTest.cpp
@@ -233,6 +243,9 @@
gen-cpp/DebugProtoTest_types.cpp gen-cpp/DebugProtoTest_types.h: $(top_srcdir)/test/DebugProtoTest.thrift
$(THRIFT) --gen cpp:dense $<
+gen-cpp/EnumTest_types.cpp gen-cpp/EnumTest_types.h: $(top_srcdir)/test/EnumTest.thrift
+ $(THRIFT) --gen cpp $<
+
gen-cpp/OptionalRequiredTest_types.cpp gen-cpp/OptionalRequiredTest_types.h: $(top_srcdir)/test/OptionalRequiredTest.thrift
$(THRIFT) --gen cpp:dense $<