Some improvements to jtree and regiongraph and started work on regiongraph unit tests
[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 BOOST_CHECK_THROW( colors f("BLUEISH"), Exception );
49
50 colors f = e;
51 colors g(f);
52 BOOST_CHECK( f == colors::BLUE );
53 BOOST_CHECK( g == colors::BLUE );
54 BOOST_CHECK( static_cast<colors::value>(f) == static_cast<colors::value>(e) );
55 BOOST_CHECK( static_cast<colors::value>(e) == static_cast<colors::value>(g) );
56 BOOST_CHECK( static_cast<colors::value>(f) == static_cast<colors::value>(g) );
57 }
58
59
60 BOOST_AUTO_TEST_CASE( StreamTest ) {
61 std::stringstream ss1, ss2, ss3, ss4, ss5, ss6, ss7;
62 std::string s;
63
64 ss1 << colors(colors::RED);
65 ss1 >> s;
66 BOOST_CHECK_EQUAL( s, "RED" );
67
68 ss2 << colors(colors::GREEN);
69 std::getline( ss2, s );
70 BOOST_CHECK_EQUAL( s, "GREEN" );
71
72 ss3 << colors(colors::BLUE);
73 ss3 >> s;
74 BOOST_CHECK_EQUAL( s, "BLUE" );
75
76 colors c;
77 ss4 << colors(colors::RED);
78 ss4 >> c;
79 BOOST_CHECK_EQUAL( c, colors::RED );
80 ss5 << colors(colors::GREEN);
81 ss5 >> c;
82 BOOST_CHECK_EQUAL( c, colors::GREEN );
83 ss6 << colors(colors::BLUE);
84 ss6 >> c;
85 BOOST_CHECK_EQUAL( c, colors::BLUE );
86
87 ss7 << "BLUEISH";
88 BOOST_CHECK_THROW( ss7 >> c, Exception );
89 }