-
Notifications
You must be signed in to change notification settings - Fork 66
/
test_input.c
53 lines (45 loc) · 1.3 KB
/
test_input.c
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
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "shoco.h"
#define BUFFER_SIZE 1024
static double ratio(int original, int compressed) {
return ((double)(original - compressed) / (double)original);
}
static int percent(double ratio) {
return ratio * 100;
}
static void print(char *in, double ratio) {
printf("'%s' (%d%%)\n", in, percent(ratio));
}
int main(int argc, char ** argv) {
char buffer[BUFFER_SIZE] = { 0 };
char comp[BUFFER_SIZE] = { 0 };
char out[BUFFER_SIZE] = { 0 };
int count = 0;
double ratios = 0;
double rat = 0;
int inlen, complen, outlen;
while (fgets(buffer, BUFFER_SIZE, stdin) != NULL) {
char *end = strchr(buffer, '\n');
if (end != NULL)
*end = '\0';
else
break;
inlen = strlen(buffer);
complen = shoco_compress(buffer, 0, comp, BUFFER_SIZE);
outlen = shoco_decompress(comp, complen, out, BUFFER_SIZE);
rat = ratio(inlen, complen);
if (complen != 0) {
++count;
ratios += rat;
}
if ((argc > 1) && ((strcmp(argv[1], "-v") == 0) || (strcmp(argv[1], "--verbose") == 0))) {
print(buffer, rat);
}
assert(inlen == outlen);
assert(strcmp(buffer, out) == 0);
}
printf("Number of compressed strings: %d, average compression ratio: %d%%\n", count, percent(ratios / count));
return 0;
}