<commit_before>/*
 * File:   wallwritetest.c
 * Author: pete
 *
 * Created on Dec 2, 2013, 10:54:37 AM
 */

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "crecon.h"
#include "crecon_impl.h"

/*
 * Simple C Test Suite
 */

typedef struct {
    char *str_a;
    char *str_b;
    char **str_array;
} comp_object;


#define BUFFER_STRING_LENGTH 30

recon_status test1_write_mobj_field(recon_wall_object object) {
    recon_status status = RECON_OK;
    int no_array_elements = 3, i = 0;
    char buffer_string[BUFFER_STRING_LENGTH];
    char field_name[BUFFER_STRING_LENGTH];
    char *tofree;
    comp_object myobj;
    recon_wall_object_mobj msgobj;
    msgpack_packer *packer;
    msgpack_sbuffer *buffer;

    // Assemble the user object
    myobj.str_a = (char*) malloc(strlen("string_a") + 1);
    myobj.str_a = "string_a";
    myobj.str_b = (char*) malloc(strlen("string_b") + 1);
    myobj.str_b = "string_b";
    myobj.str_array = (char**) malloc(no_array_elements * sizeof (char*));
    for (i = 0; i < no_array_elements; i++) {
        sprintf(buffer_string, "string_array_elem%i", i);
        myobj.str_array[i] = (char*) malloc(strlen(buffer_string) + 1);
        memcpy(myobj.str_array[i], buffer_string, strlen(buffer_string) + 1);
    }
    // Prepare recon_wall_object_mobj object to help construction
    status = recon_wall_object_new_mobj(&msgobj);
    if (status != RECON_OK) {
        return status;
    }
    status = recon_wall_object_get_mobj_packer(msgobj, &packer);
    if (status != RECON_OK) {
        return status;
    }
    status = recon_wall_object_get_mobj_buffer(msgobj, &buffer);
    if (status != RECON_OK) {
        return status;
    }
    // Manually construct msgpack version of object
    msgpack_pack_map(packer, 3); //myobj : {str_a, str_b, str_array}
    msgpack_pack_raw(packer, strlen("str_a"));
    msgpack_pack_raw_body(packer, "str_a", strlen("str_a"));
    msgpack_pack_raw(packer, strlen(myobj.str_a));
    msgpack_pack_raw_body(packer, myobj.str_a, strlen(myobj.str_a));
    msgpack_pack_raw(packer, strlen("str_b"));
    msgpack_pack_raw_body(packer, "str_b", strlen("str_b"));
    msgpack_pack_raw(packer, strlen(myobj.str_b));
    msgpack_pack_raw_body(packer, myobj.str_b, strlen(myobj.str_b));
    msgpack_pack_raw(packer, strlen("str_array"));
    msgpack_pack_raw_body(packer, "str_array", strlen("str_array"));
    msgpack_pack_array(packer, no_array_elements);
    for (i = 0; i < no_array_elements; i++) {
        msgpack_pack_raw(packer, strlen(myobj.str_array[i]));
        msgpack_pack_raw_body(packer, myobj.str_array[i], strlen(myobj.str_array[i]));
    }
    // Finished manual construction
    status = recon_wall_object_finish_mobj(msgobj);
    if (status != RECON_OK) {
        return status;
    }
    // Check it looks right
    printf("Object to be written to wall:\n\n");
    status = recon_wall_object_print_mobj(msgobj);
    if (status != RECON_OK) {
        return status;
    }
    // Serialize it to wall file
    status = recon_wall_start_field_entry(object);
    if (status != RECON_OK) {
        return status;
    }
    status = recon_wall_object_add_field_object(object, "myobj", msgobj);
    if (status != RECON_OK) {
        return status;
    }
    status = recon_wall_end_field_entry(object);
    /*free(myobj.str_a);
    free(myobj.str_b);
    for (i = 0; i < no_array_elements; i++) {
        free(myobj.str_array[i]);
    }
    free(myobj.str_array);*/
    return status;
}

recon_status test1_read_myobj(recon_wall_object object) {
    int fieldindex = -1, noelements = 0, i = 0, j = 0, len_array = 0;
    comp_object myobj;
    recon_booleantype isstring;
    recon_wall_object_mobj msgobj;
    recon_wall_object_field field;
    recon_wall_object_field_element elem;
    char *fieldname;
    char **fieldarray;
    recon_status status = RECON_OK;
    status = recon_wall_object_find_field(object, "myobj", &fieldindex);
    if (status != RECON_OK) {
        return status;
    }
    status = recon_wall_object_get_field(object, fieldindex, &field);
    if (status != RECON_OK) {
        return status;
    }
    status = recon_wall_object_get_field_ischar(field, &isstring);
    if (status != RECON_OK) {
        return status;
    }
    status = recon_wall_object_get_field_value(field, (void**) &msgobj);
    if (status != RECON_OK) {
        return status;
    }
    printf("Object fetched from wall (msgpack form):\n\n");
    status = recon_wall_object_print_mobj(msgobj);
    printf("After field elements have been tokenized\n\n");
    if (!isstring) {
        status = recon_wall_object_parse_field_value_elements(field);
        if (status != RECON_OK) {
            return status;
        }
        for (i = 0; i < noelements; i++) {
            status = status = recon_wall_object_get_field_value_element(field, i, &elem);
            if (status != RECON_OK) {
                return status;
            }
            status =  recon_wall_object_extract_field_value_element(elem, &fieldname, &fieldarray, &len_array);
            if (status != RECON_OK) {
                return status;
            }
            printf("name: %s\n", fieldname);
            printf("value: %s", fieldarray[0]);
            for (j = 1; j < len_array; j++) {
                printf(", %s", fieldarray[j]);
            }
            printf("\n");
        }
    }
    return status;
}

void test1() {
    int i, j;
    char* signalname, *aliasname, *transform, *signalnamegot, *aliasnamegot;
    char* fieldname;
    recon_status status;
    recon_wall wall;
    recon_wall wall2;<commit_msg>Tidying<commit_after>/*
 * File:   wallwritetest.c
 * Author: pete
 *
 * Created on Dec 2, 2013, 10:54:37 AM
 */

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "crecon.h"
#include "crecon_impl.h"

/*
 * Simple C Test Suite
 */

typedef struct {
    char *str_a;
    char *str_b;
    char **str_array;
} comp_object;


#define BUFFER_STRING_LENGTH 30

recon_status test1_write_mobj_field(recon_wall_object object) {
    recon_status status = RECON_OK;
    int no_array_elements = 3, i = 0;
    char buffer_string[BUFFER_STRING_LENGTH];
    comp_object myobj;
    recon_wall_object_mobj msgobj;
    msgpack_packer *packer;
    msgpack_sbuffer *buffer;

    // Assemble the user object
    myobj.str_a = (char*) malloc(strlen("string_a") + 1);
    strcpy(myobj.str_a, "string_a");
    myobj.str_b = (char*) malloc(strlen("string_b") + 1);
    strcpy(myobj.str_b, "string_b");
    myobj.str_array = (char**) malloc(no_array_elements * sizeof (char*));
    for (i = 0; i < no_array_elements; i++) {
        sprintf(buffer_string, "string_array_elem%i", i);
        myobj.str_array[i] = (char*) malloc(strlen(buffer_string) + 1);
        strcpy(myobj.str_array[i], buffer_string);
    }
    // Prepare recon_wall_object_mobj object to help construction
    status = recon_wall_object_new_mobj(&msgobj);
    if (status != RECON_OK) {
        return status;
    }
    status = recon_wall_object_get_mobj_packer(msgobj, &packer);
    if (status != RECON_OK) {
        return status;
    }
    status = recon_wall_object_get_mobj_buffer(msgobj, &buffer);
    if (status != RECON_OK) {
        return status;
    }
    // Manually construct msgpack version of object
    msgpack_pack_map(packer, 3); //myobj : {str_a, str_b, str_array}
    msgpack_pack_raw(packer, strlen("str_a"));
    msgpack_pack_raw_body(packer, "str_a", strlen("str_a"));
    msgpack_pack_raw(packer, strlen(myobj.str_a));
    msgpack_pack_raw_body(packer, myobj.str_a, strlen(myobj.str_a));
    msgpack_pack_raw(packer, strlen("str_b"));
    msgpack_pack_raw_body(packer, "str_b", strlen("str_b"));
    msgpack_pack_raw(packer, strlen(myobj.str_b));
    msgpack_pack_raw_body(packer, myobj.str_b, strlen(myobj.str_b));
    msgpack_pack_raw(packer, strlen("str_array"));
    msgpack_pack_raw_body(packer, "str_array", strlen("str_array"));
    msgpack_pack_array(packer, no_array_elements);
    for (i = 0; i < no_array_elements; i++) {
        msgpack_pack_raw(packer, strlen(myobj.str_array[i]));
        msgpack_pack_raw_body(packer, myobj.str_array[i], strlen(myobj.str_array[i]));
    }
    // Finished manual construction
    status = recon_wall_object_finish_mobj(msgobj);
    if (status != RECON_OK) {
        return status;
    }
    // Check it looks right
    printf("Object to be written to wall:\n\n");
    status = recon_wall_object_print_mobj(msgobj);
    if (status != RECON_OK) {
        return status;
    }
    // Serialize it to wall file
    status = recon_wall_start_field_entry(object);
    if (status != RECON_OK) {
        return status;
    }
    status = recon_wall_object_add_field_object(object, "myobj", msgobj);
    if (status != RECON_OK) {
        return status;
    }
    status = recon_wall_end_field_entry(object);
    free(myobj.str_a);
    free(myobj.str_b);
    for (i = 0; i < no_array_elements; i++) {
        free(myobj.str_array[i]);
    }
    free(myobj.str_array);
    return status;
}

recon_status test1_read_myobj(recon_wall_object object) {
    int fieldindex = -1, noelements = 0, i = 0, j = 0, len_array = 0;
    comp_object myobj;
    recon_booleantype isstring;
    recon_wall_object_mobj msgobj;
    recon_wall_object_field field;
    recon_wall_object_field_element elem;
    char *fieldname;
    char **fieldarray;
    recon_status status = RECON_OK;
    status = recon_wall_object_find_field(object, "myobj", &fieldindex);
    if (status != RECON_OK) {
        return status;
    }
    status = recon_wall_object_get_field(object, fieldindex, &field);
    if (status != RECON_OK) {
        return status;
    }
    status = recon_wall_object_get_field_ischar(field, &isstring);
    if (status != RECON_OK) {
        return status;
    }
    status = recon_wall_object_get_field_value(field, (void**) &msgobj);
    if (status != RECON_OK) {
        return status;
    }
    printf("Object fetched from wall (msgpack form):\n\n");
    status = recon_wall_object_print_mobj(msgobj);
    printf("After field elements have been tokenized\n\n");
    if (!isstring) {
        status = recon_wall_object_parse_field_value_elements(field);
        if (status != RECON_OK) {
            return status;
        }
        for (i = 0; i < noelements; i++) {
            status = status = recon_wall_object_get_field_value_element(field, i, &elem);
            if (status != RECON_OK) {
                return status;
            }
            status = recon_wall_object_extract_field_value_element(elem, &fieldname, &fieldarray, &len_array);
            if (status != RECON_OK) {
                return status;
            }
            printf("name: %s\n", fieldname);
            printf("value: %s", fieldarray[0]);
            for (j = 1; j < len_array; j++) {
                printf(", %s", fieldarray[j]);
            }
            printf("\n");
        }
    }
    return status;
}

void test1() {
    int i, j;
    char* signalname, *aliasname, *transform, *signalnamegot, *aliasnamegot;
    char* fieldname;
    recon_status status;
    recon_wall wall;
    recon_wall wall2;
{'fraction_non_alphanumeric': 0.12170074768038916, 'fraction_numerical': 0.006215656247184938, 'mean_word_length': 3.104251386321627, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4304694', 'n_tokens_mistral': 3949, 'n_tokens_neox': 3899, 'n_words': 781}