disjoint_set_test.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // (C) Copyright Jeremy Siek 2002.
  2. // Distributed under the Boost Software License, Version 1.0. (See
  3. // accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. #include <boost/pending/disjoint_sets.hpp>
  6. #include <boost/test/minimal.hpp>
  7. #include <boost/cstdlib.hpp>
  8. template <typename DisjointSet>
  9. struct test_disjoint_set {
  10. static void do_test()
  11. {
  12. // The following tests are pretty lame, just a basic sanity check.
  13. // Industrial strength tests still need to be written.
  14. #if !defined(__MWERKS__) || __MWERKS__ > 0x3003
  15. std::size_t elts[]
  16. #else
  17. std::size_t elts[4]
  18. #endif
  19. = { 0, 1, 2, 3 };
  20. const int N = sizeof(elts)/sizeof(*elts);
  21. DisjointSet ds(N);
  22. ds.make_set(elts[0]);
  23. ds.make_set(elts[1]);
  24. ds.make_set(elts[2]);
  25. ds.make_set(elts[3]);
  26. BOOST_CHECK(ds.find_set(0) != ds.find_set(1));
  27. BOOST_CHECK(ds.find_set(0) != ds.find_set(2));
  28. BOOST_CHECK(ds.find_set(0) != ds.find_set(3));
  29. BOOST_CHECK(ds.find_set(1) != ds.find_set(2));
  30. BOOST_CHECK(ds.find_set(1) != ds.find_set(3));
  31. BOOST_CHECK(ds.find_set(2) != ds.find_set(3));
  32. ds.union_set(0, 1);
  33. ds.union_set(2, 3);
  34. BOOST_CHECK(ds.find_set(0) != ds.find_set(3));
  35. int a = ds.find_set(0);
  36. BOOST_CHECK(a == ds.find_set(1));
  37. int b = ds.find_set(2);
  38. BOOST_CHECK(b == ds.find_set(3));
  39. ds.link(a, b);
  40. BOOST_CHECK(ds.find_set(a) == ds.find_set(b));
  41. BOOST_CHECK(1 == ds.count_sets(elts, elts + N));
  42. ds.normalize_sets(elts, elts + N);
  43. ds.compress_sets(elts, elts + N);
  44. BOOST_CHECK(1 == ds.count_sets(elts, elts + N));
  45. }
  46. };
  47. int
  48. test_main(int, char*[])
  49. {
  50. using namespace boost;
  51. {
  52. typedef
  53. disjoint_sets_with_storage<identity_property_map, identity_property_map,
  54. find_with_path_halving> ds_type;
  55. test_disjoint_set<ds_type>::do_test();
  56. }
  57. {
  58. typedef
  59. disjoint_sets_with_storage<identity_property_map, identity_property_map,
  60. find_with_full_path_compression> ds_type;
  61. test_disjoint_set<ds_type>::do_test();
  62. }
  63. return boost::exit_success;
  64. }