From 89075e2964ca830f0c49da36fcc72b5fcb603449 Mon Sep 17 00:00:00 2001
From: Hraban Luyat <hraban@0brg.net>
Date: Sat, 13 Apr 2024 15:39:58 -0400
Subject: [PATCH 2/2] test: dynamic space size envvar and precedence

---
 tests/memory-args.test.sh | 22 +++++++++++++++++
 tests/save7.test.sh       | 51 +++++++++++++++++++++++++++++++++------
 2 files changed, 66 insertions(+), 7 deletions(-)
 create mode 100755 tests/memory-args.test.sh

diff --git a/tests/memory-args.test.sh b/tests/memory-args.test.sh
new file mode 100755
index 000000000..72ef0cc79
--- /dev/null
+++ b/tests/memory-args.test.sh
@@ -0,0 +1,22 @@
+#!/bin/sh
+
+. ./subr.sh
+
+use_test_subdirectory
+
+set -e
+
+# Allow slight shrinkage if heap relocation has to adjust for alignment
+SBCL_DYNAMIC_SPACE_SIZE=234mb run_sbcl_with_args --script <<EOF
+(assert (<= 0 (- (* 234 1024 1024) (sb-ext:dynamic-space-size)) 65536))
+EOF
+
+SBCL_DYNAMIC_SPACE_SIZE=555mb run_sbcl_with_args --dynamic-space-size 234mb --script <<EOF
+(assert (<= 0 (- (* 234 1024 1024) (sb-ext:dynamic-space-size)) 65536))
+EOF
+
+run_sbcl_with_args --dynamic-space-size 234mb --script <<EOF
+(assert (<= 0 (- (* 234 1024 1024) (sb-ext:dynamic-space-size)) 65536))
+EOF
+
+exit $EXIT_TEST_WIN
diff --git a/tests/save7.test.sh b/tests/save7.test.sh
index faa647e1a..1f2821b5d 100644
--- a/tests/save7.test.sh
+++ b/tests/save7.test.sh
@@ -25,6 +25,9 @@ echo "::: Success"
 
 run_sbcl_with_args --noinform --control-stack-size 384KB --dynamic-space-size 260MB \
     --disable-debugger --no-userinit --no-sysinit --noprint <<EOF
+  ; allow slight shrinkage if heap relocation has to adjust for alignment
+  (defun dynamic-space-size-good-p (expected-mb)
+    (<= 0 (- (* expected-mb 1024 1024) (dynamic-space-size)) 65536))
   (save-lisp-and-die "$tmpcore" :executable t :save-runtime-options :accept-runtime-options)
 EOF
 chmod u+x "$tmpcore"
@@ -57,6 +60,29 @@ if [ $? -ne 0 ]; then
 fi
 echo "::: Success"
 
+echo "::: Running :DYNAMIC-SPACE-SIZE-ENV-ACCEPT"
+SBCL_DYNAMIC_SPACE_SIZE=432MB ./"$tmpcore" --no-userinit --no-sysinit --noprint <<EOF
+  (when (dynamic-space-size-good-p 432)
+    (exit :code 42))
+EOF
+if [ $? -ne 42 ]; then
+  exit 1
+fi
+echo "::: Success"
+
+echo "::: Running :DYNAMIC-SPACE-SIZE-PRECEDENCE"
+SBCL_DYNAMIC_SPACE_SIZE=432MB ./"$tmpcore" --dynamic-space-size 333MB \
+  --no-userinit --no-sysinit --noprint <<EOF
+  (when (dynamic-space-size-good-p 333))
+    (exit :code 42))
+EOF
+status=$?
+if [ $status -ne 42 ]; then
+    echo "saved core should have prioritized memory specification from arg"
+    exit 1
+fi
+echo "::: Success"
+
 echo "::: Running :DYNAMIC-SPACE-SIZE-ARG"
 run_sbcl_with_core "$tmpcore" --noinform --control-stack-size 640KB \
     --tls-limit 5000 \
@@ -66,11 +92,8 @@ run_sbcl_with_core "$tmpcore" --noinform --control-stack-size 640KB \
        sb-vm:n-word-bytes))
   (assert (eql (extern-alien "thread_control_stack_size" unsigned) (* 640 1024)))
   (assert (eql (extern-alien "dynamic_values_bytes" (unsigned 32))
-               (* 5000 tls-element-size)))
-  ; allow slight shrinkage if heap relocation has to adjust for alignment
-  (defun dynamic-space-size-good-p ()
-    (<= 0 (- (* 260 1048576) (dynamic-space-size)) 65536))
-  (assert (dynamic-space-size-good-p))
+               (* 5000 tls-element-size)))
+  (assert (dynamic-space-size-good-p 260))
   (save-lisp-and-die "${tmpcore}2" :executable t :save-runtime-options t)
 EOF
 chmod u+x "${tmpcore}2"
@@ -82,15 +105,29 @@ echo "::: INFO: prepared test core"
   (when (and (eql (extern-alien "thread_control_stack_size" unsigned) (* 640 1024))
              (eql (extern-alien "dynamic_values_bytes" (unsigned 32))
                   (* 5000 tls-element-size))
-             (dynamic-space-size-good-p))
+             (dynamic-space-size-good-p 260))
     (exit :code 42))
 EOF
 status=$?
-rm "$tmpcore" "${tmpcore}2"
 if [ $status -ne 42 ]; then
     echo "re-saved executable used wrong memory size options"
     exit 1
 fi
 echo "::: Success"
 
+echo "::: Running :DYNAMIC-SPACE-SIZE-ENV-IGNORE"
+SBCL_DYNAMIC_SPACE_SIZE=432MB ./"${tmpcore}2" --no-userinit --no-sysinit --noprint <<EOF
+  (when (dynamic-space-size-good-p 260)
+    (exit :code 42))
+EOF
+status=$?
+if [ $status -ne 42 ]; then
+    echo "re-saved executable should have ignored memory specification from env"
+    exit 1
+fi
+echo "::: Success"
+rm "$tmpcore" "${tmpcore}2"
+
+
+
 exit $EXIT_TEST_WIN
-- 
2.51.2

