static void test_create_duplicate_spell(CuTest * tc) { spell *sp; struct log_t *log; strlist *sl = 0; test_setup(); test_log_stderr(0); /* suppress the "duplicate spell" error message */ log = test_log_start(LOG_CPERROR, &sl); CuAssertPtrEquals(tc, NULL, find_spell("testspell")); sp = create_spell("testspell"); CuAssertPtrEquals(tc, NULL, create_spell("testspell")); CuAssertPtrNotNull(tc, sl); CuAssertStrEquals(tc, "create_spell: duplicate name '%s'", sl->s); CuAssertPtrEquals(tc, NULL, sl->next); CuAssertPtrEquals(tc, sp, find_spell("testspell")); test_log_stop(log, sl); test_log_stderr(1); /* or teardown complains that stderr logging is off */ test_teardown(); }
static void test_group_readwrite_dead_faction(CuTest *tc) { gamedata data; storage store; faction *f, *f2; unit * u; group *g; ally *al; int fno; test_cleanup(); f = test_create_faction(0); fno = f->no; CuAssertPtrEquals(tc, f, factions); CuAssertPtrEquals(tc, 0, f->next); f2 = test_create_faction(0); CuAssertPtrEquals(tc, f2, factions->next); u = test_create_unit(f2, test_create_region(0, 0, 0)); CuAssertPtrNotNull(tc, u); g = join_group(u, "group"); CuAssertPtrNotNull(tc, g); al = ally_add(&g->allies, f); CuAssertPtrNotNull(tc, al); CuAssertPtrEquals(tc, f, factions); destroyfaction(&factions); CuAssertTrue(tc, !f->_alive); CuAssertPtrEquals(tc, f2, factions); mstream_init(&data.strm); gamedata_init(&data, &store, RELEASE_VERSION); write_game(&data); free_gamedata(); f = f2 = NULL; data.strm.api->rewind(data.strm.handle); read_game(&data); mstream_done(&data.strm); gamedata_done(&data); CuAssertPtrEquals(tc, 0, findfaction(fno)); f2 = factions; CuAssertPtrNotNull(tc, f2); u = f2->units; CuAssertPtrNotNull(tc, u); g = get_group(u); CuAssertPtrNotNull(tc, g); CuAssertPtrEquals(tc, 0, g->allies); test_cleanup(); }
void test_compute_inverse(CuTest* tc) { double x,y, dx, dy; sip_t* wcs = sip_from_string(wcsfile, 0, NULL); CuAssertPtrNotNull(tc, wcs); printf("Read:\n"); sip_print_to(wcs, stdout); CuAssertIntEquals(tc, 4, wcs->a_order); CuAssertIntEquals(tc, 4, wcs->b_order); CuAssertIntEquals(tc, 0, wcs->ap_order); CuAssertIntEquals(tc, 0, wcs->bp_order); CuAssertIntEquals(tc, 0, sip_ensure_inverse_polynomials(wcs)); printf("After ensuring inverse:\n"); sip_print_to(wcs, stdout); CuAssertIntEquals(tc, 4, wcs->a_order); CuAssertIntEquals(tc, 4, wcs->b_order); CuAssertIntEquals(tc, 5, wcs->ap_order); CuAssertIntEquals(tc, 5, wcs->bp_order); dx = dy = 100; for (y=0; y<=sip_imageh(wcs); y+=dy) { for (x=0; x<=sip_imagew(wcs); x+=dx) { double ra,dec; double x2, y2; anbool ok; sip_pixelxy2radec(wcs, x, y, &ra, &dec); ok = sip_radec2pixelxy(wcs, ra, dec, &x2, &y2); CuAssertTrue(tc, ok); CuAssertDblEquals(tc, x, x2, 1e-2); CuAssertDblEquals(tc, y, y2, 1e-2); printf("x,y %g,%g --> error %g,%g\n", x,y, x2-x, y2-y); } } sip_free(wcs); }
static void test_skills(CuTest * tc) { const char * data = "{\"skills\": { \"de\" : { \"alchemy\" : \"ALCHEMIE\", \"crossbow\" : [ \"ARMBRUST\", \"KREUZBOGEN\" ] }}}"; const struct locale * lang; cJSON *json = cJSON_Parse(data); test_cleanup(); lang = get_or_create_locale("de"); CuAssertPtrNotNull(tc, json); CuAssertIntEquals(tc, NOSKILL, get_skill("potato", lang)); json_config(json); CuAssertIntEquals(tc, NOSKILL, get_skill("potato", lang)); CuAssertIntEquals(tc, SK_CROSSBOW, get_skill("armbrust", lang)); CuAssertIntEquals(tc, SK_CROSSBOW, get_skill("kreuz", lang)); CuAssertIntEquals(tc, SK_ALCHEMY, get_skill("alchemie", lang)); CuAssertStrEquals(tc, "ALCHEMIE", locale_string(lang, "skill::alchemy")); CuAssertStrEquals(tc, "ARMBRUST", locale_string(lang, "skill::crossbow")); test_cleanup(); }
void test_mapDropBucket(CuTest *tc) { HM_HASHMAP *new_map = HM_initialize_hashmap(HASHMAP_SIZE); char *key = strdup("Hello world"); int values[] = { 42, 37, 65 }; HM_putValue(new_map, key, ( (void *) &values[0] ) ); HM_putValue(new_map, key, ( (void *) &values[1] ) ); HM_putValue(new_map, key, ( (void *) &values[2] ) ); CuAssertPtrNotNull(tc, new_map); CuAssertTrue(tc, new_map->entries_used == 1); CuAssertTrue(tc, HM_getChainLength(new_map, "Hello world") == 3); int wasDropped = HM_dropBucket(new_map, "Hello world"); CuAssertTrue(tc, wasDropped); CuAssertTrue(tc, new_map->entries_used == 0); CuAssertTrue(tc, HM_getValue(new_map, "Hello world") == NULL); HM_destroyHashmap( &new_map ); free(key); }
static void testMtime(CuTest *tc) { const char *s, *mtime2; char *mtime1; int r; r = aug_set(aug, "/files/etc/hosts/1/alias[last() + 1]", "new"); CuAssertIntEquals(tc, 0, r); r = aug_get(aug, "/augeas/files/etc/hosts/mtime", &s); CuAssertIntEquals(tc, 1, r); mtime1 = strdup(s); CuAssertPtrNotNull(tc, mtime1); r = aug_save(aug); CuAssertIntEquals(tc, 0, r); r = aug_get(aug, "/augeas/files/etc/hosts/mtime", &mtime2); CuAssertIntEquals(tc, 1, r); CuAssertStrNotEqual(tc, mtime1, mtime2); CuAssertStrNotEqual(tc, "0", mtime2); }
void test_message(CuTest *tc) { message *msg; message_type *mtype; test_setup(); mtype = mt_create(mt_new("custom", NULL), NULL, 0); CuAssertPtrEquals(tc, mtype, (void *)mt_find("custom")); CuAssertIntEquals(tc, 0, mtype->nparameters); CuAssertPtrEquals(tc, NULL, (void *)mtype->pnames); CuAssertPtrEquals(tc, NULL, (void *)mtype->types); msg = msg_message("custom", ""); CuAssertPtrNotNull(tc, msg); CuAssertIntEquals(tc, 1, msg->refcount); CuAssertPtrEquals(tc, NULL, msg->parameters); CuAssertPtrEquals(tc, mtype, (void *)msg->type); CuAssertPtrEquals(tc, msg, msg_addref(msg)); CuAssertIntEquals(tc, 2, msg->refcount); msg_release(msg); CuAssertIntEquals(tc, 1, msg->refcount); msg_release(msg); test_teardown(); }
static void test_remove_dead_factions(CuTest *tc) { faction *f, *fm; region *r; int fno; test_cleanup(); r = test_create_region(0, 0, 0); fm = get_or_create_monsters(); f = test_create_faction(0); assert(fm && r && f); test_create_unit(f, r); test_create_unit(fm, r); remove_empty_factions(); CuAssertPtrEquals(tc, f, findfaction(f->no)); CuAssertPtrNotNull(tc, get_monsters()); fm->units = 0; f->_alive = false; fno = f->no; remove_empty_factions(); CuAssertPtrEquals(tc, 0, findfaction(fno)); CuAssertPtrEquals(tc, fm, get_monsters()); test_cleanup(); }
static void test_buildingtype_exists(CuTest * tc) { region *r; building *b; building_type *btype, *btype2; test_setup(); btype2 = test_create_buildingtype("castle"); assert(btype2); btype = test_create_buildingtype("lighhouse"); btype->maxsize = 10; r = test_create_plain(0, 0); b = test_create_building(r, btype); CuAssertPtrNotNull(tc, b); b->size = 10; CuAssertTrue(tc, !buildingtype_exists(r, NULL, false)); CuAssertTrue(tc, !buildingtype_exists(r, btype2, false)); CuAssertTrue(tc, buildingtype_exists(r, btype, false)); b->size = 9; fset(b, BLD_MAINTAINED); CuAssertTrue(tc, !buildingtype_exists(r, btype, false)); btype->maxsize = 0; freset(b, BLD_MAINTAINED); CuAssertTrue(tc, buildingtype_exists(r, btype, false)); btype->maxsize = 10; b->size = 10; fset(b, BLD_MAINTAINED); CuAssertTrue(tc, buildingtype_exists(r, btype, true)); freset(b, BLD_MAINTAINED); CuAssertTrue(tc, !buildingtype_exists(r, btype, true)); test_teardown(); }
void test_mapPollCollision(CuTest *tc) { HM_HASHMAP *new_map = HM_initialize_hashmap(HASHMAP_SIZE); char *key = strdup("Hello world"); int values[] = { 42, 37, 65 }; HM_putValue(new_map, key, ( (void *) &values[0] ) ); HM_putValue(new_map, key, ( (void *) &values[1] ) ); HM_putValue(new_map, key, ( (void *) &values[2] ) ); CuAssertPtrNotNull(tc, new_map); CuAssertTrue(tc, new_map->entries_used == 1); CuAssertTrue(tc, HM_getChainLength(new_map, "Hello world") == 3); CuAssertTrue(tc, *( ( int *) HM_poll(new_map, key) ) == values[0] ); CuAssertTrue(tc, *( ( int *) HM_poll(new_map, key) ) == values[1] ); CuAssertTrue(tc, *( ( int *) HM_poll(new_map, key) ) == values[2] ); CuAssertTrue(tc, new_map->entries_used == 0); HM_destroyHashmap( &new_map ); free(key); }
static void test_stype_defaults(CuTest *tc) { ship_type *stype; test_setup(); stype = st_get_or_create("hodor"); CuAssertPtrNotNull(tc, stype); CuAssertStrEquals(tc, "hodor", stype->_name); CuAssertPtrEquals(tc, NULL, stype->construction); CuAssertPtrEquals(tc, NULL, stype->coasts); CuAssertDblEquals(tc, 0.0, stype->damage, 0.0); CuAssertDblEquals(tc, 1.0, stype->storm, 0.0); CuAssertDblEquals(tc, 1.0, stype->tac_bonus, 0.01); CuAssertIntEquals(tc, 0, stype->cabins); CuAssertIntEquals(tc, 0, stype->cargo); CuAssertIntEquals(tc, 0, stype->combat); CuAssertIntEquals(tc, 0, stype->fishing); CuAssertIntEquals(tc, 0, stype->range); CuAssertIntEquals(tc, 0, stype->cptskill); CuAssertIntEquals(tc, 0, stype->minskill); CuAssertIntEquals(tc, 0, stype->sumskill); CuAssertIntEquals(tc, 0, stype->at_bonus); CuAssertIntEquals(tc, 0, stype->df_bonus); CuAssertIntEquals(tc, 0, stype->flags); test_teardown(); }
void TestMultiHandler(CuTest *tc) { struct Handler *handlers[THREAD_NUM]; struct TestObj obj[THREAD_NUM]; int i = 0; for (i = 0; i < THREAD_NUM; i ++) { handlers[i] = NewHandler(); CuAssertPtrNotNull(tc, handlers[i]); handlers[i]->HandleMessage = MultiHandleMessageImpl; obj[i].context = tc; obj[i].handler = handlers[i]; obj[i].i = i; } StartTime(); for (i = 0; i < THREAD_NUM; i ++) { pthread_create(&t[i], NULL, MultiHandlerSub, &obj[i]); } pthread_t ct; pthread_create(&ct, NULL, CollectThread, handlers[0]); LoopStart(handlers[0]->loop); }
void TestBencodeDictGetNext( CuTest * tc ) { bencode_t ben; bencode_t ben2; char *str = strdup("d3:foo3:bare"); const char *ren; int len, ret; bencode_init(&ben, str, strlen(str)); ret = bencode_dict_get_next(&ben, &ben2, &ren, &len); CuAssertTrue(tc, 1 == ret); CuAssertTrue(tc, !strncmp("foo", ren, len)); bencode_string_value(&ben2, &ren, &len); CuAssertPtrNotNull(tc, ren); CuAssertTrue(tc, !strncmp("bar", ren, len)); free(str); }
static void test_give_men_requires_contact(CuTest * tc) { struct give env = { 0 }; message * msg; order *ord; test_setup_ex(tc); env.f1 = test_create_faction(NULL); env.f2 = test_create_faction(NULL); setup_give(&env); msg = give_men(1, env.src, env.dst, NULL); CuAssertStrEquals(tc, "feedback_no_contact", test_get_messagetype(msg)); CuAssertIntEquals(tc, 1, env.dst->number); CuAssertIntEquals(tc, 1, env.src->number); ord = create_order(K_GIVE, env.f1->locale, "%s ALLES PERSONEN", itoa36(env.dst->no)); test_clear_messages(env.f1); give_cmd(env.src, ord); CuAssertPtrEquals(tc, NULL, test_find_messagetype(env.f1->msgs, "give_person")); CuAssertPtrNotNull(tc, test_find_messagetype(env.f1->msgs, "feedback_no_contact")); msg_release(msg); free_order(ord); test_teardown(); }
void Test__cstring_import(CuTest* tc) { CSTRING* a=NULL; char *str=NULL; // passing NULL, 0 a = cstring_import(NULL, 0); CuAssertPtrEquals(tc, NULL, a); // passing static string 'abcde', length 5 a = cstring_import("abcde", 5); CuAssertPtrNotNull(tc, a); CuAssertTrue(tc, 0!=a->memsize); CuAssertIntEquals(tc, 1, a->canfree); CuAssertIntEquals(tc, 5, a->length); CuAssertPtrNotNull(tc, a->string); CuAssertStrEquals(tc, "abcde", a->string); cstring_free(&a); // passing strdup'd string 'abc...xyz', strlen(strdup'd string) str = strdup("abcdefghijklmnopqrstuvwxyz"); a = cstring_import(str, strlen(str)); CuAssertPtrNotNull(tc, a); CuAssertTrue(tc, 0!=a->memsize); CuAssertIntEquals(tc, 1, a->canfree); CuAssertIntEquals(tc, 26, a->length); CuAssertPtrNotNull(tc, a->string); CuAssertStrEquals(tc, str, a->string); free(str); cstring_free(&a); // passing strdup'd string 'abc...xyz', length 5 str = strdup("abcdefghijklmnopqrstuvwxyz"); a = cstring_import(str, 5); CuAssertPtrNotNull(tc, a); CuAssertTrue(tc, 0!=a->memsize); CuAssertIntEquals(tc, 1, a->canfree); CuAssertIntEquals(tc, 5, a->length); CuAssertPtrNotNull(tc, a->string); CuAssertStrEquals(tc, "abcde", a->string); free(str); cstring_free(&a); }
/** * see https://bugs.eressea.de/view.php?id=2234 */ static void test_maintain_buildings(CuTest *tc) { region *r; building *b; building_type *btype; unit *u; faction *f; maintenance *req; item_type *itype; test_cleanup(); btype = test_create_buildingtype("Hort"); btype->maxsize = 10; r = test_create_region(0, 0, 0); f = test_create_faction(0); u = test_create_unit(f, r); b = test_create_building(r, btype); itype = test_create_itemtype("money"); b->size = btype->maxsize; u_set_building(u, b); // this building has no upkeep, it just works: b->flags = 0; maintain_buildings(r); CuAssertIntEquals(tc, BLD_MAINTAINED, fval(b, BLD_MAINTAINED)); CuAssertPtrEquals(tc, 0, f->msgs); CuAssertPtrEquals(tc, 0, r->msgs); req = calloc(2, sizeof(maintenance)); req[0].number = 100; req[0].rtype = itype->rtype; btype->maintenance = req; // we cannot afford to pay: b->flags = 0; maintain_buildings(r); CuAssertIntEquals(tc, 0, fval(b, BLD_MAINTAINED)); CuAssertPtrNotNull(tc, test_find_messagetype(f->msgs, "maintenancefail")); CuAssertPtrNotNull(tc, test_find_messagetype(r->msgs, "maintenance_nowork")); test_clear_messagelist(&f->msgs); test_clear_messagelist(&r->msgs); // we can afford to pay: i_change(&u->items, itype, 100); b->flags = 0; maintain_buildings(r); CuAssertIntEquals(tc, BLD_MAINTAINED, fval(b, BLD_MAINTAINED)); CuAssertIntEquals(tc, 0, i_get(u->items, itype)); CuAssertPtrEquals(tc, 0, r->msgs); CuAssertPtrEquals(tc, 0, test_find_messagetype(f->msgs, "maintenance_nowork")); CuAssertPtrNotNull(tc, test_find_messagetype(f->msgs, "maintenance")); test_clear_messagelist(&f->msgs); // this building has no owner, it doesn't work: u_set_building(u, NULL); b->flags = 0; maintain_buildings(r); CuAssertIntEquals(tc, 0, fval(b, BLD_MAINTAINED)); CuAssertPtrEquals(tc, 0, f->msgs); CuAssertPtrNotNull(tc, test_find_messagetype(r->msgs, "maintenance_noowner")); test_clear_messagelist(&r->msgs); test_cleanup(); }
void test_inmemory_fitsbin_2(CuTest* ct) { fitsbin_t* fb; int i; int N = 6; double outdata[6]; double* indata; fitsbin_chunk_t chunk; fitsbin_chunk_t* ch; fb = fitsbin_open_in_memory(); CuAssertPtrNotNull(ct, fb); CuAssertIntEquals(ct, 0, fitsbin_write_primary_header(fb)); for (i=0; i<N; i++) { outdata[i] = i*i; } fitsbin_chunk_init(&chunk); chunk.tablename = "test2"; chunk.itemsize = 1; //chunk.nrows = N * sizeof(double); chunk.data = outdata; CuAssertIntEquals(ct, 0, fitsbin_write_chunk_header(fb, &chunk)); for (i=0; i<N; i++) { CuAssertIntEquals(ct, 0, fitsbin_write_items(fb, &chunk, &outdata[i], sizeof(double))); } CuAssertIntEquals(ct, 0, fitsbin_fix_chunk_header(fb, &chunk)); fitsbin_chunk_reset(&chunk); chunk.tablename = "test2B"; chunk.itemsize = sizeof(double); //chunk.nrows = N; chunk.data = outdata; CuAssertIntEquals(ct, 0, fitsbin_write_chunk_header(fb, &chunk)); for (i=0; i<N; i++) { CuAssertIntEquals(ct, 0, fitsbin_write_items(fb, &chunk, outdata + (N-1) - i, 1)); } CuAssertIntEquals(ct, 0, fitsbin_fix_chunk_header(fb, &chunk)); fitsbin_chunk_clean(&chunk); // writing shouldn't affect the data values for (i=0; i<N; i++) { CuAssertIntEquals(ct, outdata[i], i*i); } CuAssertIntEquals(ct, 0, fitsbin_switch_to_reading(fb)); fitsbin_chunk_init(&chunk); chunk.tablename = "test2"; fitsbin_add_chunk(fb, &chunk); chunk.tablename = "test2B"; fitsbin_add_chunk(fb, &chunk); CuAssertIntEquals(ct, 0, fitsbin_read(fb)); ch = fitsbin_get_chunk(fb, 0); CuAssertIntEquals(ct, 1, ch->itemsize); CuAssertIntEquals(ct, N * sizeof(double), ch->nrows); indata = ch->data; CuAssertPtrNotNull(ct, indata); CuAssertIntEquals(ct, 0, memcmp(outdata, indata, sizeof(outdata))); ch = fitsbin_get_chunk(fb, 1); CuAssertIntEquals(ct, sizeof(double), ch->itemsize); CuAssertIntEquals(ct, N, ch->nrows); indata = ch->data; CuAssertPtrNotNull(ct, indata); for (i=0; i<N; i++) { CuAssertDblEquals(ct, outdata[N-1 - i], indata[i], 1e-10); } CuAssertIntEquals(ct, 0, fitsbin_close(fb)); }
static void hacf_param_list_add_test(CuTest *tc) { hacf_param_list_t *list = create_hacf_param_list(p); hacf_param_t *key1 = create_hacf_param(p, KEY1, HASH1); CuAssertIntEquals(tc, 0, add_hacf_param(list, key1)); CuAssertPtrNotNull(tc, list->first); }
void test_plot_wcs1(CuTest* tc) { plot_args_t myargs; plot_args_t* pargs = &myargs; int W, H; plotxy_t* xy; //plotimage_t* img; log_init(LOG_VERB); W = H = 5; plotstuff_init(pargs); plotstuff_set_size(pargs, W, H); pargs->outformat = PLOTSTUFF_FORMAT_PNG; pargs->outfn = "test-out1.png"; plotstuff_set_color(pargs, "black"); plotstuff_run_command(pargs, "fill"); plotstuff_output(pargs); plotstuff_free(pargs); // perfectly centered circle. plotstuff_init(pargs); plotstuff_set_size(pargs, W, H); pargs->outformat = PLOTSTUFF_FORMAT_PNG; pargs->outfn = "test-out2.png"; plotstuff_set_color(pargs, "black"); plotstuff_run_command(pargs, "fill"); xy = plotstuff_get_config(pargs, "xy"); plot_xy_vals(xy, 3, 3); plotstuff_set_color(pargs, "white"); plotstuff_set_marker(pargs, "circle"); plotstuff_set_markersize(pargs, 1); plotstuff_run_command(pargs, "xy"); plotstuff_output(pargs); plotstuff_free(pargs); // perfectly centered circle. plotstuff_init(pargs); plotstuff_set_size(pargs, W, H); pargs->outformat = PLOTSTUFF_FORMAT_PNG; pargs->outfn = "test-out3.png"; plotstuff_set_color(pargs, "black"); plotstuff_run_command(pargs, "fill"); xy = plotstuff_get_config(pargs, "xy"); plot_xy_vals(xy, 2, 2); plot_xy_set_offsets(xy, 0, 0); plotstuff_set_color(pargs, "white"); plotstuff_set_marker(pargs, "circle"); plotstuff_set_markersize(pargs, 1); plotstuff_run_command(pargs, "xy"); plotstuff_output(pargs); plotstuff_free(pargs); { unsigned char* img; int ww, hh; int i; img = cairoutils_read_png("test-out2.png", &ww, &hh); CuAssertPtrNotNull(tc, img); CuAssertIntEquals(tc, W, ww); CuAssertIntEquals(tc, H, hh); printf("image:\n"); for (i=0; i<W*H; i++) { printf("%02x ", (int)img[i*4]); if (i%W == (W-1)) printf("\n"); } printf("\n"); /* 00 00 00 00 00 00 80 e8 80 00 00 f3 44 f3 00 00 7c e8 7c 00 00 00 00 00 00 */ } }
void test_read_raw(CuTest* tc) { char* line1 = "0001 00008 1| |" " 2.31750494| 2.23184345| -16.3| -9.0|" " 68| 73| 1.7| 1.8|" "1958.89|1951.94| 4|" "1.0|1.0|0.9|1.0|" "12.146|0.158|12.146|0.223|" "999| | |" " 2.31754222| 2.23186444|1.67|1.54|" " 88.0|100.8|" " |-0.2\r\n"; char* line2 = "0001 00013 1| |" " 1.12558209| 2.26739400| 27.7| -0.5|" " 9| 12| 1.2| 1.2|" "1990.76|1989.25| 8|" "1.0|0.8|1.0|0.7|" "10.488|0.038| 8.670|0.015|" "999|T| |" " 1.12551889| 2.26739556|1.81|1.52|" " 9.3| 12.7|" " |-0.2\r\n"; // not the third line in the file... char* line3 = "0005 01505 1| |" " 3.25200241| 2.94960582| 47.8| -13.6|" " 8| 11| 1.0| 1.0|" "1990.57|1989.33|11|" "0.6|1.2|0.7|1.2|" " 9.275|0.020| 8.738|0.015|" "999|T| 1040AB |" " 3.25189250| 2.94963750|1.76|1.45|" " 8.2| 10.5|" "P|-0.2\r\n"; // line 3 from suppl_1.dat char* supp1 = "0002 01127 2|H|" "004.36837051|+00.31948829| -32.1| -9.1|" " 13.6| 8.3| 8.2| 5.0|" "H| | |" "10.279|0.041|" " 75| | 1397B\r\n"; char* fn = "/tmp/test-tycho2-0"; tycho2_entry entry1; tycho2_entry entry2; tycho2_entry entry3; tycho2_entry entry4; tycho2_fits* out; tycho2_fits* in; tycho2_entry* ein1; tycho2_entry* ein2; tycho2_entry* ein3; tycho2_entry* ein4; memset(&entry1, 0, sizeof(tycho2_entry)); memset(&entry2, 0, sizeof(tycho2_entry)); memset(&entry3, 0, sizeof(tycho2_entry)); memset(&entry4, 0, sizeof(tycho2_entry)); CuAssertIntEquals(tc, 0, tycho2_guess_is_supplement(line1)); CuAssertIntEquals(tc, 0, tycho2_parse_entry(line1, &entry1)); check_line1(tc, &entry1); CuAssertIntEquals(tc, 0, tycho2_guess_is_supplement(line2)); CuAssertIntEquals(tc, 0, tycho2_parse_entry(line2, &entry2)); check_line2(tc, &entry2); CuAssertIntEquals(tc, 0, tycho2_guess_is_supplement(line3)); CuAssertIntEquals(tc, 0, tycho2_parse_entry(line3, &entry3)); check_line3(tc, &entry3); CuAssertIntEquals(tc, 1, tycho2_guess_is_supplement(supp1)); CuAssertIntEquals(tc, 0, tycho2_supplement_parse_entry(supp1, &entry4)); check_supp1(tc, &entry4); out = tycho2_fits_open_for_writing(fn); CuAssertPtrNotNull(tc, out); CuAssertIntEquals(tc, 0, tycho2_fits_count_entries(out)); CuAssertIntEquals(tc, 0, tycho2_fits_write_headers(out)); CuAssertIntEquals(tc, 0, tycho2_fits_write_entry(out, &entry1)); CuAssertIntEquals(tc, 0, tycho2_fits_write_entry(out, &entry2)); CuAssertIntEquals(tc, 0, tycho2_fits_write_entry(out, &entry3)); CuAssertIntEquals(tc, 0, tycho2_fits_write_entry(out, &entry4)); CuAssertIntEquals(tc, 4, tycho2_fits_count_entries(out)); CuAssertIntEquals(tc, 0, tycho2_fits_fix_headers(out)); CuAssertIntEquals(tc, 0, tycho2_fits_close(out)); out = NULL; memset(&entry1, 0, sizeof(tycho2_entry)); memset(&entry2, 0, sizeof(tycho2_entry)); memset(&entry3, 0, sizeof(tycho2_entry)); memset(&entry4, 0, sizeof(tycho2_entry)); in = tycho2_fits_open(fn); CuAssertPtrNotNull(tc, in); CuAssertIntEquals(tc, 4, tycho2_fits_count_entries(in)); ein1 = tycho2_fits_read_entry(in); CuAssertPtrNotNull(tc, ein1); check_line1(tc, ein1); ein2 = tycho2_fits_read_entry(in); CuAssertPtrNotNull(tc, ein2); check_line2(tc, ein2); ein3 = tycho2_fits_read_entry(in); CuAssertPtrNotNull(tc, ein3); check_line3(tc, ein3); ein4 = tycho2_fits_read_entry(in); CuAssertPtrNotNull(tc, ein4); check_supp1(tc, ein4); CuAssertIntEquals(tc, 0, tycho2_fits_close(in)); in = NULL; }
/* Checks the accuracy of the HITS computation */ void test_hits(CuTest *tc) { /* Compute the HITS score of the following graph * +-->2---+ * | | | * | v v * 1-->5<--3 * ^ ^ | * | | | * +---4<--+ * * The link matrix L[i,j], where L[i,j] = 1 means 'i' links to 'j' is: * * +- -+ * | 0 1 0 0 1 | * | 0 0 1 0 1 | * L = | 0 0 0 1 1 | * | 1 0 0 0 1 | * | 0 0 0 0 0 | * +- -+ */ char test_dir[] = "test-pagedb-XXXXXX"; mkdtemp(test_dir); PageDB *db; int ret = page_db_new(&db, test_dir); CuAssert(tc, db!=0? db->error->message: "NULL", ret == 0); db->persist = 0; char *urls[5] = {"1", "2", "3", "4", "5" }; LinkInfo links_1[] = {{"2", 0.1}, {"5", 0.1}}; LinkInfo links_2[] = {{"3", 0.1}, {"5", 0.1}}; LinkInfo links_3[] = {{"4", 0.1}, {"5", 0.1}}; LinkInfo links_4[] = {{"1", 0.1}, {"5", 0.1}}; LinkInfo *links[5] = { links_1, links_2, links_3, links_4, 0 }; int n_links[5] = {2, 2, 2, 2, 0}; for (int i=0; i<5; ++i) { CrawledPage *cp = crawled_page_new(urls[i]); for (int j=0; j<n_links[i]; ++j) crawled_page_add_link(cp, links[i][j].url, links[i][j].score); cp->score = i/5.0; crawled_page_set_hash64(cp, i); PageInfoList *pil; CuAssert(tc, db->error->message, page_db_add(db, cp, &pil) == 0); page_info_list_delete(pil); crawled_page_delete(cp); } PageDBLinkStream *st; CuAssert(tc, db->error->message, page_db_link_stream_new(&st, db) == 0); st->only_diff_domain = 0; Hits *hits; ret = hits_new(&hits, test_dir, 5); CuAssert(tc, hits!=0? hits->error->message: "NULL", ret == 0); hits->precision = 1e-8; CuAssert(tc, hits->error->message, hits_compute(hits, st, page_db_link_stream_next, page_db_link_stream_reset) == 0); page_db_link_stream_delete(st); uint64_t idx; float *h_score; float *a_score; float h_scores[5] = {0.250, 0.250, 0.250, 0.250, 0.000}; float a_scores[5] = {0.125, 0.125, 0.125, 0.125, 0.500}; for (int i=0; i<5; ++i) { CuAssert(tc, db->error->message, page_db_get_idx(db, page_db_hash(urls[i]), &idx) == 0); CuAssertPtrNotNull(tc, h_score = mmap_array_idx(hits->h1, idx)); CuAssertPtrNotNull(tc, a_score = mmap_array_idx(hits->a1, idx)); CuAssertDblEquals(tc, h_scores[i], *h_score, 1e-6); CuAssertDblEquals(tc, a_scores[i], *a_score, 1e-6); } CuAssert(tc, hits->error->message, hits_delete(hits) == 0); page_db_delete(db); }
/** * Tests all hash map functions after adding and removing all of the given keys * and values. * @param test * @param links The key-value pairs to be added and removed. * @param notKeys Some keys not in the table to test contains and get. * @param numLinks The number of key-value pairs to be added and removed. * @param numNotKeys The number of keys not in the table. * @param numBuckets The initial number of buckets (capacity) in the table. */ void testCase(CuTest* test, HashLink* links, const char** notKeys, int numLinks, int numNotKeys, int numBuckets) { HashMap* map = hashMapNew(numBuckets); Histogram hist; // Add links for (int i = 0; i < numLinks; i++) { hashMapPut(map, links[i].key, links[i].value); } // Print table printf("\nAfter adding all key-value pairs:"); hashMapPrint(map); // Check size CuAssertIntEquals(test, numLinks, hashMapSize(map)); // Check capacity CuAssertIntEquals(test, map->capacity, hashMapCapacity(map)); // Check empty buckets int sum = 0; for (int i = 0; i < map->capacity; i++) { if (map->table[i] == NULL) { sum++; } } CuAssertIntEquals(test, sum, hashMapEmptyBuckets(map)); // Check table load CuAssertIntEquals(test, (float)numLinks / map->capacity, hashMapTableLoad(map)); // Check contains and get on valid keys. for (int i = 0; i < numLinks; i++) { CuAssertIntEquals(test, 1, hashMapContainsKey(map, links[i].key)); int* value = hashMapGet(map, links[i].key); CuAssertPtrNotNull(test, value); CuAssertIntEquals(test, links[i].value, *value); } // Check contains and get on invalid keys. for (int i = 0; i < numNotKeys; i++) { CuAssertIntEquals(test, 0, hashMapContainsKey(map, notKeys[i])); CuAssertPtrEquals(test, NULL, hashMapGet(map, notKeys[i])); } // Check that all links are present and have a unique key. histFromTable(&hist, map); CuAssertIntEquals(test, numLinks, hist.size); assertHistCounts(test, &hist); histCleanUp(&hist); // Remove keys for (int i = 0; i < numLinks; i++) { hashMapRemove(map, links[i].key); } // Print table printf("\nAfter removing all key-value pairs:"); hashMapPrint(map); // Check size CuAssertIntEquals(test, 0, hashMapSize(map)); // Check capacity CuAssertIntEquals(test, map->capacity, hashMapCapacity(map)); // Check empty buckets CuAssertIntEquals(test, map->capacity, hashMapEmptyBuckets(map)); // Check table load CuAssertIntEquals(test, 0, hashMapTableLoad(map)); // Check contains and get on valid keys. for (int i = 0; i < numLinks; i++) { CuAssertIntEquals(test, 0, hashMapContainsKey(map, links[i].key)); CuAssertPtrEquals(test, NULL, hashMapGet(map, links[i].key)); } // Check contains and get on invalid keys. for (int i = 0; i < numNotKeys; i++) { CuAssertIntEquals(test, 0, hashMapContainsKey(map, notKeys[i])); CuAssertPtrEquals(test, NULL, hashMapGet(map, notKeys[i])); } // Check that there are no links in the table. histFromTable(&hist, map); CuAssertIntEquals(test, 0, hist.size); assertHistCounts(test, &hist); histCleanUp(&hist); hashMapDelete(map); }
void test_interface(CuTest * tc) { CuAssertPtrNotNull(tc, svc.reset); CuAssertPtrNotNull(tc, svc.get_regions); CuAssertPtrNotNull(tc, svc.add_event); CuAssertTrue(tc, 0!=svc.max_directions); CuAssertPtrNotNull(tc, svc.regions); CuAssertPtrNotNull(tc, svc.regions->create); CuAssertPtrNotNull(tc, svc.regions->destroy); CuAssertPtrNotNull(tc, svc.regions->get_xy); CuAssertPtrNotNull(tc, svc.regions->get_units); CuAssertPtrNotNull(tc, svc.units); CuAssertPtrNotNull(tc, svc.units->create); CuAssertPtrNotNull(tc, svc.units->destroy); CuAssertPtrNotNull(tc, svc.units->get_uid); CuAssertPtrNotNull(tc, svc.units->get_region); CuAssertPtrNotNull(tc, svc.units->get); CuAssertPtrNotNull(tc, svc.units->get_moves); CuAssertPtrNotNull(tc, svc.units->get_movement_speed); }
/* Tests the loading/dumping of PageInfo from and into LMDB values */ void test_page_info_serialization(CuTest *tc) { printf("%s\n", __func__); MDB_val val; PageInfo pi1 = { .url = "test_url_123", .first_crawl = 123, .last_crawl = 456, .n_changes = 100, .n_crawls = 20, .score = 0.7, .content_hash_length = 8, .content_hash = "1234567" }; CuAssertTrue(tc, page_info_dump(&pi1, &val) == 0); CuAssertDblEquals(tc, 0.7, page_info_dump_get_score(&val), 1e-6); PageInfo *pi2 = page_info_load(&val); CuAssertPtrNotNull(tc, pi2); free(val.mv_data); CuAssertStrEquals(tc, pi1.url, pi2->url); CuAssertTrue(tc, pi1.first_crawl == pi2->first_crawl); CuAssertTrue(tc, pi1.last_crawl == pi2->last_crawl); CuAssertTrue(tc, pi1.n_changes == pi2->n_changes); CuAssertTrue(tc, pi1.n_crawls == pi2->n_crawls); CuAssertTrue(tc, pi1.score == pi2->score); CuAssertTrue(tc, pi1.content_hash_length == pi2->content_hash_length); CuAssertStrEquals(tc, pi1.content_hash, pi2->content_hash); page_info_delete(pi2); } /* Tests all the database operations on a very simple crawl of just two pages */ void test_page_db_simple(CuTest *tc) { printf("%s\n", __func__); char test_dir[] = "test-pagedb-XXXXXX"; mkdtemp(test_dir); PageDB *db; int ret = page_db_new(&db, test_dir); CuAssert(tc, db!=0? db->error->message: "NULL", ret == 0); db->persist = 0; CrawledPage *cp1 = crawled_page_new("www.yahoo.com"); crawled_page_add_link(cp1, "a", 0.1); crawled_page_add_link(cp1, "b", 0.2); crawled_page_add_link(cp1, "www.google.com", 0.3); crawled_page_set_hash64(cp1, 1000); cp1->score = 0.5; CrawledPage *cp2 = crawled_page_new("www.bing.com"); crawled_page_add_link(cp2, "x", 1.1); crawled_page_add_link(cp2, "y", 1.2); crawled_page_set_hash64(cp2, 2000); cp2->score = 0.2; PageInfoList *pil; CuAssert(tc, db->error->message, page_db_add(db, cp1, &pil) == 0); page_info_list_delete(pil); CuAssert(tc, db->error->message, page_db_add(db, cp2, &pil) == 0); page_info_list_delete(pil); crawled_page_set_hash64(cp2, 3000); CuAssert(tc, db->error->message, page_db_add(db, cp2, &pil) == 0); page_info_list_delete(pil); MMapArray *scores = 0; CuAssert(tc, db->error->message, page_db_get_scores(db, &scores) == 0); size_t idx; CuAssert(tc, db->error->message, page_db_get_idx(db, page_db_hash("www.yahoo.com"), &idx) == 0); CuAssertDblEquals( tc, 0.5, *(float*)mmap_array_idx(scores, idx), 1e-6); CuAssert(tc, db->error->message, page_db_get_idx(db, page_db_hash("x"), &idx) == 0); CuAssertDblEquals( tc, 1.1, *(float*)mmap_array_idx(scores, idx), 1e-6); CHECK_DELETE(tc, scores->error->message, mmap_array_delete(scores)); crawled_page_delete(cp1); crawled_page_delete(cp2); char pi_out[1000]; char *print_pages[] = {"www.yahoo.com", "www.google.com", "www.bing.com"}; for (size_t i=0; i<3; ++i) { PageInfo *pi; CuAssert(tc, db->error->message, page_db_get_info(db, page_db_hash(print_pages[i]), &pi) == 0); CuAssertPtrNotNull(tc, pi); switch(i) { case 0: CuAssertIntEquals(tc, 1, pi->n_crawls); CuAssertIntEquals(tc, 0, pi->n_changes); break; case 1: CuAssertIntEquals(tc, 0, pi->n_crawls); break; case 2: CuAssertIntEquals(tc, 2, pi->n_crawls); CuAssertIntEquals(tc, 1, pi->n_changes); break; } page_info_print(pi, pi_out); page_info_delete(pi); /* show on screen the page info: * * Mon Apr 6 15:34:50 2015|Mon Apr 6 15:34:50 2015|1.00e+00|0.00e+00|www.yahoo.com * Thu Jan 1 01:00:00 1970|Thu Jan 1 01:00:00 1970|0.00e+00|0.00e+00|www.google.com * Mon Apr 6 15:34:50 2015|Mon Apr 6 15:34:50 2015|2.00e+00|1.00e+00|www.bing.com */ #if 0 printf("%s\n", pi_out); #endif } PageDBLinkStream *es; CuAssert(tc, db->error->message, page_db_link_stream_new(&es, db) == 0); es->only_diff_domain = 0; if (es->state == stream_state_init) { Link link; int i=0; while (page_db_link_stream_next(es, &link) == stream_state_next) { switch(i++) { case 0: CuAssertIntEquals(tc, 0, link.from); CuAssertIntEquals(tc, 1, link.to); break; case 1: CuAssertIntEquals(tc, 0, link.from); CuAssertIntEquals(tc, 2, link.to); break; case 2: CuAssertIntEquals(tc, 0, link.from); CuAssertIntEquals(tc, 3, link.to); break; case 3: CuAssertIntEquals(tc, 4, link.from); CuAssertIntEquals(tc, 5, link.to); break; case 4: CuAssertIntEquals(tc, 4, link.from); CuAssertIntEquals(tc, 6, link.to); break; default: CuFail(tc, "too many links"); break; } } CuAssertTrue(tc, es->state != stream_state_error); } page_db_link_stream_delete(es); page_db_delete(db); }
static void check_flag(CuTest *tc, const char *name, int flag) { const struct race *rc = race_with_flag(name); CuAssertPtrNotNull(tc, rc); CuAssertIntEquals(tc, flag, rc->flags); }
static void test_recreate_world(CuTest * tc) { test_cleanup(); CuAssertPtrEquals(tc, 0, get_locale("de")); CuAssertPtrEquals(tc, 0, (void *)rt_find("horse")); CuAssertPtrNotNull(tc, get_resourcetype(R_LIFE)); CuAssertPtrNotNull(tc, get_resourcetype(R_PERMAURA)); CuAssertPtrNotNull(tc, get_resourcetype(R_AURA)); CuAssertPtrNotNull(tc, (void *)rt_find("money")); test_create_world(); CuAssertPtrEquals(tc, default_locale, get_locale("de")); CuAssertPtrNotNull(tc, default_locale); CuAssertPtrNotNull(tc, findregion(0, 0)); CuAssertPtrNotNull(tc, (void *)rt_find("horse")); CuAssertPtrNotNull(tc, get_resourcetype(R_HORSE)); CuAssertPtrNotNull(tc, (void *)rt_find("money")); CuAssertPtrNotNull(tc, get_resourcetype(R_LIFE)); CuAssertPtrNotNull(tc, get_resourcetype(R_SILVER)); CuAssertPtrNotNull(tc, get_resourcetype(R_AURA)); CuAssertPtrNotNull(tc, get_resourcetype(R_PERMAURA)); CuAssertPtrNotNull(tc, get_resourcetype(R_PEASANT)); CuAssertPtrNotNull(tc, get_resourcetype(R_UNIT)); test_cleanup(); CuAssertPtrEquals(tc, 0, get_locale("de")); CuAssertPtrEquals(tc, 0, (void*)rt_find("horse")); CuAssertPtrEquals(tc, 0, (void*)get_resourcetype(R_HORSE)); CuAssertPtrNotNull(tc, (void *)rt_find("money")); CuAssertPtrNotNull(tc, get_resourcetype(R_LIFE)); CuAssertPtrNotNull(tc, get_resourcetype(R_SILVER)); CuAssertPtrNotNull(tc, get_resourcetype(R_AURA)); CuAssertPtrNotNull(tc, get_resourcetype(R_PERMAURA)); CuAssertPtrNotNull(tc, get_resourcetype(R_PEASANT)); CuAssertPtrNotNull(tc, get_resourcetype(R_UNIT)); CuAssertPtrEquals(tc, 0, findregion(0, 0)); }
static void test_buildings(CuTest * tc) { const char * data = "{\"buildings\": { " "\"house\" : { " "\"maintenance\" : " "{ \"type\" : \"iron\", \"amount\" : 1, \"flags\" : [ \"required\", \"variable\" ] }" "," "\"construction\" : {" "\"maxsize\" : 20," "\"reqsize\" : 10," "\"minskill\" : 1," "\"materials\" : {" "\"stone\" : 2," "\"iron\" : 1" "}}}," "\"shed\" : {" "\"maintenance\" : [" "{ \"type\" : \"iron\", \"amount\" : 1 }," "{ \"type\" : \"stone\", \"amount\" : 2 }" "]}" "}}"; cJSON *json = cJSON_Parse(data); const building_type *bt; test_cleanup(); CuAssertPtrNotNull(tc, json); CuAssertPtrEquals(tc, 0, buildingtypes); json_config(json); CuAssertPtrNotNull(tc, buildingtypes); bt = bt_find("shed"); CuAssertPtrNotNull(tc, bt); CuAssertPtrNotNull(tc, bt->maintenance); CuAssertPtrEquals(tc, (void *)get_resourcetype(R_IRON), (void *)bt->maintenance[0].rtype); CuAssertPtrEquals(tc, (void *)get_resourcetype(R_STONE), (void *)bt->maintenance[1].rtype); CuAssertIntEquals(tc, 1, bt->maintenance[0].number); CuAssertIntEquals(tc, 2, bt->maintenance[1].number); CuAssertIntEquals(tc, 0, bt->maintenance[2].number); bt = bt_find("house"); CuAssertPtrNotNull(tc, bt); CuAssertPtrNotNull(tc, bt->maintenance); CuAssertIntEquals(tc, 1, bt->maintenance[0].number); CuAssertPtrEquals(tc, (void *)get_resourcetype(R_IRON), (void *)bt->maintenance[0].rtype); CuAssertIntEquals(tc, MTF_VARIABLE|MTF_VITAL, bt->maintenance[0].flags); CuAssertIntEquals(tc, 0, bt->maintenance[1].number); CuAssertPtrNotNull(tc, bt->construction); CuAssertPtrNotNull(tc, bt->construction->materials); CuAssertIntEquals(tc, 2, bt->construction->materials[0].number); CuAssertPtrEquals(tc, (void *)get_resourcetype(R_STONE), (void *)bt->construction->materials[0].rtype); CuAssertIntEquals(tc, 1, bt->construction->materials[1].number); CuAssertPtrEquals(tc, (void *)get_resourcetype(R_IRON), (void *)bt->construction->materials[1].rtype); CuAssertIntEquals(tc, 0, bt->construction->materials[2].number); CuAssertIntEquals(tc, 10, bt->construction->reqsize); CuAssertIntEquals(tc, 20, bt->construction->maxsize); CuAssertIntEquals(tc, 1, bt->construction->minskill); CuAssertPtrEquals(tc, 0, bt->construction->improvement); test_cleanup(); }
void Testredblack_tree_walk_postorder(CuTest *tc) { test_redblack_tree_t *root; test_redblack_tree_t *elem; void *pret; bool ret; root = NULL; elem = malloc(sizeof(*elem)); elem->key = 8; ret = redblack_tree_insert(root, elem, redblack_tree, BTREE_COMPARE); CuAssertTrue(tc, ret); elem = malloc(sizeof(*elem)); elem->key = 6; ret = redblack_tree_insert(root, elem, redblack_tree, BTREE_COMPARE); CuAssertTrue(tc, ret); elem = malloc(sizeof(*elem)); elem->key = 5; ret = redblack_tree_insert(root, elem, redblack_tree, BTREE_COMPARE); CuAssertTrue(tc, ret); elem = malloc(sizeof(*elem)); elem->key = 7; ret = redblack_tree_insert(root, elem, redblack_tree, BTREE_COMPARE); CuAssertTrue(tc, ret); elem = malloc(sizeof(*elem)); elem->key = 10; ret = redblack_tree_insert(root, elem, redblack_tree, BTREE_COMPARE); CuAssertTrue(tc, ret); elem = malloc(sizeof(*elem)); elem->key = 9; ret = redblack_tree_insert(root, elem, redblack_tree, BTREE_COMPARE); CuAssertTrue(tc, ret); elem = malloc(sizeof(*elem)); elem->key = 11; ret = redblack_tree_insert(root, elem, redblack_tree, BTREE_COMPARE); CuAssertTrue(tc, ret); walk_state = true; prev_value = -1; redblack_tree_walk_postorder(root, redblack_tree, redblack_tree_walk_postorder_cb, NULL); CuAssertIntEquals(tc,true, walk_state); pret = redblack_tree_remove(root, 8, redblack_tree, BTREE_COMPARE, BTREE_COMPARE_KEY); CuAssertPtrNotNull(tc, pret); pret = redblack_tree_remove(root, 6, redblack_tree, BTREE_COMPARE, BTREE_COMPARE_KEY); CuAssertPtrNotNull(tc, pret); pret = redblack_tree_remove(root, 5, redblack_tree, BTREE_COMPARE, BTREE_COMPARE_KEY); CuAssertPtrNotNull(tc, pret); pret = redblack_tree_remove(root, 7, redblack_tree, BTREE_COMPARE, BTREE_COMPARE_KEY); CuAssertPtrNotNull(tc, pret); pret = redblack_tree_remove(root, 10, redblack_tree, BTREE_COMPARE, BTREE_COMPARE_KEY); CuAssertPtrNotNull(tc, pret); pret = redblack_tree_remove(root, 9, redblack_tree, BTREE_COMPARE, BTREE_COMPARE_KEY); CuAssertPtrNotNull(tc, pret); pret = redblack_tree_remove(root, 11, redblack_tree, BTREE_COMPARE, BTREE_COMPARE_KEY); CuAssertPtrNotNull(tc, pret); CuAssertPtrEquals(tc, NULL, root); }
static void testGet(CuTest *tc) { int r; const char *value; struct augeas *aug; aug = aug_init(root, loadpath, AUG_NO_STDINC|AUG_NO_LOAD); CuAssertPtrNotNull(tc, aug); CuAssertIntEquals(tc, AUG_NOERROR, aug_error(aug)); /* Make sure we're looking at the right thing */ r = aug_match(aug, "/augeas/version/save/*", NULL); CuAssertTrue(tc, r > 1); CuAssertIntEquals(tc, AUG_NOERROR, aug_error(aug)); /* aug_get returns 1 and the value if exactly one node matches */ r = aug_get(aug, "/augeas/version/save/*[1]", &value); CuAssertIntEquals(tc, 1, r); CuAssertPtrNotNull(tc, value); CuAssertIntEquals(tc, AUG_NOERROR, aug_error(aug)); /* aug_get returns 0 and no value when no node matches */ r = aug_get(aug, "/augeas/version/save/*[ last() + 1 ]", &value); CuAssertIntEquals(tc, 0, r); CuAssertPtrEquals(tc, NULL, value); CuAssertIntEquals(tc, AUG_NOERROR, aug_error(aug)); /* aug_get should return an error when multiple nodes match */ r = aug_get(aug, "/augeas/version/save/*", &value); CuAssertIntEquals(tc, -1, r); CuAssertPtrEquals(tc, NULL, value); CuAssertIntEquals(tc, AUG_EMMATCH, aug_error(aug)); /* augeas should prepend context if relative path given */ r = aug_set(aug, "/augeas/context", "/augeas/version"); r = aug_get(aug, "save/*[1]", &value); CuAssertIntEquals(tc, 1, r); CuAssertPtrNotNull(tc, value); CuAssertIntEquals(tc, AUG_NOERROR, aug_error(aug)); /* augeas should still work with an empty context */ r = aug_set(aug, "/augeas/context", ""); r = aug_get(aug, "/augeas/version", &value); CuAssertIntEquals(tc, 1, r); CuAssertPtrNotNull(tc, value); CuAssertIntEquals(tc, AUG_NOERROR, aug_error(aug)); /* augeas should ignore trailing slashes in context */ r = aug_set(aug, "/augeas/context", "/augeas/version/"); r = aug_get(aug, "save/*[1]", &value); CuAssertIntEquals(tc, 1, r); CuAssertPtrNotNull(tc, value); CuAssertIntEquals(tc, AUG_NOERROR, aug_error(aug)); /* augeas should create non-existent context path */ r = aug_set(aug, "/augeas/context", "/context/foo"); r = aug_set(aug, "bar", "value"); r = aug_get(aug, "/context/foo/bar", &value); CuAssertIntEquals(tc, 1, r); CuAssertPtrNotNull(tc, value); CuAssertIntEquals(tc, AUG_NOERROR, aug_error(aug)); aug_close(aug); }
void test_fitsbin_2(CuTest* ct) { fitsbin_t* in, *out; int i; int N = 6; double outdata[6]; double* indata; char* fn; fitsbin_chunk_t chunk; fitsbin_chunk_t* ch; fn = get_tmpfile(0); printf("Writing to %s\n", fn); out = fitsbin_open_for_writing(fn); CuAssertPtrNotNull(ct, out); CuAssertIntEquals(ct, 0, fitsbin_write_primary_header(out)); for (i=0; i<N; i++) { outdata[i] = i*i; } fitsbin_chunk_init(&chunk); chunk.tablename = "test2"; chunk.itemsize = 1; //chunk.nrows = N * sizeof(double); chunk.data = outdata; CuAssertIntEquals(ct, 0, fitsbin_write_chunk_header(out, &chunk)); for (i=0; i<N; i++) { CuAssertIntEquals(ct, 0, fitsbin_write_items(out, &chunk, &outdata[i], sizeof(double))); } CuAssertIntEquals(ct, 0, fitsbin_fix_chunk_header(out, &chunk)); fitsbin_chunk_reset(&chunk); chunk.tablename = "test2B"; chunk.itemsize = sizeof(double); //chunk.nrows = N; chunk.data = outdata; CuAssertIntEquals(ct, 0, fitsbin_write_chunk_header(out, &chunk)); for (i=0; i<N; i++) { CuAssertIntEquals(ct, 0, fitsbin_write_items(out, &chunk, outdata + (N-1) - i, 1)); } CuAssertIntEquals(ct, 0, fitsbin_fix_chunk_header(out, &chunk)); CuAssertIntEquals(ct, fitsbin_close(out), 0); fitsbin_chunk_clean(&chunk); // writing shouldn't affect the data values for (i=0; i<N; i++) { CuAssertIntEquals(ct, outdata[i], i*i); } in = fitsbin_open(fn); CuAssertPtrNotNull(ct, in); fitsbin_chunk_init(&chunk); chunk.tablename = "test2"; fitsbin_add_chunk(in, &chunk); chunk.tablename = "test2B"; fitsbin_add_chunk(in, &chunk); CuAssertIntEquals(ct, 0, fitsbin_read(in)); ch = fitsbin_get_chunk(in, 0); CuAssertIntEquals(ct, 1, ch->itemsize); CuAssertIntEquals(ct, N * sizeof(double), ch->nrows); indata = ch->data; CuAssertPtrNotNull(ct, indata); CuAssertIntEquals(ct, 0, memcmp(outdata, indata, sizeof(outdata))); ch = fitsbin_get_chunk(in, 1); CuAssertIntEquals(ct, sizeof(double), ch->itemsize); CuAssertIntEquals(ct, N, ch->nrows); indata = ch->data; CuAssertPtrNotNull(ct, indata); for (i=0; i<N; i++) { CuAssertDblEquals(ct, outdata[N-1 - i], indata[i], 1e-10); } CuAssertIntEquals(ct, 0, fitsbin_close(in)); }