xref: /relibc/tests/wchar/fwide.c (revision ed19381547d66b76981ea1e4ff942c5a4da45ab7)
1 #include <assert.h>
2 #include <stdio.h>
3 #include <wchar.h>
4 
5 int test_initial_orientation(void) {
6 	FILE *f = tmpfile();
7 	assert(fwide(f, 0) == 0);
8 	return 0;
9 }
10 
11 int test_manual_byte_orientation(void) {
12 	FILE *f = tmpfile();
13 
14 	// set manually to byte orientation
15 	assert(fwide(f, -483) == -1);
16 
17 	// Cannot change to wchar orientation
18 	assert(fwide(f, 1) == -1);
19 
20 	fclose(f);
21 	return 0;
22 }
23 
24 int test_manual_wchar_orientation(void) {
25 	FILE *f = tmpfile();
26 
27 	// set manually to wchar orientation
28 	assert(fwide(f, 483) == 1);
29 
30 	// Cannot change to byte orientation
31 	assert(fwide(f, -1) == 1);
32 
33 	fclose(f);
34 	return 0;
35 }
36 
37 int test_orientation_after_fprintf(void) {
38 	// open file and write bytes; implicitly setting the bytes orientation
39 	FILE *f = tmpfile();
40 	assert(fprintf(f, "blah\n") == 5);
41 
42 	// Check that bytes orientation is set
43 	assert(fwide(f, 0) == -1);
44 
45 	fclose(f);
46 	return 0;
47 }
48 
49 int main() {
50 	int(*tests[])(void) = {
51 		&test_initial_orientation,
52 		&test_manual_byte_orientation,
53 		&test_manual_wchar_orientation,
54 		&test_orientation_after_fprintf,
55 	};
56 	for(int i=0; i<sizeof(tests)/sizeof(int(*)(void)); i++) {
57 		printf("%d\n", (*tests[i])());
58 	}
59 }
60 
61