File: buffertest.cc

package info (click to toggle)
wvstreams 4.4.1-1.1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 8,148 kB
  • ctags: 8,556
  • sloc: cpp: 64,151; ansic: 8,188; sh: 5,994; makefile: 498; perl: 277; tcl: 114
file content (165 lines) | stat: -rw-r--r-- 3,976 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
 * Worldvisions Weaver Software:
 *   Copyright (C) 1997-2002 Net Integration Technologies, Inc.
 *
 * WvBuf test program.  Comments below indicate correct results.
 *
 */

#include "wvbuf.h"
#include <stdio.h>
#include <stdlib.h>

//#define DEBUG_STRESS

int main()
{
    WvInPlaceBuf b(1024);
    WvDynBuf bb;
    char *s, xx[1024];
    size_t in, i, max, total;
    
    printf("InPlaceBuffer TEST\n");
    printf("A %20u used, %u free, offset %d/%d\n",
	   b.used(), b.free(), b.strchr('c'), b.strchr((unsigned char)'c'));

    b.put("frogs on ice", 13);
    printf("B %20u used, %u free, offset %d/%d\n",
	   b.used(), b.free(), b.strchr('c'), b.strchr((unsigned char)'c'));
    
    s = (char *)b.get(8);
    printf("C s: %s\n", s);
    
    printf("D %20u used, %u free, offset %d/%d\n",
	   b.used(), b.free(), b.strchr('c'), b.strchr((unsigned char)'c'));

    s = (char *)b.get(5);
    printf("E s: %s\n", s);
    
    printf("F %20u used, %u free, offset %d/%d\n",
	   b.used(), b.free(), b.strchr('c'), b.strchr((unsigned char)'c'));
           
    b.zap();
    printf("G %20u used, %u free, offset %d/%d\n",
	   b.used(), b.free(), b.strchr('c'), b.strchr((unsigned char)'c'));
    printf("\n");
    
    printf("BUFFER TEST\n");
    
    printf("A %20u used, offset %d/%d\n",
	   bb.used(), bb.strchr('c'), bb.strchr((unsigned char)'c'));
    
    bb.put("frogs on ice", 13);
    printf("B %20u used, offset %d/%d\n",
	   bb.used(), bb.strchr('c'), bb.strchr((unsigned char)'c'));
    
    bb.put("frogs on rice", 14);
    printf("C %20u used, offset %d/%d\n",
	   bb.used(), bb.strchr('c'), bb.strchr((unsigned char)'c'));
    
    s = (char *)bb.get(8);
    printf("D s: %s\n", s);  // "frogs on ice"
    
    printf("E %20u used, offset %d/%d\n",
	   bb.used(), bb.strchr('c'), bb.strchr((unsigned char)'c'));
    
    bb.put("frogs on bryce", 15);
    s = (char *)bb.get(5);
    printf("F s: %s\n", s);  // " ice"

    printf("G %20u used, offset %d/%d\n",
	   bb.used(), bb.strchr('c'), bb.strchr((unsigned char)'c'));
    
    s = (char *)bb.get(16);
    printf("H s: %s\n", s);  // "frogs on rice"
    
    printf("I %20u used, offset %d/%d\n",
	   bb.used(), bb.strchr('c'), bb.strchr((unsigned char)'c'));

    bb.unget(12);
    printf("I2 %19u used, offset %d/%d\n",
	   bb.used(), bb.strchr('c'), bb.strchr((unsigned char)'c'));
    
    s = (char *)bb.get(11);
    printf("J s: %s\n", s);  // "s on rice"
    
    s = (char *)bb.get(14);
    printf("J2 s: %s\n", s);  // "rogs on bryce"
    
    printf("K %20u used, offset %d/%d\n",
	   bb.used(), bb.strchr('c'), bb.strchr((unsigned char)'c'));
    printf("\n");
    
    printf("BUFFER STRESS\n");
    in = max = total = 0;
    while (1)
    {
	i = random() % sizeof(xx);
	s = (char *)bb.alloc(i);
	memcpy(s, xx, i);
#ifdef DEBUG_STRESS
        fprintf(stderr, "alloc(%d)\n", i);
#endif
	in += i;
	total += i;
        size_t lastalloc = i;
        
	i = random() % sizeof(xx);
	if (i > lastalloc)
	    i = lastalloc;
#ifdef DEBUG_STRESS
        fprintf(stderr, "unalloc(%d)\n", i);
#endif
	bb.unalloc(i);
	in -= i;
	
	i = random() % sizeof(xx);
	if (i > in)
	    i = in;
#ifdef DEBUG_STRESS
        fprintf(stderr, "get(%d)\n", i);
#endif
	bb.get(i);
	in -= i;
	
	i = random() % sizeof(xx);
#ifdef DEBUG_STRESS
        fprintf(stderr, "put(%d)\n", i);
#endif
	bb.put(xx, i);
	in += i;
	total += i;

	i = random() % sizeof(xx);
	if (i > in)
	    i = in;
#ifdef DEBUG_STRESS
        fprintf(stderr, "get(%d)\n", i);
#endif
	bb.get(i);
	in -= i;
        size_t lastput = i;
        
	i = random() % sizeof(xx);
	if (i > lastput)
	    i = lastput;
#ifdef DEBUG_STRESS
        fprintf(stderr, "unget(%d)\n", i);
#endif
	bb.unget(i);
	in += i;
        
        assert(bb.used() == in);
	if (bb.used() > max)
	{
	    max = bb.used();
	    printf("New max: %u bytes in subbuffers after %u bytes\n",
		   max, total);
	}
#ifdef DEBUG_STRESS
	fprintf(stderr, "[%6d]", in);
#endif
    }

    return 0;
}