1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1999-2011 AT&T Intellectual Property          *
5 *                      and is licensed under the                       *
6 *                 Eclipse Public License, Version 1.0                  *
7 *                    by AT&T Intellectual Property                     *
8 *                                                                      *
9 *                A copy of the License is available at                 *
10 *          http://www.eclipse.org/org/documents/epl-v10.html           *
11 *         (with md5 checksum b35adb5213ca9657e911e9befb180842)         *
12 *                                                                      *
13 *              Information and Software Systems Research               *
14 *                            AT&T Research                             *
15 *                           Florham Park NJ                            *
16 *                                                                      *
17 *               Glenn Fowler <glenn.s.fowler@gmail.com>                *
18 *                                                                      *
19 ***********************************************************************/
20 #include	"sftest.h"
21 
22 static int Bufcount = 0;
23 
24 #if __STD_C
readbuf(Sfio_t * f,Void_t * buf,size_t n,Sfdisc_t * disc)25 ssize_t readbuf(Sfio_t* f, Void_t* buf, size_t n, Sfdisc_t* disc)
26 #else
27 ssize_t readbuf(f,buf,n,disc)
28 Sfio_t*		f;
29 Void_t*		buf;
30 size_t		n;
31 Sfdisc_t*	disc;
32 #endif
33 {
34 	Bufcount += 1;
35 	return sfrd(f,buf,n,disc);
36 }
37 
38 Sfdisc_t Disc = {readbuf, (Sfwrite_f)0, (Sfseek_f)0, (Sfexcept_f)0, (Sfdisc_t*)0};
39 
tmain()40 tmain()
41 {
42 	Sfio_t	*f, *sf;
43 	char	*ss, *s, *tmp;
44 	int	n, i;
45 	char	zero[SF_BUFSIZE*2];
46 	char	buf[SF_BUFSIZE], little[512];
47 	Sfoff_t	one, two;
48 
49 	s = "123456789\n";
50 	n = strlen(s);
51 	if(!(f = sfopen((Sfio_t*)0, tstfile("sf", 0),"w")))
52 		terror("Opening file to write");
53 	for(i = 0; i < 1000; ++i)
54 		if(sfwrite(f,s,n) != n)
55 			terror("Writing data");
56 
57 	if(!(f = sfopen(f, tstfile("sf", 0),"r")))
58 		terror("Opening file to read");
59 
60 	if(sfseek(f,(Sfoff_t)128,0) != (Sfoff_t)128)
61 		terror("Bad seek to 128");
62 	if(sfseek(f,(Sfoff_t)0,1) != (Sfoff_t)128)
63 		terror("Bad seek(0,1) to 128");
64 
65 	if(sfseek(f,(Sfoff_t)0,2) != (i*n))
66 		terror("Bad file length");
67 	if(sftell(f) != (i*n))
68 		terror("sftell");
69 	for(; i > 0; --i)
70 	{	sfseek(f,(Sfoff_t)(-i*n),2);
71 		if(!(ss = sfgetr(f,'\n',1)))
72 			terror("sfgetr");
73 		if(strncmp(ss,s,sfvalue(f)-1) != 0)
74 			terror("Expect=%s",s);
75 	}
76 
77 	if(!(f = sfopen(f,tstfile("sf", 0),"w")) )
78 		terror("Open to write");
79 	for(n = sizeof(zero)-1; n >= 0; --n)
80 		zero[n] = 0;
81 	if(sfwrite(f,zero,sizeof(zero)) != sizeof(zero))
82 		terror("Writing data");
83 	one = sfseek(f,(Sfoff_t)0,2);
84 	two = (Sfoff_t)lseek(sffileno(f), (off_t)0, 2);
85 	if(one != two)
86 		terror("seeking1");
87 	if(sfseek(f,(Sfoff_t)(-1),2) != (Sfoff_t)lseek(sffileno(f), (off_t)(-1), 2))
88 		terror("seeking2");
89 
90 	if(!(f = sfopen(f,tstfile("sf", 0),"w")))
91 		terror("Open to write2");
92 	for(n = 0; n < sizeof(buf); n++)
93 		buf[n] = n;
94 	for(n = 0; n < 256; n++)
95 		if(sfwrite(f,buf,sizeof(buf)) != sizeof(buf))
96 			terror("Writing data 2");
97 	if(!(f = sfopen(f,tstfile("sf", 0),"r")))
98 		terror("Open to read2");
99 	if(sfgetc(f) != 0 && sfgetc(f) != 1)
100 		terror("Get first 2 bytes");
101 
102 	if(sfseek(f,(Sfoff_t)(128*sizeof(buf)),0) != (Sfoff_t)128*sizeof(buf) )
103 		terror("Seeking ");
104 	for(n = 0; n < 128; ++n)
105 		if(sfread(f,buf,sizeof(buf)) != sizeof(buf))
106 			terror("Reading data");
107 
108 	if(!(f = sfopen(f,tstfile("sf", 0),"r")))
109 		terror("Open to read3");
110 	sfsetbuf(f,little,sizeof(little));
111 	if(sfread(f, buf, 10) != 10)
112 		terror("sfread failed");
113 	if(sftell(f) != (Sfoff_t)10)
114 		terror("sftell failed");
115 	if(sfseek(f, (Sfoff_t)10, SEEK_CUR|SF_PUBLIC) != (Sfoff_t)20)
116 		terror("sfseek failed");
117 	sfseek(f, (Sfoff_t)0, SEEK_SET);
118 
119 	if(!(sf = sfnew((Sfio_t*)0, little, sizeof(little), sffileno(f), SF_READ)) )
120 		terror("sfnew failed");
121 	if(sfread(f, buf, 10) != 10)
122 		terror("sfread failed2");
123 	if(sftell(f) != 10)
124 		terror("sftell failed2");
125 
126 	if(sfseek(sf, (Sfoff_t)4000, SEEK_SET) != (Sfoff_t)4000)
127 		terror("sfseek failed on sf");
128 	sfsync(sf);
129 
130 	if(sfseek(f, (Sfoff_t)10, SEEK_CUR|SF_PUBLIC) != (Sfoff_t)4010)
131 		terror("sfseek public failed");
132 
133 	/* test to see if the buffering algorithm does the right thing */
134 	if(!(f = sfopen(NIL(Sfio_t*),tstfile("sf", 0),"w")) )
135 		terror("Opening test file to write");
136 	for(i = 0; i < 8192; ++i)
137 		if(sfputr(f,"123456789",'\n') != 10)
138 			terror("writing test data");
139 	if(!(f = sfopen(f,tstfile("sf", 0),"r")) )
140 		terror("Opening test file to read");
141 	sfdisc(f,&Disc);
142 	sfsetbuf(f,NIL(Void_t*),8192);
143 	for(i = 0; i < 8192; ++i)
144 	{	sfseek(f, (Sfoff_t)(i*10), 0);
145 		if(!(s = sfgetr(f, '\n', SF_STRING)) )
146 			terror("Reading data");
147 		if(strcmp(s,"123456789") != 0)
148 			terror("Bad data");
149 	}
150 	if(Bufcount != 10)
151 		terror("Bad buffer filling count");
152 	sfclose(f);
153 
154 	/* test buffer alignment for read streams - from a Daytona case */
155 	tmp = tstfile("sf", 0); /* create a small file of data */
156 	if(!(f = sfopen(NIL(Sfio_t*), tmp, "w")) )
157 		terror("Opening to write");
158 	for(i = 0; i < 500; ++i)
159 		if(sfputr(f,"123456789",'\n') != 10)
160 			terror("writing test data");
161 	sfclose(f);
162 
163 	if(!(f = sfopen(NIL(Sfio_t*), tmp, "r")) )
164 		terror("Opening to read");
165 	sfsetbuf(f, buf, 512);
166 	if(sfseek(f, (Sfoff_t)2741, SEEK_SET) != (Sfoff_t)2741)
167 		terror("Bad seek");
168 	if(!(s = sfreserve(f, 100, -1)) )
169 		terror("Bad sfreserve");
170 	if(sfseek(f, (Sfoff_t)0, SEEK_CUR) != (Sfoff_t)2841)
171 		terror("Bad file position");
172 	if(sfseek(f, (Sfoff_t)3224, SEEK_SET) != (Sfoff_t)3224)
173 		terror("Bad seek");
174 	if(!(s = sfreserve(f, 6, -1)) )
175 		terror("Bad sfreserve");
176 	if(strncmp(s, "56789\n", 6) != 0)
177 		terror("Bad reserved data");
178 
179 	texit(0);
180 }
181