Jeff Thompson | fa30664 | 2013-06-17 15:06:57 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2013, Regents of the University of California |
| 4 | * Alexander Afanasyev |
| 5 | * Zhenkai Zhu |
| 6 | * |
| 7 | * BSD license, See the LICENSE file for more information |
| 8 | * |
| 9 | * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu> |
| 10 | * Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 11 | */ |
| 12 | |
| 13 | #include <sstream> |
| 14 | #include "ndn-cpp/fields/name.h" |
| 15 | #include "ndn-cpp/fields/name-component.h" |
| 16 | #include "ndn-cpp/error.h" |
| 17 | #include "ndn-cpp/wire/ccnb.h" |
| 18 | |
| 19 | #define BOOST_TEST_MAIN 1 |
| 20 | |
| 21 | #include <boost/test/unit_test.hpp> |
| 22 | |
| 23 | using namespace ndn; |
| 24 | using namespace std; |
| 25 | using namespace boost; |
| 26 | |
| 27 | BOOST_AUTO_TEST_SUITE(NameTests) |
| 28 | |
| 29 | BOOST_AUTO_TEST_CASE (Component) |
| 30 | { |
| 31 | name::Component x; |
| 32 | BOOST_CHECK_EQUAL (x.size (), 0); |
| 33 | |
| 34 | x = name::Component ("test"); |
| 35 | BOOST_CHECK_EQUAL (x.size (), 4); |
| 36 | |
| 37 | x = name::Component ("%21test"); |
| 38 | BOOST_CHECK_EQUAL (x.size (), 5); |
| 39 | |
| 40 | BOOST_CHECK_EQUAL (x.toUri (), "!test"); |
| 41 | |
| 42 | x = name::Component ("%20test"); |
| 43 | BOOST_CHECK_EQUAL (x.size (), 5); |
| 44 | |
| 45 | BOOST_CHECK_EQUAL (x.toUri (), "%20test"); |
| 46 | } |
| 47 | |
| 48 | BOOST_AUTO_TEST_CASE (Basic) |
| 49 | { |
| 50 | Name empty = Name (); |
| 51 | Name root = Name ("/"); |
| 52 | BOOST_CHECK_EQUAL (empty, root); |
| 53 | BOOST_CHECK_EQUAL (empty, Name ("/")); |
| 54 | BOOST_CHECK_EQUAL (root.size(), 0); |
| 55 | |
| 56 | empty.append ("hello"); |
| 57 | empty.append ("world"); |
| 58 | BOOST_CHECK_EQUAL (empty.size(), 2); |
| 59 | BOOST_CHECK_EQUAL (empty.toUri(), "/hello/world"); |
| 60 | empty = empty + root; |
| 61 | BOOST_CHECK_EQUAL (empty.toUri(), "/hello/world"); |
| 62 | BOOST_CHECK_EQUAL (empty.get (0).toUri (), "hello"); |
| 63 | BOOST_CHECK_EQUAL (empty.getSubName(1, 1), Name("/world")); |
| 64 | Name name("/hello/world"); |
| 65 | BOOST_CHECK_EQUAL (empty, name); |
| 66 | BOOST_CHECK_EQUAL (name, Name("/hello") + Name("/world")); |
| 67 | |
| 68 | name.appendSeqNum (1); |
| 69 | name.appendSeqNum (255); |
| 70 | name.appendSeqNum (256); |
| 71 | name.appendSeqNum (1234567890); |
| 72 | |
| 73 | BOOST_CHECK_EQUAL (name.toUri (), "/hello/world/%00%01/%00%FF/%00%01%00/%00I%96%02%D2"); |
| 74 | |
| 75 | BOOST_CHECK_EQUAL (name.get (5).toSeqNum (), 1234567890); |
| 76 | BOOST_CHECK_EQUAL (name.get (4).toSeqNum (), 256); |
| 77 | BOOST_CHECK_EQUAL (name.get (3).toSeqNum (), 255); |
| 78 | BOOST_CHECK_EQUAL (name.get (2).toSeqNum (), 1); |
| 79 | |
| 80 | BOOST_CHECK_EQUAL (name.get (-1).toUri (), "%00I%96%02%D2"); |
| 81 | |
| 82 | BOOST_CHECK_EQUAL (Name ("/%00").get (0).toSeqNum (), 0); |
| 83 | |
| 84 | const char tmp [] = {0, 1, 2, 3, 0x50}; |
| 85 | BOOST_CHECK_EQUAL (Name ().append (tmp, sizeof(tmp)).toUri (), "/%00%01%02%03P"); |
| 86 | |
| 87 | string entree("entr\u00E9e"); |
| 88 | BOOST_CHECK_EQUAL (Name ().append (entree.c_str(), entree.size()).toUri (), "/entr%C3%A9e"); |
| 89 | Name entreeName("/entr%C3%A9e"); |
| 90 | BOOST_CHECK_EQUAL_COLLECTIONS (entreeName.get(0).begin(), entreeName.get(0).end(), entree.begin(), entree.end() ); |
| 91 | |
| 92 | Name appendName ("/hello/you"); |
| 93 | appendName.append (Name ("/hello/you/too")); |
| 94 | BOOST_CHECK_EQUAL (appendName.toUri (), "/hello/you/hello/you/too"); |
| 95 | |
| 96 | Name appendSelf ("/hello/you"); |
| 97 | appendSelf.append (appendSelf); |
| 98 | BOOST_CHECK_EQUAL (appendSelf.toUri (), "/hello/you/hello/you"); |
| 99 | |
| 100 | Name withVersion ("/hello.txt/%FD%95-%25U1%DE%04/%00%01"); |
| 101 | BOOST_REQUIRE_NO_THROW (withVersion.get (1).toVersion ()); |
| 102 | BOOST_CHECK_EQUAL (withVersion.get (1).toVersion (), 1370203370106261); |
| 103 | |
| 104 | BOOST_CHECK_EQUAL (Name("/!").toUri(), "/%21"); |
| 105 | } |
| 106 | |
| 107 | BOOST_AUTO_TEST_CASE (Advanced) |
| 108 | { |
| 109 | BOOST_REQUIRE_THROW (Name (""), error::Name); |
| 110 | BOOST_REQUIRE_THROW (Name ("//"), error::Name); |
| 111 | BOOST_REQUIRE_THROW (Name ("ndn://"), error::Name); |
| 112 | BOOST_REQUIRE_THROW (Name ("bla"), error::Name); |
| 113 | BOOST_REQUIRE_THROW (Name ("bla/"), error::Name); |
| 114 | BOOST_REQUIRE_THROW (Name ("http:/test"), error::Name); |
| 115 | |
| 116 | BOOST_CHECK_EQUAL (Name ("ndn:///").toUri (), "/"); |
| 117 | BOOST_CHECK_EQUAL (Name ("ccnx:///").toUri (), "/"); |
| 118 | BOOST_CHECK_EQUAL (Name ("/").toUri (), "/"); |
| 119 | BOOST_CHECK_EQUAL (Name ("///").toUri (), "/"); |
| 120 | BOOST_CHECK_EQUAL (Name ("////////////////////////////////////////////////").toUri (), "/"); |
| 121 | BOOST_CHECK_EQUAL (Name ("ndn://bla:random@something-else:0000/actual/name").toUri (), "/actual/name"); |
| 122 | BOOST_CHECK_EQUAL (Name ("/slash/").toUri (), "/slash"); |
| 123 | |
| 124 | BOOST_CHECK_EQUAL (Name ().append (".", 1).toUri (), "/..."); |
| 125 | BOOST_CHECK_EQUAL (Name ("/.").get(0).size(), 0); |
| 126 | BOOST_CHECK_EQUAL (Name ("/..").get(0).size(), 0); |
| 127 | BOOST_CHECK_EQUAL (Name ("/...").get(0).size(), 0); |
| 128 | BOOST_CHECK_EQUAL (Name ("/....").get(0).size(), 1); |
| 129 | |
| 130 | BOOST_CHECK_EQUAL (Name ().append ("", 0).toUri (), "/..."); |
| 131 | const char tmp [] = {'.'}; |
| 132 | BOOST_CHECK_EQUAL (Name ().append (tmp, sizeof(tmp)).toUri (), "/...."); |
| 133 | } |
| 134 | |
| 135 | BOOST_AUTO_TEST_CASE (Ordering) |
| 136 | { |
| 137 | // check "canonical" ordering |
| 138 | BOOST_CHECK_EQUAL (Name ("/test"), Name ("/test")); |
| 139 | BOOST_CHECK_GE (Name ("/test"), Name ("/aaaa")); |
| 140 | BOOST_CHECK_GT (Name ("/test"), Name ("/aaaa")); |
| 141 | BOOST_CHECK_GT (Name ("/test/test/test"), Name ("/test/test")); |
| 142 | BOOST_CHECK_GE (Name ("/test/test/test"), Name ("/test/test")); |
| 143 | BOOST_CHECK_GE (Name ("/test/test/test"), Name ("/test/test/test")); |
| 144 | |
| 145 | BOOST_CHECK_LE (Name ("/test"), Name ("/aaaaa")); |
| 146 | BOOST_CHECK_LT (Name ("/test"), Name ("/aaaaa")); |
| 147 | BOOST_CHECK_LT (Name ("/test/test"), Name ("/test/test/test")); |
| 148 | BOOST_CHECK_LE (Name ("/test/test"), Name ("/test/test/test")); |
| 149 | BOOST_CHECK_LE (Name ("/test/test/test"), Name ("/test/test/test")); |
| 150 | } |
| 151 | |
| 152 | BOOST_AUTO_TEST_CASE (Encoding) |
| 153 | { |
| 154 | std::stringbuf buffer; |
| 155 | std::ostream output (&buffer); |
| 156 | |
| 157 | Name root = Name ("/"); |
| 158 | wire::Ccnb::appendName(output, root); |
| 159 | BOOST_CHECK_EQUAL (buffer.str().size(), 2); |
| 160 | } |
| 161 | |
| 162 | BOOST_AUTO_TEST_SUITE_END() |