Пример #1
0
static sexp sexp_random_uint_range_stub (sexp ctx, sexp self, sexp_sint_t n, sexp arg0, sexp arg1) {
  sexp res;
  if (! sexp_exact_integerp(arg0))
    return sexp_type_exception(ctx, self, SEXP_FIXNUM, arg0);
  if (! sexp_exact_integerp(arg1))
    return sexp_type_exception(ctx, self, SEXP_FIXNUM, arg1);
  res = sexp_make_unsigned_integer(ctx, random_uint_range(sexp_uint_value(arg0), sexp_uint_value(arg1)));
  return res;
}
Пример #2
0
void test_duplicates(unsigned int num)
{
    const struct map_config map_conf = {
        .size           = MAP_DEFAULT_SIZE,
        .lower_bound    = MAP_DEFAULT_LOWER_BOUND,
        .upper_bound    = MAP_DEFAULT_UPPER_BOUND,
        .static_size    = false,
        .key_compare    = &compare_int,
        .key_hash       = &hash_uint,
        .data_delete    = NULL,
    };
    struct map *m;
    struct random *random;
    unsigned int i, n, dups1, dups2;;
    int err;
    
    m = map_new(&map_conf);
    random = random_new();
    
    assert(m);
    assert(random);
    
    dups1 = 0;
    
    for(i = 0; i < num; ++i) {
        n = random_uint(random);
        
        if(map_contains(m, (void *)(long) n)) {
            dups1 += 1;
        } else {
            err = map_insert(m, (void *)(long) n, (void *)(long) n);
            if(err < 0) {
                fprintf(stderr, "Warning: "
                        "Map unable to insert %u in %uth iteration: %s\n",
                        n, i, strerror(-err));
            }
        }
    }
    
    random_delete(random);
    map_clear(m);
    
    srand(time(NULL));
    
    dups2 = 0;
    
    for(i = 0; i < num; ++i) {
        n = rand();
        
        if(map_contains(m, (void *)(long) n)) {
            dups2 += 1;
        } else {
            err = map_insert(m, (void *)(long) n, (void *)(long) n);
            if(err < 0) {
                fprintf(stderr, "Warning: "
                "Map unable to insert %u in %uth iteration: %s\n",
                n, i, strerror(-err));
            }
        }
    }
    
    map_delete(m);
    
    fprintf(stdout,
            "random: After %u iterations there are %u (%lf %%) duplicates\n"
            "rand(): After %u iterations there are %u (%lf %%) duplicates\n", 
            num, dups1, (double) dups1 / num, num, dups2, (double) dups2 / num);
}

void test_range(unsigned int num)
{
    struct random *rand;
    unsigned int i, n;
    
    rand = random_new();
    assert(rand);
    
    for(i = 0; i < num; ++i) {
        n = random_uint_range(rand, 0, 100);
        assert(n <= 100);
        
        n = random_uint_range(rand, 1000, 10000);
        assert(n >= 1000 && n <= 10000);
        
        n = random_uint_range(rand, 99999, 123456);
        assert(n >= 99999 && n <= 123456);
        
        n = random_uint_range(rand, (unsigned int) 1e6 + 1, (unsigned int) 1e8);
        assert(n >= (unsigned int) 1e6 + 1 && n <= (unsigned int) 1e8);
        
        n = random_uint_range(rand, 0, 0);
        assert(n == 0);
        
        n = random_uint_range(rand, 100, 100);
        assert(n == 100);
    }
    
    random_delete(rand);
}

int main(int argc, char *argv[])
{
    unsigned int num;
    
    if(argc == 2)
        num = atoi(argv[1]);
    else
        num = 0;
    
    test_randomness();
    
    if(num) {
        test_seeding(num);
        test_duplicates(num);
        test_range(num);
    }
    
    fprintf(stdout, "Tests finished\n");
    
    return EXIT_SUCCESS;
}