PostgreSQL Source Code git master
test_lfind.c
Go to the documentation of this file.
1/*--------------------------------------------------------------------------
2 *
3 * test_lfind.c
4 * Test correctness of optimized linear search functions.
5 *
6 * Copyright (c) 2022-2025, PostgreSQL Global Development Group
7 *
8 * IDENTIFICATION
9 * src/test/modules/test_lfind/test_lfind.c
10 *
11 * -------------------------------------------------------------------------
12 */
13
14#include "postgres.h"
15
16#include "fmgr.h"
17#include "port/pg_lfind.h"
18
19/*
20 * Convenience macros for testing both vector and scalar operations. The 2x
21 * factor is to make sure iteration works
22 */
23#define LEN_NO_TAIL(vectortype) (2 * sizeof(vectortype))
24#define LEN_WITH_TAIL(vectortype) (LEN_NO_TAIL(vectortype) + 3)
25
27
28/* workhorse for test_lfind8 */
29static void
31{
32 uint8 charbuf[LEN_WITH_TAIL(Vector8)];
33 const int len_no_tail = LEN_NO_TAIL(Vector8);
34 const int len_with_tail = LEN_WITH_TAIL(Vector8);
35
36 memset(charbuf, 0xFF, len_with_tail);
37 /* search tail to test one-byte-at-a-time path */
38 charbuf[len_with_tail - 1] = key;
39 if (key > 0x00 && pg_lfind8(key - 1, charbuf, len_with_tail))
40 elog(ERROR, "pg_lfind8() found nonexistent element '0x%x'", key - 1);
41 if (key < 0xFF && !pg_lfind8(key, charbuf, len_with_tail))
42 elog(ERROR, "pg_lfind8() did not find existing element '0x%x'", key);
43 if (key < 0xFE && pg_lfind8(key + 1, charbuf, len_with_tail))
44 elog(ERROR, "pg_lfind8() found nonexistent element '0x%x'", key + 1);
45
46 memset(charbuf, 0xFF, len_with_tail);
47 /* search with vector operations */
48 charbuf[len_no_tail - 1] = key;
49 if (key > 0x00 && pg_lfind8(key - 1, charbuf, len_no_tail))
50 elog(ERROR, "pg_lfind8() found nonexistent element '0x%x'", key - 1);
51 if (key < 0xFF && !pg_lfind8(key, charbuf, len_no_tail))
52 elog(ERROR, "pg_lfind8() did not find existing element '0x%x'", key);
53 if (key < 0xFE && pg_lfind8(key + 1, charbuf, len_no_tail))
54 elog(ERROR, "pg_lfind8() found nonexistent element '0x%x'", key + 1);
55}
56
60{
69
71}
72
73/* workhorse for test_lfind8_le */
74static void
76{
77 uint8 charbuf[LEN_WITH_TAIL(Vector8)];
78 const int len_no_tail = LEN_NO_TAIL(Vector8);
79 const int len_with_tail = LEN_WITH_TAIL(Vector8);
80
81 memset(charbuf, 0xFF, len_with_tail);
82 /* search tail to test one-byte-at-a-time path */
83 charbuf[len_with_tail - 1] = key;
84 if (key > 0x00 && pg_lfind8_le(key - 1, charbuf, len_with_tail))
85 elog(ERROR, "pg_lfind8_le() found nonexistent element <= '0x%x'", key - 1);
86 if (key < 0xFF && !pg_lfind8_le(key, charbuf, len_with_tail))
87 elog(ERROR, "pg_lfind8_le() did not find existing element <= '0x%x'", key);
88 if (key < 0xFE && !pg_lfind8_le(key + 1, charbuf, len_with_tail))
89 elog(ERROR, "pg_lfind8_le() did not find existing element <= '0x%x'", key + 1);
90
91 memset(charbuf, 0xFF, len_with_tail);
92 /* search with vector operations */
93 charbuf[len_no_tail - 1] = key;
94 if (key > 0x00 && pg_lfind8_le(key - 1, charbuf, len_no_tail))
95 elog(ERROR, "pg_lfind8_le() found nonexistent element <= '0x%x'", key - 1);
96 if (key < 0xFF && !pg_lfind8_le(key, charbuf, len_no_tail))
97 elog(ERROR, "pg_lfind8_le() did not find existing element <= '0x%x'", key);
98 if (key < 0xFE && !pg_lfind8_le(key + 1, charbuf, len_no_tail))
99 elog(ERROR, "pg_lfind8_le() did not find existing element <= '0x%x'", key + 1);
100}
101
103Datum
105{
114
116}
117
119Datum
121{
122#define TEST_ARRAY_SIZE 135
123 uint32 test_array[TEST_ARRAY_SIZE] = {0};
124
125 test_array[8] = 1;
126 test_array[64] = 2;
127 test_array[TEST_ARRAY_SIZE - 1] = 3;
128
129 if (pg_lfind32(1, test_array, 4))
130 elog(ERROR, "pg_lfind32() found nonexistent element");
131 if (!pg_lfind32(1, test_array, TEST_ARRAY_SIZE))
132 elog(ERROR, "pg_lfind32() did not find existing element");
133
134 if (pg_lfind32(2, test_array, 32))
135 elog(ERROR, "pg_lfind32() found nonexistent element");
136 if (!pg_lfind32(2, test_array, TEST_ARRAY_SIZE))
137 elog(ERROR, "pg_lfind32() did not find existing element");
138
139 if (pg_lfind32(3, test_array, 96))
140 elog(ERROR, "pg_lfind32() found nonexistent element");
141 if (!pg_lfind32(3, test_array, TEST_ARRAY_SIZE))
142 elog(ERROR, "pg_lfind32() did not find existing element");
143
144 if (pg_lfind32(4, test_array, TEST_ARRAY_SIZE))
145 elog(ERROR, "pg_lfind32() found nonexistent element");
146
148}
uint8_t uint8
Definition: c.h:486
uint32_t uint32
Definition: c.h:488
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
#define PG_RETURN_VOID()
Definition: fmgr.h:349
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193
static bool pg_lfind8_le(uint8 key, uint8 *base, uint32 nelem)
Definition: pg_lfind.h:58
static bool pg_lfind32(uint32 key, const uint32 *base, uint32 nelem)
Definition: pg_lfind.h:153
static bool pg_lfind8(uint8 key, uint8 *base, uint32 nelem)
Definition: pg_lfind.h:26
uintptr_t Datum
Definition: postgres.h:69
uint64 Vector8
Definition: simd.h:60
#define LEN_NO_TAIL(vectortype)
Definition: test_lfind.c:23
PG_FUNCTION_INFO_V1(test_lfind8)
PG_MODULE_MAGIC
Definition: test_lfind.c:26
Datum test_lfind8(PG_FUNCTION_ARGS)
Definition: test_lfind.c:59
#define TEST_ARRAY_SIZE
Datum test_lfind32(PG_FUNCTION_ARGS)
Definition: test_lfind.c:120
Datum test_lfind8_le(PG_FUNCTION_ARGS)
Definition: test_lfind.c:104
#define LEN_WITH_TAIL(vectortype)
Definition: test_lfind.c:24
static void test_lfind8_le_internal(uint8 key)
Definition: test_lfind.c:75
static void test_lfind8_internal(uint8 key)
Definition: test_lfind.c:30