[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [PATCH] test-freading: remove a temporary file
From: |
Eric Blake |
Subject: |
Re: [PATCH] test-freading: remove a temporary file |
Date: |
Mon, 16 Nov 2009 06:28:30 -0700 |
User-agent: |
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.23) Gecko/20090812 Thunderbird/2.0.0.23 Mnenhy/0.7.6.666 |
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
According to Jim Meyering on 11/15/2009 9:21 AM:
>> The second unlink is better omitted, since when the message "file operations
>> failed"
>> message is printed, the user will likely want to inspect the contents of the
>> file and fix
>> the test.
>
> Hi Bruno,
>
> I think it's better to clean up unconditionally.
> Anyone who is serious about investigating a failure can be
> expected to run the debugger.
>
> Since Eric wrote that test, it's his call.
Actually, Bruno's written about as much of that test as I have, but yes, I
had the first commit. But that was back in the days before the idiom of
calling ASSERT at every syscall, to stop testing the moment a failure is
encountered, even if the failure is unrelated to the function under test.
Since the test doesn't use ungetc, which is about the only stdio function
where I know we still have problems on older libc implementations, I don't
see any harm in failing instead of skipping this test if our assumptions fail.
I'll push this patch instead:
- --
Don't work too hard, make some time for fun as well!
Eric Blake address@hidden
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (Cygwin)
Comment: Public key at home.comcast.net/~ericblake/eblake.gpg
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
iEYEARECAAYFAksBU34ACgkQ84KuGfSFAYD1aQCeNT4vQcKn06YlHUwnDyhk+EIU
UD4AoKjOwIgMVzrSrdJD7VuVoVxzw3QL
=THch
-----END PGP SIGNATURE-----
>From 85ab667ee565d80f8fab1d5ad337a040e1588b33 Mon Sep 17 00:00:00 2001
From: Eric Blake <address@hidden>
Date: Mon, 16 Nov 2009 06:27:10 -0700
Subject: [PATCH] test-freading: clean up temporary file
* tests/test-freading.c (main): Remove file on success, and use
ASSERT more liberally.
Reported by Jim Meyering.
Signed-off-by: Eric Blake <address@hidden>
---
ChangeLog | 7 +++
tests/test-freading.c | 112 +++++++++++++++++--------------------------------
2 files changed, 46 insertions(+), 73 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index 17e3888..f8d5343 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2009-11-16 Eric Blake <address@hidden>
+
+ test-freading: clean up temporary file
+ * tests/test-freading.c (main): Remove file on success, and use
+ ASSERT more liberally.
+ Reported by Jim Meyering.
+
2009-11-15 Eric Blake <address@hidden>
setenv: work around FreeBSD bug
diff --git a/tests/test-freading.c b/tests/test-freading.c
index dfa5ffb..eb5699d 100644
--- a/tests/test-freading.c
+++ b/tests/test-freading.c
@@ -30,15 +30,15 @@
#endif
#define ASSERT(expr) \
- do \
- { \
- if (!(expr)) \
- { \
+ do \
+ { \
+ if (!(expr)) \
+ { \
fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
- fflush (stderr); \
- abort (); \
- } \
- } \
+ fflush (stderr); \
+ abort (); \
+ } \
+ } \
while (0)
#define TESTFILE "t-freading.tmp"
@@ -50,39 +50,29 @@ main (void)
/* Create a file with some contents. Write-only file is never reading. */
fp = fopen (TESTFILE, "w");
- if (fp == NULL)
- goto skip;
+ ASSERT (fp);
ASSERT (!freading (fp));
- if (fwrite ("foobarsh", 1, 8, fp) < 8)
- goto skip;
+ ASSERT (fwrite ("foobarsh", 1, 8, fp) == 8);
ASSERT (!freading (fp));
- if (fclose (fp))
- goto skip;
+ ASSERT (fclose (fp) == 0);
/* Open it in read-only mode. Read-only file is always reading. */
fp = fopen (TESTFILE, "r");
- if (fp == NULL)
- goto skip;
+ ASSERT (fp);
ASSERT (freading (fp));
- if (fgetc (fp) != 'f')
- goto skip;
+ ASSERT (fgetc (fp) == 'f');
ASSERT (freading (fp));
- if (fseek (fp, 2, SEEK_CUR))
- goto skip;
+ ASSERT (fseek (fp, 2, SEEK_CUR) == 0);
ASSERT (freading (fp));
- if (fgetc (fp) != 'b')
- goto skip;
+ ASSERT (fgetc (fp) == 'b');
ASSERT (freading (fp));
fflush (fp);
ASSERT (freading (fp));
- if (fgetc (fp) != 'a')
- goto skip;
+ ASSERT (fgetc (fp) == 'a');
ASSERT (freading (fp));
- if (fseek (fp, 0, SEEK_END))
- goto skip;
+ ASSERT (fseek (fp, 0, SEEK_END) == 0);
ASSERT (freading (fp));
- if (fclose (fp))
- goto skip;
+ ASSERT (fclose (fp) == 0);
/* Open it in read-write mode. POSIX requires a reposition (fseek,
fsetpos, rewind) or EOF when transitioning from read to write;
@@ -91,33 +81,25 @@ main (void)
at EOF. */
/* First a scenario with only fgetc, fseek, fputc. */
fp = fopen (TESTFILE, "r+");
- if (fp == NULL)
- goto skip;
+ ASSERT (fp);
ASSERT (!freading (fp));
- if (fgetc (fp) != 'f')
- goto skip;
+ ASSERT (fgetc (fp) == 'f');
ASSERT (freading (fp));
- if (fseek (fp, 2, SEEK_CUR))
- goto skip;
+ ASSERT (fseek (fp, 2, SEEK_CUR) == 0);
/* freading (fp) is undefined here, but fwriting (fp) is false. */
- if (fgetc (fp) != 'b')
- goto skip;
+ ASSERT (fgetc (fp) == 'b');
ASSERT (freading (fp));
/* This fseek call is necessary when switching from reading to writing.
See the description of fopen(), ISO C 99 7.19.5.3.(6). */
- if (fseek (fp, 0, SEEK_CUR) != 0)
- goto skip;
+ ASSERT (fseek (fp, 0, SEEK_CUR) == 0);
/* freading (fp) is undefined here, but fwriting (fp) is false. */
- if (fputc ('x', fp) != 'x')
- goto skip;
+ ASSERT (fputc ('x', fp) == 'x');
ASSERT (!freading (fp));
- if (fseek (fp, 0, SEEK_END))
- goto skip;
+ ASSERT (fseek (fp, 0, SEEK_END) == 0);
/* freading (fp) is undefined here, because on some implementations (e.g.
glibc) fseek causes a buffer to be read.
fwriting (fp) is undefined as well. */
- if (fclose (fp))
- goto skip;
+ ASSERT (fclose (fp) == 0);
/* Open it in read-write mode. POSIX requires a reposition (fseek,
fsetpos, rewind) or EOF when transitioning from read to write;
@@ -126,53 +108,37 @@ main (void)
at EOF. */
/* Here a scenario that includes fflush. */
fp = fopen (TESTFILE, "r+");
- if (fp == NULL)
- goto skip;
+ ASSERT (fp);
ASSERT (!freading (fp));
- if (fgetc (fp) != 'f')
- goto skip;
+ ASSERT (fgetc (fp) == 'f');
ASSERT (freading (fp));
- if (fseek (fp, 2, SEEK_CUR))
- goto skip;
+ ASSERT (fseek (fp, 2, SEEK_CUR) == 0);
/* freading (fp) is undefined here, but fwriting (fp) is false. */
- if (fgetc (fp) != 'b')
- goto skip;
+ ASSERT (fgetc (fp) == 'b');
ASSERT (freading (fp));
fflush (fp);
/* freading (fp) is undefined here, but fwriting (fp) is false. */
- if (fgetc (fp) != 'x')
- goto skip;
+ ASSERT (fgetc (fp) == 'x');
ASSERT (freading (fp));
/* This fseek call is necessary when switching from reading to writing.
See the description of fopen(), ISO C 99 7.19.5.3.(6). */
- if (fseek (fp, 0, SEEK_CUR) != 0)
- goto skip;
+ ASSERT (fseek (fp, 0, SEEK_CUR) == 0);
/* freading (fp) is undefined here, but fwriting (fp) is false. */
- if (fputc ('z', fp) != 'z')
- goto skip;
+ ASSERT (fputc ('z', fp) == 'z');
ASSERT (!freading (fp));
- if (fseek (fp, 0, SEEK_END))
- goto skip;
+ ASSERT (fseek (fp, 0, SEEK_END) == 0);
/* freading (fp) is undefined here, because on some implementations (e.g.
glibc) fseek causes a buffer to be read.
fwriting (fp) is undefined as well. */
- if (fclose (fp))
- goto skip;
+ ASSERT (fclose (fp) == 0);
/* Open it in append mode. Write-only file is never reading. */
fp = fopen (TESTFILE, "a");
- if (fp == NULL)
- goto skip;
+ ASSERT (fp);
ASSERT (!freading (fp));
- if (fwrite ("bla", 1, 3, fp) < 3)
- goto skip;
+ ASSERT (fwrite ("bla", 1, 3, fp) == 3);
ASSERT (!freading (fp));
- if (fclose (fp))
- goto skip;
-
+ ASSERT (fclose (fp) == 0);
+ ASSERT (remove (TESTFILE) == 0);
return 0;
-
- skip:
- fprintf (stderr, "Skipping test: file operations failed.\n");
- return 77;
}
--
1.6.5.rc1