File: unit-ar.c

package info (click to toggle)
tla 1.3.5%2Bdfsg1-2
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 22,292 kB
  • ctags: 11,952
  • sloc: ansic: 149,771; sh: 16,009; xml: 2,689; lisp: 1,927; makefile: 1,064; cpp: 363; tcl: 230; awk: 48; sed: 25
file content (278 lines) | stat: -rw-r--r-- 6,713 bytes parent folder | download | duplicates (3)
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/* unit-ar.c - test ar.c
 *
 ****************************************************************
 * Copyright (C) 2000 Thomas Lord
 * 
 * See the file "COPYING" for further information about
 * the copyright and warranty status of this work.
 */



#include "hackerlab/machine/alignment.h"
#include "hackerlab/bugs/panic.h"
#include "hackerlab/char/str.h"
#include "hackerlab/arrays/ar.h"
#include "hackerlab/fmt/cvt.h"
#include "hackerlab/cmd/main.h"



static t_uchar * program_name = "unit-ar";
static t_uchar * usage = "[options]";
static t_uchar * version_string = "1.0";

#define OPTS(OP, OP2) \
  OP (opt_help_msg, "h", "help", 0, \
      "Display a help message and exit.") \
  OP (opt_version, "V", "version", 0, \
      "Display a release identifier string") \
  OP2 (opt_version, 0, 0, 0, "and exit.") \
  OP (opt_iterations, "i", "iterations n", 1,  \
      "Perform <n> tests.")

enum options
{
  OPTS (OPT_ENUM, OPT_IGN)  
};

struct opt_desc opts[] = 
{
  OPTS (OPT_DESC, OPT_DESC)
    {-1, 0, 0, 0, 0}
};



#define HASH_INDEX(X) ((((X) >> 8) & 0xff) ^ ((X) & 0xff))

static void
check_array (size_t size, unsigned long * ar)
{
  int x;
  /* invariant (0 == ((unsigned long)ar & (MACHINE_ALIGNMENT - 1))); */
  invariant (size == ar_size ((void *)ar, lim_use_must_malloc, sizeof (*ar)));
  for (x = 0; x < size; ++x)
    invariant (ar[x] == HASH_INDEX (x));
}



int
main (int argc, char * argv[])
{
  int errn;
  int o;
  struct opt_parsed * option;
  unsigned int iterations;

  option = 0;
  iterations = 10000;

  while (1)
    {
      o = opt_standard (lim_use_must_malloc, &option, opts, &argc, argv, program_name, usage, version_string, 0, opt_help_msg, opt_none, opt_version);

      if (o == opt_none)
	break;

      switch (o)
	{
	default:
	  safe_printfmt (2, "unhandled option `%s'\n", option->opt_string);
	  panic ("internal error parsing arguments");

	usage_error:
	  opt_usage (2, argv[0], program_name, usage, 1);
	  panic_exit ();

	bogus_arg:
	  safe_printfmt (2, "ill-formed argument for `%s' (`%s')\n", option->opt_string, option->arg_string);
	  goto usage_error;

	case opt_iterations:
	  if (cvt_decimal_to_uint (&errn, &iterations, option->arg_string, str_length (option->arg_string)))
	    goto bogus_arg;
	  break;
	}
    }


  {
#  define MAX_ARRAYS 1024
    unsigned long * arrays[1024];
    size_t sizes[1024];
    int n_arrays;
    enum ar_op
      {
	op_new = 0,
	op_ar_size,
	op_ar_setsize,
	op_ar_compact,
	op_ar_free,
	op_push,
	op_pop,
	op_copy,
	n_ops = op_copy + 1
      };
    int counts[n_ops];
    int x;

    for (x = 0; x < n_ops; ++x)
      counts[x] = 0;

    n_arrays = 0;
    while (iterations--)
      {
	int choose;
	enum ar_op op;
	if (!n_arrays)
	  {
	    op = op_new;
	    choose = 0;
	  }
	else
	  {
	    op = (enum ar_op)(random () % n_ops);
	    choose = (random () % n_arrays);
	  }

	counts[op]++;
	switch (op)
	  {
	    {
	    case op_new:
	      if (n_arrays == MAX_ARRAYS)
		break;
	      else
		{
		  size_t size;
		  int x;
		  
		  size = 1 + random () % 1024;
		  arrays[n_arrays] = 0;
		  ar_setsize ((void **)&arrays[n_arrays], lim_use_must_malloc, size, sizeof (unsigned long));
		  for (x = 0; x < size; ++x)
		    arrays[n_arrays][x] = HASH_INDEX(x);
		  sizes[n_arrays] = size;
		  ++n_arrays;
		}


	    case op_copy:
	      if (n_arrays == MAX_ARRAYS)
		break;
	      else
		{
		  check_array (sizes[choose], arrays[choose]);
		  arrays[n_arrays] = (unsigned long *)ar_copy ((void *)arrays[choose], lim_use_must_malloc, sizeof (unsigned long));
		  sizes[n_arrays] = sizes[choose];
		  check_array (sizes[n_arrays], arrays[n_arrays]);
		  ++n_arrays;
		}
	      
	    case op_ar_size:
	      check_array (sizes[choose], arrays[choose]);
	      invariant (sizes[choose] == ar_size ((void *)arrays[choose], lim_use_must_malloc, sizeof (*arrays[choose])));
	      break;

	    case op_ar_compact:
	      check_array (sizes[choose], arrays[choose]);
	      ar_compact ((void **)&arrays[choose], lim_use_must_malloc, sizeof (unsigned long));
	      check_array (sizes[choose], arrays[choose]);
	      break;

	    case op_push:
	      check_array (sizes[choose], arrays[choose]);
	      *(unsigned long *)ar_push ((void **)&arrays[choose], lim_use_must_malloc, sizeof (unsigned long)) = HASH_INDEX(sizes[choose]);
	      ++sizes[choose];
	      check_array (sizes[choose], arrays[choose]);
	      break;

	    case op_pop:
	      {
		unsigned long val;

		check_array (sizes[choose], arrays[choose]);
		if (!sizes[choose])
		  break;
		val = arrays[choose][sizes[choose] - 1];
		invariant (val == *(unsigned long *)ar_pop ((void **)&arrays[choose], lim_use_must_malloc, sizeof (unsigned long)));
		--sizes[choose];
		check_array (sizes[choose], arrays[choose]);
		break;
	      }

	    case op_ar_setsize:
	      {
		size_t size;
		size_t old_size;
		size_t smaller;
		size_t x;

		check_array (sizes[choose], arrays[choose]);
		old_size = sizes[choose];
		size = random () % 1024;
		smaller = (old_size < size ? old_size : size);
		ar_setsize ((void **)&arrays[choose], lim_use_must_malloc, size, sizeof (unsigned long));
		sizes[choose] = size;
		for (x = 0; x < smaller; ++x)
		  invariant (arrays[choose][x] == HASH_INDEX (x));
		for (x = smaller; x < size; ++x)
		  {
		    arrays[choose][x] = HASH_INDEX (x);
		  }
		check_array (sizes[choose], arrays[choose]);
		break;
	      }

	    case op_ar_free:
	      {
		unsigned long * ar;

		check_array (sizes[choose], arrays[choose]);
		ar = arrays[choose];
		arrays[choose] = arrays[n_arrays - 1];
		sizes[choose ] = sizes[n_arrays - 1];
		--n_arrays;
		ar_free ((void **)&ar, lim_use_must_malloc);
		invariant (ar == 0);
		break;
	      }

	    default:
	      while (1)
		panic ("unknown operation");
	    }
	  }
      }
    {
      int total;

      total = 0;

      safe_printfmt (1, "op_new: %d tests\n", counts[op_new]);
      total += counts[op_new];
      safe_printfmt (1, "op_ar_size: %d tests\n", counts[op_ar_size]);
      total += counts[op_ar_size];
      safe_printfmt (1, "op_ar_setsize: %d tests\n", counts[op_ar_setsize]);
      total += counts[op_ar_setsize];
      safe_printfmt (1, "op_ar_compact: %d tests\n", counts[op_ar_compact]);
      total += counts[op_ar_compact];
      safe_printfmt (1, "op_ar_free: %d tests\n", counts[op_ar_free]);
      total += counts[op_ar_free];
      safe_printfmt (1, "op_push: %d tests\n", counts[op_push]);
      total += counts[op_push];
      safe_printfmt (1, "op_pop: %d tests\n", counts[op_pop]);
      total += counts[op_pop];
      safe_printfmt (1, "op_copy: %d tests\n", counts[op_copy]);
      total += counts[op_copy];
      safe_printfmt (1, "TOTAL tests: %d\n", total);
    }
    
  }
  return 0;
}