add unit test for stack allocator

This commit is contained in:
arvidn 2016-11-19 11:22:02 -05:00 committed by Arvid Norberg
parent b25ea5329f
commit f83e16f0cc
3 changed files with 96 additions and 0 deletions

View File

@ -120,6 +120,7 @@ test-suite libtorrent :
test_stat_cache.cpp
test_enum_net.cpp
test_linked_list.cpp
test_stack_allocator.cpp
test_file_progress.cpp ]
[ run test_gzip.cpp ]

View File

@ -21,6 +21,7 @@ test_programs = \
test_receive_buffer \
test_resume \
test_ssl \
test_stack_allocator \
test_storage \
test_time_critical \
test_torrent \
@ -209,6 +210,7 @@ test_receive_buffer_SOURCES = test_receive_buffer.cpp
test_storage_SOURCES = test_storage.cpp
test_time_critical_SOURCES = test_time_critical.cpp
test_resume_SOURCES = test_resume.cpp
test_stack_allocator_SOURCES = test_stack_allocator.cpp
test_ssl_SOURCES = test_ssl.cpp
test_torrent_SOURCES = test_torrent.cpp
test_tracker_SOURCES = test_tracker.cpp

View File

@ -0,0 +1,93 @@
/*
Copyright (c) 2016, 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/stack_allocator.hpp"
using libtorrent::aux::stack_allocator;
TORRENT_TEST(copy_string)
{
stack_allocator a;
int const idx1 = a.copy_string("testing");
// attempt to trigger a reallocation
a.allocate(100000);
int const idx2 = a.copy_string(std::string("foobar"));
TEST_CHECK(strcmp(a.ptr(idx1), "testing") == 0);
TEST_CHECK(strcmp(a.ptr(idx2), "foobar") == 0);
}
TORRENT_TEST(copy_buffer)
{
stack_allocator a;
int const idx1 = a.copy_buffer("testing", 8);
// attempt to trigger a reallocation
a.allocate(100000);
TEST_CHECK(strcmp(a.ptr(idx1), "testing") == 0);
}
TORRENT_TEST(allocate)
{
stack_allocator a;
int const idx1 = a.allocate(100);
char* ptr = a.ptr(idx1);
for (int i = 0; i < 100; ++i)
ptr[i] = char(i % 256);
// attempt to trigger a reallocation
a.allocate(100000);
ptr = a.ptr(idx1);
for (int i = 0; i < 100; ++i)
TEST_CHECK(ptr[i] == char(i % 256));
}
TORRENT_TEST(swap)
{
stack_allocator a1;
stack_allocator a2;
int const idx1 = a1.copy_string("testing");
int const idx2 = a2.copy_string("foobar");
a1.swap(a2);
TEST_CHECK(strcmp(a1.ptr(idx2), "foobar") == 0);
TEST_CHECK(strcmp(a2.ptr(idx1), "testing") == 0);
}