File: GEOSContextTest.cpp

package info (click to toggle)
geos 3.14.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 31,212 kB
  • sloc: cpp: 199,103; xml: 56,065; ansic: 6,162; sh: 287; makefile: 26
file content (104 lines) | stat: -rw-r--r-- 2,491 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <tut/tut.hpp>
// geos
#include <geos_c.h>

#include "capi_test_utils.h"

namespace tut {
//
// Test Group
//

struct test_geoscontext_data : public capitest::utility {

        static void count_notice(const char* fmt, ...)
        {
            (void) fmt;
            num_notice++;
        }

        static void count_error(const char* fmt, ...)
        {
            (void) fmt;
            num_error++;
        }

        void provokeError(GEOSContextHandle_t& context) {
            GEOSGeom_createEmptyCollection_r(context, 999999); // Produce an error
        }

        void provokeNotice(GEOSContextHandle_t& context) {
            GEOSGeometry* g = fromWKT("POLYGON ((0 0, 1 0, 0 1, 1 1, 0 0))");
            GEOSisValid_r(context, g); // Produce a notice
            GEOSGeom_destroy(g);
        }

        static int num_notice;
        static int num_error;
};

int test_geoscontext_data::num_notice = 0;
int test_geoscontext_data::num_error = 0;

typedef test_group<test_geoscontext_data> group;
typedef group::object object;

group test_geoscontext("capi::GEOSContext");

// Test "new" style error and notice handlers
template<>
template<>
void object::test<1>()
{
    GEOSContextHandle_t context = GEOS_init_r();

    std::string errorMsg;
    std::string noticeMsg;

    GEOSContext_setErrorMessageHandler_r(context, [](const char* message, void* userdata) {
        (void) message;
        std::string& msg = *static_cast<std::string*>(userdata);
        msg.append("error");
    }, &errorMsg);

    GEOSContext_setNoticeMessageHandler_r(context, [](const char* message, void* userdata) {
        (void) message;
        std::string& msg = *static_cast<std::string*>(userdata);
        msg.append("notice");
    }, &noticeMsg);

    provokeError(context);
    provokeNotice(context);

    ensure_equals(errorMsg, "error");
    ensure_equals(noticeMsg, "notice");

    finishGEOS_r(context);
}

// Test "old" style error and notice handlers
template<>
template<>
void object::test<2>()
{
    GEOSContextHandle_t context = GEOS_init_r();

    GEOSContext_setErrorHandler_r(context, count_error);
    GEOSContext_setNoticeHandler_r(context, count_notice);

    ensure_equals(num_error, 0);
    ensure_equals(num_notice, 0);

    provokeError(context);
    ensure_equals(num_error, 1);
    ensure_equals(num_notice, 0);

    provokeNotice(context);
    ensure_equals(num_error, 1);
    ensure_equals(num_notice, 1);

    finishGEOS_r(context);
}

} // namespace tut