|
| 1 | +/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8 |
| 2 | +// test_enum.cpp |
| 3 | + |
| 4 | +// Copyright 2026 Gennaro Prota |
| 5 | +// Distributed under the Boost Software License, Version 1.0. |
| 6 | +// (See accompanying file LICENSE_1_0.txt or copy at |
| 7 | +// https://www.boost.org/LICENSE_1_0.txt) |
| 8 | + |
| 9 | +// Tests serialization of enumerators. In particular it checks that a value of |
| 10 | +// a scoped enum whose underlying type is wider than int round trips at full |
| 11 | +// width, rather than being truncated to int on save. |
| 12 | + |
| 13 | +#include <cstddef> |
| 14 | +#include <cstdio> |
| 15 | +#include <fstream> |
| 16 | + |
| 17 | +#include <boost/config.hpp> |
| 18 | + |
| 19 | +#if defined(BOOST_NO_STDC_NAMESPACE) |
| 20 | +namespace std{ |
| 21 | + using ::remove; |
| 22 | +} |
| 23 | +#endif |
| 24 | + |
| 25 | +#include <boost/serialization/nvp.hpp> |
| 26 | +#include "test_tools.hpp" |
| 27 | + |
| 28 | +// underlying type is int, so the archived form is unchanged from earlier |
| 29 | +// releases of the library |
| 30 | +enum color { red, green = 3, blue = 100 }; |
| 31 | + |
| 32 | +#ifndef BOOST_NO_CXX11_SCOPED_ENUMS |
| 33 | + |
| 34 | +// the value 2^32 does not fit in a 32 bit int; a plain cast to int on save |
| 35 | +// used to truncate it |
| 36 | +enum class wide : long long { |
| 37 | + one = 1, |
| 38 | + big = 1LL << 32 |
| 39 | +}; |
| 40 | + |
| 41 | +// a narrow underlying type: still stored through int, so unchanged on the |
| 42 | +// wire, but it must round trip |
| 43 | +enum class narrow : signed char { |
| 44 | + minus_one = -1, |
| 45 | + two = 2, |
| 46 | + hundred = 100 |
| 47 | +}; |
| 48 | + |
| 49 | +#endif // BOOST_NO_CXX11_SCOPED_ENUMS |
| 50 | + |
| 51 | +int test_main(int /* argc */, char * /* argv */[]){ |
| 52 | + const char * testfile = boost::archive::tmpnam(NULL); |
| 53 | + BOOST_REQUIRE(NULL != testfile); |
| 54 | + |
| 55 | + const color c_save = blue; |
| 56 | + #ifndef BOOST_NO_CXX11_SCOPED_ENUMS |
| 57 | + const wide w_save = wide::big; |
| 58 | + const narrow n_save = narrow::minus_one; |
| 59 | + #endif |
| 60 | + { |
| 61 | + test_ostream os(testfile, TEST_STREAM_FLAGS); |
| 62 | + test_oarchive oa(os, TEST_ARCHIVE_FLAGS); |
| 63 | + oa << boost::serialization::make_nvp("c", c_save); |
| 64 | + #ifndef BOOST_NO_CXX11_SCOPED_ENUMS |
| 65 | + oa << boost::serialization::make_nvp("w", w_save); |
| 66 | + oa << boost::serialization::make_nvp("n", n_save); |
| 67 | + #endif |
| 68 | + } |
| 69 | + color c_load = red; |
| 70 | + #ifndef BOOST_NO_CXX11_SCOPED_ENUMS |
| 71 | + wide w_load = wide::one; |
| 72 | + narrow n_load = narrow::two; |
| 73 | + #endif |
| 74 | + { |
| 75 | + test_istream is(testfile, TEST_STREAM_FLAGS); |
| 76 | + test_iarchive ia(is, TEST_ARCHIVE_FLAGS); |
| 77 | + ia >> boost::serialization::make_nvp("c", c_load); |
| 78 | + #ifndef BOOST_NO_CXX11_SCOPED_ENUMS |
| 79 | + ia >> boost::serialization::make_nvp("w", w_load); |
| 80 | + ia >> boost::serialization::make_nvp("n", n_load); |
| 81 | + #endif |
| 82 | + } |
| 83 | + BOOST_CHECK(c_save == c_load); |
| 84 | + #ifndef BOOST_NO_CXX11_SCOPED_ENUMS |
| 85 | + BOOST_CHECK(w_save == w_load); |
| 86 | + // the point of the test: the value survived without being truncated |
| 87 | + BOOST_CHECK(static_cast<long long>(w_load) == (1LL << 32)); |
| 88 | + BOOST_CHECK(n_save == n_load); |
| 89 | + #endif |
| 90 | + |
| 91 | + std::remove(testfile); |
| 92 | + return EXIT_SUCCESS; |
| 93 | +} |
0 commit comments