All of lore.kernel.org
 help / color / mirror / Atom feed
From: Artem Bityutskiy <dedekind1@gmail.com>
To: MTD list <linux-mtd@lists.infradead.org>
Cc: Adrian Hunter <adrian.hunter@nokia.com>
Subject: [PATCH 25/27] fs-tests: integck: do not use tests_random_no
Date: Wed, 13 Apr 2011 18:19:05 +0300	[thread overview]
Message-ID: <1302707947-6143-26-git-send-email-dedekind1@gmail.com> (raw)
In-Reply-To: <1302707947-6143-1-git-send-email-dedekind1@gmail.com>

From: Artem Bityutskiy <Artem.Bityutskiy@nokia.com>

Do not use the shared 'tests_random_no()' function but use our own
simple implementation instead. We do not need those rather complex
and difficult to understand computations.

Signed-off-by: Artem Bityutskiy <Artem.Bityutskiy@nokia.com>
---
 tests/fs-tests/integrity/integck.c |  119 +++++++++++++++++++----------------
 1 files changed, 65 insertions(+), 54 deletions(-)

diff --git a/tests/fs-tests/integrity/integck.c b/tests/fs-tests/integrity/integck.c
index 5cb7bca..2dbe9de 100644
--- a/tests/fs-tests/integrity/integck.c
+++ b/tests/fs-tests/integrity/integck.c
@@ -196,6 +196,17 @@ static int is_truncation(struct write_info *w)
 }
 
 /*
+ * Return a random number between 0 and max - 1.
+ */
+static unsigned int random_no(unsigned int max)
+{
+	assert(max < RAND_MAX);
+	if (max == 0)
+		return 0;
+	return rand() % max;
+}
+
+/*
  * Allocate a buffer of 'size' bytes and fill it with zeroes.
  */
 static void *zalloc(size_t size)
@@ -567,11 +578,11 @@ static void file_unlink(struct dir_entry_info *entry)
 static struct dir_entry_info *pick_entry(struct file_info *file)
 {
 	struct dir_entry_info *entry;
-	size_t r;
+	unsigned int r;
 
 	if (!file->link_count)
 		return NULL;
-	r = tests_random_no(file->link_count);
+	r = random_no(file->link_count);
 	entry = file->links;
 	while (entry && r--)
 		entry = entry->next_link;
@@ -660,7 +671,7 @@ static struct fd_info *file_open(struct file_info *file)
 	char *path;
 
 	path = dir_path(file->links->parent, file->links->name);
-	if (tests_random_no(100) == 1)
+	if (random_no(100) == 1)
 		flags |= O_SYNC;
 	fd = open(path, flags);
 	CHECK(fd != -1);
@@ -806,30 +817,30 @@ static void get_offset_and_size(struct file_info *file,
 				off_t *offset,
 				size_t *size)
 {
-	size_t r, n;
+	unsigned int r, n;
 
-	r = tests_random_no(100);
+	r = random_no(100);
 	if (r == 0 && grow)
 		/* 1 time in 100, when growing, write off the end of the file */
-		*offset = file->length + tests_random_no(10000000);
+		*offset = file->length + random_no(10000000);
 	else if (r < 4)
 		/* 3 (or 4) times in 100, write at the beginning of file */
 		*offset = 0;
 	else if (r < 52 || !grow)
 		/* 48 times in 100, write into the file */
-		*offset = tests_random_no(file->length);
+		*offset = random_no(file->length);
 	else
 		/* 48 times in 100, write at the end of the  file */
 		*offset = file->length;
 	/* Distribute the size logarithmically */
-	if (tests_random_no(1000) == 0)
-		r = tests_random_no(fsinfo.log10_initial_free + 2);
+	if (random_no(1000) == 0)
+		r = random_no(fsinfo.log10_initial_free + 2);
 	else
-		r = tests_random_no(fsinfo.log10_initial_free);
+		r = random_no(fsinfo.log10_initial_free);
 	n = 1;
 	while (r--)
 		n *= 10;
-	*size = tests_random_no(n);
+	*size = random_no(n);
 	if (!grow && *offset + *size > file->length)
 		*size = file->length - *offset;
 	if (*size == 0)
@@ -875,7 +886,7 @@ static void file_mmap_write(struct file_info *file)
 		write_cnt += 1;
 		w = w->next;
 	}
-	r = tests_random_no(write_cnt);
+	r = random_no(write_cnt);
 	w = file->writes;
 	for (i = 0; w && w->next && i < r; i++)
 		w = w->next;
@@ -897,15 +908,15 @@ static void file_mmap_write(struct file_info *file)
 	CHECK(addr != MAP_FAILED);
 
 	/* Randomly select a part of the mmapped area to write */
-	size = tests_random_no(w->size);
+	size = random_no(w->size);
 	if (size > free_space)
 		size = free_space;
 	if (size == 0)
 		size = 1;
-	offset = w->offset + tests_random_no(w->size - size);
+	offset = w->offset + random_no(w->size - size);
 
 	/* Write it */
-	seed = tests_random_no(MAX_RANDOM_SEED);
+	seed = random_no(MAX_RANDOM_SEED);
 	srand(seed);
 	waddr = addr + (offset - offs);
 	for (i = 0; i < size; i++)
@@ -926,19 +937,19 @@ static void file_write(struct file_info *file, int fd)
 	int truncate = 0;
 
 	if (fsinfo.can_mmap && !full && !file->deleted &&
-	    tests_random_no(100) == 1) {
+	    random_no(100) == 1) {
 		file_mmap_write(file);
 		return;
 	}
 
 	get_offset_and_size(file, &offset, &size);
-	seed = tests_random_no(MAX_RANDOM_SEED);
+	seed = random_no(MAX_RANDOM_SEED);
 	actual = file_write_data(file, fd, offset, size, seed);
 
 	if (offset + actual <= file->length && shrink)
 		/* 1 time in 100, when shrinking
 		   truncate after the write */
-		if (tests_random_no(100) == 0)
+		if (random_no(100) == 0)
 			truncate = 1;
 
 	if (actual != 0)
@@ -1006,7 +1017,7 @@ static void file_truncate(struct file_info *file, int fd)
 {
 	size_t new_length;
 
-	new_length = tests_random_no(file->length);
+	new_length = random_no(file->length);
 
 	if (file_ftruncate(file, fd, new_length))
 		file_truncate_info(file, new_length);
@@ -1431,15 +1442,15 @@ static char *make_name(struct dir_info *dir)
 
 	do {
 		found = 0;
-		if (tests_random_no(5) == 1) {
-			int i, n = tests_random_no(fsinfo.max_name_len) + 1;
+		if (random_no(5) == 1) {
+			int i, n = random_no(fsinfo.max_name_len) + 1;
 
 			CHECK(n > 0 && n < 256);
 			for (i = 0; i < n; i++)
-				name[i] = 'a' + tests_random_no(26);
+				name[i] = 'a' + random_no(26);
 			name[i] = '\0';
 		} else
-			sprintf(name, "%u", (unsigned)tests_random_no(1000000));
+			sprintf(name, "%u", random_no(1000000));
 		for (entry = dir->first; entry; entry = entry->next) {
 			if (strcmp(entry->name, name) == 0) {
 				found = 1;
@@ -1456,9 +1467,9 @@ static struct file_info *pick_file(void)
 
 	for (;;) {
 		struct dir_entry_info *entry;
-		size_t r;
+		unsigned int r;
 
-		r = tests_random_no(dir->number_of_entries);
+		r = random_no(dir->number_of_entries);
 		entry = dir->first;
 		while (entry && r) {
 			entry = entry->next;
@@ -1482,13 +1493,13 @@ static struct dir_info *pick_dir(void)
 {
 	struct dir_info *dir = top_dir;
 
-	if (tests_random_no(40) >= 30)
+	if (random_no(40) >= 30)
 		return dir;
 	for (;;) {
 		struct dir_entry_info *entry;
 		size_t r;
 
-		r = tests_random_no(dir->number_of_entries);
+		r = random_no(dir->number_of_entries);
 		entry = dir->first;
 		while (entry && r) {
 			entry = entry->next;
@@ -1514,7 +1525,7 @@ static struct dir_info *pick_dir(void)
 		if (!entry)
 			return dir;
 		dir = entry->dir;
-		if (tests_random_no(40) >= 30)
+		if (random_no(40) >= 30)
 			return dir;
 	}
 }
@@ -1524,15 +1535,15 @@ static char *pick_rename_name(struct dir_info **parent,
 {
 	struct dir_info *dir = pick_dir();
 	struct dir_entry_info *entry;
-	size_t r;
+	unsigned int r;
 
 	*parent = dir;
 	*rename_entry = NULL;
 
-	if (grow || tests_random_no(20) < 10)
+	if (grow || random_no(20) < 10)
 		return dup_string(make_name(dir));
 
-	r = tests_random_no(dir->number_of_entries);
+	r = random_no(dir->number_of_entries);
 	entry = dir->first;
 	while (entry && r) {
 		entry = entry->next;
@@ -1673,15 +1684,15 @@ static char *pick_symlink_target(const char *symlink_path)
 {
 	struct dir_info *dir;
 	struct dir_entry_info *entry;
-	size_t r;
 	char *path, *rel_path;
+	unsigned int r;
 
 	dir = pick_dir();
 
-	if (tests_random_no(100) < 10)
+	if (random_no(100) < 10)
 		return dir_path(dir, make_name(dir));
 
-	r = tests_random_no(dir->number_of_entries);
+	r = random_no(dir->number_of_entries);
 	entry = dir->first;
 	while (entry && r) {
 		entry = entry->next;
@@ -1692,7 +1703,7 @@ static char *pick_symlink_target(const char *symlink_path)
 	if (!entry)
 		return dir_path(dir, make_name(dir));
 	path = dir_path(dir, entry->name);
-	if (tests_random_no(20) < 10)
+	if (random_no(20) < 10)
 		return path;
 	rel_path = relative_path(symlink_path, path);
 	free(path);
@@ -1742,20 +1753,20 @@ static void operate_on_file(struct file_info *file);
 static void operate_on_entry(struct dir_entry_info *entry)
 {
 	/* 1 time in 1000 rename */
-	if (tests_random_no(1000) == 0) {
+	if (random_no(1000) == 0) {
 		rename_entry(entry);
 		return;
 	}
 	if (entry->type == 's') {
 		symlink_check(entry->symlink);
 		/* If shrinking, 1 time in 50, remove a symlink */
-		if (shrink && tests_random_no(50) == 0)
+		if (shrink && random_no(50) == 0)
 			symlink_remove(entry->symlink);
 		return;
 	}
 	if (entry->type == 'd') {
 		/* If shrinking, 1 time in 50, remove a directory */
-		if (shrink && tests_random_no(50) == 0) {
+		if (shrink && random_no(50) == 0) {
 			dir_remove(entry->dir);
 			return;
 		}
@@ -1763,13 +1774,13 @@ static void operate_on_entry(struct dir_entry_info *entry)
 	}
 	if (entry->type == 'f') {
 		/* If shrinking, 1 time in 10, remove a file */
-		if (shrink && tests_random_no(10) == 0) {
+		if (shrink && random_no(10) == 0) {
 			file_delete(entry->file);
 			return;
 		}
 		/* If not growing, 1 time in 10, unlink a file with links > 1 */
 		if (!grow && entry->file->link_count > 1 &&
-		    tests_random_no(10) == 0) {
+		    random_no(10) == 0) {
 			file_unlink_file(entry->file);
 			return;
 		}
@@ -1780,11 +1791,11 @@ static void operate_on_entry(struct dir_entry_info *entry)
 /* Randomly select something to do with a directory */
 static void operate_on_dir(struct dir_info *dir)
 {
-	size_t r;
 	struct dir_entry_info *entry;
 	struct file_info *file;
+	unsigned int r;
 
-	r = tests_random_no(14);
+	r = random_no(14);
 	if (r == 0 && grow)
 		/* When growing, 1 time in 14 create a file */
 		file_new(dir, make_name(dir));
@@ -1794,12 +1805,12 @@ static void operate_on_dir(struct dir_info *dir)
 	else if (r == 2 && grow && (file = pick_file()) != NULL)
 		/* When growing, 1 time in 14 create a hard link */
 		link_new(dir, make_name(dir), file);
-	else if (r == 3 && grow && tests_random_no(5) == 0)
+	else if (r == 3 && grow && random_no(5) == 0)
 		/* When growing, 1 time in 70 create a symbolic link */
 		symlink_new(dir, make_name(dir));
 	else {
 		/* Otherwise randomly select an entry to operate on */
-		r = tests_random_no(dir->number_of_entries);
+		r = random_no(dir->number_of_entries);
 		entry = dir->first;
 		while (entry && r) {
 			entry = entry->next;
@@ -1819,24 +1830,24 @@ static void operate_on_file(struct file_info *file)
 		return;
 	}
 	/* Try to keep about 20 files open */
-	if (open_files_count < 20 && tests_random_no(2) == 0) {
+	if (open_files_count < 20 && random_no(2) == 0) {
 		file_open(file);
 		return;
 	}
 	/* Try to keep up to 40 files open */
-	if (open_files_count < 40 && tests_random_no(20) == 0) {
+	if (open_files_count < 40 && random_no(20) == 0) {
 		file_open(file);
 		return;
 	}
 	/* Occasionly truncate */
-	if (shrink && tests_random_no(100) == 0) {
+	if (shrink && random_no(100) == 0) {
 		file_truncate_file(file);
 		return;
 	}
 	/* Mostly just write */
 	file_write_file(file);
 	/* Once in a while check it too */
-	if (tests_random_no(100) == 1) {
+	if (random_no(100) == 1) {
 		int fd = -2;
 
 		if (file->links)
@@ -1853,9 +1864,9 @@ static void operate_on_file(struct file_info *file)
 /* Randomly select something to do with an open file */
 static void operate_on_open_file(struct fd_info *fdi)
 {
-	size_t r;
+	unsigned int r;
 
-	r = tests_random_no(1000);
+	r = random_no(1000);
 	if (shrink && r < 5)
 		file_truncate(fdi->file, fdi->fd);
 	else if (r < 21)
@@ -1865,7 +1876,7 @@ static void operate_on_open_file(struct fd_info *fdi)
 	else {
 		file_write(fdi->file, fdi->fd);
 		if (r >= 999) {
-			if (tests_random_no(100) >= 50)
+			if (random_no(100) >= 50)
 				CHECK(fsync(fdi->fd) == 0);
 			else
 				CHECK(fdatasync(fdi->fd) == 0);
@@ -1876,7 +1887,7 @@ static void operate_on_open_file(struct fd_info *fdi)
 /* Select an open file at random */
 static void operate_on_an_open_file(void)
 {
-	size_t r;
+	unsigned int r;
 	struct open_file_info *ofi;
 
 	/* When shrinking, close all open files 1 time in 128 */
@@ -1904,7 +1915,7 @@ static void operate_on_an_open_file(void)
 				ofi = ofi->next;
 		}
 	}
-	r = tests_random_no(open_files_count);
+	r = random_no(open_files_count);
 	for (ofi = open_files; ofi; ofi = ofi->next, --r)
 		if (!r) {
 			operate_on_open_file(ofi->fdi);
@@ -1915,7 +1926,7 @@ static void operate_on_an_open_file(void)
 static void do_an_operation(void)
 {
 	/* Half the time operate on already open files */
-	if (tests_random_no(100) < 50)
+	if (random_no(100) < 50)
 		operate_on_dir(top_dir);
 	else
 		operate_on_an_open_file();
-- 
1.7.2.3

  parent reply	other threads:[~2011-04-13 15:16 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-04-13 15:18 [PATCH 00/27] mtd-utils: make integck test independent Artem Bityutskiy
2011-04-13 15:16 ` Artem Bityutskiy
2011-04-13 15:18 ` [PATCH 01/27] fs-tests: integck: shrink dir_entry_info structure size Artem Bityutskiy
2011-04-13 15:18 ` [PATCH 02/27] fs-tests: integck: shrink file_info " Artem Bityutskiy
2011-04-13 15:18 ` [PATCH 03/27] fs-tests: integck: srink file_info structure even more Artem Bityutskiy
2011-04-13 15:18 ` [PATCH 04/27] fs-tests: integck: abuse random_offset field nicer Artem Bityutskiy
2011-04-13 15:18 ` [PATCH 05/27] fs-tests: integck: shrink write_info even more Artem Bityutskiy
2011-04-13 15:18 ` [PATCH 06/27] fs-tests: integck: change tests defaults Artem Bityutskiy
2011-04-13 15:18 ` [PATCH 07/27] fs-tests: integck: implement own parameters parsing Artem Bityutskiy
2011-04-13 15:18 ` [PATCH 08/27] fs-tests: integck: clean-up copy_string Artem Bityutskiy
2011-04-13 15:18 ` [PATCH 09/27] fs-tests: integck: get rid of tests_check_test_file_system Artem Bityutskiy
2011-04-13 15:18 ` [PATCH 10/27] fs-tests: integck: make integck function return error Artem Bityutskiy
2011-04-13 15:18 ` [PATCH 11/27] fs-tests: integck: move mem_page_size to fsinfo Artem Bityutskiy
2011-04-13 15:18 ` [PATCH 12/27] fs-tests: integck: move more variables " Artem Bityutskiy
2011-04-13 15:18 ` [PATCH 13/27] fs-tests: integck: add own get_free_space function Artem Bityutskiy
2011-04-13 15:18 ` [PATCH 14/27] fs-tests: integck: move log10_initial_free to fsinfo Artem Bityutskiy
2011-04-13 15:18 ` [PATCH 15/27] fs-tests: integck: remove trailing backslashes from mount point Artem Bityutskiy
2011-04-13 15:18 ` [PATCH 16/27] fs-tests: integck: do not use tests_cat_pid Artem Bityutskiy
2011-04-13 15:18 ` [PATCH 17/27] fs-tests: integck: clean up test directory creation Artem Bityutskiy
2011-04-13 15:18 ` [PATCH 18/27] fs-tests: integck: do not use tests_clear_dir Artem Bityutskiy
2011-04-13 15:18 ` [PATCH 19/27] fs-tests: integck: do not use space after cast Artem Bityutskiy
2011-04-13 15:19 ` [PATCH 20/27] fs-tests: integck: prefer to check for success Artem Bityutskiy
2011-04-13 15:19 ` [PATCH 21/27] fs-tests: integck: do not use tests_fs_is_rootfs Artem Bityutskiy
2011-04-13 15:19 ` [PATCH 22/27] fs-tests: integck: do not use tests_remount Artem Bityutskiy
2011-04-13 15:19 ` [PATCH 23/27] fs-tests: integck: do not use tests_get_total_space Artem Bityutskiy
2011-04-13 15:19 ` [PATCH 24/27] fs-tests: integck: do not use global common variables Artem Bityutskiy
2011-04-13 15:19 ` Artem Bityutskiy [this message]
2011-04-13 15:19 ` [PATCH 26/27] fs-tests: integck: implement own version of CHECK Artem Bityutskiy
2011-04-13 15:19 ` [PATCH 27/27] fs-tests: integck: do not expect max name length to be 256 Artem Bityutskiy

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=1302707947-6143-26-git-send-email-dedekind1@gmail.com \
    --to=dedekind1@gmail.com \
    --cc=adrian.hunter@nokia.com \
    --cc=linux-mtd@lists.infradead.org \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.