Skip to content

Commit 0e8828b

Browse files
committed
test: Clean up files created in a few tests
test-fgets-eof, test-ungetc-ftell and test-wchar all created test files but failed to remove them afterwards. posix-io wasn't using the per-target test filename provided by meson. Signed-off-by: Keith Packard <[email protected]>
1 parent 0a72073 commit 0e8828b

File tree

4 files changed

+37
-15
lines changed

4 files changed

+37
-15
lines changed

test/posix-io.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@
3838
#include <stdlib.h>
3939
#include <string.h>
4040

41-
static const char file_name[] = "posix-io-test-file";
41+
#ifndef TEST_FILE_NAME
42+
#define TEST_FILE_NAME "posix-io-test-file"
43+
#endif
44+
45+
static const char file_name[] = TEST_FILE_NAME;
4246
static const char test_string[] = "hello, world\n";
4347

4448
static void test_cleanup(void)

test/test-fgets-eof.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535

3636
#include <stdio.h>
3737
#include <string.h>
38+
#include <unistd.h>
3839

3940
#ifndef TEST_FILE_NAME
4041
#define TEST_FILE_NAME "FGETSEOF.TXT"
@@ -56,7 +57,10 @@ int main(void)
5657
fclose(file);
5758

5859
file = fopen( TEST_FILE_NAME, "r" );
59-
if(file == NULL) return 1;
60+
if(file == NULL) {
61+
unlink( TEST_FILE_NAME );
62+
return 1;
63+
}
6064

6165
/*Calling fgets to reach EOF and check on the returned value*/
6266
pchar = fgets( line, 12, file);
@@ -76,6 +80,7 @@ int main(void)
7680
}
7781

7882
fclose( file );
83+
unlink( TEST_FILE_NAME );
7984

8085
return ret;
8186
}

test/test-ungetc-ftell.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535

3636
#include <stdio.h>
3737
#include <string.h>
38+
#include <unistd.h>
3839

3940
#ifndef TEST_FILE_NAME
4041
#define TEST_FILE_NAME "UNGETCFTELL.DAT"
@@ -54,7 +55,10 @@ int main(void) {
5455
fclose(file);
5556

5657
file = fopen( TEST_FILE_NAME, "rb" );
57-
if(file == NULL) return 1;
58+
if(file == NULL) {
59+
unlink(TEST_FILE_NAME);
60+
return 1;
61+
}
5862

5963
first = fgetc(file);
6064
printf("First character read: %c\n", first);
@@ -71,9 +75,11 @@ int main(void) {
7175

7276
if (position == 0) {
7377
printf("Test passed: ungetc and ftell working as expected.\n");
78+
unlink(TEST_FILE_NAME);
7479
return 0;
7580
} else {
7681
printf("Test failed: Incorrect position after ungetc.\n");
82+
unlink(TEST_FILE_NAME);
7783
return 1;
7884
}
7985
}

test/test-wchar.c

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* SPDX-License-Identifier: BSD-3-Clause
3-
*
3+
*
44
* Copyright © 2024, Synopsys Inc.
55
* Copyright © 2024, Solid Sands B.V.
66
*
@@ -36,7 +36,11 @@
3636

3737
#include <stdio.h>
3838
#include <wchar.h>
39+
#include <unistd.h>
3940

41+
#ifndef TEST_FILE_NAME
42+
#define TEST_FILE_NAME "WCHAR.DAT"
43+
#endif
4044

4145
const wchar_t *string = L"Hello\n";
4246

@@ -47,31 +51,34 @@ int main (void)
4751
wint_t res;
4852

4953
/* Create testfile.dat in write mode */
50-
file = fopen("testfile.dat", "w");
54+
file = fopen(TEST_FILE_NAME, "w");
5155
if (file == NULL) return 1;
52-
56+
5357
/* Write wide characters to the file */
5458
fputws(string, file);
55-
fclose(file);
56-
59+
fclose(file);
60+
5761
/* Open testfile.dat in read mode */
58-
file = fopen("testfile.dat", "r");
62+
file = fopen(TEST_FILE_NAME, "r");
63+
64+
for (ref = string; *ref != L'\0'; ref++) {
5965

60-
for (ref = string; *ref != L'\0'; ref++) {
61-
6266
/* Read wide-char by wide-char from the file */
63-
res = fgetwc(file);
67+
res = fgetwc(file);
6468
if((wchar_t) res != *ref) {
6569
printf("Test Failed: Failed to read wide character from file\n");
70+
unlink(TEST_FILE_NAME);
6671
return 1;
6772
}
68-
}
73+
}
6974

7075
if(fgetwc(file) != WEOF) {
7176
printf("Test Failed: Failed to check if end-of-file reached\n");
72-
return 1;
77+
unlink(TEST_FILE_NAME);
78+
return 1;
7379
}
74-
80+
7581
printf("Test Passed: Wide characters were written & read successfuly\n");
82+
unlink(TEST_FILE_NAME);
7683
return 0;
7784
}

0 commit comments

Comments
 (0)