Added unit tests for Enum
authorJoris Mooij <joris.mooij@tuebingen.mpg.de>
Wed, 24 Mar 2010 13:48:12 +0000 (14:48 +0100)
committerJoris Mooij <joris.mooij@tuebingen.mpg.de>
Wed, 24 Mar 2010 13:48:12 +0000 (14:48 +0100)
ChangeLog
Makefile
tests/unit/enum.cpp [new file with mode: 0644]

index c0eb580..9de45c2 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -34,7 +34,7 @@ git HEAD
 * Removed RandomDRegularGraph()
 * Compressed Makefile
 * Added unit tests for Var, SmallSet, VarSet, Graph, BipartiteGraph,
-  WeightedGraph
+  WeightedGraph, Enum
 * Added unit testing framework
 * Added initialization of TRWBP weights by sampling spanning trees
 * Cleaned up MR code:
index 37767aa..baf6247 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -122,7 +122,7 @@ examples : examples/example$(EE) examples/example_bipgraph$(EE) examples/example
 
 matlabs : matlab/dai$(ME) matlab/dai_readfg$(ME) matlab/dai_writefg$(ME) matlab/dai_potstrength$(ME)
 
-unittests : tests/unit/var$(EE) tests/unit/smallset$(EE) tests/unit/varset$(EE) tests/unit/graph$(EE) tests/unit/bipgraph$(EE) tests/unit/weightedgraph$(EE)
+unittests : tests/unit/var$(EE) tests/unit/smallset$(EE) tests/unit/varset$(EE) tests/unit/graph$(EE) tests/unit/bipgraph$(EE) tests/unit/weightedgraph$(EE) tests/unit/enum$(EE)
        echo Running unit tests...
        tests/unit/var
        tests/unit/smallset
@@ -130,6 +130,7 @@ unittests : tests/unit/var$(EE) tests/unit/smallset$(EE) tests/unit/varset$(EE)
        tests/unit/graph
        tests/unit/bipgraph
        tests/unit/weightedgraph
+       tests/unit/enum
 
 tests : tests/testdai$(EE) tests/testem/testem$(EE) tests/testbbp$(EE) $(unittests)
 
@@ -203,6 +204,8 @@ tests/unit/bipgraph$(EE) : tests/unit/bipgraph.cpp $(HEADERS) $(LIB)/libdai$(LE)
        $(CC) $(CCO)$@ $< $(LIBS) $(BOOSTLIBS_UTF)
 tests/unit/weightedgraph$(EE) : tests/unit/weightedgraph.cpp $(HEADERS) $(LIB)/libdai$(LE)
        $(CC) $(CCO)$@ $< $(LIBS) $(BOOSTLIBS_UTF)
+tests/unit/enum$(EE) : tests/unit/enum.cpp $(HEADERS) $(LIB)/libdai$(LE)
+       $(CC) $(CCO)$@ $< $(LIBS) $(BOOSTLIBS_UTF)
 
 
 # TESTS
@@ -309,7 +312,7 @@ clean :
        -rm matlab/*$(ME)
        -rm examples/example$(EE) examples/example_bipgraph$(EE) examples/example_varset$(EE) examples/example_permute$(EE) examples/example_sprinkler$(EE) examples/example_sprinkler_gibbs$(EE) examples/example_sprinkler_em$(EE)
        -rm tests/testdai$(EE) tests/testem/testem$(EE) tests/testbbp$(EE)
-       -rm tests/unit/var$(EE) tests/unit/smallset$(EE) tests/unit/varset$(EE) tests/unit/graph$(EE) tests/unit/bipgraph$(EE) tests/unit/weightedgraph$(EE)
+       -rm tests/unit/var$(EE) tests/unit/smallset$(EE) tests/unit/varset$(EE) tests/unit/graph$(EE) tests/unit/bipgraph$(EE) tests/unit/weightedgraph$(EE) tests/unit/enum$(EE)
        -rm utils/fg2dot$(EE) utils/createfg$(EE) utils/fginfo$(EE)
        -rm -R doc
        -rm -R lib
@@ -342,6 +345,7 @@ clean :
        -del tests\unit\graph$(EE)
        -del tests\unit\bipgraph$(EE)
        -del tests\unit\weightedgraph$(EE)
+       -del tests\unit\enum$(EE)
        -del $(LIB)\libdai$(LE)
        -rmdir lib
 endif
diff --git a/tests/unit/enum.cpp b/tests/unit/enum.cpp
new file mode 100644 (file)
index 0000000..f732655
--- /dev/null
@@ -0,0 +1,107 @@
+/*  This file is part of libDAI - http://www.libdai.org/
+ *
+ *  libDAI is licensed under the terms of the GNU General Public License version
+ *  2, or (at your option) any later version. libDAI is distributed without any
+ *  warranty. See the file COPYING for more details.
+ *
+ *  Copyright (C) 2010  Joris Mooij      [joris dot mooij at libdai dot org]
+ */
+
+
+#define BOOST_TEST_DYN_LINK
+
+
+#include <dai/enum.h>
+#include <strstream>
+#include <iostream>
+#include <string>
+
+
+using namespace dai;
+
+
+#define BOOST_TEST_MODULE EnumTest
+
+
+#include <boost/test/unit_test.hpp>
+
+
+DAI_ENUM(colors,RED,GREEN,BLUE);
+
+
+BOOST_AUTO_TEST_CASE( ConstructorsTest ) {
+    colors c;
+    BOOST_CHECK( c == colors::RED );
+    BOOST_CHECK_EQUAL( static_cast<size_t>(c), 0 );
+    BOOST_CHECK_EQUAL( strcmp( static_cast<char const *>(c), "RED" ), 0 );
+
+    colors d(colors::GREEN);
+    BOOST_CHECK( d == colors::GREEN );
+    BOOST_CHECK_EQUAL( static_cast<size_t>(d), 1 );
+    BOOST_CHECK_EQUAL( strcmp( static_cast<char const *>(d), "GREEN" ), 0 );
+
+    colors e("BLUE");
+    BOOST_CHECK( e == colors::BLUE );
+    BOOST_CHECK_EQUAL( static_cast<size_t>(e), 2 );
+    BOOST_CHECK_EQUAL( strcmp( static_cast<char const *>(e), "BLUE" ), 0 );
+
+    bool thrown = false;
+    try {
+        colors f("BLUEISH");
+    } catch( Exception &e ) {
+        if( e.code() == Exception::UNKNOWN_ENUM_VALUE )
+            thrown = true;
+        else
+            throw;
+    }
+    BOOST_CHECK( thrown );
+
+    colors f = e;
+    colors g(f);
+    BOOST_CHECK( f == colors::BLUE );
+    BOOST_CHECK( g == colors::BLUE );
+    BOOST_CHECK( static_cast<colors::value>(f) == static_cast<colors::value>(e) );
+    BOOST_CHECK( static_cast<colors::value>(e) == static_cast<colors::value>(g) );
+    BOOST_CHECK( static_cast<colors::value>(f) == static_cast<colors::value>(g) );
+}
+
+
+BOOST_AUTO_TEST_CASE( StreamTest ) {
+    std::stringstream ss1, ss2, ss3, ss4, ss5, ss6, ss7;
+    std::string s;
+
+    ss1 << colors(colors::RED);
+    ss1 >> s;
+    BOOST_CHECK_EQUAL( s, "RED" );
+
+    ss2 << colors(colors::GREEN);
+    std::getline( ss2, s );
+    BOOST_CHECK_EQUAL( s, "GREEN" );
+
+    ss3 << colors(colors::BLUE);
+    ss3 >> s;
+    BOOST_CHECK_EQUAL( s, "BLUE" );
+
+    colors c;
+    ss4 << colors(colors::RED);
+    ss4 >> c;
+    BOOST_CHECK_EQUAL( c, colors::RED );
+    ss5 << colors(colors::GREEN);
+    ss5 >> c;
+    BOOST_CHECK_EQUAL( c, colors::GREEN );
+    ss6 << colors(colors::BLUE);
+    ss6 >> c;
+    BOOST_CHECK_EQUAL( c, colors::BLUE );
+
+    bool thrown = false;
+    try {
+        ss7 << "BLUEISH";
+        ss7 >> c;
+    } catch( Exception &e ) {
+        if( e.code() == Exception::UNKNOWN_ENUM_VALUE )
+            thrown = true;
+        else
+            throw;
+    }
+    BOOST_CHECK( thrown );
+}