f732655e4b2b3816c7d83a3d24d79f5400cafa5f
[libdai.git] / tests / unit / enum.cpp
1 /* This file is part of libDAI - http://www.libdai.org/
2 *
3 * libDAI is licensed under the terms of the GNU General Public License version
4 * 2, or (at your option) any later version. libDAI is distributed without any
5 * warranty. See the file COPYING for more details.
6 *
7 * Copyright (C) 2010 Joris Mooij [joris dot mooij at libdai dot org]
8 */
9
10
11 #define BOOST_TEST_DYN_LINK
12
13
14 #include <dai/enum.h>
15 #include <strstream>
16 #include <iostream>
17 #include <string>
18
19
20 using namespace dai;
21
22
23 #define BOOST_TEST_MODULE EnumTest
24
25
26 #include <boost/test/unit_test.hpp>
27
28
29 DAI_ENUM(colors,RED,GREEN,BLUE);
30
31
32 BOOST_AUTO_TEST_CASE( ConstructorsTest ) {
33 colors c;
34 BOOST_CHECK( c == colors::RED );
35 BOOST_CHECK_EQUAL( static_cast<size_t>(c), 0 );
36 BOOST_CHECK_EQUAL( strcmp( static_cast<char const *>(c), "RED" ), 0 );
37
38 colors d(colors::GREEN);
39 BOOST_CHECK( d == colors::GREEN );
40 BOOST_CHECK_EQUAL( static_cast<size_t>(d), 1 );
41 BOOST_CHECK_EQUAL( strcmp( static_cast<char const *>(d), "GREEN" ), 0 );
42
43 colors e("BLUE");
44 BOOST_CHECK( e == colors::BLUE );
45 BOOST_CHECK_EQUAL( static_cast<size_t>(e), 2 );
46 BOOST_CHECK_EQUAL( strcmp( static_cast<char const *>(e), "BLUE" ), 0 );
47
48 bool thrown = false;
49 try {
50 colors f("BLUEISH");
51 } catch( Exception &e ) {
52 if( e.code() == Exception::UNKNOWN_ENUM_VALUE )
53 thrown = true;
54 else
55 throw;
56 }
57 BOOST_CHECK( thrown );
58
59 colors f = e;
60 colors g(f);
61 BOOST_CHECK( f == colors::BLUE );
62 BOOST_CHECK( g == colors::BLUE );
63 BOOST_CHECK( static_cast<colors::value>(f) == static_cast<colors::value>(e) );
64 BOOST_CHECK( static_cast<colors::value>(e) == static_cast<colors::value>(g) );
65 BOOST_CHECK( static_cast<colors::value>(f) == static_cast<colors::value>(g) );
66 }
67
68
69 BOOST_AUTO_TEST_CASE( StreamTest ) {
70 std::stringstream ss1, ss2, ss3, ss4, ss5, ss6, ss7;
71 std::string s;
72
73 ss1 << colors(colors::RED);
74 ss1 >> s;
75 BOOST_CHECK_EQUAL( s, "RED" );
76
77 ss2 << colors(colors::GREEN);
78 std::getline( ss2, s );
79 BOOST_CHECK_EQUAL( s, "GREEN" );
80
81 ss3 << colors(colors::BLUE);
82 ss3 >> s;
83 BOOST_CHECK_EQUAL( s, "BLUE" );
84
85 colors c;
86 ss4 << colors(colors::RED);
87 ss4 >> c;
88 BOOST_CHECK_EQUAL( c, colors::RED );
89 ss5 << colors(colors::GREEN);
90 ss5 >> c;
91 BOOST_CHECK_EQUAL( c, colors::GREEN );
92 ss6 << colors(colors::BLUE);
93 ss6 >> c;
94 BOOST_CHECK_EQUAL( c, colors::BLUE );
95
96 bool thrown = false;
97 try {
98 ss7 << "BLUEISH";
99 ss7 >> c;
100 } catch( Exception &e ) {
101 if( e.code() == Exception::UNKNOWN_ENUM_VALUE )
102 thrown = true;
103 else
104 throw;
105 }
106 BOOST_CHECK( thrown );
107 }