File: cu_tester.c

package info (click to toggle)
pgpointcloud 1.2.5-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,892 kB
  • sloc: sql: 40,767; ansic: 11,045; xml: 935; makefile: 297; cpp: 282; perl: 248; python: 178; sh: 92
file content (242 lines) | stat: -rw-r--r-- 7,612 bytes parent folder | download | duplicates (3)
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/***********************************************************************
 * cu_tester.c
 *
 *        Testing harness for PgSQL PointClouds
 *
 * Portions Copyright (c) 2012, OpenGeo
 *
 ***********************************************************************/

#include "cu_tester.h"
#include "CUnit/Basic.h"
#include <stdio.h>
#include <string.h>

/* Contains the most recent error message generated by rterror. */
static char cu_error_msg[MAX_CUNIT_MSG_LENGTH + 1];

/* ADD YOUR SUITE HERE (1 of 2) */
extern CU_SuiteInfo schema_suite;
extern CU_SuiteInfo patch_suite;
extern CU_SuiteInfo point_suite;
extern CU_SuiteInfo bytes_suite;
extern CU_SuiteInfo lazperf_suite;
extern CU_SuiteInfo sort_suite;
extern CU_SuiteInfo util_suite;

/**
 * CUnit error handler
 * Log message in a global var instead of printing in stdout/stderr
 *
 * CAUTION: Not stop execution on pcerror case !!!
 */
static void cu_message_handler(const char *fmt, va_list ap)
{
  vsnprintf(cu_error_msg, MAX_CUNIT_MSG_LENGTH - 1, fmt, ap);
  cu_error_msg[MAX_CUNIT_MSG_LENGTH - 1] = '\0';
  va_end(ap);
}

void cu_error_msg_reset() { memset(cu_error_msg, '\0', MAX_CUNIT_MSG_LENGTH); }

/*
** The main() function for setting up and running the tests.
** Returns a CUE_SUCCESS on successful running, another
** CUnit error code on failure.
*/
int main(int argc, char *argv[])
{
  /* ADD YOUR SUITE HERE (2 of 2) */
  CU_SuiteInfo suites[] = {schema_suite, patch_suite,       point_suite,
                           bytes_suite,  lazperf_suite,     sort_suite,
                           util_suite,   CU_SUITE_INFO_NULL};

  int index;
  char *suite_name;
  CU_pSuite suite_to_run;
  char *test_name;
  CU_pTest test_to_run;
  CU_ErrorCode errCode = 0;
  CU_pTestRegistry registry;
  int num_run;
  int num_failed;

  /* Set up to use the system memory management / logging */
  pc_install_default_handlers();

  pc_set_handlers(0, 0, 0, cu_message_handler, cu_message_handler,
                  cu_message_handler);

  /* initialize the CUnit test registry */
  if (CUE_SUCCESS != CU_initialize_registry())
  {
    errCode = CU_get_error();
    printf("    Error attempting to initialize registry: %d.  See CUError.h "
           "for error code list.\n",
           errCode);
    return errCode;
  }

  /* Register all the test suites. */
  if (CUE_SUCCESS != CU_register_suites(suites))
  {
    errCode = CU_get_error();
    printf("    Error attempting to register test suites: %d.  See CUError.h "
           "for error code list.\n",
           errCode);
    return errCode;
  }

  /* Run all tests using the CUnit Basic interface */
  CU_basic_set_mode(CU_BRM_VERBOSE);
  if (argc <= 1)
  {
    errCode = CU_basic_run_tests();
  }
  else
  {
    /* NOTE: The cunit functions used here (CU_get_registry,
     * CU_get_suite_by_name, and CU_get_test_by_name) are listed with the
     * following warning: "Internal CUnit system functions.  Should not be
     * routinely called by users." However, there didn't seem to be any
     * other way to get tests by name, so we're calling them. */
    registry = CU_get_registry();
    for (index = 1; index < argc; index++)
    {
      suite_name = argv[index];
      test_name = NULL;
      suite_to_run = CU_get_suite_by_name(suite_name, registry);
      if (NULL == suite_to_run)
      {
        /* See if it's a test name instead of a suite name. */
        suite_to_run = registry->pSuite;
        while (suite_to_run != NULL)
        {
          test_to_run = CU_get_test_by_name(suite_name, suite_to_run);
          if (test_to_run != NULL)
          {
            /* It was a test name. */
            test_name = suite_name;
            suite_name = suite_to_run->pName;
            break;
          }
          suite_to_run = suite_to_run->pNext;
        }
      }
      if (suite_to_run == NULL)
      {
        printf("\n'%s' does not appear to be either a suite name or a "
               "test "
               "name.\n\n",
               suite_name);
      }
      else
      {
        if (test_name != NULL)
        {
          /* Run only this test. */
          printf("\nRunning test '%s' in suite '%s'.\n", test_name, suite_name);
          /* This should be CU_basic_run_test, but that method is
           * broken, see:
           *     https://sourceforge.net/tracker/?func=detail&aid=2851925&group_id=32992&atid=407088
           * This one doesn't output anything for success, so we have
           * to do it manually. */
          errCode = CU_run_test(suite_to_run, test_to_run);
          if (errCode != CUE_SUCCESS)
          {
            printf("    Error attempting to run tests: %d.  See "
                   "CUError.h for "
                   "error code list.\n",
                   errCode);
          }
          else
          {
            num_run = CU_get_number_of_asserts();
            num_failed = CU_get_number_of_failures();
            printf("\n    %s - asserts - %3d passed, %3d failed, "
                   "%3d total.\n\n",
                   (0 == num_failed ? "PASSED" : "FAILED"),
                   (num_run - num_failed), num_failed, num_run);
          }
        }
        else
        {
          /* Run all the tests in the suite. */
          printf("\nRunning all tests in suite '%s'.\n", suite_name);
          /* This should be CU_basic_run_suite, but that method is
           * broken, see:
           *     https://sourceforge.net/tracker/?func=detail&aid=2851925&group_id=32992&atid=407088
           * This one doesn't output anything for success, so we have
           * to do it manually. */
          errCode = CU_run_suite(suite_to_run);
          if (errCode != CUE_SUCCESS)
          {
            printf("    Error attempting to run tests: %d.  See "
                   "CUError.h for "
                   "error code list.\n",
                   errCode);
          }
          else
          {
            num_run = CU_get_number_of_tests_run();
            num_failed = CU_get_number_of_tests_failed();
            printf("\n    %s -   tests - %3d passed, %3d failed, "
                   "%3d total.\n",
                   (0 == num_failed ? "PASSED" : "FAILED"),
                   (num_run - num_failed), num_failed, num_run);
            num_run = CU_get_number_of_asserts();
            num_failed = CU_get_number_of_failures();
            printf("           - asserts - %3d passed, %3d failed, "
                   "%3d total.\n\n",
                   (num_run - num_failed), num_failed, num_run);
          }
        }
      }
    }
    /* Presumably if the CU_basic_run_[test|suite] functions worked, we
     * wouldn't have to do this. */
    CU_basic_show_failures(CU_get_failure_list());
    printf("\n\n"); /* basic_show_failures leaves off line breaks. */
  }
  num_failed = CU_get_number_of_failures();
  CU_cleanup_registry();
  return num_failed;
}

/* UTILITY ************************************************************/

char *file_to_str(const char *fname)
{
  FILE *fr;
  char fullpath[512];
  size_t lnsz;
  size_t sz = 8192;
  char *str = pcalloc(sz);
  char *ptr = str;
  size_t MAXLINELEN = 8192;
  char buf[MAXLINELEN];

  snprintf(fullpath, 512, "%s/lib/cunit/%s", PROJECT_SOURCE_DIR, fname);
  fr = fopen(fullpath, "rt");

  while (fr && fgets(buf, MAXLINELEN, fr) != NULL)
  {
    if (buf[0] == '\0')
      continue;
    lnsz = strlen(buf);
    if (ptr - str + lnsz > sz)
    {
      size_t bsz = ptr - str;
      sz *= 2;
      str = pcrealloc(str, sz);
      ptr = str + bsz;
    }
    memcpy(ptr, buf, lnsz);
    ptr += lnsz;
  }

  *ptr = '\0';
  fclose(fr);

  return str;
}