build: Reviving support for precompiled headers
This commit also includes an update of ./waf, which has several
improvements. In particular, clang++ is now default compiler on OSX
platform.
This commit also includes reorganization of tests. All unit tests are
now under tests/unit-tests and integrated tests are under
tests/integrated. This change allows small compilation optimization,
partially related to precompiled headers.
Change-Id: I4c171c04d18e9cb83e461264a35b9ed85ea4d50e
diff --git a/tests/unit-tests/util/config-file-home/.ndn/client.conf b/tests/unit-tests/util/config-file-home/.ndn/client.conf
new file mode 100644
index 0000000..ba9f623
--- /dev/null
+++ b/tests/unit-tests/util/config-file-home/.ndn/client.conf
@@ -0,0 +1,2 @@
+a=/path/to/nowhere
+b=some-othervalue.01
diff --git a/tests/unit-tests/util/config-file-malformed-home/.ndn/client.conf b/tests/unit-tests/util/config-file-malformed-home/.ndn/client.conf
new file mode 100644
index 0000000..7898192
--- /dev/null
+++ b/tests/unit-tests/util/config-file-malformed-home/.ndn/client.conf
@@ -0,0 +1 @@
+a
diff --git a/tests/unit-tests/util/test-config-file.cpp b/tests/unit-tests/util/test-config-file.cpp
new file mode 100644
index 0000000..9a0b680
--- /dev/null
+++ b/tests/unit-tests/util/test-config-file.cpp
@@ -0,0 +1,115 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2014 Regents of the University of California.
+ *
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
+ *
+ * ndn-cxx library is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License and GNU Lesser
+ * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ */
+
+#include "util/config-file.hpp"
+
+#include <cstdlib>
+
+#include "boost-test.hpp"
+
+namespace ndn {
+namespace tests {
+
+class ConfigFileFixture
+{
+public:
+ ConfigFileFixture()
+ {
+ m_HOME = std::getenv("HOME");
+ }
+
+ ~ConfigFileFixture()
+ {
+ setenv("HOME", m_HOME.c_str(), 1);
+ // std::cerr << "restoring home = " << m_HOME << std::endl;
+ }
+
+protected:
+ std::string m_HOME;
+};
+
+BOOST_FIXTURE_TEST_SUITE(UtilTestConfigFile, ConfigFileFixture)
+
+BOOST_AUTO_TEST_CASE(TestParse)
+{
+ using namespace boost::filesystem;
+ // std::cerr << "current home = " << std::getenv("HOME") << std::endl;
+
+ setenv("HOME", "tests/unit-tests/util/config-file-home", 1);
+
+ path homePath(absolute(std::getenv("HOME")));
+ homePath /= ".ndn/client.conf";
+
+ try
+ {
+ ConfigFile config;
+
+ BOOST_REQUIRE_EQUAL(config.getPath(), homePath);
+
+ const ConfigFile::Parsed& parsed = config.getParsedConfiguration();
+ BOOST_CHECK_EQUAL(parsed.get<std::string>("a"), "/path/to/nowhere");
+ BOOST_CHECK_EQUAL(parsed.get<std::string>("b"), "some-othervalue.01");
+ }
+ catch(const std::runtime_error& error)
+ {
+ BOOST_FAIL("Unexpected exception: " << error.what());
+ }
+}
+
+BOOST_AUTO_TEST_CASE(EmptyPathParse)
+{
+ // std::cerr << "current home = " << std::getenv("HOME") << std::endl;
+
+ setenv("HOME", "tests/unit-tests/util/does/not/exist", 1);
+ try
+ {
+ ConfigFile config;
+ }
+ catch(const std::runtime_error& error)
+ {
+ BOOST_FAIL("Unexpected exception: " << error.what());
+ }
+}
+
+BOOST_AUTO_TEST_CASE(MalformedParse)
+{
+ using namespace boost::filesystem;
+ // std::cerr << "current home = " << std::getenv("HOME") << std::endl;
+
+ setenv("HOME", "tests/unit-tests/util/config-file-malformed-home", 1);
+
+ bool fileWasMalformed = false;
+ try
+ {
+ ConfigFile config;
+ }
+ catch(const ConfigFile::Error& error)
+ {
+ fileWasMalformed = true;
+ }
+
+ BOOST_REQUIRE(fileWasMalformed);
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+} // namespace tests
+} // namespace ndn
diff --git a/tests/unit-tests/util/test-io.cpp b/tests/unit-tests/util/test-io.cpp
new file mode 100644
index 0000000..1b369b8
--- /dev/null
+++ b/tests/unit-tests/util/test-io.cpp
@@ -0,0 +1,55 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2014 Regents of the University of California.
+ *
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
+ *
+ * ndn-cxx library is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License and GNU Lesser
+ * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ */
+
+#include "util/io.hpp"
+#include "security/key-chain.hpp"
+
+#include "boost-test.hpp"
+
+namespace ndn {
+
+BOOST_AUTO_TEST_SUITE(UtilTestIo)
+
+BOOST_AUTO_TEST_CASE(Basic)
+{
+ BOOST_REQUIRE_NO_THROW(KeyChain("sqlite3", "file"));
+ KeyChain keyChain("sqlite3", "file");
+
+ Name identity("/TestIO/Basic");
+ identity.appendVersion();
+
+ Name certName;
+ BOOST_REQUIRE_NO_THROW(certName = keyChain.createIdentity(identity));
+ shared_ptr<IdentityCertificate> idCert;
+ BOOST_REQUIRE_NO_THROW(idCert = keyChain.getCertificate(certName));
+
+ std::string file("/tmp/TestIO-Basic");
+ io::save(*idCert, file);
+ shared_ptr<IdentityCertificate> readCert = io::load<IdentityCertificate>(file);
+
+ BOOST_CHECK(static_cast<bool>(readCert));
+ BOOST_CHECK(idCert->getName() == readCert->getName());
+ keyChain.deleteIdentity(identity);
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+} // namespace ndn
diff --git a/tests/unit-tests/util/test-regex.cpp b/tests/unit-tests/util/test-regex.cpp
new file mode 100644
index 0000000..c5ef435
--- /dev/null
+++ b/tests/unit-tests/util/test-regex.cpp
@@ -0,0 +1,465 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2014 Regents of the University of California.
+ *
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
+ *
+ * ndn-cxx library is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License and GNU Lesser
+ * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ *
+ * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/>
+ */
+
+#include "util/regex/regex-backref-manager.hpp"
+#include "util/regex/regex-component-matcher.hpp"
+#include "util/regex/regex-component-set-matcher.hpp"
+#include "util/regex/regex-pattern-list-matcher.hpp"
+#include "util/regex/regex-repeat-matcher.hpp"
+#include "util/regex/regex-backref-matcher.hpp"
+#include "util/regex/regex-top-matcher.hpp"
+#include "util/regex.hpp"
+
+#include "boost-test.hpp"
+
+using namespace std;
+namespace ndn {
+
+BOOST_AUTO_TEST_SUITE(UtilTestRegex)
+
+BOOST_AUTO_TEST_CASE(ComponentMatcher)
+{
+
+ shared_ptr<RegexBackrefManager> backRef = make_shared<RegexBackrefManager>();
+ shared_ptr<RegexComponentMatcher> cm = make_shared<RegexComponentMatcher>("a", backRef);
+ bool res = cm->match(Name("/a/b/"), 0, 1);
+ BOOST_CHECK_EQUAL(res, true);
+ BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 1);
+ BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
+
+ backRef = make_shared<RegexBackrefManager>();
+ cm = make_shared<RegexComponentMatcher>("a", backRef);
+ res = cm->match(Name("/a/b/"), 1, 1);
+ BOOST_CHECK_EQUAL(res, false);
+ BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
+
+ backRef = make_shared<RegexBackrefManager>();
+ cm = make_shared<RegexComponentMatcher>("(c+)\\.(cd)", backRef);
+ res = cm->match(Name("/ccc.cd/b/"), 0, 1);
+ BOOST_CHECK_EQUAL(res, true);
+ BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 1);
+ BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("ccc.cd"));
+ BOOST_CHECK_EQUAL(backRef->getBackref(0)->getMatchResult()[0].toUri(), string("ccc"));
+ BOOST_CHECK_EQUAL(backRef->getBackref(1)->getMatchResult()[0].toUri(), string("cd"));
+}
+
+BOOST_AUTO_TEST_CASE(ComponentSetMatcher)
+{
+
+ shared_ptr<RegexBackrefManager> backRef = make_shared<RegexBackrefManager>();
+ shared_ptr<RegexComponentSetMatcher> cm = make_shared<RegexComponentSetMatcher>("<a>", backRef);
+ bool res = cm->match(Name("/a/b/"), 0, 1);
+ BOOST_CHECK_EQUAL(res, true);
+ BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 1);
+ BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
+
+ res = cm->match(Name("/a/b/"), 1, 1);
+ BOOST_CHECK_EQUAL(res, false);
+ BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
+
+ res = cm->match(Name("/a/b/"), 0, 2);
+ BOOST_CHECK_EQUAL(res, false);
+ BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
+
+ backRef = make_shared<RegexBackrefManager>();
+ cm = make_shared<RegexComponentSetMatcher>("[<a><b><c>]", backRef);
+ res = cm->match(Name("/a/b/d"), 1, 1);
+ BOOST_CHECK_EQUAL(res, true);
+ BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 1);
+ BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("b"));
+
+ res = cm->match(Name("/a/b/d"), 2, 1);
+ BOOST_CHECK_EQUAL(res, false);
+ BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
+
+ backRef = make_shared<RegexBackrefManager>();
+ cm = make_shared<RegexComponentSetMatcher>("[^<a><b><c>]", backRef);
+ res = cm->match(Name("/b/d"), 1, 1);
+ BOOST_CHECK_EQUAL(res, true);
+ BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 1);
+ BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("d"));
+
+}
+
+BOOST_AUTO_TEST_CASE(RepeatMatcher)
+{
+
+ shared_ptr<RegexBackrefManager> backRef = make_shared<RegexBackrefManager>();
+ shared_ptr<RegexRepeatMatcher> cm = make_shared<RegexRepeatMatcher>("[<a><b>]*", backRef, 8);
+ bool res = cm->match(Name("/a/b/c"), 0, 0);
+ BOOST_CHECK_EQUAL(res, true);
+ BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
+
+ cm->match(Name("/a/b/c"), 0, 2);
+ BOOST_CHECK_EQUAL(res, true);
+ BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2);
+ BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
+ BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
+
+
+
+ backRef = make_shared<RegexBackrefManager>();
+ cm = make_shared<RegexRepeatMatcher>("[<a><b>]+", backRef, 8);
+ res = cm->match(Name("/a/b/c"), 0, 0);
+ BOOST_CHECK_EQUAL(res, false);
+ BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
+
+ res = cm->match(Name("/a/b/c"), 0, 2);
+ BOOST_CHECK_EQUAL(res, true);
+ BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2);
+ BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
+ BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
+
+
+
+ backRef = make_shared<RegexBackrefManager>();
+ cm = make_shared<RegexRepeatMatcher>("<.*>*", backRef, 4);
+ res = cm->match(Name("/a/b/c/d/e/f/"), 0, 6);
+ BOOST_CHECK_EQUAL(res, true);
+ BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 6);
+ BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
+ BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
+ BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toUri(), string("c"));
+ BOOST_CHECK_EQUAL(cm->getMatchResult()[3].toUri(), string("d"));
+ BOOST_CHECK_EQUAL(cm->getMatchResult()[4].toUri(), string("e"));
+ BOOST_CHECK_EQUAL(cm->getMatchResult()[5].toUri(), string("f"));
+
+
+
+ backRef = make_shared<RegexBackrefManager>();
+ cm = make_shared<RegexRepeatMatcher>("<>*", backRef, 2);
+ res = cm->match(Name("/a/b/c/d/e/f/"), 0, 6);
+ BOOST_CHECK_EQUAL(res, true);
+ BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 6);
+ BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
+ BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
+ BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toUri(), string("c"));
+ BOOST_CHECK_EQUAL(cm->getMatchResult()[3].toUri(), string("d"));
+ BOOST_CHECK_EQUAL(cm->getMatchResult()[4].toUri(), string("e"));
+ BOOST_CHECK_EQUAL(cm->getMatchResult()[5].toUri(), string("f"));
+
+
+
+ backRef = make_shared<RegexBackrefManager>();
+ cm = make_shared<RegexRepeatMatcher>("<a>?", backRef, 3);
+ res = cm->match(Name("/a/b/c"), 0, 0);
+ BOOST_CHECK_EQUAL(res, true);
+ BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
+
+ cm = make_shared<RegexRepeatMatcher>("<a>?", backRef, 3);
+ res = cm->match(Name("/a/b/c"), 0, 1);
+ BOOST_CHECK_EQUAL(res, true);
+ BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 1);
+ BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
+
+ cm = make_shared<RegexRepeatMatcher>("<a>?", backRef, 3);
+ res = cm->match(Name("/a/b/c"), 0, 2);
+ BOOST_CHECK_EQUAL(res, false);
+ BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
+
+
+
+ backRef = make_shared<RegexBackrefManager>();
+ cm = make_shared<RegexRepeatMatcher>("[<a><b>]{3}", backRef, 8);
+ res = cm->match(Name("/a/b/a/d/"), 0, 2);
+ BOOST_CHECK_EQUAL(res, false);
+ BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
+
+ res = cm->match(Name("/a/b/a/d/"), 0, 3);
+ BOOST_CHECK_EQUAL(res, true);
+ BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 3);
+ BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
+ BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
+ BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toUri(), string("a"));
+
+ res = cm->match(Name("/a/b/a/d/"), 0, 4);
+ BOOST_CHECK_EQUAL(res, false);
+ BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
+
+
+
+ backRef = make_shared<RegexBackrefManager>();
+ cm = make_shared<RegexRepeatMatcher>("[<a><b>]{2,3}", backRef, 8);
+ res = cm->match(Name("/a/b/a/d/e/"), 0, 2);
+ BOOST_CHECK_EQUAL(res, true);
+ BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2);
+ BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
+ BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
+
+ res = cm->match(Name("/a/b/a/d/e/"), 0, 3);
+ BOOST_CHECK_EQUAL(res, true);
+ BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 3);
+ BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
+ BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
+ BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toUri(), string("a"));
+
+ res = cm->match(Name("/a/b/a/b/e/"), 0, 4);
+ BOOST_CHECK_EQUAL(res, false);
+ BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
+
+ res = cm->match(Name("/a/b/a/d/e/"), 0, 1);
+ BOOST_CHECK_EQUAL(res, false);
+ BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
+
+
+ backRef = make_shared<RegexBackrefManager>();
+ cm = make_shared<RegexRepeatMatcher>("[<a><b>]{2,}", backRef, 8);
+ res = cm->match(Name("/a/b/a/d/e/"), 0, 2);
+ BOOST_CHECK_EQUAL(res, true);
+ BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2);
+ BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
+ BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
+
+ res = cm->match(Name("/a/b/a/b/e/"), 0, 4);
+ BOOST_CHECK_EQUAL(res, true);
+ BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4);
+ BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
+ BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
+ BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toUri(), string("a"));
+ BOOST_CHECK_EQUAL(cm->getMatchResult()[3].toUri(), string("b"));
+
+ res = cm->match(Name("/a/b/a/d/e/"), 0, 1);
+ BOOST_CHECK_EQUAL(res, false);
+ BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
+
+
+
+ backRef = make_shared<RegexBackrefManager>();
+ cm = make_shared<RegexRepeatMatcher>("[<a><b>]{,2}", backRef, 8);
+ res = cm->match(Name("/a/b/a/b/e/"), 0, 3);
+ BOOST_CHECK_EQUAL(res, false);
+ BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
+
+ res = cm->match(Name("/a/b/a/b/e/"), 0, 2);
+ BOOST_CHECK_EQUAL(res, true);
+ BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2);
+ BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
+ BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
+
+ res = cm->match(Name("/a/b/a/d/e/"), 0, 1);
+ BOOST_CHECK_EQUAL(res, true);
+ BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 1);
+ BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
+
+ res = cm->match(Name("/a/b/a/d/e/"), 0, 0);
+ BOOST_CHECK_EQUAL(res, true);
+ BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
+}
+
+BOOST_AUTO_TEST_CASE(BackRefMatcher)
+{
+
+ shared_ptr<RegexBackrefManager> backRef = make_shared<RegexBackrefManager>();
+ shared_ptr<RegexBackrefMatcher> cm = make_shared<RegexBackrefMatcher>("(<a><b>)", backRef);
+ backRef->pushRef(static_pointer_cast<RegexMatcher>(cm));
+ cm->lateCompile();
+ bool res = cm->match(Name("/a/b/c"), 0, 2);
+ BOOST_CHECK_EQUAL(res, true);
+ BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2);
+ BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
+ BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
+ BOOST_CHECK_EQUAL(backRef->size(), 1);
+
+ backRef = make_shared<RegexBackrefManager>();
+ cm = make_shared<RegexBackrefMatcher>("(<a>(<b>))", backRef);
+ backRef->pushRef(cm);
+ cm->lateCompile();
+ res = cm->match(Name("/a/b/c"), 0, 2);
+ BOOST_CHECK_EQUAL(res, true);
+ BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2);
+ BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
+ BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
+ BOOST_CHECK_EQUAL(backRef->size(), 2);
+ BOOST_CHECK_EQUAL(backRef->getBackref(0)->getMatchResult()[0].toUri(), string("a"));
+ BOOST_CHECK_EQUAL(backRef->getBackref(0)->getMatchResult()[1].toUri(), string("b"));
+ BOOST_CHECK_EQUAL(backRef->getBackref(1)->getMatchResult()[0].toUri(), string("b"));
+}
+
+BOOST_AUTO_TEST_CASE(BackRefMatcherAdvanced)
+{
+
+ shared_ptr<RegexBackrefManager> backRef = make_shared<RegexBackrefManager>();
+ shared_ptr<RegexRepeatMatcher> cm = make_shared<RegexRepeatMatcher>("([<a><b>])+", backRef, 10);
+ bool res = cm->match(Name("/a/b/c"), 0, 2);
+ BOOST_CHECK_EQUAL(res, true);
+ BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2);
+ BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
+ BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
+ BOOST_CHECK_EQUAL(backRef->size(), 1);
+ BOOST_CHECK_EQUAL(backRef->getBackref(0)->getMatchResult()[0].toUri(), string("b"));
+}
+
+BOOST_AUTO_TEST_CASE(BackRefMatcherAdvanced2)
+{
+
+ shared_ptr<RegexBackrefManager> backRef = make_shared<RegexBackrefManager>();
+ shared_ptr<RegexPatternListMatcher> cm = make_shared<RegexPatternListMatcher>("(<a>(<b>))<c>", backRef);
+ bool res = cm->match(Name("/a/b/c"), 0, 3);
+ BOOST_CHECK_EQUAL(res, true);
+ BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 3);
+ BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
+ BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
+ BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toUri(), string("c"));
+ BOOST_CHECK_EQUAL(backRef->size(), 2);
+ BOOST_CHECK_EQUAL(backRef->getBackref(0)->getMatchResult()[0].toUri(), string("a"));
+ BOOST_CHECK_EQUAL(backRef->getBackref(0)->getMatchResult()[1].toUri(), string("b"));
+ BOOST_CHECK_EQUAL(backRef->getBackref(1)->getMatchResult()[0].toUri(), string("b"));
+}
+
+BOOST_AUTO_TEST_CASE(PatternListMatcher)
+{
+
+ shared_ptr<RegexBackrefManager> backRef = make_shared<RegexBackrefManager>();
+ shared_ptr<RegexPatternListMatcher> cm = make_shared<RegexPatternListMatcher>("<a>[<a><b>]", backRef);
+ bool res = cm->match(Name("/a/b/c"), 0, 2);
+ BOOST_CHECK_EQUAL(res, true);
+ BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 2);
+ BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
+ BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
+
+ backRef = make_shared<RegexBackrefManager>();
+ cm = make_shared<RegexPatternListMatcher>("<>*<a>", backRef);
+ res = cm->match(Name("/a/b/c"), 0, 1);
+ BOOST_CHECK_EQUAL(res, true);
+ BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 1);
+ BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
+
+ backRef = make_shared<RegexBackrefManager>();
+ cm = make_shared<RegexPatternListMatcher>("<>*<a>", backRef);
+ res = cm->match(Name("/a/b/c"), 0, 2);
+ BOOST_CHECK_EQUAL(res, false);
+ BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
+
+ backRef = make_shared<RegexBackrefManager>();
+ cm = make_shared<RegexPatternListMatcher>("<>*<a><>*", backRef);
+ res = cm->match(Name("/a/b/c"), 0, 3);
+ BOOST_CHECK_EQUAL(res, true);
+ BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 3);
+ BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
+ BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
+ BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toUri(), string("c"));
+
+}
+
+BOOST_AUTO_TEST_CASE(TopMatcher)
+{
+
+ shared_ptr<RegexTopMatcher> cm = make_shared<RegexTopMatcher>("^<a><b><c>");
+ bool res = cm->match(Name("/a/b/c/d"));
+ BOOST_CHECK_EQUAL(res, true);
+ BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4);
+ BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
+ BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
+ BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toUri(), string("c"));
+ BOOST_CHECK_EQUAL(cm->getMatchResult()[3].toUri(), string("d"));
+
+ cm = make_shared<RegexTopMatcher>("<b><c><d>$");
+ res = cm->match(Name("/a/b/c/d"));
+ BOOST_CHECK_EQUAL(res, true);
+ BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4);
+ BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
+ BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
+ BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toUri(), string("c"));
+ BOOST_CHECK_EQUAL(cm->getMatchResult()[3].toUri(), string("d"));
+
+ cm = make_shared<RegexTopMatcher>("^<a><b><c><d>$");
+ res = cm->match(Name("/a/b/c/d"));
+ BOOST_CHECK_EQUAL(res, true);
+ BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4);
+ BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
+ BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
+ BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toUri(), string("c"));
+ BOOST_CHECK_EQUAL(cm->getMatchResult()[3].toUri(), string("d"));
+
+ res = cm->match(Name("/a/b/c/d/e"));
+ BOOST_CHECK_EQUAL(res, false);
+ BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 0);
+
+ cm = make_shared<RegexTopMatcher>("<a><b><c><d>");
+ res = cm->match(Name("/a/b/c/d"));
+ BOOST_CHECK_EQUAL(res, true);
+ BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4);
+ BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
+ BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
+ BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toUri(), string("c"));
+ BOOST_CHECK_EQUAL(cm->getMatchResult()[3].toUri(), string("d"));
+
+
+ cm = make_shared<RegexTopMatcher>("<b><c>");
+ res = cm->match(Name("/a/b/c/d"));
+ BOOST_CHECK_EQUAL(res, true);
+ BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4);
+ BOOST_CHECK_EQUAL(cm->getMatchResult()[0].toUri(), string("a"));
+ BOOST_CHECK_EQUAL(cm->getMatchResult()[1].toUri(), string("b"));
+ BOOST_CHECK_EQUAL(cm->getMatchResult()[2].toUri(), string("c"));
+ BOOST_CHECK_EQUAL(cm->getMatchResult()[3].toUri(), string("d"));
+}
+
+BOOST_AUTO_TEST_CASE(TopMatcherAdvanced)
+{
+ shared_ptr<Regex> cm = make_shared<Regex>("^(<.*>*)<.*>");
+ bool res = cm->match(Name("/n/a/b/c"));
+ BOOST_CHECK_EQUAL(res, true);
+ BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4);
+ BOOST_CHECK_EQUAL(cm->expand("\\1"), Name("/n/a/b/"));
+
+ cm = make_shared<Regex>("^(<.*>*)<.*><c>(<.*>)<.*>");
+ res = cm->match(Name("/n/a/b/c/d/e/"));
+ BOOST_CHECK_EQUAL(res, true);
+ BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 6);
+ BOOST_CHECK_EQUAL(cm->expand("\\1\\2"), Name("/n/a/d/"));
+
+ cm = make_shared<Regex>("(<.*>*)<.*>$");
+ res = cm->match(Name("/n/a/b/c/"));
+ BOOST_CHECK_EQUAL(res, true);
+ BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4);
+ BOOST_CHECK_EQUAL(cm->expand("\\1"), Name("/n/a/b/"));
+
+ cm = make_shared<Regex>("<.*>(<.*>*)<.*>$");
+ res = cm->match(Name("/n/a/b/c/"));
+ BOOST_CHECK_EQUAL(res, true);
+ BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4);
+ BOOST_CHECK_EQUAL(cm->expand("\\1"), Name("/a/b/"));
+
+ cm = make_shared<Regex>("<a>(<>*)<>$");
+ res = cm->match(Name("/n/a/b/c/"));
+ BOOST_CHECK_EQUAL(res, true);
+ BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 4);
+ BOOST_CHECK_EQUAL(cm->expand("\\1"), Name("/b/"));
+
+ cm = make_shared<Regex>("^<ndn><(.*)\\.(.*)><DNS>(<>*)<>");
+ res = cm->match(Name("/ndn/ucla.edu/DNS/yingdi/mac/ksk-1/"));
+ BOOST_CHECK_EQUAL(res, true);
+ BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 6);
+ BOOST_CHECK_EQUAL(cm->expand("<ndn>\\2\\1\\3"), Name("/ndn/edu/ucla/yingdi/mac/"));
+
+ cm = make_shared<Regex>("^<ndn><(.*)\\.(.*)><DNS>(<>*)<>", "<ndn>\\2\\1\\3");
+ res = cm->match(Name("/ndn/ucla.edu/DNS/yingdi/mac/ksk-1/"));
+ BOOST_CHECK_EQUAL(res, true);
+ BOOST_CHECK_EQUAL(cm->getMatchResult().size(), 6);
+ BOOST_CHECK_EQUAL(cm->expand(), Name("/ndn/edu/ucla/yingdi/mac/"));
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+} // namespace ndn
diff --git a/tests/unit-tests/util/test-scheduler.cpp b/tests/unit-tests/util/test-scheduler.cpp
new file mode 100644
index 0000000..4ef4ed1
--- /dev/null
+++ b/tests/unit-tests/util/test-scheduler.cpp
@@ -0,0 +1,234 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2014 Regents of the University of California.
+ *
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
+ *
+ * ndn-cxx library is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License and GNU Lesser
+ * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ */
+
+#include "util/scheduler.hpp"
+
+#include "boost-test.hpp"
+
+namespace ndn {
+
+BOOST_AUTO_TEST_SUITE(UtilTestScheduler)
+
+struct SchedulerFixture
+{
+ SchedulerFixture()
+ : count1(0)
+ , count2(0)
+ , count3(0)
+ , count4(0)
+ {
+ }
+
+ void
+ event1()
+ {
+ BOOST_CHECK_EQUAL(count3, 1);
+ ++count1;
+ }
+
+ void
+ event2()
+ {
+ ++count2;
+ }
+
+ void
+ event3()
+ {
+ BOOST_CHECK_EQUAL(count1, 0);
+ ++count3;
+ }
+
+ void
+ event4()
+ {
+ ++count4;
+ }
+
+ int count1;
+ int count2;
+ int count3;
+ int count4;
+};
+
+BOOST_FIXTURE_TEST_CASE(Events, SchedulerFixture)
+{
+ boost::asio::io_service io;
+
+ Scheduler scheduler(io);
+ scheduler.scheduleEvent(time::milliseconds(500), bind(&SchedulerFixture::event1, this));
+
+ EventId i = scheduler.scheduleEvent(time::seconds(1), bind(&SchedulerFixture::event2, this));
+ scheduler.cancelEvent(i);
+
+ scheduler.scheduleEvent(time::milliseconds(250), bind(&SchedulerFixture::event3, this));
+
+ i = scheduler.scheduleEvent(time::milliseconds(50), bind(&SchedulerFixture::event2, this));
+ scheduler.cancelEvent(i);
+
+ i = scheduler.schedulePeriodicEvent(time::milliseconds(1500), time::milliseconds(500), bind(&SchedulerFixture::event4, this));
+ scheduler.scheduleEvent(time::seconds(4), bind(&Scheduler::cancelEvent, &scheduler, i));
+
+ io.run();
+
+ BOOST_CHECK_EQUAL(count1, 1);
+ BOOST_CHECK_EQUAL(count2, 0);
+ BOOST_CHECK_EQUAL(count3, 1);
+ BOOST_CHECK_GE(count4, 2);
+}
+
+BOOST_AUTO_TEST_CASE(CancelEmptyEvent)
+{
+ boost::asio::io_service io;
+ Scheduler scheduler(io);
+
+ EventId i;
+ scheduler.cancelEvent(i);
+}
+
+struct SelfCancelFixture
+{
+ SelfCancelFixture()
+ : m_scheduler(m_io)
+ {
+ }
+
+ void
+ cancelSelf()
+ {
+ m_scheduler.cancelEvent(m_selfEventId);
+ }
+
+ boost::asio::io_service m_io;
+ Scheduler m_scheduler;
+ EventId m_selfEventId;
+};
+
+BOOST_FIXTURE_TEST_CASE(SelfCancel, SelfCancelFixture)
+{
+ m_selfEventId = m_scheduler.scheduleEvent(time::milliseconds(100),
+ bind(&SelfCancelFixture::cancelSelf, this));
+
+ BOOST_REQUIRE_NO_THROW(m_io.run());
+}
+
+struct SelfRescheduleFixture
+{
+ SelfRescheduleFixture()
+ : m_scheduler(m_io)
+ , m_count(0)
+ {
+ }
+
+ void
+ reschedule()
+ {
+ EventId eventId = m_scheduler.scheduleEvent(time::milliseconds(100),
+ bind(&SelfRescheduleFixture::reschedule, this));
+ m_scheduler.cancelEvent(m_selfEventId);
+ m_selfEventId = eventId;
+
+ if(m_count < 5)
+ m_count++;
+ else
+ m_scheduler.cancelEvent(m_selfEventId);
+ }
+
+ void
+ reschedule2()
+ {
+ m_scheduler.cancelEvent(m_selfEventId);
+
+
+ if(m_count < 5)
+ {
+ m_selfEventId = m_scheduler.scheduleEvent(time::milliseconds(100),
+ bind(&SelfRescheduleFixture::reschedule2, this));
+ m_count++;
+ }
+ }
+
+ void
+ doNothing()
+ {
+ m_count++;
+ }
+
+ void
+ reschedule3()
+ {
+ m_scheduler.cancelEvent(m_selfEventId);
+
+ m_scheduler.scheduleEvent(time::milliseconds(100),
+ bind(&SelfRescheduleFixture::doNothing, this));
+ m_scheduler.scheduleEvent(time::milliseconds(100),
+ bind(&SelfRescheduleFixture::doNothing, this));
+ m_scheduler.scheduleEvent(time::milliseconds(100),
+ bind(&SelfRescheduleFixture::doNothing, this));
+ m_scheduler.scheduleEvent(time::milliseconds(100),
+ bind(&SelfRescheduleFixture::doNothing, this));
+ m_scheduler.scheduleEvent(time::milliseconds(100),
+ bind(&SelfRescheduleFixture::doNothing, this));
+ m_scheduler.scheduleEvent(time::milliseconds(100),
+ bind(&SelfRescheduleFixture::doNothing, this));
+ }
+
+ boost::asio::io_service m_io;
+ Scheduler m_scheduler;
+ EventId m_selfEventId;
+ int m_count;
+
+};
+
+BOOST_FIXTURE_TEST_CASE(Reschedule, SelfRescheduleFixture)
+{
+ m_selfEventId = m_scheduler.scheduleEvent(time::seconds(0),
+ bind(&SelfRescheduleFixture::reschedule, this));
+
+ BOOST_REQUIRE_NO_THROW(m_io.run());
+
+ BOOST_CHECK_EQUAL(m_count, 5);
+}
+
+BOOST_FIXTURE_TEST_CASE(Reschedule2, SelfRescheduleFixture)
+{
+ m_selfEventId = m_scheduler.scheduleEvent(time::seconds(0),
+ bind(&SelfRescheduleFixture::reschedule2, this));
+
+ BOOST_REQUIRE_NO_THROW(m_io.run());
+
+ BOOST_CHECK_EQUAL(m_count, 5);
+}
+
+BOOST_FIXTURE_TEST_CASE(Reschedule3, SelfRescheduleFixture)
+{
+ m_selfEventId = m_scheduler.scheduleEvent(time::seconds(0),
+ bind(&SelfRescheduleFixture::reschedule3, this));
+
+ BOOST_REQUIRE_NO_THROW(m_io.run());
+
+ BOOST_CHECK_EQUAL(m_count, 6);
+}
+
+
+BOOST_AUTO_TEST_SUITE_END()
+
+} // namespace ndn
diff --git a/tests/unit-tests/util/test-time.cpp b/tests/unit-tests/util/test-time.cpp
new file mode 100644
index 0000000..d3a659c
--- /dev/null
+++ b/tests/unit-tests/util/test-time.cpp
@@ -0,0 +1,77 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2014 Regents of the University of California.
+ *
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
+ *
+ * ndn-cxx library is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License and GNU Lesser
+ * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ */
+
+#include "util/time.hpp"
+
+#include "boost-test.hpp"
+
+namespace ndn {
+
+BOOST_AUTO_TEST_SUITE(UtilTestTime)
+
+BOOST_AUTO_TEST_CASE(SystemClock)
+{
+ time::system_clock::TimePoint value = time::system_clock::now();
+ time::system_clock::TimePoint referenceTime =
+ time::fromUnixTimestamp(time::milliseconds(1390966967032LL));
+
+ BOOST_CHECK_GT(value, referenceTime);
+
+ BOOST_CHECK_EQUAL(time::toIsoString(referenceTime), "20140129T034247.032000");
+ BOOST_CHECK_EQUAL(time::toString(referenceTime), "2014-01-29 03:42:47");
+ BOOST_CHECK_EQUAL(time::toString(referenceTime), "2014-01-29 03:42:47");
+
+ // Unfortunately, not all systems has lv_LV locale installed :(
+ // BOOST_CHECK_EQUAL(time::toString(referenceTime, "%Y. gada %d. %B",
+ // std::locale("lv_LV.UTF-8")),
+ // "2014. gada 29. Janvāris");
+
+ BOOST_CHECK_EQUAL(time::toString(referenceTime, "%Y -- %d -- %B",
+ std::locale("C")),
+ "2014 -- 29 -- January");
+
+ BOOST_CHECK_EQUAL(time::fromIsoString("20140129T034247.032000"), referenceTime);
+ BOOST_CHECK_EQUAL(time::fromIsoString("20140129T034247.032000Z"), referenceTime);
+ BOOST_CHECK_EQUAL(time::fromString("2014-01-29 03:42:47"),
+ time::fromUnixTimestamp(time::seconds(1390966967)));
+
+ // Unfortunately, not all systems has lv_LV locale installed :(
+ // BOOST_CHECK_EQUAL(time::fromString("2014. gada 29. Janvāris", "%Y. gada %d. %B",
+ // std::locale("lv_LV.UTF-8")),
+ // time::fromUnixTimestamp(time::seconds(1390953600)));
+
+ BOOST_CHECK_EQUAL(time::fromString("2014 -- 29 -- January", "%Y -- %d -- %B",
+ std::locale("C")),
+ time::fromUnixTimestamp(time::seconds(1390953600)));
+}
+
+BOOST_AUTO_TEST_CASE(SteadyClock)
+{
+ time::steady_clock::TimePoint oldValue = time::steady_clock::now();
+ usleep(100);
+ time::steady_clock::TimePoint newValue = time::steady_clock::now();
+
+ BOOST_CHECK_GT(newValue, oldValue);
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+} // namespace ndn