back ported bitfield test from libtorrent_aio

This commit is contained in:
Arvid Norberg 2013-12-24 08:34:30 +00:00
parent 444223c2e3
commit 9fdae8f525
4 changed files with 170 additions and 40 deletions

View File

@ -99,6 +99,7 @@ feature launcher : none valgrind : composite ;
feature.compose <launcher>valgrind : <testing.launcher>"valgrind --tool=memcheck -v --num-callers=20 --read-var-info=yes --track-origins=yes --error-exitcode=222" <valgrind>on ;
test-suite libtorrent :
[ run test_bitfield.cpp ]
[ run test_torrent_info.cpp ]
[ run test_time.cpp ]
[ run test_file_storage.cpp ]

View File

@ -1,4 +1,5 @@
test_programs = \
test_bitfield \
test_file \
test_file_storage \
test_privacy \
@ -119,6 +120,7 @@ libtest_la_SOURCES = main.cpp \
../ed25519/src/sign.c \
../ed25519/src/verify.c
test_bitfield_SOURCES = test_bitfield.cpp
test_file_SOURCES = test_file.cpp
test_file_storage_SOURCES = test_file_storage.cpp
test_privacy_SOURCES = test_privacy.cpp

167
test/test_bitfield.cpp Normal file
View File

@ -0,0 +1,167 @@
/*
Copyright (c) 2008-2013, Arvid Norberg
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the distribution.
* Neither the name of the author nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#include "test.hpp"
#include "libtorrent/bitfield.hpp"
using namespace libtorrent;
void print_bitfield(bitfield const& b)
{
std::string out;
for (int i = 0; i < b.size(); ++i)
{
out += b.get_bit(i) ? "1" : "0";
}
printf("%s\n", out.c_str());
}
void test_iterators(bitfield& test1)
{
test1.set_all();
int num = 0;
printf("expecting %d ones\n", int(test1.size()));
for (bitfield::const_iterator i = test1.begin(); i != test1.end(); ++i)
{
printf("%d", *i);
TEST_EQUAL(*i, true);
num += *i;
}
printf("\n");
TEST_EQUAL(num, test1.size());
TEST_EQUAL(num, test1.count());
}
int test_main()
{
bitfield test1(10, false);
TEST_EQUAL(test1.size(), 10);
TEST_EQUAL(test1.empty(), false);
TEST_EQUAL(test1.count(), 0);
test1.set_bit(9);
TEST_EQUAL(test1.count(), 1);
test1.clear_bit(9);
TEST_EQUAL(test1.count(), 0);
test1.set_bit(2);
TEST_EQUAL(test1.count(), 1);
test1.set_bit(1);
test1.set_bit(9);
TEST_EQUAL(test1.count(), 3);
TEST_CHECK(test1.all_set() == false);
test1.clear_bit(2);
TEST_EQUAL(test1.count(), 2);
int distance = std::distance(test1.begin(), test1.end());
fprintf(stderr, "%d\n", distance);
TEST_CHECK(distance == 10);
print_bitfield(test1);
test1.set_all();
TEST_EQUAL(test1.count(), 10);
test1.clear_all();
TEST_EQUAL(test1.count(), 0);
test1.resize(2);
test1.set_bit(0);
test1.resize(16, true);
TEST_EQUAL(test1.count(), 15);
test1.resize(20, true);
TEST_EQUAL(test1.count(), 19);
TEST_EQUAL(test1.get_bit(0), true);
TEST_EQUAL(test1.get_bit(1), false);
bitfield test2 = test1;
print_bitfield(test2);
TEST_EQUAL(test2.count(), 19);
TEST_EQUAL(test2.get_bit(0), true);
TEST_EQUAL(test2.get_bit(1), false);
TEST_EQUAL(test2.get_bit(2), true);
test1.set_bit(1);
test1.resize(1);
TEST_EQUAL(test1.count(), 1);
test1.resize(100, true);
TEST_CHECK(test1.all_set() == true);
TEST_CHECK(test1.count() == 100);
test1.resize(200, false);
TEST_CHECK(test1.all_set() == false);
TEST_CHECK(test1.count() == 100);
test1.resize(50, false);
TEST_CHECK(test1.all_set() == true);
TEST_CHECK(test1.count() == 50);
test1.resize(101, true);
TEST_CHECK(test1.all_set() == true);
TEST_CHECK(test1.count() == 101);
boost::uint8_t b1[] = { 0x08, 0x10 };
test1.assign((char*)b1, 14);
print_bitfield(test1);
TEST_EQUAL(test1.count(), 2);
TEST_EQUAL(test1.get_bit(3), false);
TEST_EQUAL(test1.get_bit(4), true);
TEST_EQUAL(test1.get_bit(5), false);
TEST_EQUAL(test1.get_bit(10), false);
TEST_EQUAL(test1.get_bit(11), true);
TEST_EQUAL(test1.get_bit(12), false);
test1 = bitfield();
TEST_EQUAL(test1.size(), 0);
TEST_EQUAL(test1.empty(), true);
TEST_EQUAL(bitfield().empty(), true);
test1 = test2;
TEST_EQUAL(test1.size(), 20);
TEST_EQUAL(test1.count(), 19);
TEST_EQUAL(test1.get_bit(0), true);
TEST_EQUAL(test1.get_bit(1), false);
TEST_EQUAL(test1.get_bit(2), true);
boost::uint8_t b2[] = { 0x08, 0x10, 0xff, 0xff, 0xff, 0xff, 0xf, 0xc, 0x7f };
test1.assign((char*)b2, 72);
print_bitfield(test1);
TEST_EQUAL(test1.count(), 47);
boost::uint8_t b3[] = { 0x08, 0x10, 0xff, 0xff, 0xff, 0xff, 0xf, 0xc };
test1.assign((char*)b3, 64);
print_bitfield(test1);
TEST_EQUAL(test1.count(), 40);
for (int i = 0; i < 100; ++i)
{
test1.resize(i, false);
test_iterators(test1);
}
return 0;
}

View File

@ -35,7 +35,6 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/http_tracker_connection.hpp"
#include "libtorrent/buffer.hpp"
#include "libtorrent/entry.hpp"
#include "libtorrent/bitfield.hpp"
#include "libtorrent/torrent_info.hpp"
#include "libtorrent/escape_string.hpp"
#include "libtorrent/broadcast_socket.hpp"
@ -358,45 +357,6 @@ int test_main()
TEST_CHECK(common_bits(&h1[0], &h2[0], 20) == 16 * 4 + 3);
// test bitfield
bitfield test1(10, false);
TEST_CHECK(test1.count() == 0);
test1.set_bit(9);
TEST_CHECK(test1.count() == 1);
test1.clear_bit(9);
TEST_CHECK(test1.count() == 0);
test1.set_bit(2);
TEST_CHECK(test1.count() == 1);
test1.set_bit(1);
test1.set_bit(9);
TEST_CHECK(test1.count() == 3);
TEST_CHECK(test1.all_set() == false);
test1.clear_bit(2);
TEST_CHECK(test1.count() == 2);
int distance = std::distance(test1.begin(), test1.end());
std::cerr << distance << std::endl;
TEST_CHECK(distance == 10);
test1.set_all();
TEST_CHECK(test1.count() == 10);
test1.clear_all();
TEST_CHECK(test1.count() == 0);
test1.resize(2);
test1.set_bit(0);
test1.resize(16, true);
TEST_CHECK(test1.count() == 15);
test1.resize(20, true);
TEST_CHECK(test1.count() == 19);
test1.set_bit(1);
test1.resize(1);
TEST_CHECK(test1.count() == 1);
test1.resize(100, true);
TEST_CHECK(test1.all_set() == true);
// test address_to_bytes
TEST_EQUAL(address_to_bytes(address_v4::from_string("10.11.12.13")), "\x0a\x0b\x0c\x0d");
TEST_EQUAL(address_to_bytes(address_v4::from_string("16.5.127.1")), "\x10\x05\x7f\x01");