Search
lxdream.org :: lxdream/test/testdata.c :: diff
lxdream 0.9.1
released Jun 29
Download Now
filename test/testdata.c
changeset 185:6755a04c447f
next190:f7653df5e832
author nkeynes
date Tue Aug 01 21:56:48 2006 +0000 (17 years ago)
permissions -rw-r--r--
last change Remove no-longer-needed logging
file annotate diff log raw
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/test/testdata.c Tue Aug 01 21:56:48 2006 +0000
1.3 @@ -0,0 +1,189 @@
1.4 +/**
1.5 + * $Id: testdata.c,v 1.1 2006-07-11 01:35:23 nkeynes Exp $
1.6 + *
1.7 + * Test data loader.
1.8 + *
1.9 + * Copyright (c) 2006 Nathan Keynes.
1.10 + *
1.11 + * This program is free software; you can redistribute it and/or modify
1.12 + * it under the terms of the GNU General Public License as published by
1.13 + * the Free Software Foundation; either version 2 of the License, or
1.14 + * (at your option) any later version.
1.15 + *
1.16 + * This program is distributed in the hope that it will be useful,
1.17 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.18 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.19 + * GNU General Public License for more details.
1.20 + */
1.21 +
1.22 +#include <stdlib.h>
1.23 +#include <string.h>
1.24 +#include <ctype.h>
1.25 +#include <math.h>
1.26 +#include "testdata.h"
1.27 +
1.28 +#define DEFAULT_SIZE 1024
1.29 +
1.30 +/* get the next 32-byte aligned address that is no less than x */
1.31 +#define ALIGN_32(x) ((char *)((((unsigned int)(x))+0x1F)&0xFFFFFFE0))
1.32 +
1.33 +void free_test_dataset( test_data_t tests )
1.34 +{
1.35 + test_data_t next;
1.36 +
1.37 + do {
1.38 + next = tests->next;
1.39 + free(tests);
1.40 + tests = next;
1.41 + } while( next != NULL );
1.42 +}
1.43 +
1.44 +test_data_block_t get_test_data( test_data_t data, char *name )
1.45 +{
1.46 + int i;
1.47 + for( i=0; i<MAX_DATA_BLOCKS; i++ ) {
1.48 + if( data->item[i].name != NULL &&
1.49 + strcmp(name, data->item[i].name) == 0 ) {
1.50 + return &data->item[i];
1.51 + }
1.52 + }
1.53 + return NULL;
1.54 +}
1.55 +
1.56 +void dump_test_dataset( FILE *f, test_data_t dataset )
1.57 +{
1.58 + test_data_t test = dataset;
1.59 + int i;
1.60 + while( test != NULL ) {
1.61 + fprintf( f, "Test: %s\n", test->test_name );
1.62 + for( i=0; i<MAX_DATA_BLOCKS; i++ ) {
1.63 + if( test->item[i].name != NULL ) {
1.64 + fprintf( f, "Block: %s, %d bytes\n", test->item[i].name, test->item[i].length );
1.65 + fwrite_dump( f, test->item[i].data, test->item[i].length );
1.66 + }
1.67 + }
1.68 + test = test->next;
1.69 + }
1.70 +}
1.71 +
1.72 +int test_block_compare( test_data_block_t block, char *result, int result_length )
1.73 +{
1.74 + if( block->length != result_length )
1.75 + return -1;
1.76 + return memcmp( block->data, result, block->length );
1.77 +}
1.78 +
1.79 +
1.80 +/**
1.81 + * Load a batch of test data from the given IO stream.
1.82 + */
1.83 +test_data_t load_test_dataset( FILE *f )
1.84 +{
1.85 + test_data_t head = NULL;
1.86 + test_data_t current = NULL;
1.87 + test_data_t last = NULL;
1.88 + int current_size = 0;
1.89 + int current_block = -1;
1.90 + char *current_end = NULL;
1.91 + char *dataptr = NULL;
1.92 +
1.93 + char buf[512];
1.94 + char *line;
1.95 + while( fgets(buf, sizeof(buf), f ) != NULL ) {
1.96 + line = buf;
1.97 + while( isspace(*line) ) /* Trim leading whitespace */
1.98 + line++;
1.99 + if( line[0] == '[' ) { /* New test */
1.100 + char *test_name = line+1;
1.101 + char *end = strchr(test_name, ']');
1.102 + if( end != NULL )
1.103 + *end = '\0';
1.104 + current_size = DEFAULT_SIZE;
1.105 + test_data_t test = calloc(current_size, 1);
1.106 +
1.107 + dataptr = (char *)(test+1);
1.108 + test->next = NULL;
1.109 + if( head == NULL )
1.110 + head = test;
1.111 + if( current != NULL )
1.112 + current->next = test;
1.113 + last = current;
1.114 + current = test;
1.115 + current_end = ((char *)test) + current_size;
1.116 + current_block = -1;
1.117 + strcpy( dataptr, test_name );
1.118 + test->test_name = dataptr;
1.119 + dataptr = ALIGN_32(dataptr + strlen(test_name)+1);
1.120 + } else if( *line == '#' ) { /* Comment */
1.121 + } else {
1.122 + char *equals = strrchr( line, '=' );
1.123 + if( equals != NULL ) {
1.124 + char *block_name = line;
1.125 + int len;
1.126 + *equals-- = '\0';
1.127 + while( isspace(*equals) )
1.128 + *equals-- = '\0';
1.129 + len = strlen(line)+1;
1.130 + if( dataptr + len > current_end ) {
1.131 + current_end += current_size;
1.132 + current_size *= 2;
1.133 + current = realloc(current, current_size );
1.134 + if( last != NULL )
1.135 + last->next = current;
1.136 + }
1.137 + current_block++;
1.138 + strcpy( dataptr, block_name );
1.139 + current->item[current_block].name = dataptr;
1.140 + dataptr = ALIGN_32(dataptr+len);
1.141 + current->item[current_block].data = dataptr;
1.142 + current->item[current_block].length = 0;
1.143 + } else {
1.144 + /* Data */
1.145 + if( current == NULL || current_block == -1 )
1.146 + continue;
1.147 + char *p = strtok(line, "\t\r\n ");
1.148 + while( p != NULL ) {
1.149 + if( dataptr + 8 > current_end ) {
1.150 + current_end += current_size;
1.151 + current_size *= 2;
1.152 + current = realloc(current, current_size );
1.153 + if( last != NULL )
1.154 + last->next = current;
1.155 + }
1.156 + int len = strlen(p);
1.157 + int datalen = 0;
1.158 + char *dot = strchr(p, '.');
1.159 + if( dot != NULL ) { /* FP */
1.160 + if( p[len-1] == 'L' ) { /* Ending in L */
1.161 + p[len-1] = '\0';
1.162 + double d = strtod(p, NULL);
1.163 + *((double *)dataptr) = d;
1.164 + datalen = 8;
1.165 + } else {
1.166 + float f = (float)strtod(p,NULL);
1.167 + *((float *)dataptr) = f;
1.168 + datalen = 4;
1.169 + }
1.170 + } else {
1.171 + unsigned long value = strtoul(p, NULL, 16);
1.172 + if( len == 8 ) {
1.173 + *((unsigned int *)dataptr) = value;
1.174 + datalen = 4;
1.175 + } else if( len == 4 ) {
1.176 + *((unsigned short *)dataptr) = value;
1.177 + datalen = 2;
1.178 + } else if( len == 2 ) {
1.179 + *((unsigned char *)dataptr) = value;
1.180 + datalen = 1;
1.181 + }
1.182 + }
1.183 + dataptr += datalen;
1.184 + current->item[current_block].length += datalen;
1.185 + p = strtok(NULL, "\t\r\n ");
1.186 + }
1.187 + }
1.188 + }
1.189 + }
1.190 + fclose(f);
1.191 + return head;
1.192 +}
.