00001 #include <boost/test/unit_test.hpp>
00002 #include "Vocabulary.hpp"
00003 #include "Converter.hpp"
00004 #include <sstream>
00005
00006 using namespace aitools::invertedindex;
00007
00008 BOOST_AUTO_TEST_SUITE(vocabulary);
00009
00010 BOOST_AUTO_TEST_CASE(default_ctor)
00011 {
00012 Vocabulary vocabulary;
00013 BOOST_CHECK(vocabulary.is_empty());
00014 BOOST_CHECK_EQUAL(vocabulary.size(), 0);
00015 }
00016
00017 BOOST_AUTO_TEST_CASE(explicit_ctor)
00018 {
00019 BOOST_CHECK_THROW(Vocabulary("/does/not/exist.txt"), std::invalid_argument);
00020
00021 }
00022
00023 BOOST_AUTO_TEST_CASE(copy_ctor)
00024 {
00025 Vocabulary vocabulary;
00026 vocabulary.insert("a");
00027 vocabulary.insert("b");
00028 vocabulary.insert("c");
00029 Vocabulary deep_copy(vocabulary);
00030 BOOST_CHECK_EQUAL(deep_copy.size(), vocabulary.size());
00031 BOOST_CHECK(deep_copy.contains("a"));
00032 BOOST_CHECK(deep_copy.contains("b"));
00033 BOOST_CHECK(deep_copy.contains("c"));
00034 }
00035
00036 BOOST_AUTO_TEST_CASE(accumulate)
00037 {
00038 Vocabulary vocabulary;
00039 vocabulary.insert("a", 10);
00040 vocabulary.accumulate("a", 10);
00041 BOOST_CHECK_EQUAL(vocabulary.find("a")->second, 20);
00042 vocabulary.accumulate("b", 10);
00043 vocabulary.accumulate("b", 10);
00044 BOOST_CHECK_EQUAL(vocabulary.find("b")->second, 20);
00045 }
00046
00047 BOOST_AUTO_TEST_CASE(clear)
00048 {
00049 Vocabulary vocabulary;
00050 vocabulary.insert("a");
00051 vocabulary.insert("b");
00052 vocabulary.insert("c");
00053 BOOST_CHECK_EQUAL(vocabulary.size(), 3);
00054 vocabulary.clear();
00055 BOOST_CHECK(vocabulary.is_empty());
00056 }
00057
00058 BOOST_AUTO_TEST_CASE(contains)
00059 {
00060 Vocabulary vocabulary;
00061 vocabulary.insert("a");
00062 vocabulary.insert("b");
00063 BOOST_CHECK(vocabulary.contains("a"));
00064 BOOST_CHECK(vocabulary.contains("b"));
00065 vocabulary.accumulate("c", 0);
00066 vocabulary.accumulate("d", 0);
00067 BOOST_CHECK(vocabulary.contains("c"));
00068 BOOST_CHECK(vocabulary.contains("d"));
00069 }
00070
00071 BOOST_AUTO_TEST_CASE(find)
00072 {
00073 Vocabulary vocabulary;
00074 vocabulary.insert("a", 10);
00075 BOOST_CHECK(vocabulary.find("a") != vocabulary.end());
00076 BOOST_CHECK(vocabulary.find("b") == vocabulary.end());
00077 BOOST_CHECK_EQUAL(vocabulary.find("a")->first, "a");
00078 BOOST_CHECK_EQUAL(vocabulary.find("a")->second, 10);
00079 }
00080
00081 BOOST_AUTO_TEST_CASE(get_words)
00082 {
00083 Vocabulary vocabulary;
00084 vocabulary.insert("a");
00085 vocabulary.insert("b");
00086 vocabulary.insert("c");
00087 std::vector<std::string> words;
00088 vocabulary.get_words(words);
00089 BOOST_CHECK_EQUAL(words.size(), vocabulary.size());
00090 for (unsigned i(0); i != words.size(); ++i)
00091 {
00092 BOOST_CHECK_EQUAL(words[i], vocabulary.find(words[i])->first);
00093 }
00094 }
00095
00096 BOOST_AUTO_TEST_CASE(save_load)
00097 {
00098 Vocabulary vocabulary;
00099 vocabulary.insert("a", 10);
00100 vocabulary.insert("b", 20);
00101 vocabulary.insert("c", 30);
00102 bfs::path file("aitools.invertedindex.vocabulary.txt");
00103 vocabulary.save(file);
00104 Vocabulary deep_copy(file);
00105 BOOST_CHECK_EQUAL(deep_copy.find("a")->first, "a");
00106 BOOST_CHECK_EQUAL(deep_copy.find("a")->second, 10);
00107 BOOST_CHECK_EQUAL(deep_copy.find("b")->first, "b");
00108 BOOST_CHECK_EQUAL(deep_copy.find("b")->second, 20);
00109 BOOST_CHECK_EQUAL(deep_copy.find("c")->first, "c");
00110 BOOST_CHECK_EQUAL(deep_copy.find("c")->second, 30);
00111 deep_copy.clear();
00112 deep_copy.load(file);
00113 BOOST_CHECK_EQUAL(deep_copy.find("a")->first, "a");
00114 BOOST_CHECK_EQUAL(deep_copy.find("a")->second, 10);
00115 BOOST_CHECK_EQUAL(deep_copy.find("b")->first, "b");
00116 BOOST_CHECK_EQUAL(deep_copy.find("b")->second, 20);
00117 BOOST_CHECK_EQUAL(deep_copy.find("c")->first, "c");
00118 BOOST_CHECK_EQUAL(deep_copy.find("c")->second, 30);
00119 bfs::remove(file);
00120 }
00121
00122 BOOST_AUTO_TEST_SUITE_END();