From bc9b3f6ff1fcc5caa67c07ad99f971c0faacf91a Mon Sep 17 00:00:00 2001 From: James Turner Date: Fri, 14 Oct 2011 21:57:34 +0100 Subject: [PATCH] Unit test for SGBinObj, and fix a bug in large-indice handling the test revealed. --- simgear/io/CMakeLists.txt | 12 +- simgear/io/sg_binobj.cxx | 4 +- simgear/io/test_binobj.cxx | 257 +++++++++++++++++++++++++++++++++++++ 3 files changed, 271 insertions(+), 2 deletions(-) create mode 100644 simgear/io/test_binobj.cxx diff --git a/simgear/io/CMakeLists.txt b/simgear/io/CMakeLists.txt index 01560b39..84acd3f0 100644 --- a/simgear/io/CMakeLists.txt +++ b/simgear/io/CMakeLists.txt @@ -65,4 +65,14 @@ target_link_libraries(decode_binobj ${WINSOCK_LIBRARY} ${ZLIB_LIBRARY} ${RT_LIBRARY}) - \ No newline at end of file + +add_executable(test_binobj test_binobj.cxx) +target_link_libraries(test_binobj + sgio sgbucket sgstructure sgthreads sgtiming sgmisc sgdebug + ${CMAKE_THREAD_LIBS_INIT} + ${WINSOCK_LIBRARY} + ${ZLIB_LIBRARY} + ${RT_LIBRARY}) + +add_test(binobj ${EXECUTABLE_OUTPUT_PATH}/test_binobj) + \ No newline at end of file diff --git a/simgear/io/sg_binobj.cxx b/simgear/io/sg_binobj.cxx index e01a5286..54f3b8de 100644 --- a/simgear/io/sg_binobj.cxx +++ b/simgear/io/sg_binobj.cxx @@ -752,7 +752,9 @@ bool SGBinObject::write_bin_file(const SGPath& file) cout << "tex coords = " << texcoords.size() << endl; version = 10; - if (wgs84_nodes.size() < 0xffff) { + if ((wgs84_nodes.size() < 0xffff) && + (normals.size() < 0xffff) && + (texcoords.size() < 0xffff)) { version = 7; // use smaller indices if possible } diff --git a/simgear/io/test_binobj.cxx b/simgear/io/test_binobj.cxx new file mode 100644 index 00000000..20c7ad33 --- /dev/null +++ b/simgear/io/test_binobj.cxx @@ -0,0 +1,257 @@ + +#ifdef HAVE_CONFIG_H +# include +#endif + +#include + +#include +#include +#include + +#include + +#include "sg_binobj.hxx" + +using std::cout; +using std::cerr; +using std::endl; +using std::string; + +#define COMPARE(a, b) \ + if ((a) != (b)) { \ + cerr << "failed:" << #a << " != " << #b << endl; \ + cerr << "\tgot:" << a << endl; \ + exit(1); \ + } + +#define VERIFY(a) \ + if (!(a)) { \ + cerr << "failed:" << #a << endl; \ + exit(1); \ + } + +void generate_points(int count, std::vector& vec) +{ + for (int i=0; i& vec) +{ + for (int i=0; i& vec) +{ + for (int i=0; i& b) +{ + for (unsigned int i=1; i points; + generate_points(1024, points); + std::vector normals; + generate_normals(1024, normals); + std::vector texCoords; + generate_tcs(10000, texCoords); + + basic.set_wgs84_nodes(points); + basic.set_normals(normals); + basic.set_texcoords(texCoords); + + bool ok = basic.write_bin_file(path); + VERIFY( ok ); + + SGBinObject rd; + ok = rd.read_bin(path.str()) ; + VERIFY( ok); + COMPARE(rd.get_version(), 7); // should be version 7 since indices are < 2^16 + COMPARE(rd.get_gbs_center(), center); + COMPARE(rd.get_gbs_radius(), 12345); + COMPARE(rd.get_wgs84_nodes().size(), points.size()); + + comparePoints(rd, points); + compareTexCoords(rd, texCoords); +} + +void test_many_tcs() +{ + SGBinObject basic; + SGPath path(simgear::Dir::current().file("many_tex.btg.gz")); + + SGVec3d center(1, 2, 3); + basic.set_gbs_center(center); + basic.set_gbs_radius(12345); + + std::vector points; + generate_points(10000, points); + std::vector normals; + generate_normals(1024, normals); + std::vector texCoords; + generate_tcs(100000, texCoords); + + basic.set_wgs84_nodes(points); + basic.set_normals(normals); + basic.set_texcoords(texCoords); + + generate_tris(basic, 20000); + + bool ok = basic.write_bin_file(path); + VERIFY( ok ); + + SGBinObject rd; + ok = rd.read_bin(path.str()) ; + VERIFY( ok); + COMPARE(rd.get_version(), 10); // should be version 10 since indices are > 2^16 + COMPARE(rd.get_wgs84_nodes().size(), points.size()); + COMPARE(rd.get_texcoords().size(), texCoords.size()); + + comparePoints(rd, points); + compareTexCoords(rd, texCoords); + compareTris(basic, rd); +} + +void test_big() +{ + SGBinObject basic; + SGPath path(simgear::Dir::current().file("big.btg.gz")); + + SGVec3d center(1, 2, 3); + basic.set_gbs_center(center); + basic.set_gbs_radius(12345); + + std::vector points; + generate_points(200000, points); + std::vector normals; + generate_normals(1024, normals); + std::vector texCoords; + generate_tcs(300000, texCoords); + + basic.set_wgs84_nodes(points); + basic.set_normals(normals); + basic.set_texcoords(texCoords); + + generate_tris(basic, 200000); + + bool ok = basic.write_bin_file(path); + VERIFY( ok ); + + SGBinObject rd; + ok = rd.read_bin(path.str()) ; + VERIFY( ok); + COMPARE(rd.get_version(), 10); // should be version 10 since indices are > 2^16 + COMPARE(rd.get_wgs84_nodes().size(), points.size()); + COMPARE(rd.get_texcoords().size(), texCoords.size()); + + comparePoints(rd, points); + compareTexCoords(rd, texCoords); + compareTris(basic, rd); +} + +int main(int argc, char* argv[]) +{ + test_empty(); + test_basic(); + test_many_tcs(); + test_big(); + + return 0; +} -- 2.39.5