From b80f0d82c215be36eae37eb660872c440581b483 Mon Sep 17 00:00:00 2001 From: "Jan (janneke) Nieuwenhuizen" Date: Sun, 16 Oct 2022 23:17:14 +0200 Subject: [PATCH] lib/tests: Add 90-mmap.c. * lib/tests/posix/90-mmap.c: New test. * build-aux/check-mescc.sh (gnu_tests): Add it. --- build-aux/check-mescc.sh | 1 + lib/tests/posix/90-mmap.c | 75 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 lib/tests/posix/90-mmap.c diff --git a/build-aux/check-mescc.sh b/build-aux/check-mescc.sh index 46479a04..cc0f3b07 100755 --- a/build-aux/check-mescc.sh +++ b/build-aux/check-mescc.sh @@ -215,6 +215,7 @@ lib/tests/io/90-stat.c lib/tests/mes/90-abtod.c lib/tests/mes/90-dtoab.c lib/tests/posix/90-execlp.c +lib/tests/posix/90-mmap.c lib/tests/posix/90-unsetenv.c lib/tests/signal/90-signal.c lib/tests/stdio/90-fopen.c diff --git a/lib/tests/posix/90-mmap.c b/lib/tests/posix/90-mmap.c new file mode 100644 index 00000000..b527453f --- /dev/null +++ b/lib/tests/posix/90-mmap.c @@ -0,0 +1,75 @@ +/* -*-comment-start: "//";comment-end:""-*- + * GNU Mes --- Maxwell Equations of Software + * Copyright © 2022 Jan (janneke) Nieuwenhuizen + * + * This file is part of GNU Mes. + * + * GNU Mes 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. + * + * GNU Mes 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 GNU Mes. If not, see . + */ + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +size_t file_size (char const* file_name) +{ + struct stat st; + stat (file_name, &st); + return st.st_size; +} + +int +main (int argc, char const *argv[]) +{ + eputs ("test:mmap\n"); +#if __GNUC__ > 11 + char* file_name = malloc (PATH_MAX); +#else + // FIXME: gcc-12.2.0 fails open using this + char file_name[PATH_MAX]; +#endif + char *srcdir = getenv ("abs_top_srcdir"); + if (! srcdir) // for running by hand + srcdir = "."; + eputs ("srcdir="); + eputs (srcdir); + eputs ("\n"); + strcpy (file_name, srcdir); + strcpy (file_name + strlen (srcdir), "/lib/tests/posix/data/open-read"); + eputs ("test mmap:"); + eputs (file_name); + eputs ("\n"); + + size_t size = file_size (file_name); + int fd = open (file_name, O_RDONLY, 0); + assert (fd != -1); + + void* data = mmap (NULL, size, PROT_READ, MAP_PRIVATE | MAP_POPULATE, fd, 0); + assert(data != MAP_FAILED); + write (1, data, size); + + int rc = munmap (data, size); + assert (rc == 0); + close (fd); + + return 0; +}