File: test.c

package info (click to toggle)
libxmp 4.6.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,040 kB
  • sloc: ansic: 60,013; sh: 3,937; makefile: 467
file content (151 lines) | stat: -rw-r--r-- 2,844 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../src/md5.h"
#include "xmp.h"

#ifndef LIBXMP_CORE_DISABLE_IT

#ifdef LIBXMP_NO_DEPACKERS
#define TEST_IT_FILE "test.it"
#else
#define TEST_IT_FILE "test.itz"
#endif
#define TEST_PLAY_TIME	4800
#define TEST_MD5_IEEE	"0fb814a84db24a21d93851cbeebe2a98"
#define TEST_MD5_X87	"97eb1ff2bb3ee8252133cdee90fb162d"

#else /* !LIBXMP_CORE_DISABLE_IT */

#define TEST_IT_FILE	"test.xm"
#define TEST_PLAY_TIME	8000
#define TEST_MD5_IEEE	"089e2fcddb8989d04d5004876d642139"
#define TEST_MD5_X87	"089e2fcddb8989d04d5004876d642139"

#endif

static inline int is_big_endian(void) {
	unsigned short w = 0x00ff;
	return (*(char *)&w == 0x00);
}

/* Convert little-endian 16 bit samples to big-endian */
static void convert_endian(unsigned char *p, int l)
{
	unsigned char b;
	int i;

	for (i = 0; i < l; i++) {
		b = p[0];
		p[0] = p[1];
		p[1] = b;
		p += 2;
	}
}

static int compare_md5(const unsigned char *d, const char *digest)
{
	int i;

	/*
	for (i = 0; i < 16 ; i++)
		printf("%02x", d[i]);
	printf("\n");
	*/

	for (i = 0; i < 16 && *digest; i++, digest += 2) {
		char hex[3];
		hex[0] = digest[0];
		hex[1] = digest[1];
		hex[2] = 0;

		if (d[i] != strtoul(hex, NULL, 16))
			return -1;
	}

	return 0;
}

int main(void)
{
	int ret;
	xmp_context c;
	struct xmp_frame_info info;
	long time;
	unsigned char digest[16];
	MD5_CTX ctx;

	c = xmp_create_context();
	if (c == NULL)
		goto err;

	ret = xmp_load_module(c, TEST_IT_FILE);
	if (ret != 0) {
		printf("can't load module\n");
		goto err;
	}

	xmp_get_frame_info(c, &info);
	if (info.total_time != TEST_PLAY_TIME) {
		printf("estimated replay time error: %d\n", info.total_time);
		goto err;
	}

	xmp_start_player(c, 22050, 0);
	xmp_set_player(c, XMP_PLAYER_MIX, 100);
	xmp_set_player(c, XMP_PLAYER_INTERP, XMP_INTERP_SPLINE);

	printf("Testing ");
	fflush(stdout);
	time = 0;

	MD5Init(&ctx);

	while (1) {
		xmp_play_frame(c);
		xmp_get_frame_info(c, &info);
		if (info.loop_count > 0)
			break;

		time += info.frame_time;

		if (is_big_endian())
			convert_endian((unsigned char *)info.buffer, info.buffer_size >> 1);

		MD5Update(&ctx, (unsigned char *)info.buffer, info.buffer_size);

		printf(".");
		fflush(stdout);
	}

	MD5Final(digest, &ctx);

	/*
	  x87 floating point results in a very slightly different output from
	  SSE and other floating point implementations, so check two hashes.
	 */
	if (compare_md5(digest, TEST_MD5_IEEE) < 0 &&
	    compare_md5(digest, TEST_MD5_X87) < 0) {
		printf("rendering error\n");
		goto err;
	}

	if ((time + 500) / 1000 != info.total_time) {
		printf("replay time error: %ld\n", time);
		goto err;
	}

	printf(" pass\n");

	xmp_release_module(c);
	xmp_free_context(c);
	exit(0);

    err:
	printf(" fail\n");
	if (c) {
		xmp_release_module(c);
		xmp_free_context(c);
	}
	exit(1);
}