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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/* bttest.c: BIT TABLE TEST
 *
 * $Id: //info.ravenbrook.com/project/mps/version/1.110/code/bttest.c#1 $
 * Copyright (c) 2001 Ravenbrook Limited.  See end of file for license.
 */


#include "mpm.h"
#include "mps.h"
#include "mpsavm.h"
#include "testlib.h"

#include <stdlib.h>
#include <stdarg.h>
#include "mpstd.h"
#include <time.h>

SRCID(bttest, "$Id: //info.ravenbrook.com/project/mps/version/1.110/code/bttest.c#1 $");


static BT bt; /* the BT which we will use */
static Size btSize; /* the size of the current BT */
static Arena arena; /* the arena which we use to allocate the BT */


#define MAX_ARGS 3


static Word args[MAX_ARGS];
static Count argCount;


static Bool argInRange(Index arg)
{
  if (bt == NULL) {
    printf("no BT\n");
    return FALSE;
  }
  if (args[arg] >= btSize) {
    printf("out of range\n");
    return FALSE;
  }
  return TRUE;
}


static Bool checkDefaultRange(Index arg)
{
  if (bt == NULL) {
    printf("no BT\n");
    return FALSE;
  }
  if (argCount == arg+1) {
    printf("range half-specified\n");
    return FALSE;
  }
  if (argCount == arg) { /* use default range */
    args[arg] = 0;
    args[arg+1] = btSize;
    return TRUE;
  }
  if (args[arg] >= args[arg+1]) {
    printf("range ill-formed\n");
    return FALSE;
  }
  if (args[arg+1] > btSize) {
    printf("range too high\n");
    return FALSE;
  }
  return TRUE; /* explicit valid range */
}   


static void quit(void)
{
  exit(0);
}


static void destroy(void)
{
  if (bt != NULL) {
    BTDestroy(bt, arena, btSize);
    bt = NULL;
  } else {
    printf("No BT to destroy\n");
  }
}

static void create(void)
{
  Res res;
  if (args[0] < 1) {
    printf("can't create a BT of size 0\n");
    return;
  }
  if (bt != NULL)
    destroy();
  res = BTCreate(&bt, arena, args[0]);
  if (res == ResOK) {
    btSize = args[0];
    BTResRange(bt, 0, btSize);
  } else {
    printf("BTCreate returned %d\n",res);
  }
}


static void set(void)
{
  if (argInRange(0))
    (BTSet)(bt, args[0]);
}


static void reset(void)
{
  if (argInRange(0))
    (BTRes)(bt, args[0]);
}


static void get(void)
{
  if (argInRange(0)) {
    Bool b = (BTGet)(bt, args[0]);
    printf(b ? "TRUE\n" : "FALSE\n");
  }
}


static void setRange(void)
{
  if (checkDefaultRange(0))
    BTSetRange(bt, args[0], args[1]);
}


static void resetRange(void)
{
  if (checkDefaultRange(0))
    BTResRange(bt, args[0], args[1]);
}


static void isSetRange(void)
{
  if (checkDefaultRange(0)) {
    Bool b = BTIsSetRange(bt, args[0], args[1]);
    printf(b ? "TRUE\n" : "FALSE\n");
  }
}


static void isResRange(void)
{
  if (checkDefaultRange(0)) {
    Bool b = BTIsResRange(bt, args[0], args[1]);
    printf(b ? "TRUE\n" : "FALSE\n");
  }
}


static void findShortResRange(void)
{
  if (checkDefaultRange(1)) {
    if (args[0] > (args[2] - args[1])) {
      printf("can't fit length in range\n");
    } else {
      Index base, limit;
      Bool b = BTFindShortResRange(&base, &limit, bt,
                                   args[1], args[2], args[0]);
      if (b)
        printf("%"PRIuLONGEST" - %"PRIuLONGEST"\n",
               (ulongest_t)base, (ulongest_t)limit);
      else
        printf("FALSE\n");
    }
  }
}


static void findShortResRangeHigh(void)
{
  if (checkDefaultRange(1)) {
    if (args[0] > (args[2] - args[1])) {
      printf("can't fit length in range\n");
    } else {
      Index base, limit;
      Bool b = BTFindShortResRangeHigh(&base, &limit, bt,
                                       args[1], args[2], args[0]);
      if (b)
        printf("%"PRIuLONGEST" - %"PRIuLONGEST"\n",
               (ulongest_t)base, (ulongest_t)limit);
      else
        printf("FALSE\n");
    }
  }
}

static void findLongResRange(void)
{
  if (checkDefaultRange(1)) {
    if (args[0] > (args[2] - args[1])) {
      printf("can't fit length in range\n");
    } else {
      Index base, limit;
      Bool b = BTFindLongResRange(&base, &limit, bt,
                                  args[1], args[2], args[0]);
      if (b)
        printf("%"PRIuLONGEST" - %"PRIuLONGEST"\n",
               (ulongest_t)base, (ulongest_t)limit);
      else
        printf("FALSE\n");
    }
  }
}


static void help(void)
{
  printf("c <s>             create a BT of size 's'\n"
         "d                 destroy the current BT\n"
         "s <i>             set the bit index 'i'\n"
         "r <i>             reset the bit index 'i'\n"
         "g <i>             get the bit index 'i'\n");
  printf("sr [<i> <i>]      set the specified range\n"
         "rr [<i> <i>]      reset the specified range\n"
         "is [<i> <i>]      is the specified range set?\n"
         "ir [<i> <i>]      is the specified range reset?\n");
  printf("f <l> [<i> <i>]   find a reset range of length 'l'.\n"
         "fh <l> [<i> <i>]  find a reset range length 'l', working downwards\n"
         "fl <l> [<i> <i>]  find a reset range of length at least 'l'\n"
         "q                 quit\n"
         "?                 print this message\n");
  printf("\n"
         "No way of testing BTSize, BTRangesSame, or BTCopyInvertRange.\n");
}


static struct commandShapeStruct {
  char *name;
  Count min_args;
  Count max_args;
  void (*fun)(void);
} commandShapes[] = {
  {"c", 1, 1, create},
  {"d", 0, 0, destroy},
  {"s", 1, 1, set},
  {"r", 1, 1, reset},
  {"g", 1, 1, get},
  {"sr", 0, 2, setRange},
  {"rr", 0, 2, resetRange},
  {"is", 0, 2, isSetRange},
  {"ir", 0, 2, isResRange},
  {"f", 1, 3, findShortResRange},
  {"fh", 1, 3, findShortResRangeHigh},
  {"fl", 1, 3, findLongResRange},
  {"?", 0, 0, help},
  {"q", 0, 0, quit},
  { NULL, 0, 0, NULL}
};


typedef struct commandShapeStruct *commandShape;


static void obeyCommand(char *command)
{
  commandShape shape = commandShapes;
  while(shape->name != NULL) {
    char *csp = shape->name;
    char *p = command;
    while (*csp == *p) {
      csp++;
      p++;
    }
    if ((*csp == 0) && ((*p == '\n') || (*p == ' '))) { /* complete match */
      argCount = 0;
      while ((*p == ' ') && (argCount < shape->max_args)) {
        /* get an argument */
        char *newP;
        long l;
        l = strtol(p, &newP, 0);
        if(l < 0) { /* negative integer */
          printf("negative integer arguments are invalid\n");
          return;
        }
        args[argCount] = (unsigned long)l;
        if (newP == p) { /* strtoul failed */
          printf("couldn't parse an integer argument\n");
          return;
        }
        p = newP;
        ++ argCount;
      }
      if (argCount < shape->min_args) {
        printf("insufficient arguments to command\n");
      } else if (*p != '\n') {
        printf("too many arguments to command\n");
      } else { /* do the command */
        shape->fun();
      }
      return;
    } else {
      ++ shape; /* try next command */
    }
  }
  printf("command not understood\n");
  help();
}


#ifdef MPS_BUILD_MV
/* disable "conversion from int to char" */
#pragma warning(disable: 4244)
#endif

static void showBT(void) {
  Index i;
  char c;
  if (bt == NULL)
    return;
  i = 0;
  while((i < btSize) && (i < 50)) {
    if (i % 10 == 0)
      c = (char)((i / 10) % 10) + '0';
    else
      c = ' ';
    putchar(c);
    ++ i;
  }
  putchar('\n');
  i = 0;
  while((i < btSize) && (i < 50)) {
    c = (char)(i % 10) +'0';
    putchar(c);
    ++ i;
  }
  putchar('\n');
  i = 0;
  while(i < btSize) {
    if (BTGet(bt,i))
      c = 'O';
    else
      c = '.';
    putchar(c);
    ++ i;
    if (i % 50 == 0)
      putchar('\n');
  }
  putchar('\n');
}

#ifdef MPS_BUILD_MV
/* disable "conversion from int to char" */
#pragma warning(default: 4244)
#endif
     

#define testArenaSIZE (((size_t)64)<<20)

extern int main(int argc, char *argv[])
{
  bt = NULL;
  btSize = 0;

  testlib_unused(argc); testlib_unused(argv);

  die(mps_arena_create((mps_arena_t*)&arena, mps_arena_class_vm(),
                       testArenaSIZE),
      "mps_arena_create");
  while(1) {
    char input[100];
    printf("bt test> ");
    fflush(stdout);
    if (fgets(input, 100, stdin)) {
      obeyCommand(input);
      showBT();
    } else {
      return 0;
    }
  }
}


/* C. COPYRIGHT AND LICENSE
 *
 * Copyright (C) 2001-2002 Ravenbrook Limited <http://www.ravenbrook.com/>.
 * All rights reserved.  This is an open source license.  Contact
 * Ravenbrook for commercial licensing options.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 * 
 * 1. Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * 
 * 2. Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * 
 * 3. Redistributions in any form must be accompanied by information on how
 * to obtain complete source code for this software and any accompanying
 * software that uses this software.  The source code must either be
 * included in the distribution or be available for no more than the cost
 * of distribution plus a nominal fee, and must be freely redistributable
 * under reasonable conditions.  For an executable file, complete source
 * code means the source code for all modules it contains. It does not
 * include source code for modules or files that typically accompany the
 * major components of the operating system on which the executable file
 * runs.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 * PURPOSE, OR NON-INFRINGEMENT, ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT HOLDERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */