Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 2 | /* |
Davide Pesavento | cf9e1c7 | 2019-12-22 17:56:07 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2019, Regents of the University of California, |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
| 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 24 | */ |
| 25 | |
Junxiao Shi | 331ade7 | 2016-08-19 14:07:19 +0000 | [diff] [blame] | 26 | #include "nfdc/format-helpers.hpp" |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 27 | |
| 28 | #include "tests/test-common.hpp" |
| 29 | |
Davide Pesavento | cf9e1c7 | 2019-12-22 17:56:07 -0500 | [diff] [blame] | 30 | #if BOOST_VERSION >= 105900 |
| 31 | #include <boost/test/tools/output_test_stream.hpp> |
| 32 | #else |
| 33 | #include <boost/test/output_test_stream.hpp> |
| 34 | #endif |
| 35 | |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 36 | namespace nfd { |
| 37 | namespace tools { |
Junxiao Shi | 331ade7 | 2016-08-19 14:07:19 +0000 | [diff] [blame] | 38 | namespace nfdc { |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 39 | namespace tests { |
| 40 | |
| 41 | using boost::test_tools::output_test_stream; |
| 42 | |
Junxiao Shi | 331ade7 | 2016-08-19 14:07:19 +0000 | [diff] [blame] | 43 | BOOST_AUTO_TEST_SUITE(Nfdc) |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 44 | BOOST_AUTO_TEST_SUITE(TestFormatHelpers) |
| 45 | |
| 46 | BOOST_AUTO_TEST_SUITE(Xml) |
| 47 | |
| 48 | BOOST_AUTO_TEST_CASE(TextEscaping) |
| 49 | { |
| 50 | output_test_stream os; |
| 51 | os << xml::Text{"\"less than\" & 'greater than' surround XML <element> tag name"}; |
| 52 | |
| 53 | BOOST_CHECK(os.is_equal(""less than" & 'greater than'" |
| 54 | " surround XML <element> tag name")); |
| 55 | } |
| 56 | |
Junxiao Shi | 7a36ac7 | 2018-03-21 15:23:22 +0000 | [diff] [blame] | 57 | BOOST_AUTO_TEST_CASE(Flag) |
| 58 | { |
| 59 | output_test_stream os; |
| 60 | os << "<A>" << xml::Flag{"B", true} << "</A><C>" << xml::Flag{"D", false} << "</C>"; |
| 61 | |
| 62 | BOOST_CHECK(os.is_equal("<A><B/></A><C></C>")); |
| 63 | } |
| 64 | |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 65 | BOOST_AUTO_TEST_CASE(DurationFormatting) |
| 66 | { |
| 67 | time::nanoseconds d1 = 53000_s + 87_ms + 3_us; |
| 68 | BOOST_CHECK_EQUAL(xml::formatDuration(d1), "PT53000.087S"); |
| 69 | |
| 70 | time::nanoseconds d2 = 56_s; |
| 71 | BOOST_CHECK_EQUAL(xml::formatDuration(d2), "PT56S"); |
| 72 | |
| 73 | time::nanoseconds d3 = 3_min; |
| 74 | BOOST_CHECK_EQUAL(xml::formatDuration(d3), "PT180S"); |
| 75 | |
| 76 | time::nanoseconds d4 = 0_ns; |
| 77 | BOOST_CHECK_EQUAL(xml::formatDuration(d4), "PT0S"); |
| 78 | |
| 79 | time::nanoseconds d5 = 1_ms; |
| 80 | BOOST_CHECK_EQUAL(xml::formatDuration(d5), "PT0.001S"); |
| 81 | |
| 82 | time::nanoseconds d6 = -53_s - 250_ms; |
| 83 | BOOST_CHECK_EQUAL(xml::formatDuration(d6), "-PT53.250S"); |
| 84 | |
| 85 | time::nanoseconds d7 = 1_us; |
| 86 | BOOST_CHECK_EQUAL(xml::formatDuration(d7), "PT0S"); |
| 87 | } |
| 88 | |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 89 | BOOST_AUTO_TEST_SUITE_END() // Xml |
| 90 | |
| 91 | BOOST_AUTO_TEST_SUITE(Text) |
| 92 | |
Junxiao Shi | 6c13562 | 2016-11-21 14:30:33 +0000 | [diff] [blame] | 93 | BOOST_AUTO_TEST_CASE(Space) |
| 94 | { |
| 95 | output_test_stream os; |
| 96 | os << 'A' << text::Spaces{-1} << 'B' << text::Spaces{0} << 'C' << text::Spaces{5} << 'D'; |
| 97 | |
| 98 | BOOST_CHECK(os.is_equal("ABC D")); |
| 99 | } |
| 100 | |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 101 | BOOST_AUTO_TEST_CASE(Sep) |
| 102 | { |
| 103 | output_test_stream os; |
| 104 | text::Separator sep(","); |
| 105 | for (int i = 1; i <= 3; ++i) { |
| 106 | os << sep << i; |
| 107 | } |
| 108 | |
| 109 | BOOST_CHECK(os.is_equal("1,2,3")); |
| 110 | } |
| 111 | |
Junxiao Shi | 056815e | 2017-01-29 16:39:19 +0000 | [diff] [blame] | 112 | static void |
| 113 | printItemAttributes(std::ostream& os, bool wantMultiLine) |
| 114 | { |
| 115 | text::ItemAttributes ia(wantMultiLine, 3); |
| 116 | os << ia("id") << 500 |
| 117 | << ia("uri") << "udp4://192.0.2.1:6363" |
| 118 | << ia.end(); |
| 119 | } |
| 120 | |
| 121 | BOOST_AUTO_TEST_CASE(ItemAttributesSingleLine) |
| 122 | { |
| 123 | output_test_stream os; |
| 124 | printItemAttributes(os, false); |
| 125 | BOOST_CHECK(os.is_equal("id=500 uri=udp4://192.0.2.1:6363")); |
| 126 | } |
| 127 | |
| 128 | BOOST_AUTO_TEST_CASE(ItemAttributesMultiLine) |
| 129 | { |
| 130 | output_test_stream os; |
| 131 | printItemAttributes(os, true); |
| 132 | BOOST_CHECK(os.is_equal(" id=500\nuri=udp4://192.0.2.1:6363\n")); |
| 133 | } |
| 134 | |
Junxiao Shi | 8dc473a | 2018-03-02 15:10:18 +0000 | [diff] [blame] | 135 | BOOST_AUTO_TEST_CASE(OnOff) |
| 136 | { |
| 137 | output_test_stream os; |
| 138 | os << 'A' << text::OnOff{true} << 'B' << text::OnOff{false} << 'C'; |
| 139 | |
| 140 | BOOST_CHECK(os.is_equal("AonBoffC")); |
| 141 | } |
| 142 | |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 143 | BOOST_AUTO_TEST_SUITE_END() // Text |
| 144 | |
| 145 | BOOST_AUTO_TEST_SUITE_END() // TestFormatHelpers |
Junxiao Shi | 331ade7 | 2016-08-19 14:07:19 +0000 | [diff] [blame] | 146 | BOOST_AUTO_TEST_SUITE_END() // Nfdc |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 147 | |
| 148 | } // namespace tests |
Junxiao Shi | 331ade7 | 2016-08-19 14:07:19 +0000 | [diff] [blame] | 149 | } // namespace nfdc |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 150 | } // namespace tools |
| 151 | } // namespace nfd |