00001
00002
00003
00004
00005 #include "libtorrent/aux_/allocate_resources_impl.hpp"
00006 #include <boost/utility.hpp>
00007
00008 #include "test.hpp"
00009
00010 using namespace libtorrent;
00011
00012 struct resource_entry
00013 {
00014 resource_entry(resource_request r_): r(r_) {}
00015 resource_request r;
00016 };
00017
00018 void fill_client_vector(std::vector<resource_entry>& v)
00019 {
00020 v.push_back(resource_request(5000, 20, 20000, 10000));
00021 v.push_back(resource_request(9000, 20, 20000, 10000));
00022 v.push_back(resource_request(8000, 20, 20000, 10000));
00023 v.push_back(resource_request(7000, 20, 20000, 10000));
00024 v.push_back(resource_request(5000, 20, 20000, 10000));
00025 v.push_back(resource_request(8000, 20, 20000, 10000));
00026 }
00027
00028 void check_client_vec(std::vector<resource_entry> const& v, int resources)
00029 {
00030 int sum = 0;
00031 int min_sum = 0;
00032 for (std::vector<resource_entry>::const_iterator i = v.begin()
00033 , end(v.end()); i != end; ++i)
00034 {
00035 TEST_CHECK(i->r.given >= i->r.min);
00036 TEST_CHECK(i->r.given <= i->r.max);
00037 sum += i->r.given;
00038 min_sum += i->r.min;
00039 }
00040 TEST_CHECK(sum <= (std::max)(resources, min_sum));
00041 }
00042
00043 int test_main()
00044 {
00045 using namespace libtorrent;
00046
00047 std::vector<resource_entry> clients;
00048 fill_client_vector(clients);
00049
00050 using aux::allocate_resources_impl;
00051
00052 allocate_resources_impl(20, clients.begin(), clients.end(), &resource_entry::r);
00053 check_client_vec(clients, 20);
00054
00055 fill_client_vector(clients);
00056 allocate_resources_impl(20000, clients.begin(), clients.end(), &resource_entry::r);
00057 check_client_vec(clients, 20000);
00058
00059 return 0;
00060 }
00061