New git HEAD version
[libdai.git] / tests / unit / enum_test.cpp
1 /* This file is part of libDAI - http://www.libdai.org/
2 *
3 * Copyright (c) 2006-2011, The libDAI authors. All rights reserved.
4 *
5 * Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
6 */
7
8
9 #include <dai/enum.h>
10 #include <strstream>
11 #include <iostream>
12 #include <string>
13
14
15 using namespace dai;
16
17
18 #define BOOST_TEST_MODULE EnumTest
19
20
21 #include <boost/test/unit_test.hpp>
22
23
24 DAI_ENUM(colors,RED,GREEN,BLUE);
25
26
27 BOOST_AUTO_TEST_CASE( ConstructorsTest ) {
28 colors c;
29 BOOST_CHECK( c == colors::RED );
30 BOOST_CHECK_EQUAL( static_cast<size_t>(c), 0 );
31 BOOST_CHECK_EQUAL( strcmp( static_cast<char const *>(c), "RED" ), 0 );
32
33 colors d(colors::GREEN);
34 BOOST_CHECK( d == colors::GREEN );
35 BOOST_CHECK_EQUAL( static_cast<size_t>(d), 1 );
36 BOOST_CHECK_EQUAL( strcmp( static_cast<char const *>(d), "GREEN" ), 0 );
37
38 colors e("BLUE");
39 BOOST_CHECK( e == colors::BLUE );
40 BOOST_CHECK_EQUAL( static_cast<size_t>(e), 2 );
41 BOOST_CHECK_EQUAL( strcmp( static_cast<char const *>(e), "BLUE" ), 0 );
42
43 BOOST_CHECK_THROW( colors f("BLUEISH"), Exception );
44
45 colors f = e;
46 colors g(f);
47 BOOST_CHECK( f == colors::BLUE );
48 BOOST_CHECK( g == colors::BLUE );
49 BOOST_CHECK( static_cast<colors::value>(f) == static_cast<colors::value>(e) );
50 BOOST_CHECK( static_cast<colors::value>(e) == static_cast<colors::value>(g) );
51 BOOST_CHECK( static_cast<colors::value>(f) == static_cast<colors::value>(g) );
52 }
53
54
55 BOOST_AUTO_TEST_CASE( StreamTest ) {
56 std::stringstream ss1, ss2, ss3, ss4, ss5, ss6, ss7;
57 std::string s;
58
59 ss1 << colors(colors::RED);
60 ss1 >> s;
61 BOOST_CHECK_EQUAL( s, "RED" );
62
63 ss2 << colors(colors::GREEN);
64 std::getline( ss2, s );
65 BOOST_CHECK_EQUAL( s, "GREEN" );
66
67 ss3 << colors(colors::BLUE);
68 ss3 >> s;
69 BOOST_CHECK_EQUAL( s, "BLUE" );
70
71 colors c;
72 ss4 << colors(colors::RED);
73 ss4 >> c;
74 BOOST_CHECK_EQUAL( c, colors::RED );
75 ss5 << colors(colors::GREEN);
76 ss5 >> c;
77 BOOST_CHECK_EQUAL( c, colors::GREEN );
78 ss6 << colors(colors::BLUE);
79 ss6 >> c;
80 BOOST_CHECK_EQUAL( c, colors::BLUE );
81
82 ss7 << "BLUEISH";
83 BOOST_CHECK_THROW( ss7 >> c, Exception );
84 }