Fixed two problems related to g++ 4.0.0 on Darwin 9.8.0
[libdai.git] / tests / unit / enum_test.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 #include <dai/enum.h>
12 #include <strstream>
13 #include <iostream>
14 #include <string>
15
16
17 using namespace dai;
18
19
20 #define BOOST_TEST_MODULE EnumTest
21
22
23 #include <boost/test/unit_test.hpp>
24
25
26 DAI_ENUM(colors,RED,GREEN,BLUE);
27
28
29 BOOST_AUTO_TEST_CASE( ConstructorsTest ) {
30 colors c;
31 BOOST_CHECK( c == colors::RED );
32 BOOST_CHECK_EQUAL( static_cast<size_t>(c), 0 );
33 BOOST_CHECK_EQUAL( strcmp( static_cast<char const *>(c), "RED" ), 0 );
34
35 colors d(colors::GREEN);
36 BOOST_CHECK( d == colors::GREEN );
37 BOOST_CHECK_EQUAL( static_cast<size_t>(d), 1 );
38 BOOST_CHECK_EQUAL( strcmp( static_cast<char const *>(d), "GREEN" ), 0 );
39
40 colors e("BLUE");
41 BOOST_CHECK( e == colors::BLUE );
42 BOOST_CHECK_EQUAL( static_cast<size_t>(e), 2 );
43 BOOST_CHECK_EQUAL( strcmp( static_cast<char const *>(e), "BLUE" ), 0 );
44
45 BOOST_CHECK_THROW( colors f("BLUEISH"), Exception );
46
47 colors f = e;
48 colors g(f);
49 BOOST_CHECK( f == colors::BLUE );
50 BOOST_CHECK( g == colors::BLUE );
51 BOOST_CHECK( static_cast<colors::value>(f) == static_cast<colors::value>(e) );
52 BOOST_CHECK( static_cast<colors::value>(e) == static_cast<colors::value>(g) );
53 BOOST_CHECK( static_cast<colors::value>(f) == static_cast<colors::value>(g) );
54 }
55
56
57 BOOST_AUTO_TEST_CASE( StreamTest ) {
58 std::stringstream ss1, ss2, ss3, ss4, ss5, ss6, ss7;
59 std::string s;
60
61 ss1 << colors(colors::RED);
62 ss1 >> s;
63 BOOST_CHECK_EQUAL( s, "RED" );
64
65 ss2 << colors(colors::GREEN);
66 std::getline( ss2, s );
67 BOOST_CHECK_EQUAL( s, "GREEN" );
68
69 ss3 << colors(colors::BLUE);
70 ss3 >> s;
71 BOOST_CHECK_EQUAL( s, "BLUE" );
72
73 colors c;
74 ss4 << colors(colors::RED);
75 ss4 >> c;
76 BOOST_CHECK_EQUAL( c, colors::RED );
77 ss5 << colors(colors::GREEN);
78 ss5 >> c;
79 BOOST_CHECK_EQUAL( c, colors::GREEN );
80 ss6 << colors(colors::BLUE);
81 ss6 >> c;
82 BOOST_CHECK_EQUAL( c, colors::BLUE );
83
84 ss7 << "BLUEISH";
85 BOOST_CHECK_THROW( ss7 >> c, Exception );
86 }