Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/cmd/tests/sfio/tmwrite.c
1810 views
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 <[email protected]> *
18
* *
19
***********************************************************************/
20
#include "sftest.h"
21
22
/* see if multiple writers do create a consistent set of records. */
23
24
#define N_WRITER 3
25
#define N_RECORD 10000
26
27
static long vrandom()
28
{
29
#define LOWBITS ((~((unsigned long)0)) >> 1)
30
static unsigned long hash = 0xdeadbeef;
31
32
return (hash = hash*0xbadbeef + 0xdeadbeef)&LOWBITS;
33
}
34
35
tmain()
36
{
37
ssize_t size[N_WRITER][N_RECORD];
38
int count[N_WRITER];
39
char record[N_WRITER][128], *s;
40
Sfio_t* fw[N_WRITER];
41
Sfio_t* fr;
42
int i, r, done;
43
44
/* create random record sizes */
45
for(i = 0; i < N_WRITER; ++i)
46
for(r = 0; r < N_RECORD; ++r)
47
size[i][r] = (ssize_t)(vrandom()%64) + 2;
48
49
/* records for different processes */
50
for(i = 0; i < N_WRITER; ++i)
51
for(r = 0; r < 128; ++r)
52
record[i][r] = '0'+i;
53
54
/* create file */
55
fr = sfopen(NIL(Sfio_t*),tstfile("sf", 0),"w+");
56
57
/* create records */
58
for(i = 0; i < N_WRITER; ++i)
59
{ fw[i] = sfopen(NIL(Sfio_t*),tstfile("sf", 0),"a");
60
count[i] = 0;
61
}
62
63
for(done = 0; done < N_WRITER; )
64
{ i = (int)(vrandom()%N_WRITER);
65
if(count[i] < N_RECORD)
66
{ r = size[i][count[i]];
67
if(!(s = sfreserve(fw[i],r,SF_LOCKR)) || sfvalue(fw[i]) < r )
68
terror("sfreserve fails in process %d", i);
69
memcpy(s,record[i],r-1);
70
s[r-1] = '\n';
71
sfwrite(fw[i],s,r);
72
73
if((count[i] += 1) == N_RECORD)
74
{ done += 1;
75
sfclose(fw[i]);
76
}
77
}
78
}
79
80
for(i = 0; i < N_WRITER; ++i)
81
count[i] = 0;
82
83
while((s = sfgetr(fr,'\n',0)) )
84
{ if((i = s[0] - '0') < 0 || i >= N_WRITER)
85
terror("Wrong record type");
86
87
for(r = sfvalue(fr)-2; r > 0; --r)
88
if(s[r] != s[0])
89
terror("Bad record%d, count=%d", i, count[i]);
90
91
if(sfvalue(fr) != size[i][count[i]])
92
terror("Record%d count=%d size=%d sfvalue=%d",
93
i, count[i], size[i][count[i]], sfvalue(fr));
94
95
count[i] += 1;
96
}
97
98
for(i = 0; i < N_WRITER; ++i)
99
if(count[i] != N_RECORD)
100
terror("Bad count%d %d", i, count[i]);
101
102
texit(0);
103
}
104
105