1 #include <stdlib.h>
2 #include <string.h>
3 #include <stdio.h>
4 #include "soundpipe.h"
5 #include "md5.h"
6 #include "test.h"
7 
8 
sp_test_create(sp_test ** t,uint32_t bufsize)9 int sp_test_create(sp_test **t, uint32_t bufsize)
10 {
11     uint32_t i;
12     *t = malloc(sizeof(sp_test));
13     sp_test *tp = *t;
14 
15     SPFLOAT *buf = malloc(sizeof(SPFLOAT) * bufsize);
16     for(i = 0; i < bufsize; i++) tp->buf = 0;
17 
18     tp->buf = buf;
19     tp->size = bufsize;
20     tp->pos = 0;
21     md5_init(&tp->state);
22     tp->md5string[32] = '\0';
23     tp->md5 = tp->md5string;
24     tp->mode = NORMAL;
25     return SP_OK;
26 }
27 
sp_test_destroy(sp_test ** t)28 int sp_test_destroy(sp_test **t)
29 {
30     sp_test *tp = *t;
31     free(tp->buf);
32     free(*t);
33     return SP_OK;
34 }
35 
sp_test_add_sample(sp_test * t,SPFLOAT sample)36 int sp_test_add_sample(sp_test *t, SPFLOAT sample)
37 {
38     if(t->pos < t->size) {
39         t->buf[t->pos] = sample;
40         t->pos++;
41     }
42     return SP_OK;
43 }
44 
sp_test_compare(sp_test * t,const char * md5hash)45 int sp_test_compare(sp_test *t, const char *md5hash)
46 {
47     md5_append(&t->state, (const md5_byte_t *)t->buf, sizeof(SPFLOAT) * t->size);
48     md5_finish(&t->state, t->digest);
49     int i;
50     char in[3], out[3];
51     int fail = 0;
52     for(i = 0; i < 16; i++) {
53         in[0] = md5hash[2 * i];
54         in[1] = md5hash[2 * i + 1];
55         in[2] = '\0';
56         sprintf(out, "%02x", t->digest[i]);
57         t->md5string[2 * i] = out[0];
58         t->md5string[2 * i + 1] = out[1];
59         if(strcmp(in, out)) {
60             fail = 1;
61         }
62     }
63 
64     if(fail) {
65         return SP_NOT_OK;
66     } else {
67         return SP_OK;
68     }
69 }
70 
sp_test_write_raw(sp_test * t,uint32_t index)71 int sp_test_write_raw(sp_test *t, uint32_t index)
72 {
73     char fname[20];
74     sprintf(fname, "%04d.raw", index);
75     FILE *fp = fopen(fname, "wb");
76     fwrite(t->buf, sizeof(SPFLOAT), t->size, fp);
77     fclose(fp);
78     return SP_OK;
79 }
80 
sp_test_verify(sp_test * t,const char * refhash)81 int sp_test_verify(sp_test *t, const char *refhash)
82 {
83     if(t->mode == NORMAL) {
84         int fail = 0;
85         if(sp_test_compare(t, refhash) == SP_NOT_OK) {
86             printf("Generated hash %s does not match reference hash %s\n",
87                     t->md5string, refhash);
88             fail = 1;
89         }
90         return fail;
91     } else {
92         sp_test_entry *tst = t->cur_entry;
93         sp_test_compare(t, refhash);
94         printf("TEST(t_%s, \"%s\", \"%s\")\n",
95                 tst->desc, tst->desc, t->md5string);
96         return 0;
97     }
98 }
99