void test_bst_min_max() { bst b; city *c; bst_init(&b, sizeof(city), comp_city_byname); // Insert some cities. city_insert(&b, (adt_add_fn_t)bst_insert, cities_sb_cj_ab); c = bst_min(&b); TEST_ASSERT_EQUAL_STRING(c->name, "alba"); c = bst_max(&b); TEST_ASSERT_EQUAL_STRING(c->name, "sibiu"); }
void test_bst_search() { bst b; bst_node *node, *left, *right; city *c; bst_init(&b, sizeof(city), comp_city_byname); // Insert some cities. city_insert(&b, (adt_add_fn_t)bst_insert, cities_sb_cj_ab); // All cities must be included in the tree. for(int i = 0; i < CITIES_SB_CJ_AB_SIZE; i++) city_search_byname(&b, (adt_search_fn_t)bst_search, &cities_sb_cj_ab[i], cities_sb_cj_ab[i].size); }
void test_bst_insert() { bst b; bst_node *node, *left, *right; city *c; bst_init(&b, sizeof(city), comp_city_byname); /* Insert some cities. sb / cj / ab */ city_insert(&b, (adt_add_fn_t)bst_insert, cities_sb_cj_ab); // sb is the root. node = bst_getroot(&b); left = bst_node_left(node); right = bst_node_right(node); c = bst_node_value(node); TEST_ASSERT_EQUAL_STRING(c->name, "sibiu"); // sb right node is empty TEST_ASSERT_NULL(right); // Descend down the tree. node = left; left = bst_node_left(node); right = bst_node_right(node); c = bst_node_value(node); TEST_ASSERT_EQUAL_STRING(c->name, "cluj"); // cj right node is empty TEST_ASSERT_NULL(right); // Descend down the tree. node = left; left = bst_node_left(node); right = bst_node_right(node); c = bst_node_value(node); TEST_ASSERT_EQUAL_STRING(c->name, "alba"); // ab has no children TEST_ASSERT_NULL(left); TEST_ASSERT_NULL(right); }
// 创建一个新城市 int city_new( City *pCity ) { if ( pCity == NULL ) return -1; int city_index = -1; for ( city_index = 0; city_index < g_city_maxcount; city_index++ ) { if ( g_city[city_index].cityid <= 0 ) { memcpy( &g_city[city_index], pCity, sizeof(City) ); g_city[city_index].cityid = city_insert( &g_city[city_index] ); g_city[city_index].actor_index = -1; city_handle_queue_clear( &g_city[city_index] ); break; } } // 没创建成功 if ( city_index >= g_city_maxcount ) return -1; // 为了遍历的效率,计算最大的索引 if ( city_index >= g_city_maxindex ) { g_city_maxindex = city_index + 1; } // 创建建筑 for ( int tmpi = 0; tmpi < g_BuildingNewCount; tmpi++ ) { if ( g_BuildingNew[tmpi].buildingkind <= 0 ) continue; int buildingindex = building_create( &g_city[city_index], g_BuildingNew[tmpi].buildingindex, g_BuildingNew[tmpi].buildingkind ); if ( buildingindex >= 0 && buildingindex < CityBuildingMax ) { // 将刚才创建好的建筑立刻完成 building_finish( &g_city[city_index], buildingindex ); } } city_reset( &g_city[city_index] ); return city_index; }