Skip to content

Commit a6e1f5c

Browse files
authored
[test] Cleanup test_files. NFC (#22036)
1 parent f080b30 commit a6e1f5c

File tree

3 files changed

+49
-26
lines changed

3 files changed

+49
-26
lines changed

test/test_core.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -5423,11 +5423,7 @@ def test_files(self):
54235423

54245424
create_file('test.file', 'some data')
54255425

5426-
def clean(out):
5427-
return '\n'.join([line for line in out.split('\n') if 'binaryen' not in line and 'wasm' not in line and 'so not running' not in line])
5428-
5429-
self.do_runf('files.cpp', ('size: 7\ndata: 100,-56,50,25,10,77,123\nloop: 100 -56 50 25 10 77 123 \ninput:hi there!\ntexto\n$\n5 : 10,30,20,11,88\nother=some data.\nseeked=me da.\nseeked=ata.\nseeked=ta.\nfscanfed: 10 - hello\n5 bytes to dev/null: 5\nok.\ntexte\n', 'size: 7\ndata: 100,-56,50,25,10,77,123\nloop: 100 -56 50 25 10 77 123 \ninput:hi there!\ntexto\ntexte\n$\n5 : 10,30,20,11,88\nother=some data.\nseeked=me da.\nseeked=ata.\nseeked=ta.\nfscanfed: 10 - hello\n5 bytes to dev/null: 5\nok.\n'),
5430-
output_nicerizer=clean)
5426+
self.do_run_in_out_file_test('test_files.c')
54315427

54325428
def test_files_m(self):
54335429
# Test for Module.stdin etc.

test/files.cpp renamed to test/test_files.c

+33-21
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@
1111
#include <sys/stat.h>
1212
#include <unistd.h>
1313

14-
int main()
15-
{
16-
// Reading
17-
14+
// Reading
15+
void test_reading() {
1816
FILE *file = fopen("somefile.binary", "rb");
1917
assert(file);
2018

@@ -38,28 +36,29 @@ int main()
3836
free(buffer);
3937

4038
// Do it again, with a loop on feof
41-
4239
printf("loop: ");
4340
file = fopen("somefile.binary", "rb");
4441
assert(file);
4542
while (!feof(file)) {
4643
char c = fgetc(file);
4744
if (c != EOF) printf("%d ", c);
4845
}
49-
fclose (file);
46+
fclose(file);
5047
printf("\n");
48+
}
5149

52-
// Standard streams
53-
50+
// Standard streams
51+
void test_stdstreams() {
5452
char gets_buffer[1024];
55-
printf("input:%s\n", gets(gets_buffer));
53+
printf("input:%s\n", fgets(gets_buffer, 1024, stdin));
5654
fwrite("texto\n", 1, 6, stdout);
5755
fwrite("texte\n", 1, 6, stderr);
5856
putchar('$');
5957
putc('\n', stdout);
58+
}
6059

61-
// Writing
62-
60+
// Writing
61+
void test_writing() {
6362
char data[5] = { 10, 30, 20, 11, 88 };
6463
FILE *outf = fopen("go.out", "wb");
6564
fwrite(data, 1, 5, outf);
@@ -73,14 +72,16 @@ int main()
7372
int num = fread(data2, 1, 10, inf);
7473
fclose(inf);
7574
printf("%d : %d,%d,%d,%d,%d\n", num, data2[0], data2[1], data2[2], data2[3], data2[4]);
75+
}
7676

77+
void test_seeking() {
7778
// Test reading a file that has not been cached
78-
79+
7980
FILE *other = fopen("test.file", "r");
8081
assert(other);
8182

8283
char otherData[1000];
83-
num = fread(otherData, 1, 9, other);
84+
int num = fread(otherData, 1, 9, other);
8485
otherData[num] = 0;
8586
printf("other=%s.\n", otherData);
8687

@@ -102,21 +103,24 @@ int main()
102103
printf("seeked=%s.\n", otherData);
103104

104105
fclose(other);
106+
}
105107

106-
// fscanf
107-
108-
outf = fopen("fscan.f", "w");
108+
// fscanf
109+
void test_fscanf() {
110+
FILE* outf = fopen("fscan.f", "w");
109111
fprintf(outf, "10 hello");
110112
fclose(outf);
111113

112114
int number;
113115
char text[100];
114-
inf = fopen("fscan.f", "r");
115-
num = fscanf(inf, "%d %s", &number, text);
116+
FILE* inf = fopen("fscan.f", "r");
117+
int num = fscanf(inf, "%d %s", &number, text);
116118
fclose(inf);
117119
printf("fscanfed: %d - %s\n", number, text);
120+
}
118121

119-
// temp files
122+
// temp files
123+
void test_tempfiles() {
120124
const char *tname = "file_XXXXXX";
121125
char tname1[100];
122126
char tname2[100];
@@ -142,20 +146,28 @@ int main()
142146
assert(strncmp("/tmp/", str, 5) == 0);
143147
}
144148

149+
char data[5] = { 10, 30, 20, 11, 88 };
145150
FILE *n = fopen("/dev/null", "w");
146151
printf("5 bytes to dev/null: %zu\n", fwrite(data, 1, 5, n));
147152
fclose(n);
148-
153+
149154
// Test file creation with O_TRUNC (regression test for #16784)
150155
const char *file_name = "test.out";
151156
int fd = open(file_name, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR);
152157
assert(fd >= 0);
153158
int status = write(fd, "blablabla\n", 10);
154159
assert(status == 10);
155160
close(fd);
161+
}
156162

163+
int main() {
164+
test_reading();
165+
test_stdstreams();
166+
test_writing();
167+
test_seeking();
168+
test_fscanf();
169+
test_tempfiles();
157170
printf("ok.\n");
158-
159171
return 0;
160172
}
161173

test/test_files.out

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
size: 7
2+
data: 100,-56,50,25,10,77,123
3+
loop: 100 -56 50 25 10 77 123
4+
input:hi there!
5+
texto
6+
texte
7+
$
8+
5 : 10,30,20,11,88
9+
other=some data.
10+
seeked=me da.
11+
seeked=ata.
12+
seeked=ta.
13+
fscanfed: 10 - hello
14+
5 bytes to dev/null: 5
15+
ok.

0 commit comments

Comments
 (0)