Skip to content

Commit b4712b0

Browse files
committed
move various print functions to debug_print.c, add cmocka-based unit test system
1 parent ce86f41 commit b4712b0

File tree

10 files changed

+670
-85
lines changed

10 files changed

+670
-85
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ OBJS = src/init.o src/relation_info.o src/utils.o src/partition_filter.o \
55
src/runtimeappend.o src/runtime_merge_append.o src/pg_pathman.o src/rangeset.o \
66
src/pl_funcs.o src/pl_range_funcs.o src/pl_hash_funcs.o src/pathman_workers.o \
77
src/hooks.o src/nodes_common.o src/xact_handling.o src/copy_stmt_hooking.o \
8-
src/pg_compat.o $(WIN32RES)
8+
src/debug_print.o src/pg_compat.o $(WIN32RES)
99

1010
EXTENSION = pg_pathman
1111
EXTVERSION = 1.1

src/debug_print.c

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/* ------------------------------------------------------------------------
2+
*
3+
* debug_print.c
4+
* Print sophisticated structs as CSTRING
5+
*
6+
* Copyright (c) 2016, Postgres Professional
7+
*
8+
* ------------------------------------------------------------------------
9+
*/
10+
11+
#include "rangeset.h"
12+
13+
#include "postgres.h"
14+
#include "nodes/bitmapset.h"
15+
#include "nodes/pg_list.h"
16+
#include "lib/stringinfo.h"
17+
18+
19+
/*
20+
* Print Bitmapset as cstring.
21+
*/
22+
#ifdef __GNUC__
23+
__attribute__((unused))
24+
#endif
25+
static char *
26+
bms_print(Bitmapset *bms)
27+
{
28+
StringInfoData str;
29+
int x;
30+
31+
initStringInfo(&str);
32+
x = -1;
33+
while ((x = bms_next_member(bms, x)) >= 0)
34+
appendStringInfo(&str, " %d", x);
35+
36+
return str.data;
37+
}
38+
39+
/*
40+
* Print list of IndexRanges as cstring.
41+
*/
42+
#ifdef __GNUC__
43+
__attribute__((unused))
44+
#endif
45+
static char *
46+
rangeset_print(List *rangeset)
47+
{
48+
StringInfoData str;
49+
ListCell *lc;
50+
bool first_irange = true;
51+
char lossy = 'L', /* Lossy IndexRange */
52+
complete = 'C'; /* Complete IndexRange */
53+
54+
initStringInfo(&str);
55+
56+
foreach (lc, rangeset)
57+
{
58+
IndexRange irange = lfirst_irange(lc);
59+
60+
/* Append comma if needed */
61+
if (!first_irange)
62+
appendStringInfo(&str, ", ");
63+
64+
if (!is_irange_valid(irange))
65+
appendStringInfo(&str, "X");
66+
else if (irange_lower(irange) == irange_upper(irange))
67+
appendStringInfo(&str, "%u%c",
68+
irange_lower(irange),
69+
(is_irange_lossy(irange) ? lossy : complete));
70+
else
71+
appendStringInfo(&str, "[%u-%u]%c",
72+
irange_lower(irange), irange_upper(irange),
73+
(is_irange_lossy(irange) ? lossy : complete));
74+
75+
first_irange = false;
76+
}
77+
78+
return str.data;
79+
}
80+
81+
/*
82+
* Print IndexRange struct as cstring.
83+
*/
84+
#ifdef __GNUC__
85+
__attribute__((unused))
86+
#endif
87+
static char *
88+
irange_print(IndexRange irange)
89+
{
90+
StringInfoData str;
91+
92+
initStringInfo(&str);
93+
94+
appendStringInfo(&str, "{ valid: %s, lossy: %s, lower: %u, upper: %u }",
95+
(is_irange_valid(irange) ? "true" : "false"),
96+
(is_irange_lossy(irange) ? "true" : "false"),
97+
irange_lower(irange),
98+
irange_upper(irange));
99+
100+
return str.data;
101+
}

src/utils.c

Lines changed: 0 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -156,90 +156,6 @@ lock_rows_visitor(Plan *plan, void *context)
156156
}
157157
}
158158

159-
/*
160-
* Print Bitmapset as cstring.
161-
*/
162-
#ifdef __GNUC__
163-
__attribute__((unused))
164-
#endif
165-
static char *
166-
bms_print(Bitmapset *bms)
167-
{
168-
StringInfoData str;
169-
int x;
170-
171-
initStringInfo(&str);
172-
x = -1;
173-
while ((x = bms_next_member(bms, x)) >= 0)
174-
appendStringInfo(&str, " %d", x);
175-
176-
return str.data;
177-
}
178-
179-
/*
180-
* Print list of IndexRanges as cstring.
181-
*/
182-
#ifdef __GNUC__
183-
__attribute__((unused))
184-
#endif
185-
static char *
186-
rangeset_print(List *rangeset)
187-
{
188-
StringInfoData str;
189-
ListCell *lc;
190-
bool first_irange = true;
191-
char lossy = 'L', /* Lossy IndexRange */
192-
complete = 'C'; /* Complete IndexRange */
193-
194-
initStringInfo(&str);
195-
196-
foreach (lc, rangeset)
197-
{
198-
IndexRange irange = lfirst_irange(lc);
199-
200-
/* Append comma if needed */
201-
if (!first_irange)
202-
appendStringInfo(&str, ", ");
203-
204-
if (!is_irange_valid(irange))
205-
appendStringInfo(&str, "X");
206-
else if (irange_lower(irange) == irange_upper(irange))
207-
appendStringInfo(&str, "%u%c",
208-
irange_lower(irange),
209-
(is_irange_lossy(irange) ? lossy : complete));
210-
else
211-
appendStringInfo(&str, "[%u-%u]%c",
212-
irange_lower(irange), irange_upper(irange),
213-
(is_irange_lossy(irange) ? lossy : complete));
214-
215-
first_irange = false;
216-
}
217-
218-
return str.data;
219-
}
220-
221-
/*
222-
* Print IndexRange struct as cstring.
223-
*/
224-
#ifdef __GNUC__
225-
__attribute__((unused))
226-
#endif
227-
static char *
228-
irange_print(IndexRange irange)
229-
{
230-
StringInfoData str;
231-
232-
initStringInfo(&str);
233-
234-
appendStringInfo(&str, "{ valid: %s, lossy: %s, lower: %u, upper: %u }",
235-
(is_irange_valid(irange) ? "true" : "false"),
236-
(is_irange_lossy(irange) ? "true" : "false"),
237-
irange_lower(irange),
238-
irange_upper(irange));
239-
240-
return str.data;
241-
}
242-
243159
/*
244160
* Get BTORDER_PROC for two types described by Oids
245161
*/

tests/cmocka/Makefile

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
PG_CONFIG = pg_config
2+
TOP_SRC_DIR = ../../src
3+
4+
CC = gcc
5+
CFLAGS = -I $(TOP_SRC_DIR) -I $(shell $(PG_CONFIG) --includedir-server)
6+
LDFLAGS = -lcmocka
7+
TEST_BIN = rangeset_tests
8+
9+
OBJ = missing_basic.o missing_list.o missing_stringinfo.o \
10+
missing_bitmapset.o rangeset_tests.o \
11+
$(TOP_SRC_DIR)/rangeset.o
12+
13+
14+
all: build_extension $(TEST_BIN)
15+
16+
$(TEST_BIN): $(OBJ)
17+
$(CC) -o $@ $^ $(CFLAGS) $(LDFLAGS)
18+
19+
%.o: %.c
20+
$(CC) -c -o $@ $< $(CFLAGS)
21+
22+
build_extension:
23+
$(MAKE) -C $(TOP_SRC_DIR)/..
24+
25+
clean:
26+
rm -f $(OBJ) $(TEST_BIN)

tests/cmocka/missing_basic.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include <stdio.h>
2+
3+
#include "postgres.h"
4+
5+
6+
void *
7+
palloc(Size size)
8+
{
9+
return malloc(size);
10+
}
11+
12+
void *
13+
repalloc(void *pointer, Size size)
14+
{
15+
return realloc(pointer, size);
16+
}
17+
18+
19+
void
20+
ExceptionalCondition(const char *conditionName,
21+
const char *errorType,
22+
const char *fileName,
23+
int lineNumber)
24+
{
25+
if (!PointerIsValid(conditionName) ||
26+
!PointerIsValid(fileName) ||
27+
!PointerIsValid(errorType))
28+
{
29+
printf("TRAP: ExceptionalCondition: bad arguments\n");
30+
}
31+
else
32+
{
33+
printf("TRAP: %s(\"%s\", File: \"%s\", Line: %d)\n",
34+
errorType, conditionName,
35+
fileName, lineNumber);
36+
37+
}
38+
39+
/* Usually this shouldn't be needed, but make sure the msg went out */
40+
fflush(stderr);
41+
42+
abort();
43+
}

tests/cmocka/missing_bitmapset.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include "postgres.h"
2+
#include "nodes/bitmapset.h"
3+
4+
5+
int
6+
bms_next_member(const Bitmapset *a, int prevbit);
7+
8+
9+
int
10+
bms_next_member(const Bitmapset *a, int prevbit)
11+
{
12+
printf("bms_next_member(): not implemented yet\n");
13+
fflush(stdout);
14+
15+
abort();
16+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy