filename | test/testdata.c |
changeset | 266:2f811793bd0a |
prev | 263:6f641270b2aa |
next | 345:7a26147e5dce |
author | nkeynes |
date | Thu Jan 11 06:51:11 2007 +0000 (17 years ago) |
permissions | -rw-r--r-- |
last change | Fix crash in texcache_evict Fix crash decoding small VQ textures Implement mip-mapped textures of all types |
view | annotate | diff | log | raw |
1 /**
2 * $Id: testdata.c,v 1.4 2007-01-06 04:07:43 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 = malloc(current_size);
103 memset( test, 0, current_size );
105 dataptr = (char *)(test+1);
106 test->next = NULL;
107 if( head == NULL )
108 head = test;
109 if( current != NULL )
110 current->next = test;
111 last = current;
112 current = test;
113 current_end = ((char *)test) + current_size;
114 current_block = -1;
115 strcpy( dataptr, test_name );
116 test->test_name = dataptr;
117 dataptr = ALIGN_32(dataptr + strlen(test_name)+1);
118 } else if( *line == '#' ) { /* Comment */
119 } else {
120 char *equals = strrchr( line, '=' );
121 if( equals != NULL ) {
122 char *block_name = line;
123 int len;
124 char *p = equals;
125 *p-- = '\0';
126 while( isspace(*p) )
127 *p-- = '\0';
128 len = strlen(line)+1;
129 if( dataptr + len > current_end ) {
130 current_end += current_size;
131 current_size *= 2;
132 current = realloc(current, current_size );
133 if( last != NULL )
134 last->next = current;
135 }
136 current_block++;
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;
143 line = equals+1;
144 while( isspace(*line) )
145 line++;
146 }
148 /* Data */
149 if( current == NULL || current_block == -1 )
150 continue;
151 char *p = strtok(line, "\t\r\n ");
152 while( p != NULL ) {
153 if( dataptr + 8 > current_end ) {
154 int old_size = current_size;
155 current_end += current_size;
156 current_size *= 2;
157 current = realloc(current, current_size );
158 memset( current + old_size, 0, old_size );
159 if( last != NULL )
160 last->next = current;
161 }
162 int len = strlen(p);
163 int datalen = 0;
164 char *dot = strchr(p, '.');
165 if( dot != NULL ) { /* FP */
166 if( p[len-1] == 'L' ) { /* Ending in L */
167 p[len-1] = '\0';
168 double d = strtod(p, NULL);
169 *((double *)dataptr) = d;
170 datalen = 8;
171 } else {
172 float f = (float)strtod(p,NULL);
173 *((float *)dataptr) = f;
174 datalen = 4;
175 }
176 } else {
177 unsigned long value = strtoul(p, NULL, 16);
178 if( len == 8 ) {
179 *((unsigned int *)dataptr) = value;
180 datalen = 4;
181 } else if( len == 4 ) {
182 *((unsigned short *)dataptr) = value;
183 datalen = 2;
184 } else if( len == 2 ) {
185 *((unsigned char *)dataptr) = value;
186 datalen = 1;
187 }
188 }
189 dataptr += datalen;
190 current->item[current_block].length += datalen;
191 p = strtok(NULL, "\t\r\n ");
192 }
193 }
194 }
195 fclose(f);
196 return head;
197 }
199 int run_tests( test_func_t *test_fns ) {
200 int test_count, test_failures = 0;
202 for( test_count=0; test_fns[test_count] != NULL; test_count++ ) {
203 if( test_fns[test_count]() != 0 ) {
204 fprintf( stderr, "Test %d failed\n", test_count+1 );
205 test_failures++;
206 }
207 }
209 /* report */
210 fprintf( stderr, "%d/%d tests passed!\n", test_count - test_failures, test_count );
211 return test_failures;
212 }
.