LCOV - code coverage report
Current view: top level - src/test/modules/test_lfind - test_lfind.c (source / functions) Coverage Total Hit
Test: Code coverage Lines: 0.0 % 87 0
Test Date: 2026-01-26 10:56:24 Functions: 0.0 % 9 0
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /*--------------------------------------------------------------------------
       2              :  *
       3              :  * test_lfind.c
       4              :  *              Test correctness of optimized linear search functions.
       5              :  *
       6              :  * Copyright (c) 2022-2026, 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              : 
      26            0 : PG_MODULE_MAGIC;
      27              : 
      28              : /* workhorse for test_lfind8 */
      29              : static void
      30            0 : test_lfind8_internal(uint8 key)
      31              : {
      32            0 :         uint8           charbuf[LEN_WITH_TAIL(Vector8)];
      33            0 :         const int       len_no_tail = LEN_NO_TAIL(Vector8);
      34            0 :         const int       len_with_tail = LEN_WITH_TAIL(Vector8);
      35              : 
      36            0 :         memset(charbuf, 0xFF, len_with_tail);
      37              :         /* search tail to test one-byte-at-a-time path */
      38            0 :         charbuf[len_with_tail - 1] = key;
      39            0 :         if (key > 0x00 && pg_lfind8(key - 1, charbuf, len_with_tail))
      40            0 :                 elog(ERROR, "pg_lfind8() found nonexistent element '0x%x'", key - 1);
      41            0 :         if (key < 0xFF && !pg_lfind8(key, charbuf, len_with_tail))
      42            0 :                 elog(ERROR, "pg_lfind8() did not find existing element '0x%x'", key);
      43            0 :         if (key < 0xFE && pg_lfind8(key + 1, charbuf, len_with_tail))
      44            0 :                 elog(ERROR, "pg_lfind8() found nonexistent element '0x%x'", key + 1);
      45              : 
      46            0 :         memset(charbuf, 0xFF, len_with_tail);
      47              :         /* search with vector operations */
      48            0 :         charbuf[len_no_tail - 1] = key;
      49            0 :         if (key > 0x00 && pg_lfind8(key - 1, charbuf, len_no_tail))
      50            0 :                 elog(ERROR, "pg_lfind8() found nonexistent element '0x%x'", key - 1);
      51            0 :         if (key < 0xFF && !pg_lfind8(key, charbuf, len_no_tail))
      52            0 :                 elog(ERROR, "pg_lfind8() did not find existing element '0x%x'", key);
      53            0 :         if (key < 0xFE && pg_lfind8(key + 1, charbuf, len_no_tail))
      54            0 :                 elog(ERROR, "pg_lfind8() found nonexistent element '0x%x'", key + 1);
      55            0 : }
      56              : 
      57            0 : PG_FUNCTION_INFO_V1(test_lfind8);
      58              : Datum
      59            0 : test_lfind8(PG_FUNCTION_ARGS)
      60              : {
      61            0 :         test_lfind8_internal(0);
      62            0 :         test_lfind8_internal(1);
      63            0 :         test_lfind8_internal(0x7F);
      64            0 :         test_lfind8_internal(0x80);
      65            0 :         test_lfind8_internal(0x81);
      66            0 :         test_lfind8_internal(0xFD);
      67            0 :         test_lfind8_internal(0xFE);
      68            0 :         test_lfind8_internal(0xFF);
      69              : 
      70            0 :         PG_RETURN_VOID();
      71              : }
      72              : 
      73              : /* workhorse for test_lfind8_le */
      74              : static void
      75            0 : test_lfind8_le_internal(uint8 key)
      76              : {
      77            0 :         uint8           charbuf[LEN_WITH_TAIL(Vector8)];
      78            0 :         const int       len_no_tail = LEN_NO_TAIL(Vector8);
      79            0 :         const int       len_with_tail = LEN_WITH_TAIL(Vector8);
      80              : 
      81            0 :         memset(charbuf, 0xFF, len_with_tail);
      82              :         /* search tail to test one-byte-at-a-time path */
      83            0 :         charbuf[len_with_tail - 1] = key;
      84            0 :         if (key > 0x00 && pg_lfind8_le(key - 1, charbuf, len_with_tail))
      85            0 :                 elog(ERROR, "pg_lfind8_le() found nonexistent element <= '0x%x'", key - 1);
      86            0 :         if (key < 0xFF && !pg_lfind8_le(key, charbuf, len_with_tail))
      87            0 :                 elog(ERROR, "pg_lfind8_le() did not find existing element <= '0x%x'", key);
      88            0 :         if (key < 0xFE && !pg_lfind8_le(key + 1, charbuf, len_with_tail))
      89            0 :                 elog(ERROR, "pg_lfind8_le() did not find existing element <= '0x%x'", key + 1);
      90              : 
      91            0 :         memset(charbuf, 0xFF, len_with_tail);
      92              :         /* search with vector operations */
      93            0 :         charbuf[len_no_tail - 1] = key;
      94            0 :         if (key > 0x00 && pg_lfind8_le(key - 1, charbuf, len_no_tail))
      95            0 :                 elog(ERROR, "pg_lfind8_le() found nonexistent element <= '0x%x'", key - 1);
      96            0 :         if (key < 0xFF && !pg_lfind8_le(key, charbuf, len_no_tail))
      97            0 :                 elog(ERROR, "pg_lfind8_le() did not find existing element <= '0x%x'", key);
      98            0 :         if (key < 0xFE && !pg_lfind8_le(key + 1, charbuf, len_no_tail))
      99            0 :                 elog(ERROR, "pg_lfind8_le() did not find existing element <= '0x%x'", key + 1);
     100            0 : }
     101              : 
     102            0 : PG_FUNCTION_INFO_V1(test_lfind8_le);
     103              : Datum
     104            0 : test_lfind8_le(PG_FUNCTION_ARGS)
     105              : {
     106            0 :         test_lfind8_le_internal(0);
     107            0 :         test_lfind8_le_internal(1);
     108            0 :         test_lfind8_le_internal(0x7F);
     109            0 :         test_lfind8_le_internal(0x80);
     110            0 :         test_lfind8_le_internal(0x81);
     111            0 :         test_lfind8_le_internal(0xFD);
     112            0 :         test_lfind8_le_internal(0xFE);
     113            0 :         test_lfind8_le_internal(0xFF);
     114              : 
     115            0 :         PG_RETURN_VOID();
     116              : }
     117              : 
     118            0 : PG_FUNCTION_INFO_V1(test_lfind32);
     119              : Datum
     120            0 : test_lfind32(PG_FUNCTION_ARGS)
     121              : {
     122              : #define TEST_ARRAY_SIZE 135
     123            0 :         uint32          test_array[TEST_ARRAY_SIZE] = {0};
     124              : 
     125            0 :         test_array[8] = 1;
     126            0 :         test_array[64] = 2;
     127            0 :         test_array[TEST_ARRAY_SIZE - 1] = 3;
     128              : 
     129            0 :         if (pg_lfind32(1, test_array, 4))
     130            0 :                 elog(ERROR, "pg_lfind32() found nonexistent element");
     131            0 :         if (!pg_lfind32(1, test_array, TEST_ARRAY_SIZE))
     132            0 :                 elog(ERROR, "pg_lfind32() did not find existing element");
     133              : 
     134            0 :         if (pg_lfind32(2, test_array, 32))
     135            0 :                 elog(ERROR, "pg_lfind32() found nonexistent element");
     136            0 :         if (!pg_lfind32(2, test_array, TEST_ARRAY_SIZE))
     137            0 :                 elog(ERROR, "pg_lfind32() did not find existing element");
     138              : 
     139            0 :         if (pg_lfind32(3, test_array, 96))
     140            0 :                 elog(ERROR, "pg_lfind32() found nonexistent element");
     141            0 :         if (!pg_lfind32(3, test_array, TEST_ARRAY_SIZE))
     142            0 :                 elog(ERROR, "pg_lfind32() did not find existing element");
     143              : 
     144            0 :         if (pg_lfind32(4, test_array, TEST_ARRAY_SIZE))
     145            0 :                 elog(ERROR, "pg_lfind32() found nonexistent element");
     146              : 
     147            0 :         PG_RETURN_VOID();
     148            0 : }
        

Generated by: LCOV version 2.3.2-1