From 13e19b131002a89b36ea8533f3e05a77c9f6de22 Mon Sep 17 00:00:00 2001
From: Paul Amonson <paul.d.amonson@intel.com>
Date: Mon, 6 May 2024 08:34:17 -0700
Subject: [PATCH] [Test] Add a Postgres SQL function for crc32c testing.

Signed-off-by: Paul Amonson <paul.d.amonson@intel.com>
---
 src/test/modules/test_crc32c/Makefile         | 20 ++++++++++
 .../modules/test_crc32c/test_crc32c--1.0.sql  |  1 +
 src/test/modules/test_crc32c/test_crc32c.c    | 39 +++++++++++++++++++
 .../modules/test_crc32c/test_crc32c.control   |  4 ++
 4 files changed, 64 insertions(+)
 create mode 100644 src/test/modules/test_crc32c/Makefile
 create mode 100644 src/test/modules/test_crc32c/test_crc32c--1.0.sql
 create mode 100644 src/test/modules/test_crc32c/test_crc32c.c
 create mode 100644 src/test/modules/test_crc32c/test_crc32c.control

diff --git a/src/test/modules/test_crc32c/Makefile b/src/test/modules/test_crc32c/Makefile
new file mode 100644
index 0000000000..5b747c6184
--- /dev/null
+++ b/src/test/modules/test_crc32c/Makefile
@@ -0,0 +1,20 @@
+MODULE_big = test_crc32c
+OBJS = test_crc32c.o
+PGFILEDESC = "test"
+EXTENSION = test_crc32c
+DATA = test_crc32c--1.0.sql
+
+first: all
+
+# test_crc32c.o:	CFLAGS+=-g
+
+ifdef USE_PGXS
+PG_CONFIG = pg_config
+PGXS := $(shell $(PG_CONFIG) --pgxs)
+include $(PGXS)
+else
+subdir = src/test/modules/test_crc32c
+top_builddir = ../../../..
+include $(top_builddir)/src/Makefile.global
+include $(top_srcdir)/contrib/contrib-global.mk
+endif
diff --git a/src/test/modules/test_crc32c/test_crc32c--1.0.sql b/src/test/modules/test_crc32c/test_crc32c--1.0.sql
new file mode 100644
index 0000000000..32f8f0fb2e
--- /dev/null
+++ b/src/test/modules/test_crc32c/test_crc32c--1.0.sql
@@ -0,0 +1 @@
+CREATE FUNCTION drive_crc32c  (count int, num int) RETURNS bigint AS 'test_crc32c.so' LANGUAGE C;
diff --git a/src/test/modules/test_crc32c/test_crc32c.c b/src/test/modules/test_crc32c/test_crc32c.c
new file mode 100644
index 0000000000..477f198316
--- /dev/null
+++ b/src/test/modules/test_crc32c/test_crc32c.c
@@ -0,0 +1,41 @@
+/* select drive_crc32c(1000000, 1024); */
+
+#include "postgres.h"
+
+#include "fmgr.h"
+
+#include "port/pg_crc32c.h"
+
+PG_MODULE_MAGIC;
+
+/*
+ * drive_crc32c(count: int, num: int) returns bigint
+ *
+ * count is the nuimber of loops to perform
+ *
+ * num is the number byte in the buffer to calculate
+ * crc32c over.
+ */
+PG_FUNCTION_INFO_V1(drive_crc32c);
+Datum
+drive_crc32c(PG_FUNCTION_ARGS)
+{
+	int64			count	= PG_GETARG_INT64(0);
+	int64			num		= PG_GETARG_INT64(1);
+	pg_crc32c		crc		= 0xFFFFFFFF;
+	const char*		data	= malloc((size_t)num);
+
+	INIT_CRC32C(crc);
+
+	while(count--)
+	{
+		memset((void*)data, count, (size_t)Min(16,num));
+		crc = COMP_CRC32C(crc, data, num);
+	}
+
+	FIN_CRC32C(crc);
+
+	free((void *)data);
+
+	PG_RETURN_INT64((int64_t)crc);
+}
diff --git a/src/test/modules/test_crc32c/test_crc32c.control b/src/test/modules/test_crc32c/test_crc32c.control
new file mode 100644
index 0000000000..878a077ee1
--- /dev/null
+++ b/src/test/modules/test_crc32c/test_crc32c.control
@@ -0,0 +1,4 @@
+comment = 'test'
+default_version = '1.0'
+module_pathname = '$libdir/test_crc32c'
+relocatable = true
-- 
2.34.1

