Пример #1
0
TEST(SegmentTest, constructors)
{
  {
    RAJA::RangeStrideSegment first(0, 10, 2);
    RAJA::RangeStrideSegment copied(first);
    ASSERT_EQ(first, copied);
    RAJA::RangeStrideSegment moved(std::move(first));
    ASSERT_EQ(moved, copied);
  }

  {
    RAJA::RangeSegment first(0, 10);
    RAJA::RangeSegment copied(first);
    ASSERT_EQ(first, copied);
    RAJA::RangeSegment moved(std::move(first));
    ASSERT_EQ(moved, copied);
  }

  {
    RAJA::ListSegment first(RAJA::RangeSegment(0, 10));
    ASSERT_EQ(RAJA::Owned, first.getIndexOwnership());

    RAJA::ListSegment copied(first);
    ASSERT_EQ(RAJA::Owned, copied.getIndexOwnership());

    ASSERT_EQ(first, copied);
    RAJA::ListSegment moved(std::move(first));
    ASSERT_EQ(moved, copied);

    RAJA::ListSegment empty(nullptr, 100);
    RAJA::ListSegment empty2(first.begin(), -5);
    ASSERT_EQ(empty, empty2);
  }
}
Пример #2
0
dialogKopiuj::dialogKopiuj(QWidget *parent, QList<File> *fileList, QString sdk, int mode, QString sourcePath, QString targetPath) :
        QDialog(parent),
        ui(new Ui::dialogKopiuj)
{
    ui->setupUi(this);
    this->setFixedSize(this->width(),this->height());
    setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
    this->sourcePath = sourcePath;
    this->targetPath = targetPath;
    this->mode = mode;

    this->filesCopiedSize = 0;

    if (this->sourcePath.at(this->sourcePath.length() - 1) != '/')
        this->sourcePath.append("/");
    if (this->targetPath.at(this->targetPath.length() - 1) != '/')
        this->targetPath.append("/");

    this->opFinished = false;
    int max = 0;
    for (int i=0; i < fileList->size(); i++)
    {
        max += fileList->at(i).fileSize.toInt();
    }
    this->ui->progressTotal->setMaximum(max);

    this->remains = fileList->size() + 1;

    this->threadProgress = new ThreadProgress;
    this->threadProgress->sdk = sdk;
    this->threadProgress->mode = mode;

    this->threadCopy = new ThreadCopy;
    this->threadCopy->sdk = sdk;
    this->threadCopy->mode = mode;
    this->threadCopy->sourcePath = this->sourcePath;
    this->threadCopy->targetPath = this->targetPath;
    this->threadCopy->fileList = fileList;
    this->threadCopy->start();

    connect(this->threadCopy, SIGNAL(nextFile(QString, QString, QString, int, int)), this, SLOT(nextFile(QString, QString, QString, int, int)));
    connect(this->threadCopy, SIGNAL(copied()), this, SLOT(copied()));
    connect(this->threadCopy, SIGNAL(isRunning()), this, SLOT(running()));
    connect(this->ui->buttonCancel, SIGNAL(clicked()), this, SLOT(close()));
    //my new OK button
    connect(this->ui->buttonCopyOK, SIGNAL(clicked()), this, SLOT(close()));
//    connect(this->threadProgress, SIGNAL(progressValue(int)), this->ui->progressFile, SLOT(setValue(int)));
    connect(this->threadProgress, SIGNAL(progressValue(int)), this, SLOT(setProgressValue(int)));

    this->setFixedHeight(180);
    this->setLayout(ui->gridLayout);
}
Пример #3
0
TEST(Optional, Shared) {
  shared_ptr<int> ptr;
  Optional<shared_ptr<int>> opt;
  EXPECT_FALSE(bool(opt));
  // empty->emplaced
  opt.emplace(new int(5));
  EXPECT_TRUE(bool(opt));
  ptr = opt.value();
  EXPECT_EQ(ptr.get(), opt->get());
  EXPECT_EQ(2, ptr.use_count());
  opt.clear();
  EXPECT_EQ(1, ptr.use_count());
  // full->copied
  opt = ptr;
  EXPECT_EQ(2, ptr.use_count());
  EXPECT_EQ(ptr.get(), opt->get());
  opt.clear();
  EXPECT_EQ(1, ptr.use_count());
  // full->moved
  opt = std::move(ptr);
  EXPECT_EQ(1, opt->use_count());
  EXPECT_EQ(nullptr, ptr.get());
  {
    Optional<shared_ptr<int>> copied(opt);
    EXPECT_EQ(2, opt->use_count());
    Optional<shared_ptr<int>> moved(std::move(opt));
    EXPECT_EQ(2, moved->use_count());
    moved.emplace(new int(6));
    EXPECT_EQ(1, moved->use_count());
    copied = moved;
    EXPECT_EQ(2, moved->use_count());
  }
}
Пример #4
0
void Line::set_drivers( DriverContainer*& drivers )
{
	if ((drivers != NULL) && drivers_ != drivers) {
		delete drivers_;
		drivers_ = new DriverContainer(*drivers); 
	}
	copied();
}
Пример #5
0
Line::Line(const Line& rhs) {
	index_ = rhs.index_;
	direction_ = rhs.direction_;
	my_road_ = rhs.my_road_;
	drivers_per_second_ = rhs.drivers_per_second_;
	processed_count_ = rhs.processed_count_;
	drivers_ = new DriverContainer(*rhs.drivers());
	copied();
}
Пример #6
0
 forceinline BoolVarImp*
 BoolVarImp::copy(Space& home, bool share) {
   if (copied())
     return static_cast<BoolVarImp*>(forward());
   else if (zero())
     return &s_zero;
   else if (one())
     return &s_one;
   else
     return new (home) BoolVarImp(home,share,*this);
 }
Пример #7
0
    void object::test<3>()
    {
        geos::geom::Envelope box(0, 100, 0, 100);

        ensure( !box.isNull() );
        ensure_equals( box.getWidth(), box.getHeight() );

        geos::geom::Envelope copied(box);

        ensure( !copied.isNull() );
        ensure( copied == box );
        ensure_equals( copied.getWidth(), copied.getHeight() );
    }
Пример #8
0
int main()
{
    std::vector<int> count(200, 0);
    std::vector<std::vector<std::vector<int> > > wholelist(200);
    count[0] = 0;
    std::vector<int> one;
    one.push_back(1);
    wholelist[0].push_back(one);
    int totalsum = 0;
    int index = 0;
    while(index != 199) {
        int currentstep = count[index];
        int currentpower = index+1;
        std::vector<std::vector<int> > list = wholelist[index];
        for(std::vector<int> each : list) {
            for(int number : each) {
                std::vector<int> copied(each);
                int nextindex = number + currentpower - 1;
                copied.push_back(nextindex + 1);
                if(count[nextindex] == 0) {
                    count[nextindex] = currentstep + 1;
                    wholelist[nextindex].push_back(copied);
                    continue;
                }
                if(count[nextindex] != 0 && count[nextindex] > currentstep + 1) {
                    count[nextindex] = currentstep + 1;
                    wholelist[nextindex].clear();
                    wholelist[nextindex].push_back(copied);
                    continue;
                }
                if(count[nextindex] == currentstep + 1) {
                    wholelist[nextindex].push_back(copied);
                    continue;
                }
            }
        }
        std::cout << count[index] << std::endl;
        index++;

    }

    int sum = 0;
    for(int each : count)
        sum += each;
    std::cout << sum << std::endl;
}
void ConstructLocalOverlappingGrid(OvlpGrid       & ovlp_grid,
				   CellRange const& owned_c,
				   Geometry  const& geom, 
				   Overlap        & ovlp,
				   VertexCorr     & v_corr,
				   CellCorr       & c_corr)
{

  typedef typename CellRange::grid_type grid_type;

  enumerated_subrange<grid_type> owned(owned_c.TheGrid(),owned_c);

  // Fixme!!
  Geometry dummy(ovlp_grid.TheGrid());
  ConstructGridVC(ovlp_grid.TheGrid(),
		  //	    SubrangeFromCells(owned_c),
                  dummy,
		  owned,
		  geom,  
		  v_corr,   // global -> local
		  c_corr);  // global -> local
 
  enumerated_subrange<grid_type> copied(ovlp.cells().copied().TheGrid(), ovlp.cells().copied());


  // construct vertex identification by shared vertices
  typedef grid_types<grid_type>                  gt;
  typedef typename gt::vertex_handle             vertex_handle;
  bijective_mapping<vertex_handle,vertex_handle> id_shared;
  typedef typename Overlap::vertex_range_type         vrange;
  typedef typename vrange::const_iterator        v_range_iter;
  for(v_range_iter shv  = ovlp.vertices().shared().begin(); 
                   shv != ovlp.vertices().shared().end(); ++shv)
    id_shared[*shv] = v_corr(*shv);

  EnlargeGridVC(ovlp_grid.TheGrid(),
		//	      SubRangeFromCells(ovlp.cells().copied()),
		copied,
		geom,
		//	      restriction_map(v_corr, ovlp.vertices().shared()), // identify shared 
		id_shared,
		v_corr,  // v_corr remains unchanged on shared vertices
		c_corr);

  ovlp_grid.fine_grid_complete();

  //--------------------- copy the overlap ranges -------------------------------

  // make private ranges
  mark_private_range(ovlp.cells().privee(), owned_c.FirstCell(),
		     ovlp.cells().exposed(),ovlp.cells().shared(), ovlp.cells().copied());
  mark_private_range(ovlp.vertices().privee(), VerticesOfCells(owned_c).FirstVertex(),
		     ovlp.vertices().exposed(),ovlp.vertices().shared(), ovlp.vertices().copied());

  //-- not necessary:  ovlp_grid.init_overlap();

  typedef typename Overlap::CoarseCell CoarseCell;
  typedef typename Overlap::coarse_grid_type coarse_grid_type;
  typedef typename grid_types<coarse_grid_type>::cell_handle coarse_cell_handle;
  // semantics: ovlp_grid.TheOverlap() = ovlp  mod  global --> local ({v,c}_corr)
  CopyOverlap(ovlp_grid.TheOverlap(),
	      ovlp,
              ovlp_grid.TheCoarseGrid(), 
	      stdext::identity<coarse_cell_handle>(),   // ovlp.TheCoarse() == ovlp_grid.TheCoarse()
              ovlp_grid.TheGrid(),      // ovlp_grid.TheGrid() == local grid,
	                                // ovlp     .TheGrid() == global_grid
	      v_corr,                   // ovlp.TheGrid() --> ovlp_grid.TheGrid()
	      c_corr);                  // (   global     -->  local  )
}                                   
Пример #10
0
int main()
{
   // check that it'll find nodes exactly MAX away
   {
      tree_type exact_dist(std::ptr_fun(tac));
        triplet c0(5, 4, 0);
        exact_dist.insert(c0);
        triplet target(7,4,0);

      std::pair<tree_type::const_iterator,double> found = exact_dist.find_nearest(target,2);
      assert(found.first != exact_dist.end());
      assert(found.second == 2);
      std::cout << "Test find_nearest(), found at exact distance away from " << target << ", found " << *found.first << std::endl;
   }

   // do the same test, except use alternate_triplet as the search key
   {
      // NOTE: stores triplet, but we search with alternate_triplet
      typedef KDTree::KDTree<3, triplet, alternate_tac> alt_tree;

      triplet actual_target(7,0,0);

      alt_tree tree;
      tree.insert( triplet(0, 0, 7) );
      tree.insert( triplet(0, 0, 7) );
      tree.insert( triplet(0, 0, 7) );
      tree.insert( triplet(3, 0, 0) );
      tree.insert( actual_target );
      tree.optimise();

      alternate_triplet target( actual_target );

      std::pair<alt_tree::const_iterator,double> found = tree.find_nearest(target);
      assert(found.first != tree.end());
      std::cout << "Test with alternate search type, found: " << *found.first << ", wanted " << actual_target << std::endl;
      assert(found.second == 0);
      assert(*found.first == actual_target);
   }


   {
      tree_type exact_dist(std::ptr_fun(tac));
        triplet c0(5, 2, 0);
        exact_dist.insert(c0);
        triplet target(7,4,0);

        // call find_nearest without a range value - it found a compile error earlier.
      std::pair<tree_type::const_iterator,double> found = exact_dist.find_nearest(target);
      assert(found.first != exact_dist.end());
      std::cout << "Test find_nearest(), found at exact distance away from " << target << ", found " << *found.first << " @ " << found.second << " should be " << std::sqrt(8) << std::endl;
      assert(found.second == std::sqrt(8));
   }

   {
      tree_type exact_dist(std::ptr_fun(tac));
        triplet c0(5, 2, 0);
        exact_dist.insert(c0);
        triplet target(7,4,0);

      std::pair<tree_type::const_iterator,double> found = exact_dist.find_nearest(target,std::sqrt(8));
      assert(found.first != exact_dist.end());
      std::cout << "Test find_nearest(), found at exact distance away from " << target << ", found " << *found.first << " @ " << found.second << " should be " << std::sqrt(8) << std::endl;
      assert(found.second == std::sqrt(8));
   }

  tree_type src(std::ptr_fun(tac));

  triplet c0(5, 4, 0); src.insert(c0);
  triplet c1(4, 2, 1); src.insert(c1);
  triplet c2(7, 6, 9); src.insert(c2);
  triplet c3(2, 2, 1); src.insert(c3);
  triplet c4(8, 0, 5); src.insert(c4);
  triplet c5(5, 7, 0); src.insert(c5);
  triplet c6(3, 3, 8); src.insert(c6);
  triplet c7(9, 7, 3); src.insert(c7);
  triplet c8(2, 2, 6); src.insert(c8);
  triplet c9(2, 0, 6); src.insert(c9);

  std::cout << src << std::endl;

  src.erase(c0);
  src.erase(c1);
  src.erase(c3);
  src.erase(c5);

  src.optimise();


  // test the efficient_replace_and_optimise()
  tree_type eff_repl = src;
  {
     std::vector<triplet> vec;
     // erased above as part of test vec.push_back(triplet(5, 4, 0));
     // erased above as part of test vec.push_back(triplet(4, 2, 1));
     vec.push_back(triplet(7, 6, 9));
     // erased above as part of test vec.push_back(triplet(2, 2, 1));
     vec.push_back(triplet(8, 0, 5));
     // erased above as part of test vec.push_back(triplet(5, 7, 0));
     vec.push_back(triplet(3, 3, 8));
     vec.push_back(triplet(9, 7, 3));
     vec.push_back(triplet(2, 2, 6));
     vec.push_back(triplet(2, 0, 6));

     eff_repl.clear();
     eff_repl.efficient_replace_and_optimise(vec);
  }


  std::cout << std::endl << src << std::endl;

  tree_type copied(src);
  std::cout << copied << std::endl;
  tree_type assigned;
  assigned = src;
  std::cout << assigned << std::endl;

  for (int loop = 0; loop != 4; ++loop)
    {
      tree_type * target;
      switch (loop)
	{
	case 0: std::cout << "Testing plain construction" << std::endl;
	  target = &src;
	  break;

	case 1: std::cout << "Testing copy-construction" << std::endl;
	  target = &copied;
	  break;

	case 2: std::cout << "Testing assign-construction" << std::endl;
	  target = &assigned;
	  break;

   default:
	case 4: std::cout << "Testing efficient-replace-and-optimise" << std::endl;
	  target = &eff_repl;
	  break;
	}
      tree_type & t = *target;

      int i=0;
      for (tree_type::const_iterator iter=t.begin(); iter!=t.end(); ++iter, ++i);
      std::cout << "iterator walked through " << i << " nodes in total" << std::endl;
      if (i!=6)
	{
	  std::cerr << "Error: does not tally with the expected number of nodes (6)" << std::endl;
	  return 1;
	}
      i=0;
      for (tree_type::const_reverse_iterator iter=t.rbegin(); iter!=t.rend(); ++iter, ++i);
      std::cout << "reverse_iterator walked through " << i << " nodes in total" << std::endl;
      if (i!=6)
	{
	  std::cerr << "Error: does not tally with the expected number of nodes (6)" << std::endl;
	  return 1;
	}

      triplet s(5, 4, 3);
      std::vector<triplet> v;
      unsigned int const RANGE = 3;

      size_t count = t.count_within_range(s, RANGE);
      std::cout << "counted " << count
		<< " nodes within range " << RANGE << " of " << s << ".\n";
      t.find_within_range(s, RANGE, std::back_inserter(v));

      std::cout << "found   " << v.size() << " nodes within range " << RANGE
		<< " of " << s << ":\n";
      std::vector<triplet>::const_iterator ci = v.begin();
      for (; ci != v.end(); ++ci)
	std::cout << *ci << " ";
      std::cout << "\n" << std::endl;

      std::cout << std::endl << t << std::endl;

      // search for all the nodes at exactly 0 dist away
      for (tree_type::const_iterator target = t.begin(); target != t.end(); ++target)
      {
         std::pair<tree_type::const_iterator,double> found = t.find_nearest(*target,0);
         assert(found.first != t.end());
         assert(*found.first == *target);
         std::cout << "Test find_nearest(), found at exact distance away from " << *target << ", found " << *found.first << std::endl;
      }

      {
         const double small_dist = 0.0001;
         std::pair<tree_type::const_iterator,double> notfound = t.find_nearest(s,small_dist);
         std::cout << "Test find_nearest(), nearest to " << s << " within " << small_dist << " should not be found" << std::endl;

         if (notfound.first != t.end())
         {
            std::cout << "ERROR found a node at dist " << notfound.second << " : " << *notfound.first << std::endl;
            std::cout << "Actual distance = " << s.distance_to(*notfound.first) << std::endl;
         }

         assert(notfound.first == t.end());
      }

      {
         std::pair<tree_type::const_iterator,double> nif = t.find_nearest_if(s,std::numeric_limits<double>::max(),Predicate());
         std::cout << "Test find_nearest_if(), nearest to " << s << " @ " << nif.second << ": " << *nif.first << std::endl;

         std::pair<tree_type::const_iterator,double> cantfind = t.find_nearest_if(s,std::numeric_limits<double>::max(),FalsePredicate());
         std::cout << "Test find_nearest_if(), nearest to " << s << " should never be found (predicate too strong)" << std::endl;
         assert(cantfind.first == t.end());
      }




      {
      std::pair<tree_type::const_iterator,double> found = t.find_nearest(s,std::numeric_limits<double>::max());
      std::cout << "Nearest to " << s << " @ " << found.second << " " << *found.first << std::endl;
      std::cout << "Should be " << found.first->distance_to(s) << std::endl;
      // NOTE: the assert does not check for an exact match, as it is not exact when -O2 or -O3 is
      // switched on.  Some sort of optimisation makes the math inexact.
      assert( fabs(found.second - found.first->distance_to(s)) < std::numeric_limits<double>::epsilon() );
      }

      {
      triplet s2(10, 10, 2);
      std::pair<tree_type::const_iterator,double> found = t.find_nearest(s2,std::numeric_limits<double>::max());
      std::cout << "Nearest to " << s2 << " @ " << found.second << " " << *found.first << std::endl;
      std::cout << "Should be " << found.first->distance_to(s2) << std::endl;
      // NOTE: the assert does not check for an exact match, as it is not exact when -O2 or -O3 is
      // switched on.  Some sort of optimisation makes the math inexact.
      assert( fabs(found.second - found.first->distance_to(s2)) < std::numeric_limits<double>::epsilon() );
      }

      std::cout << std::endl;

      std::cout << t << std::endl;

      // Testing iterators
      {
	std::cout << "Testing iterators" << std::endl;

	t.erase(c2);
	t.erase(c4);
	t.erase(c6);
	t.erase(c7);
	t.erase(c8);
	//    t.erase(c9);

	std::cout << std::endl << t << std::endl;

	std::cout << "Forward iterator test..." << std::endl;
	std::vector<triplet> forwards;
	for (tree_type::iterator i = t.begin(); i != t.end(); ++i)
	  { std::cout << *i << " " << std::flush; forwards.push_back(*i); }
	std::cout << std::endl;
	std::cout << "Reverse iterator test..." << std::endl;
	std::vector<triplet> backwards;
	for (tree_type::reverse_iterator i = t.rbegin(); i != t.rend(); ++i)
	  { std::cout << *i << " " << std::flush; backwards.push_back(*i); }
	std::cout << std::endl;
	std::reverse(backwards.begin(),backwards.end());
	assert(backwards == forwards);
      }
    }


  // Walter reported that the find_within_range() wasn't giving results that were within
  // the specified range... this is the test.
  {
     tree_type tree(std::ptr_fun(tac));
     tree.insert( triplet(28.771200,16.921600,-2.665970) );
     tree.insert( triplet(28.553101,18.649700,-2.155560) );
     tree.insert( triplet(28.107500,20.341400,-1.188940) );
     tree.optimise();

     std::deque< triplet > vectors;
     triplet sv(18.892500,20.341400,-1.188940);
     tree.find_within_range(sv, 10.0f, std::back_inserter(vectors));

     std::cout << std::endl << "Test find_with_range( " << sv << ", 10.0f) found " << vectors.size() << " candidates." << std::endl;

     // double-check the ranges
     for (std::deque<triplet>::iterator v = vectors.begin(); v != vectors.end(); ++v)
     {
        double dist = sv.distance_to(*v);
        std::cout << "  " << *v << " dist=" << dist << std::endl;
        if (dist > 10.0f)
           std::cout << "    This point is too far! But that is by design, its within a 'box' with a 'radius' of 10, not a sphere with a radius of 10" << std::endl;
        // Not a valid test, it can be greater than 10 if the point is in the corners of the box.
        // assert(dist <= 10.0f);
     }
  }


  return 0;
}
Пример #11
0
 forceinline SetVarImp*
 SetVarImp::copy(Space& home, bool share) {
   return copied() ?
     static_cast<SetVarImp*>(forward()) :
     perform_copy(home,share);
 }
Пример #12
0
int main()
{
  BinaryTree<int> empty;
  BinaryTree<int> complete = generateCompleteTree(30);
  BinaryTree<int> list = generateLinkedList(100);

  // empty tree is empty
  assert(empty.isEmpty());

  // size of complete tree
  assert(complete.getSize() == 31);

  // size of list
  assert(list.getSize() == 100);


  // depth of complete tree
  assert(complete.getDepth() == 4);

  // depth of list
  assert(list.getDepth() == 99);


  typedef BinaryTree<int>::NodeDeque traversal_type;

  // root is at the front of a preorder traversal
  traversal_type pre;
  complete.preorder(pre, complete.getRoot());
  assert(pre.front() == complete.getRoot());

  // root is at the end of a postorder traversal
  traversal_type post;
  complete.postorder(post, complete.getRoot());
  assert(post.back() == complete.getRoot());

  // root is in the middle of an inorder traversal
  traversal_type in;
  complete.inorder(in, complete.getRoot());
  assert(in.at(in.size()/2) == complete.getRoot());


  // traversals should contain all nodes of the tree
  assert(pre.size() == complete.getSize());
  assert(post.size() == complete.getSize());
  assert(in.size() == complete.getSize());

  typedef BinaryTree<int>::NodePtr iterator;
  const int query_value = 22;
  
  // 22 should be in the list and complete tree
  iterator complete_query = complete.simpleSearch(query_value);
  iterator list_query = list.simpleSearch(query_value);
  iterator empty_query = empty.simpleSearch(query_value);

  assert(complete_query != NULL);
  assert(list_query != NULL);
  assert(empty_query == NULL);
  

  // test copy ctor and assignment operator
  BinaryTree<int> copied(complete);
  BinaryTree<int> assigned;
  assigned = complete;

  // should have same size
  assert(copied.getSize() == complete.getSize());
  assert(assigned.getSize() == complete.getSize());

  // should still have 22
  iterator copy_query = copied.simpleSearch(query_value);
  iterator assign_query = assigned.simpleSearch(query_value);

  assert(copy_query != NULL);
  assert(assign_query != NULL);

  std::cout << "Passed!" << std::endl;

  return 0;
}
Пример #13
0
 ///call both of these in order to cut (setCopyOperand then  setCutSubject)
 ///call it in order to copy
 Q_INVOKABLE void setCopyOperand(pmoc::QQModel*m){if(m){_isCutAction=false;  _copyOperand=m;emit copied(m);}}
Пример #14
0
void ListResponse::addMessages(vector<Message*> messages) {
    vector<Message*> copied(messages);
    this->messages = copied;
}
Пример #15
0
Literal Literal::getNegated() const {
    Literal copied(*this);
    copied.togglePrefix();
    return copied;
}
Пример #16
0
int main()
{
   // check that it'll find nodes exactly MAX away
   {
      tree_type exact_dist(std::ptr_fun(tac));
        triplet c0(5, 4, 0);
        exact_dist.insert(c0);
        triplet target(7,4,0);

      std::pair<tree_type::const_iterator,double> found = exact_dist.find_nearest(target,2);
      assert(found.first != exact_dist.end());
      assert(found.second == 2);
      std::cout << "Test find_nearest(), found at exact distance away from " << target << ", found " << *found.first << std::endl;
   }

   {
      tree_type exact_dist(std::ptr_fun(tac));
        triplet c0(5, 2, 0);
        exact_dist.insert(c0);
        triplet target(7,4,0);

        // call find_nearest without a range value - it found a compile error earlier.
      std::pair<tree_type::const_iterator,double> found = exact_dist.find_nearest(target);
      assert(found.first != exact_dist.end());
      std::cout << "Test find_nearest(), found at exact distance away from " << target << ", found " << *found.first << " @ " << found.second << " should be " << sqrt(8) << std::endl;
      assert(found.second == sqrt(8));
   }

   {
      tree_type exact_dist(std::ptr_fun(tac));
        triplet c0(5, 2, 0);
        exact_dist.insert(c0);
        triplet target(7,4,0);

      std::pair<tree_type::const_iterator,double> found = exact_dist.find_nearest(target,sqrt(8));
      assert(found.first != exact_dist.end());
      std::cout << "Test find_nearest(), found at exact distance away from " << target << ", found " << *found.first << " @ " << found.second << " should be " << sqrt(8) << std::endl;
      assert(found.second == sqrt(8));
   }

  tree_type src(std::ptr_fun(tac));

  triplet c0(5, 4, 0); src.insert(c0);
  triplet c1(4, 2, 1); src.insert(c1);
  triplet c2(7, 6, 9); src.insert(c2);
  triplet c3(2, 2, 1); src.insert(c3);
  triplet c4(8, 0, 5); src.insert(c4);
  triplet c5(5, 7, 0); src.insert(c5);
  triplet c6(3, 3, 8); src.insert(c6);
  triplet c7(9, 7, 3); src.insert(c7);
  triplet c8(2, 2, 6); src.insert(c8);
  triplet c9(2, 0, 6); src.insert(c9);

  std::cout << src << std::endl;

  src.erase(c0);
  src.erase(c1);
  src.erase(c3);
  src.erase(c5);

  src.optimise();


  // test the efficient_replace_and_optimise()
  tree_type eff_repl = src;
  {
     std::vector<triplet> vec;
     // erased above as part of test vec.push_back(triplet(5, 4, 0));
     // erased above as part of test vec.push_back(triplet(4, 2, 1));
     vec.push_back(triplet(7, 6, 9));
     // erased above as part of test vec.push_back(triplet(2, 2, 1));
     vec.push_back(triplet(8, 0, 5));
     // erased above as part of test vec.push_back(triplet(5, 7, 0));
     vec.push_back(triplet(3, 3, 8));
     vec.push_back(triplet(9, 7, 3));
     vec.push_back(triplet(2, 2, 6));
     vec.push_back(triplet(2, 0, 6));

     eff_repl.clear();
     eff_repl.efficient_replace_and_optimise(vec);
  }


  std::cout << std::endl << src << std::endl;

  tree_type copied(src);
  std::cout << copied << std::endl;
  tree_type assigned;
  assigned = src;
  std::cout << assigned << std::endl;

  for (int loop = 0; loop != 4; ++loop)
    {
      tree_type * target;
      switch (loop)
	{
	case 0: std::cout << "Testing plain construction" << std::endl;
	  target = &src;
	  break;

	case 1: std::cout << "Testing copy-construction" << std::endl;
	  target = &copied;
	  break;

	case 2: std::cout << "Testing assign-construction" << std::endl;
	  target = &assigned;
	  break;

   default:
	case 4: std::cout << "Testing efficient-replace-and-optimise" << std::endl;
	  target = &eff_repl;
	  break;
	}
      tree_type & t = *target;

      int i=0;
      for (tree_type::const_iterator iter=t.begin(); iter!=t.end(); ++iter, ++i);
      std::cout << "iterator walked through " << i << " nodes in total" << std::endl;
      if (i!=6)
	{
	  std::cerr << "Error: does not tally with the expected number of nodes (6)" << std::endl;
	  return 1;
	}
      i=0;
      for (tree_type::const_reverse_iterator iter=t.rbegin(); iter!=t.rend(); ++iter, ++i);
      std::cout << "reverse_iterator walked through " << i << " nodes in total" << std::endl;
      if (i!=6)
	{
	  std::cerr << "Error: does not tally with the expected number of nodes (6)" << std::endl;
	  return 1;
	}

      triplet s(5, 4, 3);
      std::vector<triplet> v;
      unsigned int const RANGE = 3;

      size_t count = t.count_within_range(s, RANGE);
      std::cout << "counted " << count
		<< " nodes within range " << RANGE << " of " << s << ".\n";
      t.find_within_range(s, RANGE, std::back_inserter(v));

      std::cout << "found   " << v.size() << " nodes within range " << RANGE
		<< " of " << s << ":\n";
      std::vector<triplet>::const_iterator ci = v.begin();
      for (; ci != v.end(); ++ci)
	std::cout << *ci << " ";
      std::cout << "\n" << std::endl;

      std::cout << std::endl << t << std::endl;

      // search for all the nodes at exactly 0 dist away
      for (tree_type::const_iterator target = t.begin(); target != t.end(); ++target)
      {
         std::pair<tree_type::const_iterator,double> found = t.find_nearest(*target,0);
         assert(found.first != t.end());
         assert(*found.first == *target);
         std::cout << "Test find_nearest(), found at exact distance away from " << *target << ", found " << *found.first << std::endl;
      }

      {
         const double small_dist = 0.0001;
         std::pair<tree_type::const_iterator,double> notfound = t.find_nearest(s,small_dist);
         std::cout << "Test find_nearest(), nearest to " << s << " within " << small_dist << " should not be found" << std::endl;

         if (notfound.first != t.end())
         {
            std::cout << "ERROR found a node at dist " << notfound.second << " : " << *notfound.first << std::endl;
            std::cout << "Actual distance = " << s.distance_to(*notfound.first) << std::endl;
         }

         assert(notfound.first == t.end());
      }

      {
         std::pair<tree_type::const_iterator,double> nif = t.find_nearest_if(s,std::numeric_limits<double>::max(),Predicate());
         std::cout << "Test find_nearest_if(), nearest to " << s << " @ " << nif.second << ": " << *nif.first << std::endl;

         std::pair<tree_type::const_iterator,double> cantfind = t.find_nearest_if(s,std::numeric_limits<double>::max(),FalsePredicate());
         std::cout << "Test find_nearest_if(), nearest to " << s << " should never be found (predicate too strong)" << std::endl;
         assert(cantfind.first == t.end());
      }




      {
      std::pair<tree_type::const_iterator,double> found = t.find_nearest(s,std::numeric_limits<double>::max());
      std::cout << "Nearest to " << s << " @ " << found.second << " " << *found.first << std::endl;
      std::cout << "Should be " << found.first->distance_to(s) << std::endl;
      // NOTE: the assert does not check for an exact match, as it is not exact when -O2 or -O3 is
      // switched on.  Some sort of optimisation makes the math inexact.
      assert( fabs(found.second - found.first->distance_to(s)) < std::numeric_limits<double>::epsilon() );
      }

      {
      triplet s2(10, 10, 2);
      std::pair<tree_type::const_iterator,double> found = t.find_nearest(s2,std::numeric_limits<double>::max());
      std::cout << "Nearest to " << s2 << " @ " << found.second << " " << *found.first << std::endl;
      std::cout << "Should be " << found.first->distance_to(s2) << std::endl;
      // NOTE: the assert does not check for an exact match, as it is not exact when -O2 or -O3 is
      // switched on.  Some sort of optimisation makes the math inexact.
      assert( fabs(found.second - found.first->distance_to(s2)) < std::numeric_limits<double>::epsilon() );
      }

      std::cout << std::endl;

      std::cout << t << std::endl;

      // Testing iterators
      {
	std::cout << "Testing iterators" << std::endl;

	t.erase(c2);
	t.erase(c4);
	t.erase(c6);
	t.erase(c7);
	t.erase(c8);
	//    t.erase(c9);

	std::cout << std::endl << t << std::endl;

	std::cout << "Forward iterator test..." << std::endl;
	std::vector<triplet> forwards;
	for (tree_type::iterator i = t.begin(); i != t.end(); ++i)
	  { std::cout << *i << " " << std::flush; forwards.push_back(*i); }
	std::cout << std::endl;
	std::cout << "Reverse iterator test..." << std::endl;
	std::vector<triplet> backwards;
	for (tree_type::reverse_iterator i = t.rbegin(); i != t.rend(); ++i)
	  { std::cout << *i << " " << std::flush; backwards.push_back(*i); }
	std::cout << std::endl;
	std::reverse(backwards.begin(),backwards.end());
	assert(backwards == forwards);
      }
    }

  return 0;
}
Пример #17
0
void QuantityFormatterTest::TestBasic() {
    UErrorCode status = U_ZERO_ERROR;
#if !UCONFIG_NO_FORMATTING
    QuantityFormatter fmt;
    assertFalse(
            "adding bad variant",
            fmt.addIfAbsent("a bad variant", "{0} pounds", status));
    assertEquals("adding bad variant status", (int32_t)U_ILLEGAL_ARGUMENT_ERROR, status);
    status = U_ZERO_ERROR;
    assertFalse(
            "Adding bad pattern",
            fmt.addIfAbsent("other", "{0} {1} too many placeholders", status));
    assertEquals("adding bad pattern status", (int32_t)U_ILLEGAL_ARGUMENT_ERROR, status);
    status = U_ZERO_ERROR;
    assertFalse("isValid with no patterns", fmt.isValid());
    assertTrue(
            "Adding good pattern with no placeholders",
            fmt.addIfAbsent("zero", "no placeholder", status));
    assertTrue(
            "Adding good pattern",
            fmt.addIfAbsent("other", "{0} pounds", status));
    assertTrue("isValid with other", fmt.isValid());
    assertTrue(
            "Adding good pattern",
            fmt.addIfAbsent("one", "{0} pound", status));

    assertEquals(
            "getByVariant",
            fmt.getByVariant("bad variant")->getTextWithNoArguments(),
            " pounds");
    assertEquals(
            "getByVariant",
            fmt.getByVariant("other")->getTextWithNoArguments(),
            " pounds");
    assertEquals(
            "getByVariant",
            fmt.getByVariant("one")->getTextWithNoArguments(),
            " pound");
    assertEquals(
            "getByVariant",
            fmt.getByVariant("few")->getTextWithNoArguments(),
            " pounds");

    // Test copy constructor
    {
        QuantityFormatter copied(fmt);
        assertEquals(
                "copied getByVariant",
                copied.getByVariant("other")->getTextWithNoArguments(),
                " pounds");
        assertEquals(
                "copied getByVariant",
                copied.getByVariant("one")->getTextWithNoArguments(),
                " pound");
        assertEquals(
                "copied getByVariant",
                copied.getByVariant("few")->getTextWithNoArguments(),
                " pounds");
    }
        
    // Test assignment
    {
        QuantityFormatter assigned;
        assigned = fmt;
        assertEquals(
                "assigned getByVariant",
                assigned.getByVariant("other")->getTextWithNoArguments(),
                " pounds");
        assertEquals(
                "assigned getByVariant",
                assigned.getByVariant("one")->getTextWithNoArguments(),
                " pound");
        assertEquals(
                "assigned getByVariant",
                assigned.getByVariant("few")->getTextWithNoArguments(),
                " pounds");
    }

    // Test format.
    {
        LocalPointer<NumberFormat> numfmt(
                NumberFormat::createInstance(Locale::getEnglish(), status));
        LocalPointer<PluralRules> plurrule(
                PluralRules::forLocale("en", status));
        FieldPosition pos(FieldPosition::DONT_CARE);
        UnicodeString appendTo;
        assertEquals(
                "format singular",
                UnicodeString("1 pound"),
                fmt.format(
                        1.0,
                        *numfmt,
                        *plurrule,
                        appendTo,
                        pos,
                        status), TRUE);
        appendTo.remove();
        assertEquals(
                "format plural",
                UnicodeString("2 pounds"),
                fmt.format(
                        2.0,
                        *numfmt,
                        *plurrule,
                        appendTo,
                        pos,
                        status), TRUE);
    }
    fmt.reset();
    assertFalse("isValid after reset", fmt.isValid());
#endif
    assertSuccess("", status);
}
Пример #18
0
 forceinline FloatVarImp*
 FloatVarImp::copy(Space* home, bool share) {
   return copied() ?
     static_cast<FloatVarImp*>(forward()) :
     perform_copy(home,share);
 }