Yingdi Yu | 0c3e591 | 2015-03-17 14:22:38 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014, Regents of the University of California |
| 4 | * |
| 5 | * This file is part of NSL (NDN Signature Logger). |
| 6 | * See AUTHORS.md for complete list of NSL authors and contributors. |
| 7 | * |
| 8 | * NSL is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, |
| 10 | * either version 3 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * NSL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * NSL, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of nsl authors and contributors. |
| 20 | */ |
| 21 | |
| 22 | #include "../tree-generator.hpp" |
| 23 | |
| 24 | #include "boost-test.hpp" |
| 25 | |
| 26 | namespace nsl { |
| 27 | namespace tests { |
| 28 | |
| 29 | BOOST_AUTO_TEST_SUITE(TestTreeGenerator) |
| 30 | |
| 31 | BOOST_AUTO_TEST_CASE(HashGeneration) |
| 32 | { |
| 33 | for (size_t n = 5; n <= 8; n++) { |
| 34 | auto hash1 = TreeGenerator::getHash(Node::Index(0, 3), n); |
| 35 | SubTreeBinary tree(TreeGenerator::LOGGER_NAME, Node::Index(0, 5), |
| 36 | [&] (const Node::Index&) {}, |
| 37 | [&] (const Node::Index&, |
| 38 | const NonNegativeInteger&, |
| 39 | ndn::ConstBufferPtr) {}); |
| 40 | for (size_t i = 0; i < n; i++) { |
| 41 | auto node = make_shared<Node>(i, 0, i + 1, Node::getEmptyHash()); |
| 42 | tree.addLeaf(node); |
| 43 | } |
| 44 | auto hash2 = tree.getRoot()->getHash(); |
| 45 | BOOST_CHECK_EQUAL_COLLECTIONS(hash1->begin(), hash1->end(), hash2->begin(), hash2->end()); |
| 46 | } |
| 47 | |
| 48 | for (size_t n = 33; n <= 64; n++) { |
| 49 | auto hash1 = TreeGenerator::getHash(Node::Index(32, 5), n); |
| 50 | SubTreeBinary tree(TreeGenerator::LOGGER_NAME, Node::Index(32, 5), |
| 51 | [&] (const Node::Index&) {}, |
| 52 | [&] (const Node::Index&, |
| 53 | const NonNegativeInteger&, |
| 54 | ndn::ConstBufferPtr) {}); |
| 55 | for (size_t i = 32; i < n; i++) { |
| 56 | auto node = make_shared<Node>(i, 0, i + 1, Node::getEmptyHash()); |
| 57 | tree.addLeaf(node); |
| 58 | } |
| 59 | auto hash2 = tree.getRoot()->getHash(); |
| 60 | BOOST_CHECK_EQUAL_COLLECTIONS(hash1->begin(), hash1->end(), hash2->begin(), hash2->end()); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | BOOST_AUTO_TEST_CASE(TreeGeneration) |
| 65 | { |
| 66 | for (size_t n = 1; n <= 32; n++) { |
| 67 | auto hash1 = TreeGenerator::getSubTreeBinary(Node::Index(0, 5), n)->getRoot()->getHash(); |
| 68 | SubTreeBinary tree(TreeGenerator::LOGGER_NAME, Node::Index(0, 5), |
| 69 | [&] (const Node::Index&) {}, |
| 70 | [&] (const Node::Index&, |
| 71 | const NonNegativeInteger&, |
| 72 | ndn::ConstBufferPtr) {}); |
| 73 | for (size_t i = 0; i < n; i++) { |
| 74 | auto node = make_shared<Node>(i, 0, i + 1, Node::getEmptyHash()); |
| 75 | tree.addLeaf(node); |
| 76 | } |
| 77 | auto hash2 = tree.getRoot()->getHash(); |
| 78 | BOOST_CHECK_EQUAL_COLLECTIONS(hash1->begin(), hash1->end(), hash2->begin(), hash2->end()); |
| 79 | } |
| 80 | |
| 81 | for (size_t n = 33; n <= 64; n++) { |
| 82 | auto hash1 = TreeGenerator::getSubTreeBinary(Node::Index(32, 5), n)->getRoot()->getHash(); |
| 83 | SubTreeBinary tree(TreeGenerator::LOGGER_NAME, Node::Index(32, 5), |
| 84 | [&] (const Node::Index&) {}, |
| 85 | [&] (const Node::Index&, |
| 86 | const NonNegativeInteger&, |
| 87 | ndn::ConstBufferPtr) {}); |
| 88 | for (size_t i = 32; i < n; i++) { |
| 89 | auto node = make_shared<Node>(i, 0, i + 1, Node::getEmptyHash()); |
| 90 | tree.addLeaf(node); |
| 91 | } |
| 92 | auto hash2 = tree.getRoot()->getHash(); |
| 93 | BOOST_CHECK_EQUAL_COLLECTIONS(hash1->begin(), hash1->end(), hash2->begin(), hash2->end()); |
| 94 | } |
| 95 | |
| 96 | for (size_t n = 513; n <= 1024; n++) { |
| 97 | auto hash1 = TreeGenerator::getSubTreeBinary(Node::Index(0, 10), n)->getRoot()->getHash(); |
| 98 | auto hash2 = TreeGenerator::getHash(Node::Index(0, 10), n); |
| 99 | BOOST_CHECK_EQUAL_COLLECTIONS(hash1->begin(), hash1->end(), hash2->begin(), hash2->end()); |
| 100 | } |
| 101 | |
| 102 | for (size_t n = 1025; n <= 2048; n++) { |
| 103 | auto hash1 = TreeGenerator::getSubTreeBinary(Node::Index(1024, 10), n)->getRoot()->getHash(); |
| 104 | auto hash2 = TreeGenerator::getHash(Node::Index(1024, 10), n); |
| 105 | BOOST_CHECK_EQUAL_COLLECTIONS(hash1->begin(), hash1->end(), hash2->begin(), hash2->end()); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | BOOST_AUTO_TEST_SUITE_END() |
| 110 | |
| 111 | } // namespace tests |
| 112 | } // namespace nsl |