<commit_before>#include "brt.h"
#include "memory.h"
#include "brt-internal.h"

#include <fcntl.h>
#include <assert.h>
#include <string.h>

void test_serialize(void) {
    //    struct brt source_brt;
    int nodesize = 1024;
    struct brtnode sn, *dn;
    int fd = open("brt-serialize-test.brt", O_RDWR|O_CREAT, 0777);
    int r;
    assert(fd>=0);

    //    source_brt.fd=fd;
    char *hello_string;
    sn.nodesize = nodesize;
    sn.thisnodename = sn.nodesize*20;
    sn.height = 1;
    sn.u.n.n_children = 2;
    sn.u.n.childkeys[0]    = hello_string = toku_strdup("hello");
    sn.u.n.childkeylens[0] = 6;
    sn.u.n.totalchildkeylens = 6;
    sn.u.n.children[0] = sn.nodesize*30;
    sn.u.n.children[1] = sn.nodesize*35;
    r = toku_hashtable_create(&sn.u.n.htables[0]); assert(r==0);
    r = toku_hashtable_create(&sn.u.n.htables[1]); assert(r==0);
    r = toku_hash_insert(sn.u.n.htables[0], "a", 2, "aval", 5, BRT_NONE); assert(r==0);
    r = toku_hash_insert(sn.u.n.htables[0], "b", 2, "bval", 5, BRT_NONE); assert(r==0);
    r = toku_hash_insert(sn.u.n.htables[1], "x", 2, "xval", 5, BRT_NONE); assert(r==0);
    sn.u.n.n_bytes_in_hashtables = 3*(BRT_CMD_OVERHEAD+KEY_VALUE_OVERHEAD+2+5);

    deserialize_brtnode_from(fd, nodesize*20, &dn, nodesize);

    r = serialize_brtnode_to(fd, sn.nodesize*20, sn.nodesize, &sn);  assert(r==0);


    assert(dn->thisnodename==nodesize*20);
    assert(dn->height == 1);
    assert(dn->u.n.n_children==2);
    assert(strcmp(dn->u.n.childkeys[0], "hello")==0);
    assert(dn->u.n.childkeylens[0]==6);
    assert(dn->u.n.totalchildkeylens==6);
    assert(dn->u.n.children[0]==nodesize*30);
    assert(dn->u.n.children[1]==nodesize*35);
    {
	bytevec data; ITEMLEN datalen; int type;
	int r = toku_hash_find(dn->u.n.htables[0], "a", 2, &data, &datalen, &type);
	assert(r==0);
	assert(strcmp(data,"aval")==0);<commit_msg>Fix the serialize-test:  It was doing read then write, instead of write then read<commit_after>#include "brt.h"
#include "memory.h"
#include "brt-internal.h"

#include <fcntl.h>
#include <assert.h>
#include <string.h>

void test_serialize(void) {
    //    struct brt source_brt;
    int nodesize = 1024;
    struct brtnode sn, *dn;
    int fd = open("brt-serialize-test.brt", O_RDWR|O_CREAT, 0777);
    int r;
    assert(fd>=0);

    //    source_brt.fd=fd;
    char *hello_string;
    sn.nodesize = nodesize;
    sn.thisnodename = sn.nodesize*20;
    sn.height = 1;
    sn.u.n.n_children = 2;
    sn.u.n.childkeys[0]    = hello_string = toku_strdup("hello");
    sn.u.n.childkeylens[0] = 6;
    sn.u.n.totalchildkeylens = 6;
    sn.u.n.children[0] = sn.nodesize*30;
    sn.u.n.children[1] = sn.nodesize*35;
    r = toku_hashtable_create(&sn.u.n.htables[0]); assert(r==0);
    r = toku_hashtable_create(&sn.u.n.htables[1]); assert(r==0);
    r = toku_hash_insert(sn.u.n.htables[0], "a", 2, "aval", 5, BRT_NONE); assert(r==0);
    r = toku_hash_insert(sn.u.n.htables[0], "b", 2, "bval", 5, BRT_NONE); assert(r==0);
    r = toku_hash_insert(sn.u.n.htables[1], "x", 2, "xval", 5, BRT_NONE); assert(r==0);
    sn.u.n.n_bytes_in_hashtables = 3*(BRT_CMD_OVERHEAD+KEY_VALUE_OVERHEAD+2+5);

    r = serialize_brtnode_to(fd, sn.nodesize*20, sn.nodesize, &sn);  assert(r==0);

    r = deserialize_brtnode_from(fd, nodesize*20, &dn, nodesize);

    assert(dn->thisnodename==nodesize*20);
    assert(dn->height == 1);
    assert(dn->u.n.n_children==2);
    assert(strcmp(dn->u.n.childkeys[0], "hello")==0);
    assert(dn->u.n.childkeylens[0]==6);
    assert(dn->u.n.totalchildkeylens==6);
    assert(dn->u.n.children[0]==nodesize*30);
    assert(dn->u.n.children[1]==nodesize*35);
    {
	bytevec data; ITEMLEN datalen; int type;
	int r = toku_hash_find(dn->u.n.htables[0], "a", 2, &data, &datalen, &type);
	assert(r==0);
	assert(strcmp(data,"aval")==0);
{'fraction_non_alphanumeric': 0.1998426435877262, 'fraction_numerical': 0.03461841070023604, 'mean_word_length': 3.846251588310038, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 29, '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': '6059311', 'n_tokens_mistral': 1747, 'n_tokens_neox': 1677, 'n_words': 257}