]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/uclibc/lib/contrib/uclibc/test/unistd/tst-posix_fallocate.c
update
[l4.git] / l4 / pkg / uclibc / lib / contrib / uclibc / test / unistd / tst-posix_fallocate.c
1 #include <fcntl.h>
2 #include <sys/stat.h>
3
4 #ifndef TST_POSIX_FALLOCATE64
5 # define stat64 stat
6 # define fstat64 fstat
7 # else
8 # ifndef O_LARGEFILE
9 #  error no O_LARGEFILE but you want to test with LFS enabled
10 # endif
11 #endif
12
13 static void do_prepare (void);
14 #define PREPARE(argc, argv) do_prepare ()
15 static int do_test (void);
16 #define TEST_FUNCTION do_test ()
17 #include <test-skeleton.c>
18
19 static int fd;
20 static void
21 do_prepare (void)
22 {
23   fd = create_temp_file ("tst-posix_fallocate.", NULL);
24   if (fd == -1)
25     {
26       printf ("cannot create temporary file: %m\n");
27       exit (1);
28     }
29 }
30
31
32 static int
33 do_test (void)
34 {
35   struct stat64 st;
36
37   if (fstat64 (fd, &st) != 0)
38     {
39       puts ("1st fstat failed");
40       return 1;
41     }
42
43   if (st.st_size != 0)
44     {
45       puts ("file not created with size 0");
46       return 1;
47     }
48
49   if (posix_fallocate (fd, 512, 768) != 0)
50     {
51       puts ("1st posix_fallocate call failed");
52       return 1;
53     }
54
55   if (fstat64 (fd, &st) != 0)
56     {
57       puts ("2nd fstat failed");
58       return 1;
59     }
60
61   if (st.st_size != 512 + 768)
62     {
63       printf ("file size after 1st posix_fallocate call is %llu, expected %u\n",
64               (unsigned long long int) st.st_size, 512u + 768u);
65       return 1;
66     }
67
68   if (posix_fallocate (fd, 0, 1024) != 0)
69     {
70       puts ("2nd posix_fallocate call failed");
71       return 1;
72     }
73
74   if (fstat64 (fd, &st) != 0)
75     {
76       puts ("3rd fstat failed");
77       return 1;
78     }
79
80   if (st.st_size != 512 + 768)
81     {
82       puts ("file size changed in 2nd posix_fallocate");
83       return 1;
84     }
85
86   if (posix_fallocate (fd, 2048, 64) != 0)
87     {
88       puts ("3rd posix_fallocate call failed");
89       return 1;
90     }
91
92   if (fstat64 (fd, &st) != 0)
93     {
94       puts ("4th fstat failed");
95       return 1;
96     }
97
98   if (st.st_size != 2048 + 64)
99     {
100       printf ("file size after 3rd posix_fallocate call is %llu, expected %u\n",
101               (unsigned long long int) st.st_size, 2048u + 64u);
102       return 1;
103     }
104 #ifdef TST_POSIX_FALLOCATE64
105   if (posix_fallocate64 (fd, 4097ULL, 4294967295ULL + 2ULL) != 0)
106     {
107       puts ("4th posix_fallocate call failed");
108       return 1;
109     }
110
111   if (fstat64 (fd, &st) != 0)
112     {
113       puts ("5th fstat failed");
114       return 1;
115     }
116
117   if (st.st_size != 4097ULL + 4294967295ULL + 2ULL)
118     {
119       printf ("file size after 4th posix_fallocate call is %llu, expected %llu\n",
120               (unsigned long long int) st.st_size, 4097ULL + 4294967295ULL + 2ULL);
121       return 1;
122     }
123 #endif
124   close (fd);
125
126   return 0;
127 }