Пример #1
0
static void
test_ght_build_node_with_attributes(void)
{
    GhtAttribute *a;
    GhtCoordinate coord;
    GhtNode *node;
    stringbuffer_t *sb = stringbuffer_create();
    
    /* X, Y */
    coord.x = -127;
    coord.y = 45;
    ght_node_new_from_coordinate(&coord, 16, &node);
    /* Z */
    ght_attribute_new_from_double(simpleschema->dims[2], 1231.2, &a);
    ght_node_add_attribute(node, a);
    /* Intensity */
    ght_attribute_new_from_double(simpleschema->dims[3], 3, &a);
    ght_node_add_attribute(node, a);
    
    ght_node_to_string(node, sb, 0);
    CU_ASSERT_STRING_EQUAL("c0j8n012j80252h0  Z=1231.2:Intensity=3\n", stringbuffer_getstring(sb));
    // printf("%s\n", stringbuffer_getstring(sb));
    stringbuffer_destroy(sb);
    ght_node_free(node);
}
Пример #2
0
GhtErr
ght_node_to_string(GhtNode *node, stringbuffer_t *sb, int level)
{
	int i = 0;

	/* Print hash */
	if ( node->hash )
		ght_stringbuffer_aprintf(sb, "%*s%s", 2*level, "", node->hash);
	else
		ght_stringbuffer_aprintf(sb, "%*s%s", 2*level, "", "[hash-is-null]");

	/* Print attributes */
	if ( node->attributes )
	{
		GhtAttribute *attr = node->attributes;
		ght_stringbuffer_append(sb, "  ");
		while ( attr )
		{
			ght_attribute_to_string(attr, sb);
			if ( attr->next ) ght_stringbuffer_append(sb, ":");
			attr = attr->next;
		}
	}
	ght_stringbuffer_append(sb, "\n");

	/* Recurse into children */
	for ( i = 0; i < ght_node_num_children(node); i++ )
	{
		GHT_TRY(ght_node_to_string(node->children->nodes[i], sb, level + 1));
	}
	return GHT_OK;
}
Пример #3
0
static void
test_ght_node_serialization(void)
{
    GhtCoordinate coord;
    int x, y;
    GhtNode *node1, *node2, *node3;
    GhtErr err;
    GhtWriter *writer;
    GhtReader *reader;
    const uint8_t *bytes;
    size_t bytes_size;
    stringbuffer_t *sb1, *sb2;
    GhtAttribute *attr;
    char *hex;

    /* ght_node_new_from_coordinate(const GhtCoordinate *coord, unsigned int resolution, GhtNode **node); */
    coord.x = -127.4123;
    coord.y = 49.23141;
    err = ght_node_new_from_coordinate(&coord, GHT_MAX_HASH_LENGTH, &node1);
    CU_ASSERT_STRING_EQUAL(node1->hash, "c0v2hdm1wpzpy4vtv4");
    CU_ASSERT_EQUAL(err, GHT_OK);

    err = ght_writer_new_mem(&writer);
    err = ght_node_write(node1, writer);
    bytes = bytebuffer_getbytes(writer->bytebuffer);
    bytes_size = bytebuffer_getsize(writer->bytebuffer);
    err = ght_reader_new_mem(bytes, bytes_size, schema, &reader);
    err = ght_node_read(reader, &node2);
    
    CU_ASSERT_STRING_EQUAL(node1->hash, node2->hash);
    ght_node_free(node2);

    /* add a child */
    coord.x = -127.4125;
    coord.y = 49.23144;
    err = ght_node_new_from_coordinate(&coord, GHT_MAX_HASH_LENGTH, &node3);
    err = ght_attribute_new_from_double(schema->dims[3], 88.88, &attr);
    err = ght_node_add_attribute(node3, attr);
    err = ght_node_insert_node(node1, node3, GHT_DUPES_YES);
    CU_ASSERT_EQUAL(err, GHT_OK);

    /* add another (dupe) child */
    err = ght_node_new_from_coordinate(&coord, GHT_MAX_HASH_LENGTH, &node3);
    err = ght_node_insert_node(node1, node3, GHT_DUPES_YES);

    /* add another (dupe) child with an attribute */
    err = ght_node_new_from_coordinate(&coord, GHT_MAX_HASH_LENGTH, &node3);
    err = ght_attribute_new_from_double(schema->dims[2], 99.99, &attr);
    err = ght_node_add_attribute(node3, attr);
    err = ght_node_insert_node(node1, node3, GHT_DUPES_YES);
    
    sb1 = stringbuffer_create();
    err = ght_node_to_string(node1, sb1, 0);
    // printf("ORIGINAL\n%s\n", stringbuffer_getstring(sb1));

    err = ght_writer_new_mem(&writer);
    err = ght_node_write(node1, writer);
    bytes = bytebuffer_getbytes(writer->bytebuffer);
    bytes_size = bytebuffer_getsize(writer->bytebuffer);

    err = hexbytes_from_bytes(bytes, bytes_size, &hex);
    CU_ASSERT_STRING_EQUAL("086330763268646D3100020A77707A7079347674763400000A6374643463637839796201035800020000000001020F27000000", hex);
    // printf("\n\n%s\n", hex);
    
    err = ght_reader_new_mem(bytes, bytes_size, schema, &reader);
    err = ght_node_read(reader, &node2);
    
    sb2 = stringbuffer_create();
    err = ght_node_to_string(node2, sb2, 0);
    // printf("COPY\n%s\n", stringbuffer_getstring(sb2));
    
    CU_ASSERT_STRING_EQUAL(stringbuffer_getstring(sb1), stringbuffer_getstring(sb2));
    stringbuffer_destroy(sb2);
    stringbuffer_destroy(sb1);
    ght_node_free(node1);
    ght_node_free(node2);
    ght_writer_free(writer);
    ght_reader_free(reader);
}
Пример #4
0
static void
test_ght_build_tree_with_attributes(void)
{
    int i;
    static const char *simpledata = "test/data/simple-data.tsv";   
    GhtNodeList *nodelist;
    GhtNode *root, *node;
    GhtErr err;
    GhtAttribute attr;
    stringbuffer_t *sb;
    double d;
    
    /* Read a nodelist from a TSV file */
    nodelist = tsv_file_to_node_list(simpledata, simpleschema);
    CU_ASSERT_EQUAL(nodelist->num_nodes, 8);
    
    /* Build node list into a tree */
    root = nodelist->nodes[0];
    for ( i = 1; i < nodelist->num_nodes; i++ )
    {
        err = ght_node_insert_node(root, nodelist->nodes[i], GHT_DUPES_YES);
    }

    /* Write the tree to string:
        c0n0e
          q
            m
              m7
                dvy8yz9  Z=123.4:Intensity=5
                ky667sj  Z=123.4:Intensity=5
              qw00rg068  Z=123.4:Intensity=5
            hekkhnhj3b  Z=123.4:Intensity=5
            6myj870p99  Z=123.3:Intensity=5
            46jybv17y1  Z=123.4:Intensity=5
          r
            980jtyf1dh  Z=123.4:Intensity=5
            2khvpfu13f  Z=123.4:Intensity=5
    */
    sb = stringbuffer_create();
    ght_node_to_string(root, sb, 0);
    // printf("\n%s\n", stringbuffer_getstring(sb));
    stringbuffer_destroy(sb);

    /* Compact the tree on both attributes:
        c0n0e  Intensity=5
          q
            m  Z=123.4
              m7
                dvy8yz9
                ky667sj
              qw00rg068
            hekkhnhj3b  Z=123.4
            6myj870p99  Z=123.3
            46jybv17y1  Z=123.4
          r  Z=123.4
            980jtyf1dh
            2khvpfu13f  
    */
    sb = stringbuffer_create();
    ght_node_compact_attribute(root, simpleschema->dims[2], &attr);
    ght_node_compact_attribute(root, simpleschema->dims[3], &attr);
    ght_node_to_string(root, sb, 0);
    // printf("\n%s\n", stringbuffer_getstring(sb));
    stringbuffer_destroy(sb);
    
    /* Check that Intensity=5 has migrated all the way to the top of the tree */
    CU_ASSERT_STRING_EQUAL(root->attributes->dim->name, "Intensity");
    ght_attribute_get_value(root->attributes, &d);
    CU_ASSERT_DOUBLE_EQUAL(d, 5, 0.00000001);
    
    ght_node_free(root);
    ght_nodelist_free_shallow(nodelist);
}