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 | c52cd5e | 2022-03-05 20:40:54 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, 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 | #include <boost/test/tools/output_test_stream.hpp> |
Davide Pesavento | cf9e1c7 | 2019-12-22 17:56:07 -0500 | [diff] [blame] | 31 | |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 32 | namespace nfd { |
| 33 | namespace tools { |
Junxiao Shi | 331ade7 | 2016-08-19 14:07:19 +0000 | [diff] [blame] | 34 | namespace nfdc { |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 35 | namespace tests { |
| 36 | |
| 37 | using boost::test_tools::output_test_stream; |
| 38 | |
Junxiao Shi | 331ade7 | 2016-08-19 14:07:19 +0000 | [diff] [blame] | 39 | BOOST_AUTO_TEST_SUITE(Nfdc) |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 40 | BOOST_AUTO_TEST_SUITE(TestFormatHelpers) |
| 41 | |
| 42 | BOOST_AUTO_TEST_SUITE(Xml) |
| 43 | |
| 44 | BOOST_AUTO_TEST_CASE(TextEscaping) |
| 45 | { |
| 46 | output_test_stream os; |
| 47 | os << xml::Text{"\"less than\" & 'greater than' surround XML <element> tag name"}; |
| 48 | |
| 49 | BOOST_CHECK(os.is_equal(""less than" & 'greater than'" |
| 50 | " surround XML <element> tag name")); |
| 51 | } |
| 52 | |
Junxiao Shi | 7a36ac7 | 2018-03-21 15:23:22 +0000 | [diff] [blame] | 53 | BOOST_AUTO_TEST_CASE(Flag) |
| 54 | { |
| 55 | output_test_stream os; |
| 56 | os << "<A>" << xml::Flag{"B", true} << "</A><C>" << xml::Flag{"D", false} << "</C>"; |
| 57 | |
| 58 | BOOST_CHECK(os.is_equal("<A><B/></A><C></C>")); |
| 59 | } |
| 60 | |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 61 | BOOST_AUTO_TEST_CASE(DurationFormatting) |
| 62 | { |
| 63 | time::nanoseconds d1 = 53000_s + 87_ms + 3_us; |
| 64 | BOOST_CHECK_EQUAL(xml::formatDuration(d1), "PT53000.087S"); |
| 65 | |
| 66 | time::nanoseconds d2 = 56_s; |
| 67 | BOOST_CHECK_EQUAL(xml::formatDuration(d2), "PT56S"); |
| 68 | |
| 69 | time::nanoseconds d3 = 3_min; |
| 70 | BOOST_CHECK_EQUAL(xml::formatDuration(d3), "PT180S"); |
| 71 | |
| 72 | time::nanoseconds d4 = 0_ns; |
| 73 | BOOST_CHECK_EQUAL(xml::formatDuration(d4), "PT0S"); |
| 74 | |
| 75 | time::nanoseconds d5 = 1_ms; |
| 76 | BOOST_CHECK_EQUAL(xml::formatDuration(d5), "PT0.001S"); |
| 77 | |
| 78 | time::nanoseconds d6 = -53_s - 250_ms; |
| 79 | BOOST_CHECK_EQUAL(xml::formatDuration(d6), "-PT53.250S"); |
| 80 | |
| 81 | time::nanoseconds d7 = 1_us; |
| 82 | BOOST_CHECK_EQUAL(xml::formatDuration(d7), "PT0S"); |
| 83 | } |
| 84 | |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 85 | BOOST_AUTO_TEST_SUITE_END() // Xml |
| 86 | |
| 87 | BOOST_AUTO_TEST_SUITE(Text) |
| 88 | |
Junxiao Shi | 6c13562 | 2016-11-21 14:30:33 +0000 | [diff] [blame] | 89 | BOOST_AUTO_TEST_CASE(Space) |
| 90 | { |
| 91 | output_test_stream os; |
| 92 | os << 'A' << text::Spaces{-1} << 'B' << text::Spaces{0} << 'C' << text::Spaces{5} << 'D'; |
| 93 | |
| 94 | BOOST_CHECK(os.is_equal("ABC D")); |
| 95 | } |
| 96 | |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 97 | BOOST_AUTO_TEST_CASE(Sep) |
| 98 | { |
| 99 | output_test_stream os; |
| 100 | text::Separator sep(","); |
| 101 | for (int i = 1; i <= 3; ++i) { |
| 102 | os << sep << i; |
| 103 | } |
| 104 | |
| 105 | BOOST_CHECK(os.is_equal("1,2,3")); |
| 106 | } |
| 107 | |
Junxiao Shi | 056815e | 2017-01-29 16:39:19 +0000 | [diff] [blame] | 108 | static void |
| 109 | printItemAttributes(std::ostream& os, bool wantMultiLine) |
| 110 | { |
| 111 | text::ItemAttributes ia(wantMultiLine, 3); |
| 112 | os << ia("id") << 500 |
| 113 | << ia("uri") << "udp4://192.0.2.1:6363" |
| 114 | << ia.end(); |
| 115 | } |
| 116 | |
| 117 | BOOST_AUTO_TEST_CASE(ItemAttributesSingleLine) |
| 118 | { |
| 119 | output_test_stream os; |
| 120 | printItemAttributes(os, false); |
| 121 | BOOST_CHECK(os.is_equal("id=500 uri=udp4://192.0.2.1:6363")); |
| 122 | } |
| 123 | |
| 124 | BOOST_AUTO_TEST_CASE(ItemAttributesMultiLine) |
| 125 | { |
| 126 | output_test_stream os; |
| 127 | printItemAttributes(os, true); |
| 128 | BOOST_CHECK(os.is_equal(" id=500\nuri=udp4://192.0.2.1:6363\n")); |
| 129 | } |
| 130 | |
Junxiao Shi | 8dc473a | 2018-03-02 15:10:18 +0000 | [diff] [blame] | 131 | BOOST_AUTO_TEST_CASE(OnOff) |
| 132 | { |
| 133 | output_test_stream os; |
| 134 | os << 'A' << text::OnOff{true} << 'B' << text::OnOff{false} << 'C'; |
| 135 | |
| 136 | BOOST_CHECK(os.is_equal("AonBoffC")); |
| 137 | } |
| 138 | |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 139 | BOOST_AUTO_TEST_SUITE_END() // Text |
| 140 | |
| 141 | BOOST_AUTO_TEST_SUITE_END() // TestFormatHelpers |
Junxiao Shi | 331ade7 | 2016-08-19 14:07:19 +0000 | [diff] [blame] | 142 | BOOST_AUTO_TEST_SUITE_END() // Nfdc |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 143 | |
| 144 | } // namespace tests |
Junxiao Shi | 331ade7 | 2016-08-19 14:07:19 +0000 | [diff] [blame] | 145 | } // namespace nfdc |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 146 | } // namespace tools |
| 147 | } // namespace nfd |