Search
lxdream.org :: lxdream/test/testdata.c
lxdream 0.9.1
released Jun 29
Download Now
filename test/testdata.c
changeset 185:6755a04c447f
next190:f7653df5e832
author nkeynes
date Tue Jul 11 01:35:27 2006 +0000 (15 years ago)
permissions -rw-r--r--
last change First commit of system test framework. 3 initial test cases (incomplete):
testide, testmath, and testta
view annotate diff log raw
     1 /**
     2  * $Id: testdata.c,v 1.1 2006-07-11 01:35:23 nkeynes Exp $
     3  * 
     4  * Test data loader.
     5  *
     6  * Copyright (c) 2006 Nathan Keynes.
     7  *
     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.
    12  *
    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.
    17  */
    19 #include <stdlib.h>
    20 #include <string.h>
    21 #include <ctype.h>
    22 #include <math.h>
    23 #include "testdata.h"
    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 ) 
    31 {
    32     test_data_t next;
    34     do {
    35 	next = tests->next;
    36 	free(tests);
    37 	tests = next;
    38     } while( next != NULL );
    39 }
    41 test_data_block_t get_test_data( test_data_t data, char *name )
    42 {
    43     int i;
    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];
    48 	}
    49     }
    50     return NULL;
    51 }
    53 void dump_test_dataset( FILE *f, test_data_t dataset )
    54 {
    55     test_data_t test = dataset;
    56     int i;
    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 );
    63 	    }
    64 	}
    65 	test = test->next;
    66     }
    67 }
    69 int test_block_compare( test_data_block_t block, char *result, int result_length )
    70 {
    71     if( block->length != result_length )
    72 	return -1;
    73     return memcmp( block->data, result, block->length );
    74 }
    77 /**
    78  * Load a batch of test data from the given IO stream.
    79  */
    80 test_data_t load_test_dataset( FILE *f )
    81 {
    82     test_data_t head = NULL;
    83     test_data_t current = NULL;
    84     test_data_t last = NULL;
    85     int current_size = 0;
    86     int current_block = -1;
    87     char *current_end = NULL;
    88     char *dataptr = NULL;
    90     char buf[512];
    91     char *line;
    92     while( fgets(buf, sizeof(buf), f ) != NULL ) {
    93 	line = buf;
    94 	while( isspace(*line) ) /* Trim leading whitespace */
    95 	    line++;
    96 	if( line[0] == '[' ) { /* New test */
    97 	    char *test_name = line+1;
    98 	    char *end = strchr(test_name, ']');
    99 	    if( end != NULL )
   100 		*end = '\0';
   101 	    current_size = DEFAULT_SIZE;
   102 	    test_data_t test = calloc(current_size, 1);
   104 	    dataptr = (char *)(test+1);
   105 	    test->next = NULL;
   106 	    if( head == NULL )
   107 		head = test;
   108 	    if( current != NULL )
   109 		current->next = test;
   110 	    last = current;
   111 	    current = test;
   112 	    current_end = ((char *)test) + current_size;
   113 	    current_block = -1;
   114 	    strcpy( dataptr, test_name );
   115 	    test->test_name = dataptr;
   116 	    dataptr = ALIGN_32(dataptr + strlen(test_name)+1);
   117 	} else if( *line == '#' ) { /* Comment */
   118 	} else {
   119 	    char *equals = strrchr( line, '=' );
   120 	    if( equals != NULL ) {
   121 		char *block_name = line;
   122 		int len;
   123 		*equals-- = '\0';
   124 		while( isspace(*equals) )
   125 		    *equals-- = '\0';
   126 		len = strlen(line)+1;
   127 		if( dataptr + len > current_end ) {
   128 		    current_end += current_size;
   129 		    current_size *= 2;
   130 		    current = realloc(current, current_size );
   131 		    if( last != NULL )
   132 			last->next = current;
   133 		}
   134 		current_block++;
   135 		strcpy( dataptr, block_name );
   136 		current->item[current_block].name = dataptr;
   137 		dataptr = ALIGN_32(dataptr+len);
   138 		current->item[current_block].data = dataptr;
   139 		current->item[current_block].length = 0;
   140 	    } else {
   141 		/* Data */
   142 		if( current == NULL || current_block == -1 )
   143 		    continue;
   144 		char *p = strtok(line, "\t\r\n ");
   145 		while( p != NULL ) {
   146 		    if( dataptr + 8 > current_end ) {
   147 			current_end += current_size;
   148 			current_size *= 2;
   149 			current = realloc(current, current_size );
   150 			if( last != NULL )
   151 			    last->next = current;
   152 		    }
   153 		    int len = strlen(p);
   154 		    int datalen = 0;
   155 		    char *dot = strchr(p, '.');
   156 		    if( dot != NULL ) { /* FP */
   157 			if( p[len-1] == 'L' ) { /* Ending in L */
   158 			    p[len-1] = '\0';
   159 			    double d = strtod(p, NULL);
   160 			    *((double *)dataptr) = d;
   161 			    datalen = 8;
   162 			} else {
   163 			    float f = (float)strtod(p,NULL);
   164 			    *((float *)dataptr) = f;
   165 			    datalen = 4;
   166 			}
   167 		    } else {
   168 			unsigned long value = strtoul(p, NULL, 16);
   169 			if( len == 8 ) {
   170 			    *((unsigned int *)dataptr) = value;
   171 			    datalen = 4;
   172 			} else if( len == 4 ) {
   173 			    *((unsigned short *)dataptr) = value;
   174 			    datalen = 2;
   175 			} else if( len == 2 ) {
   176 			    *((unsigned char *)dataptr) = value;
   177 			    datalen = 1;
   178 			}
   179 		    }
   180 		    dataptr += datalen;
   181 		    current->item[current_block].length += datalen;
   182 		    p = strtok(NULL, "\t\r\n ");
   183 		}
   184 	    }
   185 	}
   186     }
   187     fclose(f);
   188     return head;
   189 }
.