title: C++, shared library, "unit tests" in main function
body: <p><strong>Background:</strong> I have a complicated application that I have inherited and am extending (graduate research project). I want to make sure my re-organizations have a positive long term effect on maintainability and usability -- in other words I want to make sure I set things up in as standard a way as possible for future people who might work on the code. I do not have time, nor is it my place, to <em>completely</em> re-structure the application.</p>
  
  <p>The application is an ecosystem model. It consists of a shared library written in C++ that does the heavy lifting. The library is included in a Java application that "drives" the simulation -- handles some database I/O and provides a GUI.</p>
  
  <p>In the course of my development, I have added a number of utility functions to the project, mostly for convenient printing of internal variables to the console (directly from the C++ portions of the code) during run-time.</p>
  
  <p>I am now factoring these functions out into a utility file (header and cpp) that is included in other files as needed. I put the functions in their own namespace following an example here: <a href="https://stackoverflow.com/questions/8226489/organising-utility-functions-in-c">Organising utility functions in C++</a></p>
  
  <p><strong>Question:</strong> If I want to write some tests for the utility functions so that I can develop and experiment w/o re-compiling and modifying/running the entire model, where and how should these tests be best included?</p>
  
  <p>Will it be a problem if I have a main() function in the util.cpp file? To my surprise, I tried this and it works; I can compile and run the util.cpp file independently. Also the primary application which includes util.cpp still compiles and runs fine. I was surprised because I thought the presence of a second main() would be a problem -- although the application entry point is in the java code. </p>
  
  <p>However I am not sure if this is the best route; I don't have enough experience to see future pitfalls with this tactic.</p>
  
  <p>Here is a short example of the my util.cpp file:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>#include "util.hpp"
  #include &lt;iostream&gt;
  #include &lt;vector&gt;
  namespace util {
    /** Prints a std::vector of doubles in a format that can be 
    * copied directly into a python console. */
    void util::pyprint_vec(const std::vector&lt;double&gt; &amp; v){
      std::cout &lt;&lt; "[";
      for(std::vector&lt;double&gt;::const_iterator it = v.begin(); it != v.end(); ++it){
        std::cout &lt;&lt; *it &lt;&lt; ", ";
      } 
      std::cout &lt;&lt; "\b\b]"; // remove the last comma
    }
  }
  
  int main() {
    using namespace util;
    using namespace std;
  
    cout &lt;&lt; "Testing some of the utility functions...\n";
    vector&lt;double&gt; a_vec(50,12.0);
    pyprint_vec(a_vec);
    cout &lt;&lt; endl;
  
    return 0;
  }
  </code></pre>
  
  <p>Ultimately I envision templating some of the functions (at which point they actually move to util.hpp) and adding to the file as well as being able to use it in other projects.
  Thanks in advance for any advice.</p>