Exemple #1
0
static void
dice_roll_test(void)
{
    struct rnd *always_one = rnd_alloc_fake_fixed(0);
    struct rnd *always_two = rnd_alloc_fake_fixed(1);
    int score;

    score = dice_roll(dice_make(0, 1), always_one, NULL);
    assert(0 == score);

    score = dice_roll(dice_make(3, 1), always_two, NULL);
    assert(3 == score);

    score = dice_roll(dice_make_plus(1, 1, 1), always_one, NULL);
    assert(2 == score);

    score = dice_roll(dice_make_plus(5, 1, -1), always_two, NULL);
    assert(4 == score);

    score = dice_roll(dice_make_plus_times(5, 1, 0, 10), always_one, NULL);
    assert(50 == score);

    score = dice_roll(dice_make_plus(0, 4, 1), always_two, NULL);
    assert(1 == score);

    rnd_free(always_one);
    rnd_free(always_two);
}
Exemple #2
0
int main(int argc, char **argv)
{
	rnd_t rnd = rnd_new();

	rnd_test(rnd);

	rnd_free(rnd);
}
Exemple #3
0
void
options_free(struct options *options)
{
    if (options) {
        rnd_free(options->rnd);
        free_or_die(options->command_name);
        free_or_die(options);
    }
}
Exemple #4
0
static void
dice_roll_and_drop_lowest_test(void)
{
    struct rnd *ascending = rnd_alloc_fake_ascending(0);
    struct rnd *always_one = rnd_alloc_fake_fixed(0);
    struct rnd *always_two = rnd_alloc_fake_fixed(1);
    int score;
    
    score = dice_roll_and_drop_lowest(dice_make(4, 6), ascending);
    assert(9 == score);
    
    score = dice_roll_and_drop_lowest(dice_make(3, 6), always_one);
    assert(2 == score);
    
    score = dice_roll_and_drop_lowest(dice_make(5, 4), always_two);
    assert(8 == score);
    
    rnd_free(ascending);
    rnd_free(always_one);
    rnd_free(always_two);
}
Exemple #5
0
static void
dice_roll_with_average_scoring_test(void)
{
    struct rnd *ascending = rnd_alloc_fake_ascending(0);
    struct rnd *always_one = rnd_alloc_fake_fixed(0);
    struct rnd *always_two = rnd_alloc_fake_fixed(1);
    int score;
    
    score = dice_roll_with_average_scoring(dice_make(3, 6), ascending);
    assert(8 == score);
    
    score = dice_roll_with_average_scoring(dice_make(3, 6), always_one);
    assert(9 == score);
    
    score = dice_roll_with_average_scoring(dice_make(3, 6), always_two);
    assert(6 == score);
    
    rnd_free(ascending);
    rnd_free(always_one);
    rnd_free(always_two);
}
static void
treasure_generate_magic_items_test(void)
{
    struct rnd *rnd = rnd_alloc_fake_min();
    struct treasure treasure;
    treasure_initialize(&treasure);

    treasure_generate_magic_items(&treasure, rnd, 1, ANY_MAGIC_ITEM);

    assert(NULL == treasure.type);
    assert(coins_is_zero(treasure.coins));
    assert(NULL == treasure.gems);
    assert(0 == treasure.gems_count);
    assert(NULL == treasure.jewelry);
    assert(0 == treasure.jewelry_count);
    assert(NULL == treasure.maps);
    assert(0 == treasure.maps_count);

    assert(NULL != treasure.magic_items);
    assert(1 == treasure.magic_items_count);

    assert(magic_item_type_potion == treasure.magic_items[0].type);
    assert(str_eq("animal control potion", treasure.magic_items[0].true_description));

    rnd_free(rnd);
    rnd = rnd_alloc_fake_median();

    treasure_generate_magic_items(&treasure, rnd, 1, ANY_MAGIC_ITEM);

    assert(NULL != treasure.magic_items);
    assert(2 == treasure.magic_items_count);

    assert(magic_item_type_potion == treasure.magic_items[0].type);
    assert(str_eq("animal control potion", treasure.magic_items[0].true_description));
    assert(magic_item_type_misc == treasure.magic_items[1].type);
    assert(str_eq("cloak of protection +2", treasure.magic_items[1].true_description));

    treasure_finalize(&treasure);
    rnd_free(rnd);
}
static void
treasure_create_json_object_for_type_A_test(void)
{
    struct rnd *rnd = rnd_alloc_fake_min();
    struct treasure treasure;
    treasure_initialize(&treasure);
    struct treasure_type *treasure_type = treasure_type_by_letter('A');
    treasure_type_generate(treasure_type, rnd, &treasure);
    struct cJSON *json_object = treasure_create_json_object(&treasure);

    assert(cJSON_IsObject(json_object));

    char *json_string = cJSON_PrintUnformatted(json_object);
    char const *expected = "{"
                           "\"struct\":\"treasure\","
                           "\"rev\":0,"
                           "\"type\":\"A\","
                           "\"coins\":{"
                               "\"struct\":\"coins\","
                               "\"rev\":0,"
                               "\"pp\":100,"
                               "\"gp\":1000,"
                               "\"ep\":1000,"
                               "\"sp\":1000,"
                               "\"cp\":1000"
                           "},"
                           "\"gems\":["
                               "{\"struct\":\"gem\",\"rev\":0,\"size\":\"very small\",\"type\":\"ornamental\",\"kind\":\"azurite\",\"colors\":\"mottled deep blue\",\"value_percent_modifier\":0,\"value_rank_modifier\":7},"
                               "{\"struct\":\"gem\",\"rev\":0,\"size\":\"very small\",\"type\":\"ornamental\",\"kind\":\"azurite\",\"colors\":\"mottled deep blue\",\"value_percent_modifier\":0,\"value_rank_modifier\":7},"
                               "{\"struct\":\"gem\",\"rev\":0,\"size\":\"very small\",\"type\":\"ornamental\",\"kind\":\"azurite\",\"colors\":\"mottled deep blue\",\"value_percent_modifier\":0,\"value_rank_modifier\":7},"
                               "{\"struct\":\"gem\",\"rev\":0,\"size\":\"very small\",\"type\":\"ornamental\",\"kind\":\"azurite\",\"colors\":\"mottled deep blue\",\"value_percent_modifier\":0,\"value_rank_modifier\":7}"
                           "],"
                           "\"jewelry\":["
                               "{\"struct\":\"jewelry\",\"rev\":0,\"has_gems\":false,\"form\":\"anklet\",\"material\":\"ivory\",\"workmanship_bonus\":12,\"exceptional_stone_bonus\":0,\"value_in_cp\":400000},"
                               "{\"struct\":\"jewelry\",\"rev\":0,\"has_gems\":false,\"form\":\"anklet\",\"material\":\"ivory\",\"workmanship_bonus\":12,\"exceptional_stone_bonus\":0,\"value_in_cp\":400000},"
                               "{\"struct\":\"jewelry\",\"rev\":0,\"has_gems\":false,\"form\":\"anklet\",\"material\":\"ivory\",\"workmanship_bonus\":12,\"exceptional_stone_bonus\":0,\"value_in_cp\":400000}"
                           "],"
                           "\"maps\":["
                               "{\"struct\":\"treasure_map\",\"rev\":0,\"is_false\":true,\"treasure\":{\"struct\":\"treasure\",\"rev\":0,\"type\":null,\"coins\":{\"struct\":\"coins\",\"rev\":0,\"pp\":0,\"gp\":0,\"ep\":0,\"sp\":0,\"cp\":0},\"gems\":[],\"jewelry\":[],\"maps\":[],\"magic_items\":[]},\"true_description\":\"false map to treasure of (no treasure) in nearby labyrinth to the north\"},"
                               "{\"struct\":\"treasure_map\",\"rev\":0,\"is_false\":true,\"treasure\":{\"struct\":\"treasure\",\"rev\":0,\"type\":null,\"coins\":{\"struct\":\"coins\",\"rev\":0,\"pp\":0,\"gp\":0,\"ep\":0,\"sp\":0,\"cp\":0},\"gems\":[],\"jewelry\":[],\"maps\":[],\"magic_items\":[]},\"true_description\":\"false map to treasure of (no treasure) in nearby labyrinth to the north\"},"
                               "{\"struct\":\"treasure_map\",\"rev\":0,\"is_false\":true,\"treasure\":{\"struct\":\"treasure\",\"rev\":0,\"type\":null,\"coins\":{\"struct\":\"coins\",\"rev\":0,\"pp\":0,\"gp\":0,\"ep\":0,\"sp\":0,\"cp\":0},\"gems\":[],\"jewelry\":[],\"maps\":[],\"magic_items\":[]},\"true_description\":\"false map to treasure of (no treasure) in nearby labyrinth to the north\"}"
                           "],"
                           "\"magic_items\":[]"
                           "}";
    assert(str_eq(expected, json_string));

    free(json_string);
    cJSON_Delete(json_object);

    treasure_finalize(&treasure);
    rnd_free(rnd);
}
static void
ability_scores_alloc_method_1_test(void)
{
    struct rnd *rnd = rnd_alloc_fake_ascending(0);
    struct ability_scores *scores = ability_scores_alloc_method_1(rnd);

    assert(15 == scores->values[0]);
    assert(15 == scores->values[1]);
    assert(13 == scores->values[2]);
    assert(13 == scores->values[3]);
    assert( 9 == scores->values[4]);
    assert( 9 == scores->values[5]);

    ability_scores_free(scores);
    rnd_free(rnd);
}
static void
get_jrand48(struct options *options, char const *arg)
{
    unsigned long const max_seed = 0x0000ffffffffffff;
    errno = 0;
    unsigned long long_seed = strtoul(arg, NULL, 0);
    if (errno || long_seed > max_seed) {
        options->error = true;
        fprintf(stderr, "%s: invalid jrand48 seed - %s\n",
                options->command_name, optarg);
        return;
    }
    unsigned short seed[3];
    seed[0] = long_seed & 0x000000000000ffff;
    seed[1] = (long_seed & 0x00000000ffff0000) >> 16;
    seed[2] = (long_seed & 0x0000ffff00000000) >> 32;
    rnd_free(options->rnd);
    options->rnd = rnd_alloc_jrand48(seed);
}
Exemple #10
0
static void
check(FILE *out, uint32_t constant)
{
    struct rnd *fake_rnd = rnd_alloc_fake_fixed(constant);
    
    generate_treasure_type_table(out);
    generate_map(fake_rnd, out);
    generate_each_treasure(fake_rnd, out);
    generate_sample_dungeon(fake_rnd, out);
    generate_random_dungeon(fake_rnd, out);
    generate_character(fake_rnd, out, characteristic_generation_method_simple);
    generate_character(fake_rnd, out, characteristic_generation_method_1);
    generate_character(fake_rnd, out, characteristic_generation_method_2);
    generate_character(fake_rnd, out, characteristic_generation_method_3);
    generate_character(fake_rnd, out, characteristic_generation_method_4);
    generate_character(fake_rnd, out, characteristic_generation_method_general_NPC);
    generate_character(fake_rnd, out, characteristic_generation_method_special_NPC);
    
    rnd_free(fake_rnd);
}
Exemple #11
0
int main(int argc, char *argv[])
{
	int i;

	for (i = 1; i < argc; i++) {
		long n;
		char junk;

		if (strncmp(argv[i], "--benchmarks=", 13) == 0) {
			FLAGS_benchmarks = argv[i] + strlen("--benchmarks=");
		} else if (sscanf(argv[i], "--cache_size=%ld%c", &n, &junk) == 1) {
			FLAGS_cache_size = n;
		} else if (sscanf(argv[i], "--num=%ld%c", &n, &junk) == 1) {
			FLAGS_num = n;

		} else if (strncmp(argv[i], "--compress=", 11) == 0) {
			if (strcmp(argv[i] + strlen("--compress="), "snappy") == 0)
				FLAGS_method = NESS_SNAPPY_METHOD;
			else if (strcmp(argv[i] + strlen("--compress="), "no") == 0)
				FLAGS_method = NESS_NO_COMPRESS;
		} else {
			fprintf(stderr, "invalid flag %s\n", argv[i]);
			exit(1);
		}
	}

	srand(time(NULL));
	rnd = rnd_new();
	if (!dbopen())
		exit(1);

	if (!run())
		exit(1);

	rnd_free(rnd);
	db_close(db);
	env_close(e);

	return 1;
}
static void
treasure_type_generate_for_A_with_fake_fixed_test(void)
{
    struct rnd *rnd = rnd_alloc_fake_fixed(25);
    struct treasure treasure;
    treasure_initialize(&treasure);
    struct treasure_type *treasure_type = treasure_type_by_letter('A');

    treasure_type_generate(treasure_type, rnd, &treasure);

    assert(0 == treasure.coins.cp);
    assert(2000 == treasure.coins.sp);
    assert(2000 == treasure.coins.ep);
    assert(6000 == treasure.coins.gp);
    assert(0 == treasure.coins.pp);
    assert(24 == treasure.gems_count);
    assert(18 == treasure.jewelry_count);
    assert(3 == treasure.magic_items_count);
    assert(0 == treasure.maps_count);

    treasure_finalize(&treasure);
    rnd_free(rnd);
}
Exemple #13
0
int main(int argc, char *argv[])
{

	int loop;
	struct nessdb *db;
	char basedir[] = "./dbbench/";
	struct timespec start;
	struct timespec end;
	struct random *rnd;

	if (argc != 2) {
		printf("./db-bench [count]\n");
		return 0;
	}
	loop = atoi(argv[1]);
	rnd = rnd_new();
	db = db_open(basedir);

	gettime(&start);
	do_bench(db, rnd, loop);
	gettime(&end);

	uint64_t bytes = loop * (KEY_SIZE + VAL_SIZE);
	long long cost_ms = time_diff_ms(start, end) + 1;
	printf("--------loop:%d, cost:%d(ms), %.f ops/sec, %6.1f MB/sec\n",
			loop,
			(int)cost_ms,
			(double)(loop/cost_ms)*1000,
			(double)((bytes/cost_ms/1048576.0) * 1000));


	db_close(db);
	rnd_free(rnd);

	return 1;
}
Exemple #14
0
static void
roll_test(void)
{
    struct rnd *always_one = rnd_alloc_fake_fixed(0);
    struct rnd *always_two = rnd_alloc_fake_fixed(1);
    int score;
    
    /* "roll" a constant number */
    score = roll("0", always_one);
    assert(0 == score);
    
    score = roll("3", always_one);
    assert(3 == score);
    
    score = roll("1+1", always_two);
    assert(2 == score);
    
    score = roll("5-1", always_two);
    assert(4 == score);
    
    score = roll("5*10", always_two);
    assert(50 == score);
    
    /* roll dice that always roll 1's */
    score = roll("0d4+1", always_one);
    assert(1 == score);
    
    score = roll("1D6+2", always_one);
    assert(3 == score);
    
    score = roll("2d10+1", always_one);
    assert(3 == score);
    
    score = roll("3d4+5", always_one);
    assert(8 == score);
    
    score = roll("1d12*5", always_one);
    assert(5 == score);
    
    score = roll("2d20+1*10", always_one);
    assert(30 == score);
    
    /* roll dice that always roll 2's */
    score = roll("1D6-2", always_two);
    assert(0 == score);
    
    score = roll("2d10-2", always_two);
    assert(2 == score);
    
    score = roll("3d4-3", always_two);
    assert(3 == score);
    
    score = roll("2d8*5", always_two);
    assert(20 == score);
    
    score = roll("2d20+1*10", always_two);
    assert(50 == score);
    
    rnd_free(always_one);
    rnd_free(always_two);
}
static void
treasure_create_json_object_test(void)
{
    struct rnd *rnd = rnd_alloc_fake_median();
    struct treasure treasure;
    treasure_initialize(&treasure);
    treasure_generate_magic_items(&treasure, rnd, 3, ANY_MAGIC_ITEM);
    struct cJSON *json_object = treasure_create_json_object(&treasure);

    assert(cJSON_IsObject(json_object));

    char *json_string = cJSON_PrintUnformatted(json_object);
    char const *expected = "{"
                           "\"struct\":\"treasure\","
                           "\"rev\":0,"
                           "\"type\":null,"
                           "\"coins\":{"
                               "\"struct\":\"coins\","
                               "\"rev\":0,"
                               "\"pp\":0,"
                               "\"gp\":0,"
                               "\"ep\":0,"
                               "\"sp\":0,"
                               "\"cp\":0"
                           "},"
                           "\"gems\":[],"
                           "\"jewelry\":[],"
                           "\"maps\":[],"
                           "\"magic_items\":["
                               "{"
                                   "\"struct\":\"magic_item\","
                                   "\"rev\":0,"
                                   "\"experience_points\":2000,"
                                   "\"true_description\":\"cloak of protection +2\","
                                   "\"true_details\":[],"
                                   "\"true_value_in_cp\":4000000,"
                                   "\"type\":\"miscellaneous\""
                               "},"
                               "{"
                                   "\"struct\":\"magic_item\","
                                   "\"rev\":0,"
                                   "\"experience_points\":2000,"
                                   "\"true_description\":\"cloak of protection +2\","
                                   "\"true_details\":[],"
                                   "\"true_value_in_cp\":4000000,"
                                   "\"type\":\"miscellaneous\""
                               "},"
                               "{"
                                   "\"struct\":\"magic_item\","
                                   "\"rev\":0,"
                                   "\"experience_points\":2000,"
                                   "\"true_description\":\"cloak of protection +2\","
                                   "\"true_details\":[],"
                                   "\"true_value_in_cp\":4000000,"
                                   "\"type\":\"miscellaneous\""
                               "}"
                           "]"
                           "}";
    assert(str_eq(expected, json_string));

    free(json_string);
    cJSON_Delete(json_object);

    treasure_finalize(&treasure);
    rnd_free(rnd);
}