示例#1
0
文件: unittest.c 项目: joamaki/hcpak
void test_bitfile3(void)
{
	u8 byte;
	u32 word;
	struct bitfile *bf = bitfile_open("/tmp/test", "r");
	struct bitfile *bf2 = bitfile_open("/tmp/test.out", "w");
	struct bitfile *bf3 = bitfile_open("/tmp/test.out2", "w");
	struct bitfile *bf4 = bitfile_open("/tmp/test.out3", "w");
	struct bitfile *bf5 = bitfile_open("/tmp/test.out4", "w");

	while(bitfile_get_byte(bf, &byte) == 0) {
		bitfile_put_byte(bf2, byte);
	}

	bitfile_rewind(bf);

	while(bitfile_get_bit(bf, &byte) == 0) {
		bitfile_put_bit(bf3, byte);
	}

	bitfile_rewind(bf);

	while(bitfile_get_u32(bf, &word) == 0) {
		bitfile_put_u32(bf4, word);
	}

	bitfile_rewind(bf);

	{
		u8 bytes[2];
		u8 bits[4];
		while(bitfile_get_byte(bf, &bytes[0]) == 0 &&
		      bitfile_get_byte(bf, &bytes[1]) == 0 &&
		      bitfile_get_bit(bf, &bits[0]) == 0 &&
		      bitfile_get_bit(bf, &bits[1]) == 0 &&
		      bitfile_get_bit(bf, &bits[2]) == 0 &&
		      bitfile_get_bit(bf, &bits[3]) == 0) {
			u8 out = bits[0] << 7 | bits[1] << 6 |
				bits[2] << 5 | bits[3] << 4;
			u8 foo[3];
			foo[0] = bytes[0];
			foo[1] = bytes[1];
			foo[2] = out;
 			bitfile_put_bits(bf5, (u8 *)&foo, 20);

/* 			bitfile_put_bit(bf5, bits[0]); */
/* 			bitfile_put_bit(bf5, bits[1]); */
/* 			bitfile_put_bit(bf5, bits[2]); */
/* 			bitfile_put_bit(bf5, bits[3]); */

		}
	}

	bitfile_close(bf);
	bitfile_close(bf2);
	bitfile_close(bf3);
	bitfile_close(bf4);
	bitfile_close(bf5);

}
示例#2
0
文件: unittest.c 项目: joamaki/hcpak
void test_bitfile2(void)
{
	struct bitfile *bf = bitfile_open("/dev/urandom", "r");
	int i;
	u8 res;
	int sum = 0;

	for(i=0; i<1000000; i++) {
		bitfile_get_bit(bf, &res);
		sum += res;
	}
	printf("the sum of first million bits from urandom: %d\n", sum);

	bitfile_rewind(bf);

	sum = 0;
	for(i=0; i<1000000; i++) {
		bitfile_get_bit(bf, &res);
		sum += res;
	}
	printf("(again) the sum first million bits from urandom: %d\n", sum);

	bitfile_close(bf);

}
示例#3
0
文件: unittest.c 项目: joamaki/hcpak
void test_bitfile4(void)
{
	u8 foo = 170;
/* 	u8 foo2; */
/* 	u8 foo3[2] = {85, 85}; */
	struct bitfile *bf = bitfile_open("/tmp/bf-test", "w");

	bitfile_put_bit(bf, 0);
	bitfile_put_bit(bf, 0);
	bitfile_put_bit(bf, 0);
	bitfile_put_bit(bf, 0);
	bitfile_put_bit(bf, 0);

	bitfile_put_byte(bf, foo);

	bitfile_put_bit(bf, 1);
	bitfile_put_bit(bf, 1);
	bitfile_put_bit(bf, 1);


/* 	bitfile_put_bit(bf, 0); */
/* 	bitfile_put_bit(bf, 1); */
/* 	bitfile_put_bit(bf, 0); */
/* 	bitfile_put_bit(bf, 1); */

/* 	bitfile_put_bit(bf, 0); */
/* 	bitfile_put_bit(bf, 1); */
/* 	bitfile_put_bit(bf, 0); */
/* 	bitfile_put_bit(bf, 1); */

/* 	bitfile_put_bit(bf, 0); */
/* 	bitfile_put_bit(bf, 0); */

/* 	foo2 = foo << 4; */
/* 	bitfile_put_bits(bf, &foo, 4); */
/* 	bitfile_put_bits(bf, &foo2, 4); */

/* 	bitfile_put_bits(bf, &foo3, 10); */
/* 	bitfile_put_bits(bf, &foo3, 6); */


	bitfile_close(bf);

}
示例#4
0
/** Test the bitfile_skip_* functions
 * @return 0 on success
 */
int test_skip()
{
    FILE *f;
    bitfile bfstruct, *bf;
    unsigned char *mem;
    int filesize, rval, ntruevals = sizeof(truevals)/sizeof(int);

    f = fopen("correct-data","rb");
    bf = &bfstruct;
    bitfile_open(f, bf);

    if ((rval = test_skip_1(bf, truevals)) != ntruevals) {
        fprintf(stderr, "error skipping over data from file (%4i of %4i entries)...\n",
            rval, ntruevals);
        return (-1);
    }

    bitfile_close(bf);

    fseek(f, 0, SEEK_END);
    filesize = ftell(f);
    mem = malloc(filesize);
    fseek(f, 0, SEEK_SET);
    fread(mem, 1, filesize, f);

    bitfile_map(mem, filesize, bf);

    if ((rval = test_skip_1(bf, truevals)) != ntruevals) {
        fprintf(stderr, "error skipping over data from memory (%4i of %4i entries)...\n",
            rval, ntruevals);
        return (-1);
    }
    
    bitfile_close(bf);
    fclose(f);

    return (0);
}
示例#5
0
/**
 * Load a graph file but using a set of externally provided buffers 
 * for the data.  This might be useful in the case that you want to managed
 * memory for the graph independently of this function.  
 *
 * To maintain the graph, only the bvgraph structure, and the two
 * external ararys: gmemory and offsets are required.  Consequently,
 * a bvgraph structure can always be patched together out of these
 * functions.
 *
 * One common way of using this function would be to first
 * load a graph on the disk (bvgraph_load(...,offset_step=-1))
 * and then call bvgraph_required_memory with an alternative
 * offset step.
 *
 * @param[in] g a newly created bvgraph structure
 * @param[in] filename the base filename for a set of bvgraph files, 
 * so filename.graph and filename.properties must exist.
 * @param[in] offset_step controls how many offsets are loaded, 
 * if offset_step = -1, then the graph file isn't loaded into memory
 * if offset_step = 0, then the graph file is loaded, but no offsets
 * no other values are supported at the moment.
 * @param[in] gmemory an arry of size gmemsize for the graph 
 * (if NULL, then this parameter is treated as internal memory)
 * @param[in] gmemsize the size of the gmemory block
 * @param[in] offsets an array of offsets
 * (if NULL, then this parameter is treated as internal memory)
 * @param[in] offsetssize the number of offsets
 * @return 0 if successful;
 * bvgraph_load_error_filename_too_long - indicates the filename was too long
 */
int bvgraph_load_external(bvgraph *g,
                          const char *filename, unsigned int filenamelen, int offset_step,
                          unsigned char *gmemory, size_t gmemsize,
                          unsigned long long* offsets, int offsetssize)
{
    int rval = 0;

    assert(offset_step == 0 || offset_step == -1 || offset_step == 1);

    if (filenamelen > BVGRAPH_MAX_FILENAME_SIZE-1) { 
        return bvgraph_load_error_filename_too_long;
    }
    
    memset(g,0,sizeof(bvgraph));

    strncpy(g->filename, filename, filenamelen);
    g->filename[filenamelen] = '\0';
    g->filenamelen = filenamelen;

    g->offset_step = offset_step;

    set_defaults(g);

    rval = parse_properties(g);
    // check for any errors
    if (rval != 0) { return rval; }

    // continue processing
    if (offset_step >= 0) 
    {
        if (offset_step == 0 || offset_step == 1) {    //modified 082911
            // in this case, we ust load the graph
            // file into memory

            // first get the filesize
            unsigned long long graphfilesize;
            char *gfilename = strappend(g->filename, g->filenamelen, ".graph", 6);
            rval = fsize(gfilename, &graphfilesize);
            free(gfilename);
            if (rval) { 
                return bvgraph_call_io_error;
            }

            if (gmemory != NULL) {
                // they want to use their own memory, make sure
                // they allocated enough!
                if (gmemsize < graphfilesize) {
                    return bvgraph_load_error_buffer_too_small;
                }
                g->memory = gmemory;
                g->memory_size = gmemsize;
                g->memory_external = 1;
            } else {
                // we have to allocate the memory ourselves
                g->memory_size = (size_t)graphfilesize;
                g->memory = malloc(sizeof(unsigned char)*g->memory_size);
                if (!g->memory) {
                    return bvgraph_call_out_of_memory;
                }
                g->memory_external = 0;
            }

            // now read the file
            gfilename = strappend(g->filename, g->filenamelen, ".graph", 6);
            {
                size_t bytesread = 0;
                FILE *gfile = fopen(gfilename, "rb");
                free(gfilename);
                if (!gfile) {
                    return bvgraph_call_io_error;
                }
                bytesread = fread(g->memory, 1, g->memory_size, gfile);
                if (bytesread != graphfilesize) {
                    return bvgraph_call_io_error;
                }
                fclose(gfile);
            }
            // we now have the graph in memory!

            if (offset_step == 1) {        //modified 082911
                // now read the file
                char *ofilename = strappend(g->filename, g->filenamelen, ".offsets", 8);
                bitfile bf;
                long long off = 0;
                int64_t i;
                FILE *ofile = fopen(ofilename, "rb");

                if (offsets != NULL) {
                    g->offsets = offsets;
                    g->offsets_external = 1;
                } else {
                    // we have to allocate the memory ourselves
                    g->offsets = (unsigned long long*) malloc(sizeof(unsigned long long)*g->n);
                    g->offsets_external = 0;
                }

                if (ofile) {
                    rval = bitfile_open(ofile, &bf);
                    if (rval) {
                        return bvgraph_call_io_error;
                    }
                    for (i = 0; i < g->n; i++){
                        off = read_offset(g, &bf) + off;
                        g->offsets[i] = off;
                    }
                } else {
                    // need to build the offsets
                    bvgraph_iterator git;
                    int rval = bvgraph_nonzero_iterator(g, &git);
                    if (rval) { return rval; }
                    g->offsets[0] = 0;
                    for (; bvgraph_iterator_valid(&git); bvgraph_iterator_next(&git)) {
                        if (git.curr+1 < g->n) {
                            g->offsets[git.curr+1] = bitfile_tell(&git.bf);
                        }
                    }
                    bvgraph_iterator_free(&git);
                }
            }
        }
    }
    else
    {
        g->memory_size = 0;
    }
     
    // presently the othercases are not supported, so we don't have to
    // worry about them!

    return (0);
}
示例#6
0
文件: unittest.c 项目: joamaki/hcpak
void test_bitfile(void)
{
	struct bitfile *bf;
	char buf[20] = {0,};
	u8 res;
	u32 res32;
	int ret = 0;

	bf = bitfile_open("bitfile.test", "w");
	assert (bf != NULL);

	bitfile_put_bytes(bf, (u8*)"HCPAK", 5);

	bitfile_put_byte(bf, 'a');
	bitfile_put_byte(bf, 'b');
	bitfile_put_byte(bf, 'c');
	bitfile_put_byte(bf, 'd');

	bitfile_put_bit(bf, 0);
	bitfile_put_bit(bf, 0);
	bitfile_put_bit(bf, 1);
	bitfile_put_bit(bf, 1);

	bitfile_put_byte(bf, 'e');
	bitfile_put_byte(bf, 'f');

	bitfile_put_u32(bf, (u32)123456);

	res = 0xff;
	bitfile_put_bits(bf, &res, 5);

	bitfile_put_bit(bf, 1);
	bitfile_put_bit(bf, 0);
	bitfile_put_bit(bf, 1);
	bitfile_put_bit(bf, 0);

	/* bitfile_put_byte(bf, 'f'); */

	bitfile_close(bf);

	bf = bitfile_open("bitfile.test", "r");
	assert (bf != NULL);

	bitfile_get_bytes(bf, (u8*)buf, 5);
	buf[5] = '\0';
	assert(!memcmp(buf, "HCPAK", 5));
	/* printf("HCPAK: '%s'\n", buf); */

	ret += bitfile_get_byte(bf, &res); /* printf("a: %c\n", res); */ assert(res == 'a');
	ret += bitfile_get_byte(bf, &res); /* printf("b: %c\n", res); */ assert(res == 'b');
	ret += bitfile_get_byte(bf, &res); /* printf("c: %c\n", res); */ assert(res == 'c');
	ret += bitfile_get_byte(bf, &res); /* printf("d: %c\n", res); */ assert(res == 'd');

	ret += bitfile_get_bit(bf, &res); /* printf("0: %d\n", res); */ assert(res == 0);
	ret += bitfile_get_bit(bf, &res); /* printf("0: %d\n", res); */ assert(res == 0);
	ret += bitfile_get_bit(bf, &res); /* printf("1: %d\n", res); */ assert(res == 1);
	ret += bitfile_get_bit(bf, &res); /* printf("1: %d\n", res); */ assert(res == 1);

	ret += bitfile_get_byte(bf, &res); /* printf("e: %c\n", res); */ assert(res == 'e');
	ret += bitfile_get_byte(bf, &res); /* printf("f: %c\n", res); */ assert(res == 'f');

	ret += bitfile_get_u32(bf, &res32);
	assert (res32 == 123456);

	ret += bitfile_get_bit(bf, &res); /* printf("1: %d\n", res); */ assert(res == 1);
	ret += bitfile_get_bit(bf, &res); /* printf("1: %d\n", res); */ assert(res == 1);
	ret += bitfile_get_bit(bf, &res); /* printf("1: %d\n", res); */ assert(res == 1);
	ret += bitfile_get_bit(bf, &res); /* printf("1: %d\n", res); */ assert(res == 1);
	ret += bitfile_get_bit(bf, &res); /* printf("1: %d\n", res); */ assert(res == 1);
	ret += bitfile_get_bit(bf, &res); /* printf("1: %d\n", res); */ assert(res == 1);
	ret += bitfile_get_bit(bf, &res); /* printf("0: %d\n", res); */ assert(res == 0);
	ret += bitfile_get_bit(bf, &res); /* printf("1: %d\n", res); */ assert(res == 1);
	ret += bitfile_get_bit(bf, &res); /* printf("0: %d\n", res); */ assert(res == 0);

	assert(ret == 0);

	/* try reading past the EOF */
	assert(bitfile_get_byte(bf, &res) != 0);

	/* try rewinding the file */
	bitfile_rewind(bf);

	bitfile_get_bytes(bf, (u8*)buf, 5);
	buf[5] = '\0';
	assert(!memcmp(buf, "HCPAK", 5));

/* 	printf("HCPAK: '%s'\n", buf); */

	bitfile_close(bf);


}