/** Create new node, taking ownership of hash parameter */ GhtErr ght_node_new_from_coordinate(const GhtCoordinate *coord, unsigned int resolution, GhtNode **node) { GhtHash *hash; assert(node != NULL); assert(coord != NULL); GHT_TRY(ght_hash_from_coordinate(coord, resolution, &hash)); GHT_TRY(ght_node_new(node)); GHT_TRY(ght_node_set_hash(*node, hash)); return GHT_OK; }
static void test_geohash_inout() { /* ght_hash_from_coordinate(const GhtCoordinate *coord, unsigned int resolution, GhtHash **rhash) */ /* ght_area_from_hash(const GhtHash *hash, GhtArea *area) */ GhtHash *hash; GhtCoordinate coord; GhtErr err; GhtArea area; coord.x = 1.0; coord.y = 1.0; err = ght_hash_from_coordinate(&coord, 20, &hash); CU_ASSERT_EQUAL(err, GHT_OK); CU_ASSERT_STRING_EQUAL(hash, "s00twy01mtw037ms06g7"); err = ght_area_from_hash(hash, &area); CU_ASSERT_EQUAL(err, GHT_OK); CU_ASSERT_DOUBLE_EQUAL(coord.x, (area.x.min + area.x.max)/2.0, 0.0000000001); CU_ASSERT_DOUBLE_EQUAL(coord.y, (area.y.min + area.y.max)/2.0, 0.0000000001); ght_hash_free(hash); coord.x = 0.0; coord.y = 0.0; err = ght_hash_from_coordinate(&coord, 20, &hash); CU_ASSERT_EQUAL(err, GHT_OK); CU_ASSERT_STRING_EQUAL(hash, "s0000000000000000000"); err = ght_area_from_hash(hash, &area); CU_ASSERT_EQUAL(err, GHT_OK); CU_ASSERT_DOUBLE_EQUAL(coord.x, (area.x.min + area.x.max)/2.0, 0.0000000001); CU_ASSERT_DOUBLE_EQUAL(coord.y, (area.y.min + area.y.max)/2.0, 0.0000000001); ght_hash_free(hash); coord.x = 90.0; coord.y = 0.0; err = ght_hash_from_coordinate(&coord, 20, &hash); CU_ASSERT_EQUAL(err, GHT_OK); CU_ASSERT_STRING_EQUAL(hash, "w0000000000000000000"); err = ght_area_from_hash(hash, &area); CU_ASSERT_EQUAL(err, GHT_OK); CU_ASSERT_DOUBLE_EQUAL(coord.x, (area.x.min + area.x.max)/2.0, 0.0000000001); CU_ASSERT_DOUBLE_EQUAL(coord.y, (area.y.min + area.y.max)/2.0, 0.0000000001); ght_hash_free(hash); coord.x = 90.0; coord.y = 45.0; err = ght_hash_from_coordinate(&coord, 20, &hash); CU_ASSERT_EQUAL(err, GHT_OK); CU_ASSERT_STRING_EQUAL(hash, "y0000000000000000000"); err = ght_area_from_hash(hash, &area); CU_ASSERT_EQUAL(err, GHT_OK); CU_ASSERT_DOUBLE_EQUAL(coord.x, (area.x.min + area.x.max)/2.0, 0.0000000001); CU_ASSERT_DOUBLE_EQUAL(coord.y, (area.y.min + area.y.max)/2.0, 0.0000000001); ght_hash_free(hash); coord.x = 180.0; coord.y = 45.0; err = ght_hash_from_coordinate(&coord, 20, &hash); CU_ASSERT_EQUAL(err, GHT_OK); CU_ASSERT_STRING_EQUAL(hash, "zbpbpbpbpbpbpbpbpbpb"); err = ght_area_from_hash(hash, &area); CU_ASSERT_EQUAL(err, GHT_OK); CU_ASSERT_DOUBLE_EQUAL(coord.x, (area.x.min + area.x.max)/2.0, 0.0000000001); CU_ASSERT_DOUBLE_EQUAL(coord.y, (area.y.min + area.y.max)/2.0, 0.0000000001); ght_hash_free(hash); coord.x = -180.0; coord.y = 45.0; err = ght_hash_from_coordinate(&coord, 20, &hash); CU_ASSERT_EQUAL(err, GHT_OK); CU_ASSERT_STRING_EQUAL(hash, "b0000000000000000000"); err = ght_area_from_hash(hash, &area); CU_ASSERT_EQUAL(err, GHT_OK); CU_ASSERT_DOUBLE_EQUAL(coord.x, (area.x.min + area.x.max)/2.0, 0.0000000001); CU_ASSERT_DOUBLE_EQUAL(coord.y, (area.y.min + area.y.max)/2.0, 0.0000000001); ght_hash_free(hash); coord.x = 179.9999; coord.y = 45.0; err = ght_hash_from_coordinate(&coord, 9, &hash); CU_ASSERT_EQUAL(err, GHT_OK); CU_ASSERT_STRING_EQUAL(hash, "zbpbpbpbj"); err = ght_area_from_hash(hash, &area); // printf("\n%s\n", hash); CU_ASSERT_EQUAL(err, GHT_OK); CU_ASSERT_DOUBLE_EQUAL(coord.x, (area.x.min + area.x.max)/2.0, 0.0001); CU_ASSERT_DOUBLE_EQUAL(coord.y, (area.y.min + area.y.max)/2.0, 0.0001); ght_hash_free(hash); }