[Liblas-commits] hg-main-tree: added run_command(), for quick unit tests of cmd l...

liblas-commits at liblas.org liblas-commits at liblas.org
Thu Aug 11 21:00:30 EDT 2011


details:   http://hg.libpc.orghg-main-tree/rev/ba0f4011686e
changeset: 1057:ba0f4011686e
user:      Michael P. Gerlek <mpg at flaxen.com>
date:      Thu Aug 11 18:00:00 2011 -0700
description:
added run_command(), for quick unit tests of cmd line apps
Subject: hg-main-tree: stubs for cmd line tests

details:   http://hg.libpc.orghg-main-tree/rev/8dc1ec49b08e
changeset: 1058:8dc1ec49b08e
user:      Michael P. Gerlek <mpg at flaxen.com>
date:      Thu Aug 11 18:00:15 2011 -0700
description:
stubs for cmd line tests

diffstat:

 test/unit/CMakeLists.txt     |   3 ++
 test/unit/Support.cpp        |  45 ++++++++++++++++++++++++++++++++++++++++
 test/unit/Support.hpp        |   5 ++++
 test/unit/SupportTest.cpp    |  18 ++++++++++++++++
 test/unit/pc2pcTest.cpp      |  49 ++++++++++++++++++++++++++++++++++++++++++++
 test/unit/pcinfoTest.cpp     |  49 ++++++++++++++++++++++++++++++++++++++++++++
 test/unit/pcpipelineTest.cpp |  49 ++++++++++++++++++++++++++++++++++++++++++++
 7 files changed, 218 insertions(+), 0 deletions(-)

diffs (265 lines):

diff -r f6000aa849f7 -r 8dc1ec49b08e test/unit/CMakeLists.txt
--- a/test/unit/CMakeLists.txt	Thu Aug 11 16:52:42 2011 -0700
+++ b/test/unit/CMakeLists.txt	Thu Aug 11 18:00:15 2011 -0700
@@ -12,6 +12,9 @@
 endif()
 
 SET(PDAL_UNITTEST_TEST_SRC
+    pc2pcTest.cpp
+    pcinfoTest.cpp
+    pcpipelineTest.cpp
     BoundsTest.cpp
     ByteSwapFilterTest.cpp
     CacheFilterTest.cpp
diff -r f6000aa849f7 -r 8dc1ec49b08e test/unit/Support.cpp
--- a/test/unit/Support.cpp	Thu Aug 11 16:52:42 2011 -0700
+++ b/test/unit/Support.cpp	Thu Aug 11 18:00:15 2011 -0700
@@ -311,3 +311,48 @@
     BOOST_CHECK_CLOSE(p.getMaximum(1), q.getMaximum(1), 1);
     BOOST_CHECK_CLOSE(p.getMaximum(2), q.getMaximum(2), 1);
 }
+
+
+// http://www.codepedia.com/1/CppStringReplace
+static std::string replaceAll(std::string result, 
+                              const std::string& replaceWhat, 
+                              const std::string& replaceWithWhat)
+{
+    while(1)
+    {
+        const int pos = result.find(replaceWhat);
+        if (pos==-1) break;
+        result.replace(pos,replaceWhat.size(),replaceWithWhat);
+    }
+    return result;
+}
+
+
+int Support::run_command(const std::string& rawcmd, std::string& output)
+{
+    const int maxbuf = 4096;
+    char buf[maxbuf];
+
+    const std::string cmd = replaceAll(rawcmd, "/", "\\");
+
+    output = "";
+    
+    FILE* fp = _popen(cmd.c_str(), "r");
+    while (!feof(fp))
+    {
+        if (fgets(buf, maxbuf, fp) == NULL)
+        {
+            if (feof(fp)) break;
+
+            if (ferror(fp))
+            {
+                throw std::runtime_error("error executing command");
+            }
+        }
+
+        output += buf;
+    }
+
+    int stat = _pclose(fp);
+    return stat;
+}
diff -r f6000aa849f7 -r 8dc1ec49b08e test/unit/Support.hpp
--- a/test/unit/Support.hpp	Thu Aug 11 16:52:42 2011 -0700
+++ b/test/unit/Support.hpp	Thu Aug 11 18:00:15 2011 -0700
@@ -96,6 +96,11 @@
     static void check_p710_p711_p712(const pdal::PointBuffer& data, const pdal::Schema& schema);
 
     static void compareBounds(const pdal::Bounds<double>& p, const pdal::Bounds<double>& q);
+
+    // executes "cmd" via popen, copying stdout into output and returning the status code
+    //
+    // note: under windows, all "/" characrters in cmd will be converted to "\\" for you
+    static int run_command(const std::string& cmd, std::string& output);
 };
 
 
diff -r f6000aa849f7 -r 8dc1ec49b08e test/unit/SupportTest.cpp
--- a/test/unit/SupportTest.cpp	Thu Aug 11 16:52:42 2011 -0700
+++ b/test/unit/SupportTest.cpp	Thu Aug 11 18:00:15 2011 -0700
@@ -207,4 +207,22 @@
     return;
 }
 
+
+BOOST_AUTO_TEST_CASE(test_run_command)
+{
+    //const char* cmd = "../../bin/Debug/pcinfo ../data/simple.las";
+    
+    // amazingly, this command works under both dos *and* unix shells
+    const char* cmd = "echo foo";
+
+    std::string output;
+    const int stat = Support::run_command(cmd, output);
+
+    BOOST_CHECK_EQUAL(output.substr(0, 3), "foo");
+    BOOST_CHECK_EQUAL(stat, 0);
+
+    return;
+}
+
+
 BOOST_AUTO_TEST_SUITE_END()
diff -r f6000aa849f7 -r 8dc1ec49b08e test/unit/pc2pcTest.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/unit/pc2pcTest.cpp	Thu Aug 11 18:00:15 2011 -0700
@@ -0,0 +1,49 @@
+/******************************************************************************
+* Copyright (c) 2011, Michael P. Gerlek (mpg at flaxen.com)
+*
+* All rights reserved.
+*
+* Redistribution and use in source and binary forms, with or without
+* modification, are permitted provided that the following
+* conditions are met:
+*
+*     * Redistributions of source code must retain the above copyright
+*       notice, this list of conditions and the following disclaimer.
+*     * Redistributions in binary form must reproduce the above copyright
+*       notice, this list of conditions and the following disclaimer in
+*       the documentation and/or other materials provided
+*       with the distribution.
+*     * Neither the name of Hobu, Inc. or Flaxen Geo Consulting nor the
+*       names of its contributors may be used to endorse or promote
+*       products derived from this software without specific prior
+*       written permission.
+*
+* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
+* OF SUCH DAMAGE.
+****************************************************************************/
+
+#include <boost/test/unit_test.hpp>
+
+#include <iostream>
+#include <sstream>
+#include <string>
+
+
+BOOST_AUTO_TEST_SUITE(pc2pcTest)
+
+BOOST_AUTO_TEST_CASE(pc2pcTest_test1)
+{
+    return;
+}
+
+BOOST_AUTO_TEST_SUITE_END()
diff -r f6000aa849f7 -r 8dc1ec49b08e test/unit/pcinfoTest.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/unit/pcinfoTest.cpp	Thu Aug 11 18:00:15 2011 -0700
@@ -0,0 +1,49 @@
+/******************************************************************************
+* Copyright (c) 2011, Michael P. Gerlek (mpg at flaxen.com)
+*
+* All rights reserved.
+*
+* Redistribution and use in source and binary forms, with or without
+* modification, are permitted provided that the following
+* conditions are met:
+*
+*     * Redistributions of source code must retain the above copyright
+*       notice, this list of conditions and the following disclaimer.
+*     * Redistributions in binary form must reproduce the above copyright
+*       notice, this list of conditions and the following disclaimer in
+*       the documentation and/or other materials provided
+*       with the distribution.
+*     * Neither the name of Hobu, Inc. or Flaxen Geo Consulting nor the
+*       names of its contributors may be used to endorse or promote
+*       products derived from this software without specific prior
+*       written permission.
+*
+* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
+* OF SUCH DAMAGE.
+****************************************************************************/
+
+#include <boost/test/unit_test.hpp>
+
+#include <iostream>
+#include <sstream>
+#include <string>
+
+
+BOOST_AUTO_TEST_SUITE(pcinfoTest)
+
+BOOST_AUTO_TEST_CASE(pcinfoTest_1)
+{
+      return;
+}
+
+BOOST_AUTO_TEST_SUITE_END()
diff -r f6000aa849f7 -r 8dc1ec49b08e test/unit/pcpipelineTest.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/unit/pcpipelineTest.cpp	Thu Aug 11 18:00:15 2011 -0700
@@ -0,0 +1,49 @@
+/******************************************************************************
+* Copyright (c) 2011, Michael P. Gerlek (mpg at flaxen.com)
+*
+* All rights reserved.
+*
+* Redistribution and use in source and binary forms, with or without
+* modification, are permitted provided that the following
+* conditions are met:
+*
+*     * Redistributions of source code must retain the above copyright
+*       notice, this list of conditions and the following disclaimer.
+*     * Redistributions in binary form must reproduce the above copyright
+*       notice, this list of conditions and the following disclaimer in
+*       the documentation and/or other materials provided
+*       with the distribution.
+*     * Neither the name of Hobu, Inc. or Flaxen Geo Consulting nor the
+*       names of its contributors may be used to endorse or promote
+*       products derived from this software without specific prior
+*       written permission.
+*
+* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
+* OF SUCH DAMAGE.
+****************************************************************************/
+
+#include <boost/test/unit_test.hpp>
+
+#include <iostream>
+#include <sstream>
+#include <string>
+
+
+BOOST_AUTO_TEST_SUITE(pcpipelineTest)
+
+BOOST_AUTO_TEST_CASE(pcpipelineTest_1)
+{
+      return;
+}
+
+BOOST_AUTO_TEST_SUITE_END()


More information about the Liblas-commits mailing list