From: Cyril Hrubis <chrubis@suse.cz>
To: ltp@lists.linux.it
Subject: [LTP] [PATCH 1/6] syscalls/madvise01: Convert to new test API
Date: Tue, 17 May 2016 17:21:48 +0200 [thread overview]
Message-ID: <20160517152148.GA13705@rei.suse.cz> (raw)
In-Reply-To: <1463042993-398-1-git-send-email-liwang@redhat.com>
Hi!
> @@ -32,107 +32,79 @@
> #include <string.h>
> #include <unistd.h>
>
> -#include "test.h"
> +#include "tst_test.h"
>
> -static void setup(void);
> -static void cleanup(void);
> -static void check_and_print(char *advice);
> +static char filename[64];
>
> -char *TCID = "madvise01";
> -int TST_TOTAL = 5;
> +static void setup(void)
> +{
> + sprintf(filename, "madvise01.%d", getpid());
> +}
>
> -int main(int argc, char *argv[])
> +static void check_and_print(char *advice)
> +{
> + if (TEST_RETURN == -1)
> + tst_res(TFAIL | TTERRNO, "madvise test for %s failed", advice);
> + else
> + tst_res(TPASS, "madvise test for %s PASSED", advice);
> +}
> +
> +static void verify_madvise(void)
> {
> - int lc, fd;
> int i = 0;
> + int fd;
> char *file = NULL;
> struct stat stat;
> - char filename[64];
> - char *progname = NULL;
> char *str_for_file = "abcdefghijklmnopqrstuvwxyz12345\n";
>
> - tst_parse_opts(argc, argv, NULL, NULL);
> -
> - setup();
> -
> - progname = *argv;
> - sprintf(filename, "%s-out.%d", progname, getpid());
> -
> - for (lc = 0; TEST_LOOPING(lc); lc++) {
> - tst_count = 0;
> -
> - fd = open(filename, O_RDWR | O_CREAT, 0664);
> - if (fd < 0)
> - tst_brkm(TBROK | TERRNO, cleanup, "open failed");
> + fd = open(filename, O_RDWR | O_CREAT, 0664);
> + if (fd < 0)
> + tst_brk(TBROK | TERRNO, "open failed");
SAFE_OPEN()
> #ifdef DEBUG
> - tst_resm(TINFO, "filename = %s opened successfully", filename);
> + tst_res(TINFO, "filename = %s opened successfully", filename);
> #endif
>
> - /* Writing 40 KB of random data into this file
> - [32 * 1280 = 40960] */
> - for (i = 0; i < 1280; i++)
> - if (write(fd, str_for_file, strlen(str_for_file)) == -1)
> - tst_brkm(TBROK | TERRNO, cleanup,
> - "write failed");
> -
> - if (fstat(fd, &stat) == -1)
> - tst_brkm(TBROK, cleanup, "fstat failed");
> -
> - /* Map the input file into memory */
> - file = mmap(NULL, stat.st_size, PROT_READ, MAP_SHARED, fd, 0);
> - if (file == MAP_FAILED)
> - tst_brkm(TBROK, cleanup, "mmap failed");
> + /* Writing 40 KB of random data into this file
> + [32 * 1280 = 40960] */
> + for (i = 0; i < 1280; i++)
> + if (write(fd, str_for_file, strlen(str_for_file)) == -1)
> + tst_brk(TBROK | TERRNO, "write failed");
SAFE_WRITE()
> - /* (1) Test case for MADV_NORMAL */
> - TEST(madvise(file, stat.st_size, MADV_NORMAL));
> - check_and_print("MADV_NORMAL");
> + if (fstat(fd, &stat) == -1)
> + tst_brk(TBROK, "fstat failed");
SAFE_STAT()
> - /* (2) Test case for MADV_RANDOM */
> - TEST(madvise(file, stat.st_size, MADV_RANDOM));
> - check_and_print("MADV_RANDOM");
> + /* Map the input file into memory */
> + file = SAFE_MMAP(NULL, stat.st_size, PROT_READ, MAP_SHARED, fd, 0);
SAFE_MMAP()
Moreover the whole intialization up to this point should be done in
setup().
> - /* (3) Test case for MADV_SEQUENTIAL */
> - TEST(madvise(file, stat.st_size, MADV_SEQUENTIAL));
> - check_and_print("MADV_SEQUENTIAL");
> + /* (1) Test case for MADV_NORMAL */
> + TEST(madvise(file, stat.st_size, MADV_NORMAL));
> + check_and_print("MADV_NORMAL");
>
> - /* (4) Test case for MADV_WILLNEED */
> - TEST(madvise(file, stat.st_size, MADV_WILLNEED));
> - check_and_print("MADV_WILLNEED");
> + /* (2) Test case for MADV_RANDOM */
> + TEST(madvise(file, stat.st_size, MADV_RANDOM));
> + check_and_print("MADV_RANDOM");
>
> - /* (5) Test case for MADV_DONTNEED */
> - TEST(madvise(file, stat.st_size, MADV_DONTNEED));
> - check_and_print("MADV_DONTNEED");
> + /* (3) Test case for MADV_SEQUENTIAL */
> + TEST(madvise(file, stat.st_size, MADV_SEQUENTIAL));
> + check_and_print("MADV_SEQUENTIAL");
>
> - if (munmap(file, stat.st_size) == -1)
> - tst_brkm(TBROK | TERRNO, cleanup, "munmap failed");
> + /* (4) Test case for MADV_WILLNEED */
> + TEST(madvise(file, stat.st_size, MADV_WILLNEED));
> + check_and_print("MADV_WILLNEED");
>
> - close(fd);
> - }
> + /* (5) Test case for MADV_DONTNEED */
> + TEST(madvise(file, stat.st_size, MADV_DONTNEED));
> + check_and_print("MADV_DONTNEED");
This can be easily converted to an array of structures describing the
test and exported as separate tests in the struct tst_test
> - cleanup();
> - tst_exit();
> -}
> -
> -static void setup(void)
> -{
> + if (munmap(file, stat.st_size) == -1)
> + tst_brk(TBROK | TERRNO, "munmap failed");
The munmap() should rather be done in cleanup but only if the file has
been mmaped().
> - tst_sig(NOFORK, DEF_HANDLER, cleanup);
> -
> - TEST_PAUSE;
> -
> - tst_tmpdir();
> + close(fd);
The fd can be closed right after the mmap().
> }
>
> -static void cleanup(void)
> -{
> - tst_rmdir();
> -
> -}
> -
> -static void check_and_print(char *advice)
> -{
> - if (TEST_RETURN == -1)
> - tst_resm(TFAIL | TTERRNO, "madvise test for %s failed", advice);
> - else
> - tst_resm(TPASS, "madvise test for %s PASSED", advice);
> -}
> +static struct tst_test test = {
> + .tid = "madvise01",
> + .test_all = verify_madvise,
> + .needs_tmpdir = 1,
> + .setup = setup,
> +};
> --
> 1.8.3.1
>
>
> --
> Mailing list info: https://lists.linux.it/listinfo/ltp
--
Cyril Hrubis
chrubis@suse.cz
prev parent reply other threads:[~2016-05-17 15:21 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-05-12 8:49 [LTP] [PATCH 1/6] syscalls/madvise01: Convert to new test API Li Wang
2016-05-12 8:49 ` [LTP] [PATCH 2/6] syscalls/madvise02: " Li Wang
2016-05-12 8:49 ` [LTP] [PATCH 3/6] syscalls/madvise03: " Li Wang
2016-05-12 8:49 ` [LTP] [PATCH 4/6] syscalls/madvise04: " Li Wang
2016-05-12 8:49 ` [LTP] [PATCH 5/6] syscalls/madvise05: " Li Wang
2016-05-12 8:49 ` [LTP] [PATCH 6/6] syscalls/madvise06: " Li Wang
2016-05-17 17:43 ` [LTP] [PATCH 5/6] syscalls/madvise05: " Cyril Hrubis
2016-05-17 17:28 ` [LTP] [PATCH 4/6] syscalls/madvise04: " Cyril Hrubis
2016-05-17 16:20 ` [LTP] [PATCH 3/6] syscalls/madvise03: " Cyril Hrubis
2016-05-18 9:04 ` Li Wang
2016-05-19 10:12 ` Li Wang
2016-05-19 10:19 ` Cyril Hrubis
2016-05-17 15:54 ` [LTP] [PATCH 2/6] syscalls/madvise02: " Cyril Hrubis
2016-05-17 15:21 ` Cyril Hrubis [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20160517152148.GA13705@rei.suse.cz \
--to=chrubis@suse.cz \
--cc=ltp@lists.linux.it \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox