From 168860ad31db4883eb541a82374afbb11c0af66e Mon Sep 17 00:00:00 2001
From: Thomas Munro <thomas.munro@gmail.com>
Date: Sat, 24 Sep 2022 01:18:57 +1200
Subject: [PATCH 1/2] Simple test for sharedtuplestore.c.

XXX Decide if this is worth keeping.
---
 src/test/modules/meson.build                |  1 +
 src/test/modules/test_sts/Makefile          | 23 ++++++
 src/test/modules/test_sts/meson.build       | 22 ++++++
 src/test/modules/test_sts/sql/test_sts.sql  |  5 ++
 src/test/modules/test_sts/test_sts--1.0.sql |  8 ++
 src/test/modules/test_sts/test_sts.c        | 82 +++++++++++++++++++++
 src/test/modules/test_sts/test_sts.control  |  4 +
 7 files changed, 145 insertions(+)
 create mode 100644 src/test/modules/test_sts/Makefile
 create mode 100644 src/test/modules/test_sts/meson.build
 create mode 100644 src/test/modules/test_sts/sql/test_sts.sql
 create mode 100644 src/test/modules/test_sts/test_sts--1.0.sql
 create mode 100644 src/test/modules/test_sts/test_sts.c
 create mode 100644 src/test/modules/test_sts/test_sts.control

diff --git a/src/test/modules/meson.build b/src/test/modules/meson.build
index a80e6e2ce2..4244d37efe 100644
--- a/src/test/modules/meson.build
+++ b/src/test/modules/meson.build
@@ -23,5 +23,6 @@ subdir('test_rbtree')
 subdir('test_regex')
 subdir('test_rls_hooks')
 subdir('test_shm_mq')
+subdir('test_sts')
 subdir('unsafe_tests')
 subdir('worker_spi')
diff --git a/src/test/modules/test_sts/Makefile b/src/test/modules/test_sts/Makefile
new file mode 100644
index 0000000000..efd5b7d154
--- /dev/null
+++ b/src/test/modules/test_sts/Makefile
@@ -0,0 +1,23 @@
+# src/test/modules/test_sts/Makefile
+
+MODULE_big = test_sts
+OBJS = \
+	$(WIN32RES) \
+	test_sts.o
+PGFILEDESC = "test_sts - test code for sharedtuplestore.c"
+
+EXTENSION = test_sts
+DATA = test_sts--1.0.sql
+
+REGRESS = test_sts
+
+ifdef USE_PGXS
+PG_CONFIG = pg_config
+PGXS := $(shell $(PG_CONFIG) --pgxs)
+include $(PGXS)
+else
+subdir = src/test/modules/test_sts
+top_builddir = ../../../..
+include $(top_builddir)/src/Makefile.global
+include $(top_srcdir)/contrib/contrib-global.mk
+endif
diff --git a/src/test/modules/test_sts/meson.build b/src/test/modules/test_sts/meson.build
new file mode 100644
index 0000000000..706292f8e8
--- /dev/null
+++ b/src/test/modules/test_sts/meson.build
@@ -0,0 +1,22 @@
+test_sts = shared_module('test_sts',
+  ['test_sts.c'],
+  kwargs: pg_mod_args,
+)
+testprep_targets += test_sts
+
+install_data(
+  'test_sts.control',
+  'test_sts--1.0.sql',
+  kwargs: contrib_data_args,
+)
+
+tests += {
+  'name': 'test_sts',
+  'sd': meson.current_source_dir(),
+  'bd': meson.current_build_dir(),
+  'regress': {
+    'sql': [
+      'test_sts',
+    ],
+  },
+}
diff --git a/src/test/modules/test_sts/sql/test_sts.sql b/src/test/modules/test_sts/sql/test_sts.sql
new file mode 100644
index 0000000000..ab52e5d040
--- /dev/null
+++ b/src/test/modules/test_sts/sql/test_sts.sql
@@ -0,0 +1,5 @@
+CREATE EXTENSION test_sts;
+
+SELECT test_sts(10, 32755);
+SELECT test_sts(10, 32756);
+SELECT test_sts(10, 32757);
diff --git a/src/test/modules/test_sts/test_sts--1.0.sql b/src/test/modules/test_sts/test_sts--1.0.sql
new file mode 100644
index 0000000000..02e6f80cc2
--- /dev/null
+++ b/src/test/modules/test_sts/test_sts--1.0.sql
@@ -0,0 +1,8 @@
+/* src/test/modules/test_sts/test_sts--1.0.sql */
+
+-- complain if script is sourced in psql, rather than via CREATE EXTENSION
+\echo Use "CREATE EXTENSION test_sts" to load this file. \quit
+
+CREATE FUNCTION test_sts(n int, size int)
+RETURNS pg_catalog.void STRICT
+AS 'MODULE_PATHNAME' LANGUAGE C;
diff --git a/src/test/modules/test_sts/test_sts.c b/src/test/modules/test_sts/test_sts.c
new file mode 100644
index 0000000000..5a2b692632
--- /dev/null
+++ b/src/test/modules/test_sts/test_sts.c
@@ -0,0 +1,82 @@
+/*--------------------------------------------------------------------------
+ *
+ * test_sts.c
+ *		Test sharedtuplestore.c
+ *
+ * Copyright (c) 2022, PostgreSQL Global Development Group
+ *
+ * IDENTIFICATION
+ *		src/test/modules/test_sts/test_sts.c
+ *
+ * -------------------------------------------------------------------------
+ */
+#include "postgres.h"
+
+#include "access/htup_details.h"
+#include "fmgr.h"
+#include "storage/sharedfileset.h"
+#include "utils/elog.h"
+#include "utils/sharedtuplestore.h"
+
+PG_MODULE_MAGIC;
+
+PG_FUNCTION_INFO_V1(test_sts);
+
+Datum
+test_sts(PG_FUNCTION_ARGS)
+{
+	int			tuples = PG_GETARG_INT32(0);
+	int			tuple_len = PG_GETARG_INT32(1);
+	SharedFileSet sfs;
+	SharedTuplestore *sts;
+	SharedTuplestoreAccessor *accessor;
+
+	SharedFileSetInit(&sfs, NULL);
+
+	sts = palloc(sts_estimate(1));
+	accessor = sts_initialize(sts, 1, 0, sizeof(int), 0, &sfs, "test_sts");
+
+	for (int i = 0; i < tuples; ++i)
+	{
+		MinimalTuple tuple;
+		int			l;
+
+		/* alternate between requested size and 100 */
+		l = (i % 2 == 0) ? 100 : tuple_len;
+
+		tuple = palloc(l);
+		memset(tuple, i & 0xff, l);
+		tuple->t_len = l;
+		sts_puttuple(accessor, &i, tuple);
+		pfree(tuple);
+	}
+	sts_end_write(accessor);
+
+	sts_begin_parallel_scan(accessor);
+	for (int i = 0; i < tuples; ++i)
+	{
+		MinimalTuple tuple;
+		int			meta;
+		int			l;
+
+		l = (i % 2 == 0) ? 100 : tuple_len;
+
+		tuple = sts_parallel_scan_next(accessor, &meta);
+		if (!tuple)
+			elog(ERROR, "expected more tuples");
+		if (i != meta)
+			elog(ERROR, "expected meta data %d, got meta data %d", i, meta);
+		if (tuple->t_len != l)
+			elog(ERROR, "expected tuple length %d, got %d", l, tuple->t_len);
+		for (int j = sizeof(tuple->t_len); j < l; ++j)
+			if (((uint8 *) tuple)[j] != (i & 0xff))
+				elog(ERROR, "expected byte %x, got %x", (i & 0xff), ((uint8 *) tuple)[j]);
+	}
+	if (sts_parallel_scan_next(accessor, NULL) != NULL)
+		elog(ERROR, "did not expect more tuples");
+	sts_end_parallel_scan(accessor);
+
+	SharedFileSetDeleteAll(&sfs);
+
+	PG_RETURN_VOID();
+}
diff --git a/src/test/modules/test_sts/test_sts.control b/src/test/modules/test_sts/test_sts.control
new file mode 100644
index 0000000000..1da4c00fd5
--- /dev/null
+++ b/src/test/modules/test_sts/test_sts.control
@@ -0,0 +1,4 @@
+comment = 'Test code for sharedtuplestore.c'
+default_version = '1.0'
+module_pathname = '$libdir/test_sts'
+relocatable = true
-- 
2.30.2

