forked from xupsh/pp4fpgas-cn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhuffman_encoding_test.cpp
66 lines (56 loc) · 2.15 KB
/
huffman_encoding_test.cpp
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
#include "huffman.h"
#include <stdio.h>
#include <stdlib.h>
void file_to_array(const char *filename, ap_uint<16> *&array, int array_length) {
printf("Start reading file [%s]\n", filename);
FILE *file = fopen(filename, "r");
if(file == NULL) {
printf("Cannot find the input file\n");
exit(1);
}
int file_value = 0;
int count = 0;
array = (ap_uint<16> *) malloc(array_length*sizeof(ap_uint<16>));
while(1) {
int eof_check = fscanf(file, "%x", &file_value);
if(eof_check == EOF) break;
else {
array[count++] = (ap_uint<16>) file_value ;
}
}
fclose(file);
if(count != array_length) exit(1);
}
int main() {
printf("Starting canonical Huffman encoding testbench\n");
FILE *output_file;
int return_val = 0;
ap_uint<16> *frequencies = NULL;
file_to_array("huffman.random256.txt", frequencies, INPUT_SYMBOL_SIZE);
Symbol in[INPUT_SYMBOL_SIZE];
for (int i = 0 ; i < INPUT_SYMBOL_SIZE; i++) {
in[i].frequency = frequencies[i];
in[i].value = i;
}
int num_nonzero_symbols;
PackedCodewordAndLength encoding[INPUT_SYMBOL_SIZE];
huffman_encoding(in, encoding, &num_nonzero_symbols);
output_file = fopen("huffman.random256.out", "w");
for(int i = 0; i < INPUT_SYMBOL_SIZE; i++)
fprintf(output_file, "%d, %x\n", i, (unsigned int) encoding[i]);
fclose(output_file);
printf ("\n***************Comparing against output data*************** \n\n");
if (system("diff huffman.random256.out huffman.random256.golden")) {
fprintf(stdout, "*******************************************\n");
fprintf(stdout, "FAIL: Output DOES NOT match the golden output\n");
fprintf(stdout, "*******************************************\n");
return_val = 1;
} else {
fprintf(stdout, "*******************************************\n");
fprintf(stdout, " PASS: The output matches the golden output\n");
fprintf(stdout, "*******************************************\n");
return_val = 0;
}
printf("Ending canonical Huffman encoding testbench\n");
return return_val;
}