Example #1
0
/*
****Test to assess the quality of the Add member of the Stats
****class which is designed to add a gpstk::Vector or an individual 
****sample to an instance of Stats

**** This test partially indirectly tests Minimum, Maximum
**** Average,Variance,StdDev which are all memebers of the
**** Stats class
*/
void xStats :: addTest (void)
{
	gpstk::Stats<double> addTest;
	CPPUNIT_ASSERT_NO_THROW(addTest.Add(10.,1));
	
	addTest.Add(20.,1);
	addTest.Add(30.,1);
	//Create a vector of 1 member with a value of 40.0
	gpstk::Vector<double> one(1,40.);
	//Create a vector with one member with a value of 1
	gpstk::Vector<double> weight(1,1);
	
	
	CPPUNIT_ASSERT_NO_THROW(addTest.Add(one,weight));
	
	CPPUNIT_ASSERT_EQUAL((unsigned) 4, addTest.N());
	CPPUNIT_ASSERT_EQUAL(10., addTest.Minimum());
	CPPUNIT_ASSERT_EQUAL(40., addTest.Maximum());
	CPPUNIT_ASSERT_DOUBLES_EQUAL(25., addTest.Average(),1E-6);
	CPPUNIT_ASSERT_DOUBLES_EQUAL(166.66667, addTest.Variance(),1E-3);
	CPPUNIT_ASSERT_DOUBLES_EQUAL(12.9099445, addTest.StdDev(),1E-6);
	CPPUNIT_ASSERT_EQUAL(4., addTest.Normalization());
	CPPUNIT_ASSERT_EQUAL(true, addTest.Weighted());
	
	gpstk::Stats<double>  addTest0;
	gpstk::Vector<double> weight0(0,1);
	addTest0.Add(10.,0);
	addTest0.Add(20.,0);
	addTest0.Add(30.,0);
	addTest0.Add(one,weight0);
	
	CPPUNIT_ASSERT_EQUAL((unsigned) 4, addTest0.N());
	CPPUNIT_ASSERT_EQUAL(10., addTest0.Minimum());
	CPPUNIT_ASSERT_EQUAL(40., addTest0.Maximum());
	CPPUNIT_ASSERT_DOUBLES_EQUAL(25., addTest0.Average(),1E-6);
	CPPUNIT_ASSERT_DOUBLES_EQUAL(166.66667, addTest0.Variance(),1E-3);
	CPPUNIT_ASSERT_DOUBLES_EQUAL(12.9099445, addTest0.StdDev(),1E-6);
	CPPUNIT_ASSERT_EQUAL(0., addTest0.Normalization());
	CPPUNIT_ASSERT_EQUAL(false, addTest0.Weighted());
	
	
	gpstk::Vector<double> two(4,20.);
	gpstk::Vector<double> weight2(1,0);
	try
	{
		CPPUNIT_ASSERT_THROW(addTest.Add(two,weight2),gpstk::Exception);
	}
	catch(gpstk::Exception& e)
	{
	}
	
}
int main(int, char *[])
{
  typedef boost::property<boost::edge_weight_t, float> EdgeWeightProperty;
  
  typedef boost::adjacency_list < boost::listS, boost::vecS, boost::directedS,
    boost::no_property, EdgeWeightProperty > Graph;
  
  typedef boost::graph_traits < Graph >::vertex_descriptor vertex_descriptor;
  typedef boost::graph_traits < Graph >::edge_descriptor edge_descriptor;
  typedef std::pair<int, int> Edge;

  // Create a graph
  Graph g;
  
  boost::graph_traits<Graph>::vertex_descriptor v0 = add_vertex(g);
  boost::graph_traits<Graph>::vertex_descriptor v1 = add_vertex(g);
  boost::graph_traits<Graph>::vertex_descriptor v2 = add_vertex(g);

  // Add weighted edges
  EdgeWeightProperty weight0(5);
  add_edge(v0, v1, weight0, g);

  EdgeWeightProperty weight1 = 3;
  add_edge(v1, v2, weight1, g);
  
  EdgeWeightProperty weight2 = 2;
  add_edge(v2, v0, weight2, g);
  
  // At this point the graph is
  /*   v0
       .
   5  / \ 2
     /___\
    v1 3 v2
  */

  // Create things for Dijkstra
  std::vector<vertex_descriptor> parents(num_vertices(g)); // To store parents
  std::vector<int> distances(num_vertices(g)); // To store distances

  // Compute shortest paths from v0 to all vertices, and store the output in parents and distances
  boost::dijkstra_shortest_paths(g, v0, boost::predecessor_map(&parents[0]).distance_map(&distances[0]));

  // Output results
  std::cout << "distances and parents:" << std::endl;
  boost::graph_traits < Graph >::vertex_iterator vertexIterator, vend;
  for (boost::tie(vertexIterator, vend) = vertices(g); vertexIterator != vend; ++vertexIterator) 
  {
    std::cout << "distance(" << *vertexIterator << ") = " << distances[*vertexIterator] << ", ";
    std::cout << "parent(" << *vertexIterator << ") = " << parents[*vertexIterator] << std::endl;
  }
  std::cout << std::endl;
  
  /*
  The output is:
  distance(0) = 0, parent(0) = 0
  distance(1) = 5, parent(1) = 0
  distance(2) = 8, parent(2) = 1
  
  which means:
  the distance from v0 to v0 is 0
  the distance from v0 to v1 is 5
  the distance from v0 to v2 is 8
  */
  return EXIT_SUCCESS;
}