I'd like to see the results of the 'physmem' module. This is not easily
possible through coreutils, nor is it convenient to compile a file manually
with -DDEBUG. A unit test is a more automatic way to check the API's results.


2023-08-13  Bruno Haible  <br...@clisp.org>

        physmem: Add tests.
        * tests/test-physmem.c: New file.
        * modules/physmem-tests: New file.

>From edd9de933cd491685296d0bb87375670ae8a1302 Mon Sep 17 00:00:00 2001
From: Bruno Haible <br...@clisp.org>
Date: Mon, 14 Aug 2023 00:49:25 +0200
Subject: [PATCH] physmem: Add tests.

* tests/test-physmem.c: New file.
* modules/physmem-tests: New file.
---
 ChangeLog             |  6 ++++++
 modules/physmem-tests | 11 +++++++++++
 tests/test-physmem.c  | 38 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 55 insertions(+)
 create mode 100644 modules/physmem-tests
 create mode 100644 tests/test-physmem.c

diff --git a/ChangeLog b/ChangeLog
index 35e95693d8..05d4466c77 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2023-08-13  Bruno Haible  <br...@clisp.org>
+
+	physmem: Add tests.
+	* tests/test-physmem.c: New file.
+	* modules/physmem-tests: New file.
+
 2023-08-13  Bruno Haible  <br...@clisp.org>
 
 	readutmp, boot-time: Fix warning on glibc 2.30..2.31 on Linux.
diff --git a/modules/physmem-tests b/modules/physmem-tests
new file mode 100644
index 0000000000..c37c41cd7e
--- /dev/null
+++ b/modules/physmem-tests
@@ -0,0 +1,11 @@
+Files:
+tests/test-physmem.c
+tests/macros.h
+
+Depends-on:
+
+configure.ac:
+
+Makefile.am:
+TESTS += test-physmem
+check_PROGRAMS += test-physmem
diff --git a/tests/test-physmem.c b/tests/test-physmem.c
new file mode 100644
index 0000000000..232cc86d12
--- /dev/null
+++ b/tests/test-physmem.c
@@ -0,0 +1,38 @@
+/* Test of getting the amount of total/available physical memory.
+   Copyright (C) 2023 Free Software Foundation, Inc.
+
+   This file is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published
+   by the Free Software Foundation, either version 3 of the License,
+   or (at your option) any later version.
+
+   This file is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
+
+/* Written by Bruno Haible <br...@clisp.org>, 2023.  */
+
+#include <config.h>
+
+#include "physmem.h"
+
+#include <stdio.h>
+
+#include "macros.h"
+
+int
+main (int argc, char *argv[])
+{
+  printf ("Total memory:     %12.f B = %6.f MiB\n",
+          physmem_total (), physmem_total () / (1024.0 * 1024.0));
+  printf ("Available memory: %12.f B = %6.f MiB\n",
+          physmem_available (), physmem_available () / (1024.0 * 1024.0));
+  ASSERT (physmem_total () >= physmem_available ());
+  ASSERT (physmem_available () >= 4 * 1024 * 1024);
+
+  return 0;
+}
-- 
2.34.1

Reply via email to