Пример #1
0
std::pair<int, std::string> test_simple_path(std::ostream& strm,int argc, const char *argv[])
{
      sparse_graph_t s10(10, graph_type_t::UNDIRECTED);
     
      // fig 17.23 p 67
      s10.insert(simple_edge_t(0,1,1));
      s10.insert(simple_edge_t(0,2,1));
      s10.insert(simple_edge_t(0,6,1));
      s10.insert(simple_edge_t(0,5,1));
      s10.insert(simple_edge_t(1,2,1));
      s10.insert(simple_edge_t(2,3,1));
      s10.insert(simple_edge_t(3,4,1));
      s10.insert(simple_edge_t(5,4,1));
      s10.insert(simple_edge_t(2,4,1));
      s10.insert(simple_edge_t(4,6,1));

      s10.insert(simple_edge_t(7,8,1));
      
      s10.insert(simple_edge_t(9,10,1));
      s10.insert(simple_edge_t(9,11,1));
      s10.insert(simple_edge_t(9,12,1));
      s10.insert(simple_edge_t(11,12,1));

      std::string dn = test_path("simple_path.dot");
      s10.graphviz(dn);
      simple_edge_path_t sp(s10);
      simple_path_t p = sp(0, 5);
      ASSERT_CONDITION("there is a path from 0 to 5", p == true);
      ASSERT_CONDITION("there is a path no path from 0 to 9", sp(0,9) == false);
      return DONE;
}
Пример #2
0
std::pair<int, std::string> test_adjacency_list_rm(std::ostream& strm,int argc, const char *argv[])
{
      std::set<adjacency_list_t::edge_tuple_t> exp_edges =
      {
            std::make_tuple(0,3,1),
            std::make_tuple(0,0,1),
            std::make_tuple(0,1,1),
            std::make_tuple(3,2,1)
      };
      
      adjacency_list_t l1(10);
      
      l1(0,3,1);
      l1(0,0,1);
      l1(0,1,1);
      l1(1,4,1);
      l1(3,2,1);
      
      l1.rm(1,4);
      std::cerr << l1 << std::endl;
      
      ASSERT(l1.size() == 4);
      ASSERT(l1.max_size() == 10);
      ASSERT(l1(1,4) == 0);
      ASSERT(l1(0,3) == 1);
      std::set<adjacency_list_t::edge_tuple_t> edges;
      for (adjacency_list_t::iterator it = l1.begin(); it != l1.end(); it++) {
            edges.insert(*it);
      }
      ASSERT_CONDITION("rm : expected edges are iterated over", exp_edges == edges);
      
      return DONE;
}
Пример #3
0
static bool param_compose_test(ot_op* op1, ot_op* op2, ot_op* expected,
                               char** msg) {
    ot_op* actual = ot_compose(op1, op2);
    ot_free_op(op1);
    ot_free_op(op2);

    if (actual == NULL) {
        if (expected == NULL) {
            return true;
        } else {
            FAIL("Operations couldn't be composed.", msg);
        }
    }
    bool equal = ot_equal(expected, actual);
    char* expected_enc = ot_encode(expected);
    char* actual_enc = ot_encode(actual);
    ASSERT_CONDITION(equal, expected_enc, actual_enc,
                     "Composed operation wasn't correct.", msg);

    ot_free_op(expected);
    ot_free_op(actual);
    free(expected_enc);
    free(actual_enc);
    return true;
}
Пример #4
0
bool TestSuite::TestListDS()
{
	printf("\n Running TestListDS...") ;

	upan::list<int> l ;

	l.push_back(100) ;
	l.push_back(200) ;
	l.push_back(300) ;

	l.insert(++l.begin(), 400) ;

	int arr[ ] = { 100, 400, 200, 300 } ;
	int i = 0 ;

  for(auto it : l)
		ASSERT_CONDITION(arr[ i++ ] == it) ;

	ASSERT_CONDITION(l.size() == 4) ;
	ASSERT_CONDITION(l.front() == 100) ;
	l.pop_front() ;
	ASSERT_CONDITION(l.size() == 3) ;
	ASSERT_CONDITION(l.front() == 400);
	ASSERT_CONDITION(l.back() == 300);

	return true ;
}
Пример #5
0
std::pair<int, std::string> test_transitive_closure_dag(std::ostream& strm,int argc, const char *argv[])
{
      std::string fn = test_path("transitive_closure_dag.dot");
      size_t size = 50;
      sparse_graph_t s10(size, graph_type_t::DIRECTED);
      
      s10.insert(simple_edge_t(0,1,1));
      s10.insert(simple_edge_t(0,2,1));
      s10.insert(simple_edge_t(0,3,1));
      s10.insert(simple_edge_t(0,5,1));
      s10.insert(simple_edge_t(0,6,1));
      s10.insert(simple_edge_t(2,3,1));
      s10.insert(simple_edge_t(3,4,1));
      s10.insert(simple_edge_t(3,5,1));
      s10.insert(simple_edge_t(4,9,1));
      s10.insert(simple_edge_t(6,4,1));
      s10.insert(simple_edge_t(6,9,1));
      s10.insert(simple_edge_t(7,6,1));
      s10.insert(simple_edge_t(8,7,1));
      s10.insert(simple_edge_t(9,10,1));
      s10.insert(simple_edge_t(9,11,1));
      s10.insert(simple_edge_t(9,12,1));
      s10.insert(simple_edge_t(11,12,1));
      
      is_dag<simple_edge_t> is_dag(s10);
      ASSERT_CONDITION("graph is a DAG", is_dag());
      
      s10.graphviz(fn);
      transitive_closure<sparse_graph_t, tc_dag<simple_edge_t>> tc(s10);
      std::string wts = test_path("transitive_closure_dag_results.dot");
      (*tc).graphviz(wts);
      ASSERT(tc(simple_edge_t(0,1,1)));
      ASSERT(tc(simple_edge_t(0,12,1)))
      ASSERT(tc(simple_edge_t(0,11,1)));
      ASSERT(tc(simple_edge_t(8,12,1)))
      
      ASSERT(tc(simple_edge_t(0,10,1)));
      ASSERT(tc(simple_edge_t(3,4,1)))
      ASSERT(!tc(simple_edge_t(1,4,1)));
      ASSERT(!tc(simple_edge_t(4,1,1)))
      
      ASSERT(tc(simple_edge_t(0,2,1)));
      ASSERT(!tc(simple_edge_t(2,0,1)))
      ASSERT(tc(simple_edge_t(2,4,1)));
      
      return DONE;
}
Пример #6
0
bool TestSuite::TestAtomicSwap()
{
	printf("\n Running TestAtomicSwap...") ;
	Mutex m ;

	ASSERT_CONDITION(m.m_iLock == 0) ;
	ASSERT_CONDITION(Atomic::Swap(m.m_iLock, 1) == 0) ;
	ASSERT_CONDITION(m.m_iLock == 1) ;
	ASSERT_CONDITION(Atomic::Swap(m.m_iLock, 0) == 1) ;
	ASSERT_CONDITION(m.m_iLock == 0) ;
	ASSERT_CONDITION(Atomic::Swap(m.m_iLock, 1) == 0) ;
	ASSERT_CONDITION(m.m_iLock == 1) ;

	return true ;
}
Пример #7
0
bool TestSuite::TestListDSPtr()
{
	printf("\n Running TestListDS PTR...") ;

	upan::list<int*> l ;

	l.push_back(new int(100)) ;
	l.push_back(new int(200)) ;
	l.push_back(new int(300)) ;

	l.insert(++l.begin(), new int(400)) ;

	int arr[ ] = { 100, 400, 200, 300 } ;
	int i = 0 ;

  for(auto it : l) 
		ASSERT_CONDITION(arr[ i++ ] == *it);

	ASSERT_CONDITION(l.size() == 4) ;
  int* v = l.front();
	ASSERT_CONDITION(*v == 100) ;
	l.pop_front();
	delete v ;
	ASSERT_CONDITION(l.size() == 3);
	ASSERT_CONDITION(*l.front() == 400);
	ASSERT_CONDITION(*l.back() == 300);

	i = 1;
  for(auto it = l.begin(); it != l.end();)
	{
		ASSERT_CONDITION(arr[ i++ ] == **it);
		delete *it;
    l.erase(it++);
	}

	ASSERT_CONDITION(l.size() == 0);
	return true ;
}
Пример #8
0
// Moves to the closest target (appending the motion).
// Returns whether a motion was successfully found.
// Updates source to the target selected.
// Removes said target from targets.
bool BufferingPlayer::move_to_closest_target(Reference_point& source, Reference_point_vec& targets, Motion& motion) {
	TIMED_TRACE_ENTER("move_to_closest_target");
	CGAL_precondition(!env->get_target_configurations().empty());
	Ref_p_vec::iterator target_reached;
	int target_index;

	// Plan a motion to the closest remaining target
	bool path_found = planner.query_closest_point(
		source, 
		targets,
		target_index,
		// Append it to the output motion
		motion);
	ASSERT_CONDITION(path_found, "targets are connected but could not find path!");

	// Reached another target
	target_reached = targets.begin() + target_index;
	source = *target_reached;
	targets.erase(target_reached);

	TIMED_TRACE_EXIT("move_to_closest_target");
	return true;
}
Пример #9
0
void test_remove_node(const int size, const int node_index)
{
  // initialize the list
  Node<int> *head = new Node<int>;
  head->next = NULL; head->data = 0;

  Node<int> *curr = head;
  for (int i = 1; i < size; ++i) {
    curr->next = new Node<int>;
    curr = curr->next;
    curr->next = NULL; curr->data = i;
  }

  // get the node we want to delete
  curr = head;
  for (int i = 0; i < node_index; ++i)
    curr = curr->next;

  // run the algorithm
  remove_node(curr);

  // check data values
  curr = head;
  for (int i = 0; i < size; ++i) {
    if (i != node_index) {
      ASSERT_CONDITION(curr->data == i, "verify node values");
      curr = curr->next;
    }
  }

  // cleanup
  for (int i = 0; i < size - 1; ++i) {
    curr = head;
    head = head->next;
    delete curr;
  }
}
Пример #10
0
int main()
{
  ASSERT_CONDITION(compute_parity_linear_in_bits(0) == 0, "linear in number of bits");
  ASSERT_CONDITION(compute_parity_linear_in_bits(1) == 1, "linear in number of bits");
  ASSERT_CONDITION(compute_parity_linear_in_bits(2) == 1, "linear in number of bits");
  ASSERT_CONDITION(compute_parity_linear_in_bits(3) == 0, "linear in number of bits");
  ASSERT_CONDITION(compute_parity_linear_in_bits(4) == 1, "linear in number of bits");
  ASSERT_CONDITION(compute_parity_linear_in_bits(5) == 0, "linear in number of bits");
  ASSERT_CONDITION(compute_parity_linear_in_bits(6) == 0, "linear in number of bits");
  ASSERT_CONDITION(compute_parity_linear_in_bits(7) == 1, "linear in number of bits");

  ASSERT_CONDITION(compute_parity_linear_in_set_bits(8) == 1, "linear in number of set bits");
  ASSERT_CONDITION(compute_parity_linear_in_set_bits(9) == 0, "linear in number of set bits");
  ASSERT_CONDITION(compute_parity_linear_in_set_bits(10) == 0, "linear in number of set bits");
  ASSERT_CONDITION(compute_parity_linear_in_set_bits(11) == 1, "linear in number of set bits");
  ASSERT_CONDITION(compute_parity_linear_in_set_bits(12) == 0, "linear in number of set bits");
  ASSERT_CONDITION(compute_parity_linear_in_set_bits(13) == 1, "linear in number of set bits");
  ASSERT_CONDITION(compute_parity_linear_in_set_bits(14) == 1, "linear in number of set bits");
  ASSERT_CONDITION(compute_parity_linear_in_set_bits(15) == 0, "linear in number of set bits");

  precompute_parity();
  ASSERT_CONDITION(compute_parity_lookup(256) == 1, "lookup table");
  ASSERT_CONDITION(compute_parity_lookup(257) == 0, "lookup table");
  ASSERT_CONDITION(compute_parity_lookup(258) == 0, "lookup table");
  ASSERT_CONDITION(compute_parity_lookup(259) == 1, "lookup table");
  ASSERT_CONDITION(compute_parity_lookup(260) == 0, "lookup table");
  ASSERT_CONDITION(compute_parity_lookup(261) == 1, "lookup table");
  ASSERT_CONDITION(compute_parity_lookup(262) == 1, "lookup table");
  ASSERT_CONDITION(compute_parity_lookup(263) == 0, "lookup table");

  return 0;
}
Пример #11
0
int main()
{
  std::cerr << "Running Test for Disjoint Set..." << std::endl;

  DisjointSet<int> test;
  for (int i = 0; i < 10; ++i)
    test.add(i);

  for (int i = 0; i < 9; ++i) {
    for (int j = i + 1; j < 10; ++j) {
      ASSERT_CONDITION(test.isConnected(i, j) == false, "No connectivity check");
    }
  }

  ASSERT_CONDITION(test.getNumElements() == 10, "Number of elements check");
  ASSERT_CONDITION(test.getNumSets() == 10, "Number of sets check");

  test.merge(1, 2);
  test.merge(1, 5);
  test.merge(2, 6);
  test.merge(8, 5);
  test.merge(3, 4);
  test.merge(9, 7);

  ASSERT_CONDITION(test.getNumElements() == 10, "Number of elements (after merge) check");
  ASSERT_CONDITION(test.getNumSets() == 4, "Number of sets (after merge) check");

  ASSERT_CONDITION(test.isConnected(1, 2) == true, "Connectivity (after merge) check");
  ASSERT_CONDITION(test.isConnected(1, 5) == true, "Connectivity (after merge) check");
  ASSERT_CONDITION(test.isConnected(1, 6) == true, "Connectivity (after merge) check");
  ASSERT_CONDITION(test.isConnected(1, 8) == true, "Connectivity (after merge) check");
  ASSERT_CONDITION(test.isConnected(3, 4) == true, "Connectivity (after merge) check");
  ASSERT_CONDITION(test.isConnected(7, 9) == true, "Connectivity (after merge) check");
  ASSERT_CONDITION(test.isConnected(0, 1) == false, "Connectivity (after merge) check");
  ASSERT_CONDITION(test.isConnected(0, 3) == false, "Connectivity (after merge) check");
  ASSERT_CONDITION(test.isConnected(0, 7) == false, "Connectivity (after merge) check");

  return 0;
}
Пример #12
0
int main()
{
  std::unordered_map<unsigned int, unsigned long long> cache;
  cache[1] = 1;
  cache[2] = 2;
  cache[3] = 4;

  ASSERT_CONDITION(climb_stairs_with_cache(4, cache) == 7, "with cache");
  ASSERT_CONDITION(climb_stairs_with_cache(5, cache) == 13, "with cache");
  ASSERT_CONDITION(climb_stairs_with_cache(6, cache) == 24, "with cache");
  ASSERT_CONDITION(climb_stairs_with_cache(7, cache) == 44, "with cache");
  ASSERT_CONDITION(climb_stairs_with_cache(8, cache) == 81, "with cache");
  ASSERT_CONDITION(climb_stairs_with_cache(25, cache) == 2555757, "with cache");
  ASSERT_CONDITION(climb_stairs_with_cache(30, cache) == 53798080, "with cache");
  ASSERT_CONDITION(climb_stairs_with_cache(35, cache) == 1132436852, "with cache");
  ASSERT_CONDITION(climb_stairs_with_cache(50, cache) == 10562230626642, "with cache");
  ASSERT_CONDITION(climb_stairs_with_cache(100, cache) == 7367864567128947527, "with cache");

  ASSERT_CONDITION(climb_stairs(4) == 7, "no cache");
  ASSERT_CONDITION(climb_stairs(5) == 13, "no cache");
  ASSERT_CONDITION(climb_stairs(6) == 24, "no cache");
  ASSERT_CONDITION(climb_stairs(7) == 44, "no cache");
  ASSERT_CONDITION(climb_stairs(8) == 81, "no cache");
  ASSERT_CONDITION(climb_stairs(25) == 2555757, "no cache");
  ASSERT_CONDITION(climb_stairs(30) == 53798080, "no cache");
  ASSERT_CONDITION(climb_stairs(35) == 1132436852, "no cache");

  return 0;
}