~ubuntu-branches/ubuntu/karmic/sgb/karmic

« back to all changes in this revision

Viewing changes to test_sample.w

  • Committer: Bazaar Package Importer
  • Author(s): Julian Gilbey
  • Date: 2002-03-20 16:42:50 UTC
  • Revision ID: james.westby@ubuntu.com-20020320164250-vycuefss2g3wg151
Tags: upstream-20020130
ImportĀ upstreamĀ versionĀ 20020130

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
% This file is part of the Stanford GraphBase (c) Stanford University 1993
 
2
@i boilerplate.w %<< legal stuff: PLEASE READ IT BEFORE MAKING ANY CHANGES!
 
3
@i gb_types.w
 
4
 
 
5
\def\title{TEST\_\,SAMPLE}
 
6
 
 
7
@* Introduction. This GraphBase program is intended to be used only
 
8
when the Stanford GraphBase is being installed. It invokes the
 
9
most critical subroutines and creates a file that can be checked
 
10
against the correct output.
 
11
The testing is not exhaustive by any means, but it is designed to detect
 
12
errors of portability---cases where different results might occur
 
13
on different systems. Thus, if nothing goes wrong, one can assume that
 
14
the GraphBase routines are probably installed satisfactorily.
 
15
 
 
16
The basic idea of {\sc TEST\_\,SAMPLE} is quite simple: We generate a graph,
 
17
then print out a few of its salient characteristics. Then we recycle
 
18
the graph and generate another, etc. The test is passed if the output
 
19
file matches a ``correct'' output file generated at Stanford by the author.
 
20
 
 
21
Actually there are two output files. The main one, containing samples of
 
22
graph characteristics, is the standard output. The other, called \.{test.gb},
 
23
is a graph that has been saved in ASCII format with |save_graph|.
 
24
 
 
25
@p
 
26
#include "gb_graph.h" /* we use the {\sc GB\_\,GRAPH} data structures */
 
27
#include "gb_io.h" /* and the GraphBase input/output routines */
 
28
@<Include headers for all of the GraphBase generation modules@>@;
 
29
@#
 
30
@<Private variables@>@;
 
31
@<Procedures@>@;
 
32
@t\4@>int main()
 
33
{@+Graph *g,*gg;@+long i;@+Vertex *v; /* temporary registers */
 
34
  printf("GraphBase samples generated by test_sample:\n");
 
35
  @<Save a graph to be restored later@>;
 
36
  @<Print samples of generated graphs@>;
 
37
  return 0; /* normal exit */
 
38
}
 
39
 
 
40
@ @<Include headers for all of the GraphBase generation modules@>=
 
41
#include "gb_basic.h" /* we test the basic graph operations */
 
42
#include "gb_books.h" /* and the graphs based on literature */
 
43
#include "gb_econ.h" /* and the graphs based on economic data */
 
44
#include "gb_games.h" /* and the graphs based on football scores */
 
45
#include "gb_gates.h" /* and the graphs based on logic circuits */
 
46
#include "gb_lisa.h" /* and the graphs based on Mona Lisa */
 
47
#include "gb_miles.h" /* and the graphs based on mileage data */
 
48
#include "gb_plane.h" /* and the planar graphs */
 
49
#include "gb_raman.h" /* and the Ramanujan graphs */
 
50
#include "gb_rand.h" /* and the random graphs */
 
51
#include "gb_roget.h" /* and the graphs based on Roget's Thesaurus */
 
52
#include "gb_save.h" /* and we save results in ASCII format */
 
53
#include "gb_words.h" /* and we also test five-letter-word graphs */
 
54
 
 
55
@ The subroutine |print_sample(g,n)| will be specified later. It prints global
 
56
characteristics of |g| and local characteristics of the |n|th vertex.
 
57
 
 
58
We begin the test cautiously by generating a graph that requires no input data
 
59
and no pseudo-random numbers. If this test fails, the fault must lie either in
 
60
{\sc GB\_\,GRAPH} or {\sc GB\_\,RAMAN}.
 
61
 
 
62
@<Print samples of generated graphs@>=
 
63
print_sample(raman(31L,3L,0L,4L),4);
 
64
 
 
65
@ Next we test part of {\sc GB\_\,BASIC} that relies on a particular
 
66
interpretation of the operation `|w>>=1|'. If this part of the test
 
67
fails, please look up `system dependencies' in the index to {\sc
 
68
GB\_\,BASIC}, and correct the problem on your system by making a change file
 
69
\.{gb\_basic.ch}. (See \.{queen\_wrap.ch} for an example of a change file.)
 
70
 
 
71
On the other hand, if {\sc TEST\_\,SAMPLE} fails only in this particular test
 
72
while passing all those that follow, chances are excellent that
 
73
you have a pretty good implementation of the GraphBase anyway,
 
74
because the bug detected here will rarely show up in practice. Ask
 
75
yourself: Can I live comfortably with such a bug?
 
76
 
 
77
@<Print samples of generated graphs@>=
 
78
print_sample(board(1L,1L,2L,-33L,1L,-0x40000000L-0x40000000L,1L),2000);
 
79
  /* coordinates 32 and 33 (only) should wrap around */
 
80
 
 
81
@ Another system-dependent part of {\sc GB\_\,BASIC} is tested here,
 
82
this time involving character codes.
 
83
 
 
84
@<Print samples of generated graphs@>=
 
85
print_sample(subsets(32L,18L,16L,0L,999L,-999L,0x80000000L,1L),1);
 
86
 
 
87
@ If \.{test.gb} fails to match \.{test.correct}, the most likely culprit
 
88
is |vert_offset|, a ``pointer hack'' in {\sc GB\_\,BASIC}. That macro
 
89
absolutely must be made to work properly, because it is used heavily.
 
90
In particular, it is used in the |complement| routine tested here,
 
91
and in the |gunion| routine tested below.
 
92
 
 
93
@<Save a graph to be restored later@>=
 
94
  g=random_graph(3L,10L,1L,1L,0L,NULL,dst,1L,2L,1L);
 
95
    /* a random multigraph with 3 vertices, 10 edges */
 
96
  gg=complement(g,1L,1L,0L); /* a copy of |g|, without multiple edges */
 
97
  v=gb_typed_alloc(1,Vertex,gg->data); /* we create a stray vertex too */
 
98
  v->name=gb_save_string("Testing");
 
99
  gg->util_types[10]='V';
 
100
  gg->ww.V=v; /* the stray vertex is now part of |gg| */
 
101
  save_graph(gg,"test.gb"); /* so it will appear in \.{test.gb} (we hope) */
 
102
  gb_recycle(g);@+gb_recycle(gg);
 
103
 
 
104
@ @<Private...@>=
 
105
static long dst[]={0x20000000,0x10000000,0x10000000};
 
106
 /* a probability distribution with frequencies 50\%, 25\%, 25\% */
 
107
 
 
108
@ Now we try to reconstruct the graph we saved before, and we also randomize
 
109
its lengths.
 
110
 
 
111
@<Print samples...@>=
 
112
g=restore_graph("test.gb");
 
113
if (i=random_lengths(g,0L,10L,12L,dst,2L))
 
114
  printf("\nFailure code %ld returned by random_lengths!\n",i);
 
115
else {
 
116
  gg=random_graph(3L,10L,1L,1L,0L,NULL,dst,1L,2L,1L); /* same as before */
 
117
  print_sample(gunion(g,gg,1L,0L),2);
 
118
  gb_recycle(g);@+gb_recycle(gg);
 
119
}
 
120
 
 
121
@ Partial evaluation of a RISC circuit involves fairly intricate pointer
 
122
manipulation, so this step should help to test the portability of the author's
 
123
favorite programming tricks.
 
124
 
 
125
@<Print samples...@>=
 
126
print_sample(partial_gates(risc(0L),1L,43210L,98765L,NULL),79);
 
127
 
 
128
@ Now we're ready to test the mechanics of reading data files,
 
129
sorting with {\sc GB\_\,SORT}, and heavy randomization. Lots of computation
 
130
takes place in this section.
 
131
 
 
132
@<Print samp...@>=
 
133
print_sample(book("homer",500L,400L,2L,12L,10000L,-123456L,789L),81);
 
134
print_sample(econ(40L,0L,400L,-111L),11);
 
135
print_sample(games(60L,70L,80L,-90L,-101L,60L,0L,999999999L),14);
 
136
print_sample(miles(50L,-500L,100L,1L,500L,5L,314159L),20);
 
137
print_sample(plane_lisa(100L,100L,50L,1L,300L,1L,200L,
 
138
              50L*299L*199L,200L*299L*199L),1294);
 
139
print_sample(plane_miles(50L,500L,-100L,1L,1L,40000L,271818L),14);
 
140
print_sample(random_bigraph(300L,3L,1000L,-1L,0L,dst,-500L,500L,666L),3);
 
141
print_sample(roget(1000L,3L,1009L,1009L),40);
 
142
 
 
143
@ Finally, here's a picky, picky test that is supposed to fail the first time,
 
144
succeed the second. (The weight vector just barely exceeds
 
145
the maximum weight threshold allowed by {\sc GB\_WORDS}. That test is
 
146
ultraconservative, but eminently reasonable nevertheless.)
 
147
 
 
148
@<Print samples...@>=
 
149
print_sample(words(100L,wt_vector,70000000L,69L),5);
 
150
wt_vector[1]++;
 
151
print_sample(words(100L,wt_vector,70000000L,69L),5);
 
152
print_sample(words(0L,NULL,0L,69L),5555);
 
153
 
 
154
@ @<Private...@>=
 
155
static long wt_vector[]=
 
156
  {100,-80589,50000,18935,-18935,18935,18935,18935,18935};
 
157
 
 
158
@* Printing the sample data. Given a graph |g| in GraphBase format and
 
159
an integer~|n|, the subroutine |print_sample(g,n)| will output
 
160
global characteristics of~|g|, such as its name and size, together with
 
161
detailed information about its |n|th vertex. Then |g| will be shredded
 
162
and recycled; the calling routine should not refer to it again.
 
163
 
 
164
@<Procedures@>=
 
165
static void pr_vert();
 
166
   /* a subroutine for printing a vertex is declared below */
 
167
static void pr_arc(); /* likewise for arcs */
 
168
static void pr_util(); /* and for utility fields in general */
 
169
static void print_sample(g,n)
 
170
  Graph *g; /* graph to be sampled and destroyed */
 
171
  int n; /* index to the sampled vertex */
 
172
{
 
173
  printf("\n");
 
174
  if (g==NULL) {
 
175
    printf("Ooops, we just ran into panic code %ld!\n",panic_code);
 
176
    if (io_errors)
 
177
      printf("(The I/O error code is 0x%lx)\n",(unsigned long)io_errors);
 
178
  }@+else {
 
179
    @<Print global characteristics of |g|@>;
 
180
    @<Print information about the |n|th vertex@>;
 
181
    gb_recycle(g);
 
182
  }
 
183
}
 
184
 
 
185
@ The graph's |util_types| are used to determine how much information
 
186
should be printed. A level parameter also helps control the verbosity of
 
187
printout. In the most verbose mode, each utility field that points to a
 
188
vertex or arc, or contains integer or string data, will be printed.
 
189
 
 
190
@<Procedures@>=
 
191
static void pr_vert(v,l,s)
 
192
  Vertex *v; /* vertex to be printed */
 
193
  int l; /* |<=0| if the output should be terse */
 
194
  char *s; /* format for graph utility fields */
 
195
{
 
196
  if (v==NULL) printf("NULL");
 
197
  else if (is_boolean(v)) printf("ONE"); /* see {\sc GB\_\,GATES} */
 
198
  else {
 
199
    printf("\"%s\"",v->name);
 
200
    pr_util(v->u,s[0],l-1,s);
 
201
    pr_util(v->v,s[1],l-1,s);
 
202
    pr_util(v->w,s[2],l-1,s);
 
203
    pr_util(v->x,s[3],l-1,s);
 
204
    pr_util(v->y,s[4],l-1,s);
 
205
    pr_util(v->z,s[5],l-1,s);
 
206
    if (l>0) {@+register Arc *a;
 
207
      for (a=v->arcs;a;a=a->next) {
 
208
        printf("\n   ");
 
209
        pr_arc(a,1,s);
 
210
      }
 
211
    }
 
212
  }
 
213
}
 
214
 
 
215
@ @<Pro...@>=
 
216
static void pr_arc(a,l,s)
 
217
  Arc *a; /* non-null arc to be printed */
 
218
  int l; /* |<=0| if the output should be terse */
 
219
  char *s; /* format for graph utility fields */
 
220
{
 
221
  printf("->");
 
222
  pr_vert(a->tip,0,s);
 
223
  if (l>0) {
 
224
    printf( ", %ld",a->len);
 
225
    pr_util(a->a,s[6],l-1,s);
 
226
    pr_util(a->b,s[7],l-1,s);
 
227
  }
 
228
}
 
229
 
 
230
@ @<Procedures@>=
 
231
static void pr_util(u,c,l,s)
 
232
  util u; /* a utility field to be printed */
 
233
  char c; /* its type code */
 
234
  int l; /* 0 if output should be terse, |-1| if pointers omitted */
 
235
  char *s; /* utility types for overall graph */
 
236
{
 
237
  switch (c) {
 
238
  case 'I': printf("[%ld]",u.I);@+break;
 
239
  case 'S': printf("[\"%s\"]",u.S?u.S:"(null)");@+break;
 
240
  case 'A': if (l<0) break;
 
241
    printf("[");
 
242
    if (u.A==NULL) printf("NULL");
 
243
    else pr_arc(u.A,l,s);
 
244
    printf("]");
 
245
    break;
 
246
  case 'V': if (l<0) break; /* avoid infinite recursion */
 
247
    printf("[");
 
248
    pr_vert(u.V,l,s);
 
249
    printf("]");
 
250
  default: break; /* case |'Z'| does nothing, other cases won't occur */
 
251
  }
 
252
}
 
253
 
 
254
@ @<Print information about the |n|th vertex@>=
 
255
printf("V%d: ",n);
 
256
if (n>=g->n || n<0) printf("index is out of range!\n");
 
257
else {
 
258
  pr_vert(g->vertices+n,1,g->util_types);
 
259
  printf("\n");
 
260
}
 
261
 
 
262
@ @<Print global characteristics of |g|@>=
 
263
printf("\"%s\"\n%ld vertices, %ld arcs, util_types %s",
 
264
      g->id,g->n,g->m,g->util_types);
 
265
pr_util(g->uu,g->util_types[8],0,g->util_types);
 
266
pr_util(g->vv,g->util_types[9],0,g->util_types);
 
267
pr_util(g->ww,g->util_types[10],0,g->util_types);
 
268
pr_util(g->xx,g->util_types[11],0,g->util_types);
 
269
pr_util(g->yy,g->util_types[12],0,g->util_types);
 
270
pr_util(g->zz,g->util_types[13],0,g->util_types);
 
271
printf("\n");
 
272
 
 
273
@* Index. We end with the customary list of identifiers, showing where
 
274
they are used and where they are defined.