LCOV - code coverage report
Current view: top level - autotest/cpp - gdal_unit_test.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 66 94 70.2 %
Date: 2024-11-21 22:18:42 Functions: 2 2 100.0 %

          Line data    Source code
       1             : ///////////////////////////////////////////////////////////////////////////////
       2             : //
       3             : // Project:  C++ Test Suite for GDAL/OGR
       4             : // Purpose:  Main program of C++ Unit Tests runner for GDAL
       5             : // Author:   Mateusz Loskot <mateusz@loskot.net>
       6             : //
       7             : ///////////////////////////////////////////////////////////////////////////////
       8             : // Copyright (c) 2006, Mateusz Loskot <mateusz@loskot.net>
       9             : /*
      10             :  * SPDX-License-Identifier: MIT
      11             :  ****************************************************************************/
      12             : 
      13             : #ifdef _MSC_VER
      14             : #define WIN32_LEAN_AND_MEAN
      15             : #endif  // _MSC_VER
      16             : 
      17             : #include "gdal_unit_test.h"
      18             : 
      19             : #include "cpl_conv.h"
      20             : #include "cpl_multiproc.h"
      21             : #include "gdal.h"
      22             : #include "ogr_api.h"
      23             : #include "ogrsf_frmts.h"
      24             : #include "test_data.h"
      25             : 
      26             : #include <algorithm>
      27             : #include <iostream>
      28             : #include <string>
      29             : 
      30             : #include "gtest_include.h"
      31             : 
      32             : namespace tut
      33             : {
      34             : // Common test data path
      35             : std::string const common::data_basedir(TUT_ROOT_DATA_DIR);
      36             : std::string const common::tmp_basedir(TUT_ROOT_TMP_DIR);
      37             : 
      38             : ::testing::AssertionResult
      39          34 : CheckEqualGeometries(OGRGeometryH lhs, OGRGeometryH rhs, double tolerance)
      40             : {
      41             :     // Test raw pointers
      42          34 :     if (nullptr == lhs)
      43             :     {
      44           0 :         return ::testing::AssertionFailure() << "lhs is null";
      45             :     }
      46          34 :     if (nullptr == rhs)
      47             :     {
      48           0 :         return ::testing::AssertionFailure() << "rhs is null";
      49             :     }
      50             : 
      51             :     // Test basic properties
      52          34 :     if (strcmp(OGR_G_GetGeometryName(lhs), OGR_G_GetGeometryName(rhs)) != 0)
      53             :     {
      54           0 :         return ::testing::AssertionFailure()
      55           0 :                << "OGR_G_GetGeometryName(lhs) = " << OGR_G_GetGeometryName(lhs)
      56           0 :                << ". OGR_G_GetGeometryName(rhs) = "
      57           0 :                << OGR_G_GetGeometryName(rhs);
      58             :     }
      59             : 
      60          34 :     if (OGR_G_GetGeometryCount(lhs) != OGR_G_GetGeometryCount(rhs))
      61             :     {
      62           0 :         return ::testing::AssertionFailure()
      63           0 :                << "OGR_G_GetGeometryCount(lhs) = "
      64           0 :                << OGR_G_GetGeometryCount(lhs)
      65           0 :                << ". OGR_G_GetGeometryCount(rhs) = "
      66           0 :                << OGR_G_GetGeometryCount(rhs);
      67             :     }
      68             : 
      69          34 :     if (OGR_G_GetPointCount(lhs) != OGR_G_GetPointCount(rhs))
      70             :     {
      71           0 :         return ::testing::AssertionFailure()
      72           0 :                << "OGR_G_GetPointCount(lhs) = " << OGR_G_GetPointCount(lhs)
      73           0 :                << ". OGR_G_GetPointCount(rhs) = " << OGR_G_GetPointCount(rhs);
      74             :     }
      75             : 
      76          34 :     if (OGR_G_GetGeometryCount(lhs) > 0)
      77             :     {
      78             :         // Test sub-geometries recursively
      79          17 :         const int count = OGR_G_GetGeometryCount(lhs);
      80          36 :         for (int i = 0; i < count; ++i)
      81             :         {
      82             :             auto res =
      83             :                 CheckEqualGeometries(OGR_G_GetGeometryRef(lhs, i),
      84          19 :                                      OGR_G_GetGeometryRef(rhs, i), tolerance);
      85          19 :             if (!res)
      86           0 :                 return res;
      87             :         }
      88             :     }
      89             :     else
      90             :     {
      91           0 :         std::unique_ptr<OGRGeometry> lhs_normalized_cpp;
      92           0 :         std::unique_ptr<OGRGeometry> rhs_normalized_cpp;
      93             :         OGRGeometryH lhs_normalized;
      94             :         OGRGeometryH rhs_normalized;
      95          17 :         if (OGRGeometryFactory::haveGEOS())
      96             :         {
      97          17 :             if (EQUAL(OGR_G_GetGeometryName(lhs), "LINEARRING"))
      98             :             {
      99             :                 // Normalize() doesn't work with LinearRing
     100             :                 OGRLineString lhs_as_ls(
     101          30 :                     *OGRGeometry::FromHandle(lhs)->toLineString());
     102          15 :                 lhs_normalized_cpp.reset(lhs_as_ls.Normalize());
     103             :                 OGRLineString rhs_as_ls(
     104          30 :                     *OGRGeometry::FromHandle(rhs)->toLineString());
     105          15 :                 rhs_normalized_cpp.reset(rhs_as_ls.Normalize());
     106             :             }
     107             :             else
     108             :             {
     109           2 :                 lhs_normalized_cpp.reset(
     110             :                     OGRGeometry::FromHandle(OGR_G_Normalize(lhs)));
     111           2 :                 rhs_normalized_cpp.reset(
     112             :                     OGRGeometry::FromHandle(OGR_G_Normalize(rhs)));
     113             :             }
     114          17 :             lhs_normalized = OGRGeometry::ToHandle(lhs_normalized_cpp.get());
     115          17 :             rhs_normalized = OGRGeometry::ToHandle(rhs_normalized_cpp.get());
     116             :         }
     117             :         else
     118             :         {
     119           0 :             lhs_normalized = lhs;
     120           0 :             rhs_normalized = rhs;
     121             :         }
     122             : 
     123             :         // Test geometry points
     124          17 :         const std::size_t csize = 3;
     125          17 :         double a[csize] = {0};
     126          17 :         double b[csize] = {0};
     127          17 :         double d[csize] = {0};
     128          17 :         double dmax = 0;
     129             : 
     130          17 :         const int count = OGR_G_GetPointCount(lhs_normalized);
     131         285 :         for (int i = 0; i < count; ++i)
     132             :         {
     133         268 :             OGR_G_GetPoint(lhs_normalized, i, &a[0], &a[1], &a[2]);
     134         268 :             OGR_G_GetPoint(rhs_normalized, i, &b[0], &b[1], &b[2]);
     135             : 
     136             :             // Test vertices
     137        1072 :             for (std::size_t c = 0; c < csize; ++c)
     138             :             {
     139         804 :                 d[c] = std::fabs(a[c] - b[c]);
     140             :             }
     141             : 
     142         268 :             const double *pos = std::max_element(d, d + csize);
     143         268 :             dmax = *pos;
     144             : 
     145         268 :             if (dmax > tolerance)
     146             :             {
     147           0 :                 return ::testing::AssertionFailure()
     148           0 :                        << "dmax = " << dmax << " is > tolerance = " << tolerance
     149           0 :                        << " on vertex " << i;
     150             :             }
     151             :         }
     152             :     }
     153             : 
     154          34 :     return ::testing::AssertionSuccess();
     155             : }
     156             : 
     157             : }  // namespace tut
     158             : 
     159           1 : int main(int argc, char *argv[])
     160             : {
     161             : #if defined(PROJ_GRIDS_PATH) && defined(PROJ_DB_TMPDIR)
     162             :     // Look for proj.db in PROJ search paths, copy it in PROJ_DB_TMPDIR, and restrict
     163             :     // PROJ search paths to PROJ_DB_TMPDIR and PROJ_GRIDS_PATH
     164           1 :     VSIMkdir(PROJ_DB_TMPDIR, 0755);
     165             :     static char szProjNetworkOff[] = "PROJ_NETWORK=OFF";
     166           1 :     putenv(szProjNetworkOff);
     167           1 :     const CPLStringList aosPathsOri(OSRGetPROJSearchPaths());
     168           1 :     bool bFoundProjDB = false;
     169           1 :     for (int i = 0; i < aosPathsOri.size(); ++i)
     170             :     {
     171             :         VSIStatBufL sStat;
     172           1 :         if (VSIStatL(CPLFormFilename(aosPathsOri[i], "proj.db", nullptr),
     173           1 :                      &sStat) == 0)
     174             :         {
     175           1 :             CPLCopyFile(CPLFormFilename(PROJ_DB_TMPDIR, "proj.db", nullptr),
     176             :                         CPLFormFilename(aosPathsOri[i], "proj.db", nullptr));
     177           1 :             bFoundProjDB = true;
     178           1 :             break;
     179             :         }
     180             :     }
     181           1 :     if (bFoundProjDB)
     182             :     {
     183           2 :         CPLStringList aosPaths;
     184           1 :         aosPaths.AddString(PROJ_DB_TMPDIR);
     185           1 :         aosPaths.AddString(PROJ_GRIDS_PATH);
     186           1 :         OSRSetPROJSearchPaths(aosPaths.List());
     187             :     }
     188             : #endif
     189             : 
     190             :     // Register GDAL/OGR drivers
     191           1 :     ::GDALAllRegister();
     192           1 :     ::OGRRegisterAll();
     193             : 
     194             :     std::cout
     195             :         << "GDAL C/C++ API tests"
     196             :         << " (" << ::GDALVersionInfo("--version") << ")"
     197           1 :         << "\n---------------------------------------------------------\n";
     198             : 
     199           1 :     argc = GDALGeneralCmdLineProcessor(argc, &argv, 0);
     200             : 
     201             :     int nRetCode;
     202             :     try
     203             :     {
     204           1 :         testing::InitGoogleTest(&argc, argv);
     205             : 
     206           1 :         nRetCode = RUN_ALL_TESTS();
     207             :     }
     208           0 :     catch (const std::exception &e)
     209             :     {
     210           0 :         nRetCode = 1;
     211           0 :         fprintf(stderr, "Caught exception %s\n", e.what());
     212             :     }
     213           0 :     catch (...)
     214             :     {
     215           0 :         nRetCode = 1;
     216           0 :         fprintf(stderr, "Caught exception of unknown type\n");
     217             :     }
     218             : 
     219           1 :     CSLDestroy(argv);
     220           1 :     GDALDestroyDriverManager();
     221             : 
     222           1 :     GDALAllRegister();
     223           1 :     GDALDestroyDriverManager();
     224             : 
     225           1 :     OGRCleanupAll();
     226             : 
     227           1 :     CPLDumpSharedList(nullptr);
     228           1 :     CPLCleanupTLS();
     229             : 
     230           1 :     return nRetCode;
     231             : }

Generated by: LCOV version 1.14