add unit test for some filesystem functions, like listing directories

This commit is contained in:
Arvid Norberg 2012-06-29 23:10:28 +00:00
parent 8fffb4bb35
commit b14479132c
3 changed files with 97 additions and 0 deletions

View File

@ -19,6 +19,7 @@ project
; ;
test-suite libtorrent : test-suite libtorrent :
[ run test_file.cpp ]
[ run test_threads.cpp ] [ run test_threads.cpp ]
[ run test_rss.cpp ] [ run test_rss.cpp ]
[ run test_bandwidth_limiter.cpp ] [ run test_bandwidth_limiter.cpp ]

View File

@ -1,4 +1,5 @@
test_programs = \ test_programs = \
test_file \
test_auto_unchoke \ test_auto_unchoke \
test_bandwidth_limiter \ test_bandwidth_limiter \
test_bdecode_performance \ test_bdecode_performance \
@ -41,6 +42,7 @@ noinst_HEADERS = test.hpp setup_transfer.hpp
libtest_la_SOURCES = main.cpp setup_transfer.cpp libtest_la_SOURCES = main.cpp setup_transfer.cpp
test_file_SOURCES = test_file.cpp
test_auto_unchoke_SOURCES = test_auto_unchoke.cpp test_auto_unchoke_SOURCES = test_auto_unchoke.cpp
test_bandwidth_limiter_SOURCES = test_bandwidth_limiter.cpp test_bandwidth_limiter_SOURCES = test_bandwidth_limiter.cpp
test_bdecode_performance_SOURCES = test_bdecode_performance.cpp test_bdecode_performance_SOURCES = test_bdecode_performance.cpp

94
test/test_file.cpp Normal file
View File

@ -0,0 +1,94 @@
/*
Copyright (c) 2012, 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 "libtorrent/file.hpp"
#include "test.hpp"
#include <vector>
#include <set>
using namespace libtorrent;
int touch_file(std::string const& filename, int size)
{
using namespace libtorrent;
std::vector<char> v;
v.resize(size);
for (int i = 0; i < size; ++i)
v[i] = i & 255;
file f;
error_code ec;
if (!f.open(filename, file::write_only, ec)) return -1;
if (ec) return -1;
file::iovec_t b = {&v[0], v.size()};
size_type written = f.writev(0, &b, 1, ec);
if (written != int(v.size())) return -3;
if (ec) return -3;
return 0;
}
int test_main()
{
error_code ec;
create_directory("file_test_dir", ec);
if (ec) fprintf(stderr, "create_directory: %s\n", ec.message().c_str());
TEST_CHECK(!ec);
std::string cwd = current_working_directory();
touch_file(combine_path("file_test_dir", "abc"), 10);
touch_file(combine_path("file_test_dir", "def"), 100);
touch_file(combine_path("file_test_dir", "ghi"), 1000);
std::set<std::string> files;
for (directory i("file_test_dir", ec); !i.done(); i.next(ec))
{
std::string f = i.file();
TEST_CHECK(files.count(f) == 0);
files.insert(f);
fprintf(stderr, " %s\n", f.c_str());
}
TEST_CHECK(files.count("abc") == 1);
TEST_CHECK(files.count("def") == 1);
TEST_CHECK(files.count("ghi") == 1);
TEST_CHECK(files.count("..") == 1);
TEST_CHECK(files.count(".") == 1);
remove_all("file_test_dir", ec);
if (ec) fprintf(stderr, "create_directory: %s\n", ec.message().c_str());
return 0;
}