2 * $Id: testdata.c,v 1.2 2006-08-02 04:13:15 nkeynes Exp $
6 * Copyright (c) 2006 Nathan Keynes.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
25 #define DEFAULT_SIZE 1024
27 /* get the next 32-byte aligned address that is no less than x */
28 #define ALIGN_32(x) ((char *)((((unsigned int)(x))+0x1F)&0xFFFFFFE0))
30 void free_test_dataset( test_data_t tests )
38 } while( next != NULL );
41 test_data_block_t get_test_data( test_data_t data, char *name )
44 for( i=0; i<MAX_DATA_BLOCKS; i++ ) {
45 if( data->item[i].name != NULL &&
46 strcmp(name, data->item[i].name) == 0 ) {
47 return &data->item[i];
53 void dump_test_dataset( FILE *f, test_data_t dataset )
55 test_data_t test = dataset;
57 while( test != NULL ) {
58 fprintf( f, "Test: %s\n", test->test_name );
59 for( i=0; i<MAX_DATA_BLOCKS; i++ ) {
60 if( test->item[i].name != NULL ) {
61 fprintf( f, "Block: %s, %d bytes\n", test->item[i].name, test->item[i].length );
62 fwrite_dump( f, test->item[i].data, test->item[i].length );
69 int test_block_compare( test_data_block_t block, char *result, int result_length )
71 if( block->length != result_length )
73 return memcmp( block->data, result, block->length );
78 * Load a batch of test data from the given IO stream.
80 test_data_t load_test_dataset( FILE *f )
82 test_data_t head = NULL;
83 test_data_t current = NULL;
84 test_data_t last = NULL;
86 int current_block = -1;
87 char *current_end = NULL;
92 while( fgets(buf, sizeof(buf), f ) != NULL ) {
94 while( isspace(*line) ) /* Trim leading whitespace */
96 if( line[0] == '[' ) { /* New test */
97 char *test_name = line+1;
98 char *end = strchr(test_name, ']');
101 current_size = DEFAULT_SIZE;
102 test_data_t test = malloc(current_size);
103 memset( test, 0, current_size );
105 dataptr = (char *)(test+1);
109 if( current != NULL )
110 current->next = test;
113 current_end = ((char *)test) + current_size;
115 strcpy( dataptr, test_name );
116 test->test_name = dataptr;
117 dataptr = ALIGN_32(dataptr + strlen(test_name)+1);
118 } else if( *line == '#' ) { /* Comment */
120 char *equals = strrchr( line, '=' );
121 if( equals != NULL ) {
122 char *block_name = line;
128 len = strlen(line)+1;
129 if( dataptr + len > current_end ) {
130 current_end += current_size;
132 current = realloc(current, current_size );
134 last->next = current;
137 strcpy( dataptr, block_name );
138 current->item[current_block].name = dataptr;
139 dataptr = ALIGN_32(dataptr+len);
140 current->item[current_block].data = dataptr;
141 current->item[current_block].length = 0;
144 while( isspace(*line) )
149 if( current == NULL || current_block == -1 )
151 char *p = strtok(line, "\t\r\n ");
153 if( dataptr + 8 > current_end ) {
154 int old_size = current_size;
155 current_end += current_size;
157 current = realloc(current, current_size );
158 memset( current + old_size, 0, old_size );
160 last->next = current;
164 char *dot = strchr(p, '.');
165 if( dot != NULL ) { /* FP */
166 if( p[len-1] == 'L' ) { /* Ending in L */
168 double d = strtod(p, NULL);
169 *((double *)dataptr) = d;
172 float f = (float)strtod(p,NULL);
173 *((float *)dataptr) = f;
177 unsigned long value = strtoul(p, NULL, 16);
179 *((unsigned int *)dataptr) = value;
181 } else if( len == 4 ) {
182 *((unsigned short *)dataptr) = value;
184 } else if( len == 2 ) {
185 *((unsigned char *)dataptr) = value;
190 current->item[current_block].length += datalen;
191 p = strtok(NULL, "\t\r\n ");
.