diff --git a/bench/bench_set.cpp b/bench/bench_set.cpp index 8d87880..5742f3b 100644 --- a/bench/bench_set.cpp +++ b/bench/bench_set.cpp @@ -11,160 +11,230 @@ #include "boost/container/set.hpp" #include "boost/container/flat_set.hpp" #include "boost/container/allocator.hpp" +#include +#include #include -#include #include #include #include #include +#include +#include using boost::timer::cpu_timer; using boost::timer::cpu_times; using boost::timer::nanosecond_type; +using namespace boost::container; #ifdef NDEBUG -static const std::size_t N = 500; +static const std::size_t N = 1000; #else -static const std::size_t N = 50; +static const std::size_t N = 100; #endif void compare_times(cpu_times time_numerator, cpu_times time_denominator){ - std::cout << "----------------------------------------------" << '\n'; - std::cout << " wall = " << ((double)time_numerator.wall/(double)time_denominator.wall) << std::endl; + std::cout << ((double)time_numerator.wall/(double)time_denominator.wall) << std::endl; std::cout << "----------------------------------------------" << '\n' << std::endl; } -std::vector sorted_unique_range; -std::vector sorted_range; -std::vector random_unique_range; -std::vector random_range; +vector sorted_unique_range_int; +vector sorted_range_int; +vector random_unique_range_int; +vector random_range_int; -void fill_ranges() +void fill_range_ints() { - sorted_unique_range.resize(N); - sorted_range.resize(N); - random_unique_range.resize(N); - random_range.resize(N); - std::srand (0); - //random_range - std::generate(random_unique_range.begin(), random_unique_range.end(), std::rand); - random_unique_range.erase(std::unique(random_unique_range.begin(), random_unique_range.end()), random_unique_range.end()); - //random_range - random_range = random_unique_range; - random_range.insert(random_range.end(), random_unique_range.begin(), random_unique_range.end()); - //sorted_unique_range - for(std::size_t i = 0, max = sorted_unique_range.size(); i != max; ++i){ - sorted_unique_range[i] = static_cast(i); + //sorted_unique_range_int + sorted_unique_range_int.resize(N); + for(std::size_t i = 0, max = sorted_unique_range_int.size(); i != max; ++i){ + sorted_unique_range_int[i] = static_cast(i); } - //sorted_range - sorted_range = sorted_unique_range; - sorted_range.insert(sorted_range.end(), sorted_unique_range.begin(), sorted_unique_range.end()); - std::sort(sorted_range.begin(), sorted_range.end()); + //sorted_range_int + sorted_range_int = sorted_unique_range_int; + sorted_range_int.insert(sorted_range_int.end(), sorted_unique_range_int.begin(), sorted_unique_range_int.end()); + std::sort(sorted_range_int.begin(), sorted_range_int.end()); + + //random_range_int + std::srand(0); + random_range_int.assign(sorted_range_int.begin(), sorted_range_int.end()); + std::random_shuffle(random_range_int.begin(), random_range_int.end()); + //random_unique_range_int + std::srand(0); + random_unique_range_int.assign(sorted_unique_range_int.begin(), sorted_unique_range_int.end()); + std::random_shuffle(random_unique_range_int.begin(), random_unique_range_int.end()); } -template -cpu_times construct_time() +vector sorted_unique_range_string; +vector sorted_range_string; +vector random_unique_range_string; +vector random_range_string; + +void fill_range_strings() { - cpu_timer sur_timer, sr_timer, rur_timer, rr_timer, copy_timer, assign_timer, destroy_timer; - //sur_timer.stop();sr_timer.stop();rur_timer.stop();rr_timer.stop();destroy_timer.stop(); + //sorted_unique_range_int + sorted_unique_range_string.resize(N); + for(std::size_t i = 0, max = sorted_unique_range_string.size(); i != max; ++i){ + std::stringstream sstr; + sstr << std::setfill('0') << std::setw(10) << i; + sorted_unique_range_string[i] = "really_long_long_prefix_to_ssb_and_increase_comparison_costs_"; + const std::string &s = sstr.str(); + sorted_unique_range_string[i].append(s.begin(), s.end()); + } + //sorted_range_string + sorted_range_string = sorted_unique_range_string; + sorted_range_string.insert(sorted_range_string.end(), sorted_unique_range_string.begin(), sorted_unique_range_string.end()); + std::sort(sorted_range_string.begin(), sorted_range_string.end()); + + //random_range_string + std::srand(0); + random_range_string.assign(sorted_range_string.begin(), sorted_range_string.end()); + std::random_shuffle(random_range_string.begin(), random_range_string.end()); + //random_unique_range_string + std::srand(0); + random_unique_range_string.assign(sorted_unique_range_string.begin(), sorted_unique_range_string.end()); + std::random_shuffle(random_unique_range_string.begin(), random_unique_range_string.end()); +} + +template +struct range_provider; + +template<> +struct range_provider +{ + typedef vector type; + + static type &sorted_unique() + { return sorted_unique_range_int; } + + static type &sorted() + { return sorted_range_int; } + + static type &random_unique() + { return random_unique_range_int; } + + static type &random() + { return random_range_int; } +}; + +template<> +struct range_provider +{ + typedef vector type; + + static type &sorted_unique() + { return sorted_unique_range_string; } + + static type &sorted() + { return sorted_range_string; } + + static type &random_unique() + { return random_unique_range_string; } + + static type &random() + { return random_range_string; } +}; + +template +cpu_times copy_destroy_time(vector &unique_range) +{ + cpu_timer copy_timer, assign_timer, destroy_timer; cpu_timer total_time; - total_time.resume(); for(std::size_t i = 0; i != N; ++i){ { - sur_timer.resume(); - T t(sorted_unique_range.begin(), sorted_unique_range.end()); - sur_timer.stop(); - } - { - sr_timer.resume(); - T t(sorted_range.begin(), sorted_range.end()); - sr_timer.stop(); + C c(unique_range.begin(), unique_range.end()); + total_time.resume(); copy_timer.resume(); - T taux(t); + C caux(c); copy_timer.stop(); assign_timer.resume(); - t = taux;; + c = caux; assign_timer.stop(); - } - { - rur_timer.resume(); - T t(random_unique_range.begin(), random_unique_range.end()); - rur_timer.stop(); - } - { - rr_timer.resume(); - T t(random_range.begin(), random_range.end()); - rr_timer.stop(); destroy_timer.resume(); } destroy_timer.stop(); + total_time.stop(); } total_time.stop(); - std::cout << " Construct sorted_unique_range " << boost::timer::format(sur_timer.elapsed(), boost::timer::default_places, "%ws wall\n"); - std::cout << " Construct sorted_range " << boost::timer::format(sr_timer.elapsed(), boost::timer::default_places, "%ws wall\n"); - std::cout << " Copy sorted range " << boost::timer::format(copy_timer.elapsed(), boost::timer::default_places, "%ws wall\n"); - std::cout << " Assign sorted range " << boost::timer::format(assign_timer.elapsed(), boost::timer::default_places, "%ws wall\n"); - std::cout << " Construct random_unique_range " << boost::timer::format(rur_timer.elapsed(), boost::timer::default_places, "%ws wall\n"); - std::cout << " Construct random_range " << boost::timer::format(rr_timer.elapsed(), boost::timer::default_places, "%ws wall\n"); - std::cout << " Destroy " << boost::timer::format(destroy_timer.elapsed(), boost::timer::default_places, "%ws wall\n"); - std::cout << " Total time = " << boost::timer::format(total_time.elapsed(), boost::timer::default_places, "%ws wall\n") << std::endl; + std::cout << " Copy sorted range " << boost::timer::format(copy_timer.elapsed(), boost::timer::default_places, "%ws\n"); + std::cout << " Assign sorted range " << boost::timer::format(assign_timer.elapsed(), boost::timer::default_places, "%ws\n"); + std::cout << " Destroy " << boost::timer::format(destroy_timer.elapsed(), boost::timer::default_places, "%ws\n"); + std::cout << " Total time = " << boost::timer::format(total_time.elapsed(), boost::timer::default_places, "%ws\n") << std::endl; return total_time.elapsed(); } -template -cpu_times insert_time() +template +cpu_times construct_time(vector &unique_range, vector &range, const char *RangeType) { - cpu_timer sur_timer,sr_timer,rur_timer,rr_timer,destroy_timer; - sur_timer.stop();sr_timer.stop();rur_timer.stop();rr_timer.stop(); + cpu_timer sur_timer, sr_timer; + + cpu_timer total_time; + + for(std::size_t i = 0; i != N; ++i){ + { + sur_timer.resume(); + total_time.resume(); + C c(unique_range.begin(), unique_range.end()); + sur_timer.stop(); + total_time.stop(); + } + { + total_time.resume(); + sr_timer.resume(); + C c(range.begin(), range.end()); + sr_timer.stop(); + total_time.stop(); + } + } + + std::cout << " Construct " << RangeType << " unique_range " << boost::timer::format(sur_timer.elapsed(), boost::timer::default_places, "%ws\n"); + std::cout << " Construct " << RangeType << " range " << boost::timer::format(sr_timer.elapsed(), boost::timer::default_places, "%ws\n"); + std::cout << " Total time = " << boost::timer::format(total_time.elapsed(), boost::timer::default_places, "%ws\n") << std::endl; + return total_time.elapsed(); +} + +template +cpu_times insert_time(vector &unique_range, vector &range, const char *RangeType) +{ + cpu_timer ur_timer,r_timer; + ur_timer.stop();r_timer.stop(); cpu_timer total_time; total_time.resume(); for(std::size_t i = 0; i != N; ++i){ { - sur_timer.resume(); - T t; - t.insert(sorted_unique_range.begin(), sorted_unique_range.end()); - sur_timer.stop(); + total_time.resume(); + ur_timer.resume(); + C c; + c.insert(unique_range.begin(), unique_range.end()); + ur_timer.stop(); + total_time.stop(); } { - sr_timer.resume(); - T t; - t.insert(sorted_range.begin(), sorted_range.end()); - sr_timer.stop(); - } - { - rur_timer.resume(); - T t; - t.insert(random_unique_range.begin(), random_unique_range.end()); - rur_timer.stop(); - } - { - rr_timer.resume(); - T t; - t.insert(random_range.begin(), random_range.end()); - rr_timer.stop(); + total_time.resume(); + r_timer.resume(); + C c; + c.insert(range.begin(), range.end()); + r_timer.stop(); + total_time.stop(); } } - total_time.stop(); - std::cout << " Insert sorted_unique_range " << boost::timer::format(sur_timer.elapsed(), boost::timer::default_places, "%ws wall\n"); - std::cout << " Insert sorted_range " << boost::timer::format(sr_timer.elapsed(), boost::timer::default_places, "%ws wall\n"); - std::cout << " Insert random_unique_range " << boost::timer::format(rur_timer.elapsed(), boost::timer::default_places, "%ws wall\n"); - std::cout << " Insert random_range " << boost::timer::format(rr_timer.elapsed(), boost::timer::default_places, "%ws wall\n"); - std::cout << " Total time = " << boost::timer::format(total_time.elapsed(), boost::timer::default_places, "%ws wall\n") << std::endl; + std::cout << " Insert " << RangeType << " unique_range " << boost::timer::format(ur_timer.elapsed(), boost::timer::default_places, "%ws\n"); + std::cout << " Insert " << RangeType << " range " << boost::timer::format(r_timer.elapsed(), boost::timer::default_places, "%ws\n"); + std::cout << " Total time = " << boost::timer::format(total_time.elapsed(), boost::timer::default_places, "%ws\n") << std::endl; return total_time.elapsed(); } -template -cpu_times search_time() +template +cpu_times search_time(vector &unique_range, const char *RangeType) { cpu_timer find_timer, lower_timer, upper_timer, equal_range_timer, count_timer; - T t(sorted_unique_range.begin(), sorted_unique_range.end()); + C c(unique_range.begin(), unique_range.end()); cpu_timer total_time; total_time.resume(); @@ -174,95 +244,84 @@ cpu_times search_time() { std::size_t found = 0; find_timer.resume(); - for(std::size_t i = 0, max = sorted_unique_range.size(); i != max; ++i){ - found += static_cast(t.end() != t.find(sorted_unique_range[i])); - } - for(std::size_t i = 0, max = sorted_unique_range.size(); i != max; ++i){ - found += static_cast(t.end() != t.find(sorted_unique_range[i])); + for(std::size_t rep = 0; rep != 2; ++rep) + for(std::size_t i = 0, max = unique_range.size(); i != max; ++i){ + found += static_cast(c.end() != c.find(unique_range[i])); } find_timer.stop(); - if(found/2 != t.size()){ - std::cout << "ERROR! all elements not found" << std::endl; + if(found/2 != c.size()){ + std::cout << "ERROR! find all elements not found" << std::endl; } } //Lower { std::size_t found = 0; lower_timer.resume(); - for(std::size_t i = 0, max = sorted_unique_range.size(); i != max; ++i){ - found += static_cast(t.end() != t.lower_bound(sorted_unique_range[i])); - } - for(std::size_t i = 0, max = sorted_unique_range.size(); i != max; ++i){ - found += static_cast(t.end() != t.lower_bound(sorted_unique_range[i])); + for(std::size_t rep = 0; rep != 2; ++rep) + for(std::size_t i = 0, max = unique_range.size(); i != max; ++i){ + found += static_cast(c.end() != c.lower_bound(unique_range[i])); } lower_timer.stop(); - if(found/2 != t.size()){ - std::cout << "ERROR! all elements not found" << std::endl; + if(found/2 != c.size()){ + std::cout << "ERROR! lower_bound all elements not found" << std::endl; } } //Upper { std::size_t found = 0; upper_timer.resume(); - for(std::size_t i = 0, max = sorted_unique_range.size(); i != max; ++i){ - found += static_cast(t.end() != t.upper_bound(sorted_unique_range[i])); - } - for(std::size_t i = 0, max = sorted_unique_range.size(); i != max; ++i){ - found += static_cast(t.end() != t.upper_bound(sorted_unique_range[i])); + for(std::size_t rep = 0; rep != 2; ++rep) + for(std::size_t i = 0, max = unique_range.size(); i != max; ++i){ + found += static_cast(c.end() != c.upper_bound(unique_range[i])); } upper_timer.stop(); - if(found/2 != (t.size()-1)){ - std::cout << "ERROR! all elements not found" << std::endl; + if(found/2 != (c.size()-1)){ + std::cout << "ERROR! upper_bound all elements not found" << std::endl; } } //Equal { std::size_t found = 0; - std::pair ret; + std::pair ret; equal_range_timer.resume(); - for(std::size_t i = 0, max = sorted_unique_range.size(); i != max; ++i){ - ret = t.equal_range(sorted_unique_range[i]); - found += static_cast(ret.first != ret.second); - } - for(std::size_t i = 0, max = sorted_unique_range.size(); i != max; ++i){ - ret = t.equal_range(sorted_unique_range[i]); + for(std::size_t rep = 0; rep != 2; ++rep) + for(std::size_t i = 0, max = unique_range.size(); i != max; ++i){ + ret = c.equal_range(unique_range[i]); found += static_cast(ret.first != ret.second); } equal_range_timer.stop(); - if(found/2 != t.size()){ - std::cout << "ERROR! all elements not found" << std::endl; + if(found/2 != c.size()){ + std::cout << "ERROR! equal_range all elements not found" << std::endl; } } //Count { std::size_t found = 0; - std::pair ret; + std::pair ret; count_timer.resume(); - for(std::size_t i = 0, max = sorted_unique_range.size(); i != max; ++i){ - found += static_cast(t.count(sorted_unique_range[i])); - } - for(std::size_t i = 0, max = sorted_unique_range.size(); i != max; ++i){ - found += static_cast(t.count(sorted_unique_range[i])); + for(std::size_t rep = 0; rep != 2; ++rep) + for(std::size_t i = 0, max = unique_range.size(); i != max; ++i){ + found += static_cast(c.count(unique_range[i])); } count_timer.stop(); - if(found/2 != t.size()){ - std::cout << "ERROR! all elements not found" << std::endl; + if(found/2 != c.size()){ + std::cout << "ERROR! count all elements not found" << std::endl; } } } total_time.stop(); - std::cout << " Find " << boost::timer::format(find_timer.elapsed(), boost::timer::default_places, "%ws wall\n"); - std::cout << " Lower Bound " << boost::timer::format(lower_timer.elapsed(), boost::timer::default_places, "%ws wall\n"); - std::cout << " Upper Bound " << boost::timer::format(upper_timer.elapsed(), boost::timer::default_places, "%ws wall\n"); - std::cout << " Equal Range " << boost::timer::format(equal_range_timer.elapsed(), boost::timer::default_places, "%ws wall\n"); - std::cout << " Count " << boost::timer::format(count_timer.elapsed(), boost::timer::default_places, "%ws wall\n"); - std::cout << " Total time = " << boost::timer::format(total_time.elapsed(), boost::timer::default_places, "%ws wall\n") << std::endl; + std::cout << " Find " << RangeType << " " << boost::timer::format(find_timer.elapsed(), boost::timer::default_places, "%ws\n"); + std::cout << " Lower Bound " << RangeType << " " << boost::timer::format(lower_timer.elapsed(), boost::timer::default_places, "%ws\n"); + std::cout << " Upper Bound " << RangeType << " " << boost::timer::format(upper_timer.elapsed(), boost::timer::default_places, "%ws\n"); + std::cout << " Equal Range " << RangeType << " " << boost::timer::format(equal_range_timer.elapsed(), boost::timer::default_places, "%ws\n"); + std::cout << " Count " << RangeType << " " << boost::timer::format(count_timer.elapsed(), boost::timer::default_places, "%ws\n"); + std::cout << " Total time = " << boost::timer::format(total_time.elapsed(), boost::timer::default_places, "%ws\n") << std::endl; return total_time.elapsed(); } -template -void extensions_time() +template +void extensions_time(vector &sorted_unique_range) { cpu_timer sur_timer,sur_opt_timer; sur_timer.stop();sur_opt_timer.stop(); @@ -270,69 +329,107 @@ void extensions_time() for(std::size_t i = 0; i != N; ++i){ { sur_timer.resume(); - T t(sorted_unique_range.begin(), sorted_unique_range.end()); + C c(sorted_unique_range.begin(), sorted_unique_range.end()); sur_timer.stop(); } { sur_opt_timer.resume(); - T t(boost::container::ordered_unique_range, sorted_unique_range.begin(), sorted_unique_range.end()); + C c(boost::container::ordered_unique_range, sorted_unique_range.begin(), sorted_unique_range.end()); sur_opt_timer.stop(); } } - std::cout << " Construct sorted_unique_range " << boost::timer::format(sur_timer.elapsed(), boost::timer::default_places, "%ws wall\n"); - std::cout << " Construct sorted_unique_range (extension) " << boost::timer::format(sur_opt_timer.elapsed(), boost::timer::default_places, "%ws wall\n"); - std::cout << "Total time (Extension/Standard):\n"; + std::cout << " Construct sorted_unique_range " << boost::timer::format(sur_timer.elapsed(), boost::timer::default_places, "%ws\n"); + std::cout << " Construct sorted_unique_range (extension) " << boost::timer::format(sur_opt_timer.elapsed(), boost::timer::default_places, "%ws\n"); + std::cout << "Extension/Standard: "; compare_times(sur_opt_timer.elapsed(), sur_timer.elapsed()); } template void launch_tests(const char *BoostContName, const char *StdContName) { + typedef range_provider get_range_t; try { - std::cout << "**********************************************" << '\n'; std::cout << "**********************************************" << '\n'; std::cout << "**********************************************" << '\n'; std::cout << '\n'; std::cout << BoostContName << " .VS " << StdContName << '\n'; std::cout << '\n'; std::cout << "**********************************************" << '\n'; - std::cout << "**********************************************" << '\n'; std::cout << "**********************************************" << '\n' << std::endl; - fill_ranges(); { - std::cout << "Construct benchmark:" << BoostContName << std::endl; - cpu_times boost_set_time = construct_time< BoostClass >(); + std::cout << "Copy/Assign/Destroy benchmark:" << BoostContName << std::endl; + cpu_times boost_set_time = copy_destroy_time< BoostClass >(get_range_t::sorted_unique()); - std::cout << "Construct benchmark:" << StdContName << std::endl; - cpu_times std_set_time = construct_time< StdClass >(); + std::cout << "Copy/Assign/Destroy benchmark:" << StdContName << std::endl; + cpu_times std_set_time = copy_destroy_time< StdClass >(get_range_t::sorted_unique()); - std::cout << "Total time (" << BoostContName << "/" << StdContName << "):\n"; + std::cout << BoostContName << "/" << StdContName << ": "; compare_times(boost_set_time, std_set_time); } { - std::cout << "Insert benchmark:" << BoostContName << std::endl; - cpu_times boost_set_time = insert_time< BoostClass >(); + std::cout << "Ordered construct benchmark:" << BoostContName << std::endl; + cpu_times boost_set_time = construct_time< BoostClass >(get_range_t::sorted_unique(), get_range_t::sorted(), "(ord)"); - std::cout << "Insert benchmark:" << StdContName << std::endl; - cpu_times std_set_time = insert_time< StdClass >(); + std::cout << "Ordered construct benchmark:" << StdContName << std::endl; + cpu_times std_set_time = construct_time< StdClass >(get_range_t::sorted_unique(), get_range_t::sorted(), "(ord)");; - std::cout << "Total time (" << BoostContName << "/" << StdContName << "):\n"; + std::cout << BoostContName << "/" << StdContName << ": "; compare_times(boost_set_time, std_set_time); } { - std::cout << "Search benchmark:" << BoostContName << std::endl; - cpu_times boost_set_time = search_time< BoostClass >(); + std::cout << "Random construct benchmark:" << BoostContName << std::endl; + cpu_times boost_set_time = construct_time< BoostClass >(get_range_t::random_unique(), get_range_t::random(), "(rnd)"); - std::cout << "Search benchmark:" << StdContName << std::endl; - cpu_times std_set_time = search_time< StdClass >(); + std::cout << "Random construct benchmark:" << StdContName << std::endl; + cpu_times std_set_time = construct_time< StdClass >(get_range_t::random_unique(), get_range_t::random(), "(rnd)");; - std::cout << "Total time (" << BoostContName << "/" << StdContName << "):\n"; + std::cout << BoostContName << "/" << StdContName << ": "; + compare_times(boost_set_time, std_set_time); + } + { + std::cout << "Ordered Insert benchmark:" << BoostContName << std::endl; + cpu_times boost_set_time = insert_time< BoostClass >(get_range_t::sorted_unique(), get_range_t::sorted(), "(ord)"); + + std::cout << "Ordered Insert benchmark:" << StdContName << std::endl; + cpu_times std_set_time = insert_time< StdClass >(get_range_t::sorted_unique(), get_range_t::sorted(), "(ord)"); + + std::cout << BoostContName << "/" << StdContName << ": "; + compare_times(boost_set_time, std_set_time); + } + { + std::cout << "Random Insert benchmark:" << BoostContName << std::endl; + cpu_times boost_set_time = insert_time< BoostClass >(get_range_t::random_unique(), get_range_t::random(), "(rnd)"); + + std::cout << "Random Insert benchmark:" << StdContName << std::endl; + cpu_times std_set_time = insert_time< StdClass >(get_range_t::random_unique(), get_range_t::random(), "(rnd)"); + + std::cout << BoostContName << "/" << StdContName << ": "; + compare_times(boost_set_time, std_set_time); + } + { + std::cout << "Ordered Search benchmark:" << BoostContName << std::endl; + cpu_times boost_set_time = search_time< BoostClass >(get_range_t::sorted_unique(), "(ord)"); + + std::cout << "Ordered Search benchmark:" << StdContName << std::endl; + cpu_times std_set_time = search_time< StdClass >(get_range_t::sorted_unique(), "(ord)"); + + std::cout << BoostContName << "/" << StdContName << ": "; + compare_times(boost_set_time, std_set_time); + } + { + std::cout << "Random Search benchmark:" << BoostContName << std::endl; + cpu_times boost_set_time = search_time< BoostClass >(get_range_t::random_unique(), "(rnd)"); + + std::cout << "Random Search benchmark:" << StdContName << std::endl; + cpu_times std_set_time = search_time< StdClass >(get_range_t::random_unique(), "(rnd)"); + + std::cout << BoostContName << "/" << StdContName << ": "; compare_times(boost_set_time, std_set_time); } { std::cout << "Extensions benchmark:" << BoostContName << std::endl; - extensions_time< BoostClass >(); + extensions_time< BoostClass >(get_range_t::sorted_unique()); } }catch(std::exception e){ @@ -343,6 +440,11 @@ void launch_tests(const char *BoostContName, const char *StdContName) int main() { using namespace boost::container; + ////////////////////////////////// + // INT + ////////////////////////////////// + + fill_range_ints(); //set vs std::set launch_tests< set , std::set > ("set", "std::set"); @@ -352,6 +454,7 @@ int main() //set(RB) vs set(SG) launch_tests< set, set, std::allocator, tree_assoc_options< tree_type >::type > > ("set(RB)", "set(SG)"); + //set(RB) vs set(SP) launch_tests< set, set, std::allocator, tree_assoc_options< tree_type >::type > > ("set(RB)", "set(SP)"); //set(sizeopt) vs set(!sizeopt) @@ -373,5 +476,42 @@ int main() //flat_multiset vs multiset launch_tests< flat_multiset , multiset > ("flat_multiset", "multiset"); + + ////////////////////////////////// + // STRING + ////////////////////////////////// + fill_range_strings(); + + //set vs std::set + launch_tests< set , std::set > + ("set", "std::set"); + //set(RB) vs set(AVL) + launch_tests< set, set, std::allocator, tree_assoc_options< tree_type >::type > > + ("set(RB)", "set(AVL)"); + //set(RB) vs set(SG) + launch_tests< set, set, std::allocator, tree_assoc_options< tree_type >::type > > + ("set(RB)", "set(SG)"); + //set(RB) vs set(SP) + launch_tests< set, set, std::allocator, tree_assoc_options< tree_type >::type > > + ("set(RB)", "set(SP)"); + //set(sizeopt) vs set(!sizeopt) + launch_tests< set, set, std::allocator, tree_assoc_options< optimize_size >::type > > + ("set(sizeopt=true)", "set(sizeopt=false)"); + //set(AVL,sizeopt) vs set(AVL,!sizeopt) + launch_tests< set, std::allocator, tree_assoc_options< tree_type >::type > + , set, std::allocator, tree_assoc_options< tree_type, optimize_size >::type > > + ("set(AVL,sizeopt=true)", "set(AVL,sizeopt=false)"); + //set vs set<..., allocator_v2> + launch_tests< set , set, allocator > > + ("set", "set" ); + //multiset vs std::set + launch_tests< multiset , std::multiset > + ("multiset", "std::multiset"); + //flat_set vs set + launch_tests< flat_set , set > + ("flat_set", "set"); + //flat_multiset vs multiset + launch_tests< flat_multiset , multiset > + ("flat_multiset", "multiset"); return 0; } diff --git a/test/check_equal_containers.hpp b/test/check_equal_containers.hpp index d2cd6b6..b5b2a2f 100644 --- a/test/check_equal_containers.hpp +++ b/test/check_equal_containers.hpp @@ -30,6 +30,23 @@ bool CheckEqual( const T1 &t1, const T2 &t2 >::type* = 0) { return t1 == t2; } + +template +bool CheckEqualIt( const T1 &i1, const T2 &i2, const C1 &c1, const C2 &c2 ) +{ + bool c1end = i1 == c1.end(); + bool c2end = i2 == c2.end(); + if( c1end != c2end ){ + return false; + } + else if(c1end){ + return true; + } + else{ + return CheckEqual(*i1, *i2); + } +} + template< class Pair1, class Pair2> bool CheckEqual( const Pair1 &pair1, const Pair2 &pair2 , typename boost::container::container_detail::enable_if_c diff --git a/test/set_test.hpp b/test/set_test.hpp index 871adce..78e771f 100644 --- a/test/set_test.hpp +++ b/test/set_test.hpp @@ -441,54 +441,54 @@ int set_test () } } { - IntType move_me(i); - boostset->insert(boostset->upper_bound(move_me), boost::move(move_me)); - stdset->insert(stdset->upper_bound(i), i); - //PrintContainers(boostset, stdset); - IntType move_me2(i); - boostmultiset->insert(boostmultiset->upper_bound(move_me2), boost::move(move_me2)); - stdmultiset->insert(stdmultiset->upper_bound(i), i); - //PrintContainers(boostmultiset, stdmultiset); - if(!CheckEqualContainers(boostset, stdset)){ - std::cout << "Error in boostset->insert(boostset->upper_bound(move_me), boost::move(move_me))" << std::endl; - return 1; - } - if(!CheckEqualContainers(boostmultiset, stdmultiset)){ - std::cout << "Error in boostmultiset->insert(boostmultiset->upper_bound(move_me2), boost::move(move_me2))" << std::endl; - return 1; - } + IntType move_me(i); + boostset->insert(boostset->upper_bound(move_me), boost::move(move_me)); + stdset->insert(stdset->upper_bound(i), i); + //PrintContainers(boostset, stdset); + IntType move_me2(i); + boostmultiset->insert(boostmultiset->upper_bound(move_me2), boost::move(move_me2)); + stdmultiset->insert(stdmultiset->upper_bound(i), i); + //PrintContainers(boostmultiset, stdmultiset); + if(!CheckEqualContainers(boostset, stdset)){ + std::cout << "Error in boostset->insert(boostset->upper_bound(move_me), boost::move(move_me))" << std::endl; + return 1; + } + if(!CheckEqualContainers(boostmultiset, stdmultiset)){ + std::cout << "Error in boostmultiset->insert(boostmultiset->upper_bound(move_me2), boost::move(move_me2))" << std::endl; + return 1; + } } { - IntType move_me(i); - IntType move_me2(i); - boostset->insert(boostset->lower_bound(move_me), boost::move(move_me2)); - stdset->insert(stdset->lower_bound(i), i); - //PrintContainers(boostset, stdset); - move_me2 = i; - boostmultiset->insert(boostmultiset->lower_bound(move_me2), boost::move(move_me2)); - stdmultiset->insert(stdmultiset->lower_bound(i), i); - //PrintContainers(boostmultiset, stdmultiset); - if(!CheckEqualContainers(boostset, stdset)){ - std::cout << "Error in boostset->insert(boostset->lower_bound(move_me), boost::move(move_me2))" << std::endl; - return 1; - } - if(!CheckEqualContainers(boostmultiset, stdmultiset)){ - std::cout << "Error in boostmultiset->insert(boostmultiset->lower_bound(move_me2), boost::move(move_me2))" << std::endl; - return 1; - } - set_test_rebalanceable(*boostset - , container_detail::bool_::value>()); - if(!CheckEqualContainers(boostset, stdset)){ - std::cout << "Error in boostset->rebalance()" << std::endl; - return 1; - } - set_test_rebalanceable(*boostmultiset - , container_detail::bool_::value>()); - if(!CheckEqualContainers(boostmultiset, stdmultiset)){ - std::cout << "Error in boostmultiset->rebalance()" << std::endl; - return 1; - } + IntType move_me(i); + IntType move_me2(i); + boostset->insert(boostset->lower_bound(move_me), boost::move(move_me2)); + stdset->insert(stdset->lower_bound(i), i); + //PrintContainers(boostset, stdset); + move_me2 = i; + boostmultiset->insert(boostmultiset->lower_bound(move_me2), boost::move(move_me2)); + stdmultiset->insert(stdmultiset->lower_bound(i), i); + //PrintContainers(boostmultiset, stdmultiset); + if(!CheckEqualContainers(boostset, stdset)){ + std::cout << "Error in boostset->insert(boostset->lower_bound(move_me), boost::move(move_me2))" << std::endl; + return 1; + } + if(!CheckEqualContainers(boostmultiset, stdmultiset)){ + std::cout << "Error in boostmultiset->insert(boostmultiset->lower_bound(move_me2), boost::move(move_me2))" << std::endl; + return 1; + } + set_test_rebalanceable(*boostset + , container_detail::bool_::value>()); + if(!CheckEqualContainers(boostset, stdset)){ + std::cout << "Error in boostset->rebalance()" << std::endl; + return 1; + } + set_test_rebalanceable(*boostmultiset + , container_detail::bool_::value>()); + if(!CheckEqualContainers(boostmultiset, stdmultiset)){ + std::cout << "Error in boostmultiset->rebalance()" << std::endl; + return 1; + } } } @@ -503,6 +503,99 @@ int set_test () } } + //Compare find/lower_bound/upper_bound in set + { + typename MyBoostSet::iterator bs_b = boostset->begin(); + typename MyBoostSet::iterator bs_e = boostset->end(); + typename MyStdSet::iterator ss_b = stdset->begin(); + typename MyStdSet::iterator ss_e = stdset->end(); + + std::size_t i = 0; + while(bs_b != bs_e){ + ++i; + typename MyBoostSet::iterator bs_i; + typename MyStdSet::iterator ss_i; + //find + bs_i = boostset->find(*bs_b); + ss_i = stdset->find(*ss_b); + if(!CheckEqualIt(bs_i, ss_i, *boostset, *stdset)){ + return -1; + } + //lower bound + bs_i = boostset->lower_bound(*bs_b); + ss_i = stdset->lower_bound(*ss_b); + if(!CheckEqualIt(bs_i, ss_i, *boostset, *stdset)){ + return -1; + } + //upper bound + bs_i = boostset->upper_bound(*bs_b); + ss_i = stdset->upper_bound(*ss_b); + if(!CheckEqualIt(bs_i, ss_i, *boostset, *stdset)){ + return -1; + } + //equal range + std::pair bs_ip; + std::pair ss_ip; + bs_ip = boostset->equal_range(*bs_b); + ss_ip = stdset->equal_range(*ss_b); + if(!CheckEqualIt(bs_ip.first, ss_ip.first, *boostset, *stdset)){ + return -1; + } + if(!CheckEqualIt(bs_ip.second, ss_ip.second, *boostset, *stdset)){ + return -1; + } + ++bs_b; + ++ss_b; + } + } + //Compare find/lower_bound/upper_bound in multiset + { + typename MyBoostMultiSet::iterator bm_b = boostmultiset->begin(); + typename MyBoostMultiSet::iterator bm_e = boostmultiset->end(); + typename MyStdMultiSet::iterator sm_b = stdmultiset->begin(); + typename MyStdMultiSet::iterator sm_e = stdmultiset->end(); + + while(bm_b != bm_e){ + typename MyBoostMultiSet::iterator bm_i; + typename MyStdMultiSet::iterator sm_i; + //find + bm_i = boostmultiset->find(*bm_b); + sm_i = stdmultiset->find(*sm_b); + if(!CheckEqualIt(bm_i, sm_i, *boostmultiset, *stdmultiset)){ + return -1; + } + //lower bound + bm_i = boostmultiset->lower_bound(*bm_b); + sm_i = stdmultiset->lower_bound(*sm_b); + if(!CheckEqualIt(bm_i, sm_i, *boostmultiset, *stdmultiset)){ + return -1; + } + //upper bound + bm_i = boostmultiset->upper_bound(*bm_b); + sm_i = stdmultiset->upper_bound(*sm_b); + if(!CheckEqualIt(bm_i, sm_i, *boostmultiset, *stdmultiset)){ + return -1; + } + //equal range + std::pair bm_ip; + std::pair sm_ip; + bm_ip = boostmultiset->equal_range(*bm_b); + sm_ip = stdmultiset->equal_range(*sm_b); + if(!CheckEqualIt(bm_ip.first, sm_ip.first, *boostmultiset, *stdmultiset)){ + return -1; + } + if(!CheckEqualIt(bm_ip.second, sm_ip.second, *boostmultiset, *stdmultiset)){ + return -1; + } + ++bm_b; + ++sm_b; + } + } + //Now do count exercise boostset->erase(boostset->begin(), boostset->end()); boostmultiset->erase(boostmultiset->begin(), boostmultiset->end());