Beispiel #1
0
static void
test_client_new(CuTest *tc)
{
    const char *err = "client.c client_new FAIL";
    client_t *c1, *c2;
    int max = STATIC_ALLOC_CLIENTS; /* For testing, use 10 is plenty */
    int middle = max / 2;
    int i;

    if (STATIC_ALLOC_CLIENTS % 2 == 1) {
        CuFail(tc, "client.c client_new FAIL -- "
               "rerun with even number of clients allocated");
    }

    for (i = 0; i < max; ++i) {
        c1 = client_new();
        if (i == middle - 1) {
            //printf("Freeing client %d\n", i);
            client_free(c1);
            c1 = NULL;
        }
    }

    c1 = client_new();
    client_free(c1);
    c2 = client_new();
    CuAssertPtrEquals_Msg(tc, err, c1, c2);

    c1 = client_new();
    CuAssertPtrEquals_Msg(tc, err, NULL, c1);

#if UNIT_TESTING_VERBOSITY_LEVEL >= 1
    printf("client.c client_new PASS\n");
#endif
}
Beispiel #2
0
static void
test_ev_watcher_new(CuTest *tc)
{
    const char *err = "client.c ev_watcher_new FAIL";
    struct ev_io *e1, *e2;
    int max = STATIC_ALLOC_CLIENTS;
    int middle = max / 2;
    int i, j;

    if (STATIC_ALLOC_CLIENTS % 2 == 1) {
        CuFail(tc, "client.c ev_watcher_new FAIL -- "
               "rerun with even number of clients allocated");
    }

    for (i = 0; i < max; ++i) {
        e1 = ev_watcher_new();
        if (i == middle - 1) {
            //printf("Freeing ev_io watcher %d\n", i);
            ev_watcher_free(e1);
            e1 = NULL;
        }
    }

    /*
     * Here we reacquire a pointer to the single ev_io struct that
     * was freed in the loop above.
     * We then free this again, and then acquire it again to make
     * sure that every single structure is checked before the
     * function returns NULL.
     * This means that e1 == e2, as we're reacquiring the same
     * ev_io struct.
     */
    e1 = ev_watcher_new();
    ev_watcher_free(e1);
    e2 = ev_watcher_new();
    CuAssertPtrEquals_Msg(tc, err, e1, e2);

    /*
     * At this stage, all of the ev_io structures have been
     * allocated.
     * The function should loop over everything, and then return
     * NULL to indicate that there is no memory left.
     */
    e1 = ev_watcher_new();
    CuAssertPtrEquals_Msg(tc, err, NULL, e1);

#if UNIT_TESTING_VERBOSITY_LEVEL >= 1
    printf("client.c ev_watcher_new PASS\n");
#endif
}
Beispiel #3
0
static void
test_world_player_foreach(CuTest *tc)
{
    const char *err = "world.c PLAYER_FOREACH macro FAIL";
    const char *username = "******";
    int max = MAX_PLAYERS;
    int mid = max / 2;
    player_t *player;
    player_t *plist[max];
    int i;

    if (max % 2 == 1) {
        CuFail(tc, "world.c PLAYER_FOREACH macro FAIL -- "
               "Rerun tests with even number of max players");
    }

    for (i = 0; i < max; ++i) {
        player = get_unused_player_slot();
        CuAssertIntEquals_Msg(tc, err, i + 1, player->index);
        player->username = username;
        if (i == mid - 1) {
            remove_player(player);
            continue;
        }

        plist[i] = player;
    }

    i = 0;
    player = NULL;
    PLAYER_LIST_FOREACH(player) {
        if (player->index == UNUSED) {
            ++i;
            continue;
        }

        CuAssertPtrEquals_Msg(tc, err, plist[i++], player);
    }

    player = get_unused_player_slot();
    CuAssertIntEquals_Msg(tc, err, mid, player->index);

    player = get_unused_player_slot();
    CuAssertPtrEquals_Msg(tc, err, NULL, player);

#if UNIT_TESTING_VERBOSITY_LEVEL >= 1
    printf("world.c PLAYER_FOREACH macro PASS\n");
#endif
}
Beispiel #4
0
static void test_piracy_cmd_errors(CuTest * tc) {
    race *r;
    faction *f;
    unit *u, *u2;
    ship_type *st_boat;

    test_setup();
    setup_piracy();

    st_boat = st_get_or_create("boat");
    r = test_create_race("pirates");
    u = test_create_unit(f = test_create_faction(r), test_create_region(0, 0, get_or_create_terrain("ocean")));
    f->locale = test_create_locale();
    u->thisorder = create_order(K_PIRACY, f->locale, "");
    assert(u && u->thisorder);

    piracy_cmd(u);
    CuAssertPtrNotNullMsg(tc, "must be on a ship for PIRACY", test_find_messagetype(f->msgs, "error144"));

    test_clear_messages(f);
    fset(r, RCF_SWIM);
    piracy_cmd(u);
    CuAssertPtrEquals_Msg(tc, "swimmers are pirates", 0, test_find_messagetype(f->msgs, "error144"));
    CuAssertPtrEquals_Msg(tc, "swimmers are pirates", 0, test_find_messagetype(f->msgs, "error146"));
    freset(r, RCF_SWIM);
    fset(r, RCF_FLY);
    CuAssertPtrEquals_Msg(tc, "flyers are pirates", 0, test_find_messagetype(f->msgs, "error144"));
    freset(r, RCF_FLY);
    test_clear_messages(f);

    u_set_ship(u, test_create_ship(u->region, st_boat));

    u2 = test_create_unit(u->faction, u->region);
    u2->thisorder = create_order(K_PIRACY, f->locale, "");
    u_set_ship(u2, u->ship);

    test_clear_messages(f);
    piracy_cmd(u2);
    CuAssertPtrNotNullMsg(tc, "must be owner for PIRACY", test_find_messagetype(f->msgs, "error146"));

    test_clear_messages(f);
    piracy_cmd(u);
    CuAssertPtrNotNullMsg(tc, "must specify target for PIRACY", test_find_messagetype(f->msgs, "piratenovictim"));
    CuAssertPtrNotNull(tc, u->thisorder);

    test_teardown();
}
Beispiel #5
0
static void test_attrib_add(CuTest * tc)
{
    attrib_type at_foo = { "foo" };
    attrib_type at_bar = { "bar" };
    attrib *a, *alist = 0;

    CuAssertPtrNotNull(tc, (a = a_new(&at_foo)));
    CuAssertPtrEquals(tc, a, a_add(&alist, a));
    CuAssertPtrEquals(tc, a, alist);

    CuAssertPtrNotNull(tc, (a = a_add(&alist, a_new(&at_foo))));
    CuAssertPtrEquals_Msg(tc, "new attribute not added after existing", alist->next, a);

    CuAssertPtrNotNull(tc, (a = a_add(&alist, a_new(&at_bar))));
    CuAssertPtrEquals_Msg(tc, "new atribute not added at end of list", alist->next->next, a);

    CuAssertPtrNotNull(tc, (a = a_add(&alist, a_new(&at_foo))));
    CuAssertPtrEquals_Msg(tc, "messages not sorted by type", alist->next->next, a);
    a_removeall(&alist, &at_foo);
    a_removeall(&alist, &at_bar);
}
Beispiel #6
0
void Test_iniparser_load(CuTest *tc)
{
    DIR *dir;
    struct dirent *curr;
    struct stat curr_stat;
    dictionary *dic;
    char ini_path[256];

    /* Dummy tests */
    dic = iniparser_load("/you/shall/not/path");
    CuAssertPtrEquals(tc, NULL, dic);

    /* Test all the good .ini files */
    dir = opendir(GOOD_INI_PATH);
    CuAssertPtrNotNullMsg(tc, "Cannot open good .ini conf directory", dir);
    for (curr = readdir(dir); curr != NULL; curr = readdir(dir)) {
        sprintf(ini_path, "%s/%s", GOOD_INI_PATH, curr->d_name);
        stat(ini_path, &curr_stat);
        if (S_ISREG(curr_stat.st_mode)) {
            dic = iniparser_load(ini_path);
            CuAssertPtrNotNullMsg(tc, ini_path, dic);
            dictionary_del(dic);
        }
    }
    closedir(dir);

    /* Test all the bad .ini files */
    dir = opendir(BAD_INI_PATH);
    CuAssertPtrNotNullMsg(tc, "Cannot open bad .ini conf directory", dir);
    for (curr = readdir(dir); curr != NULL; curr = readdir(dir)) {
        sprintf(ini_path, "%s/%s", BAD_INI_PATH, curr->d_name);
        stat(ini_path, &curr_stat);
        if (S_ISREG(curr_stat.st_mode)) {
            dic = iniparser_load(ini_path);
            CuAssertPtrEquals_Msg(tc, ini_path, NULL, dic);
            dictionary_del(dic);
        }
    }
    closedir(dir);
}