-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibtest.cpp
113 lines (79 loc) · 2.92 KB
/
libtest.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
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
/* LZW (variable code length) static library test.
* Copyright (c) 2021 Yuriy Yakimenko
*
* Permission to use, copy, modify, distribute, and sell this software and its
* documentation for any purpose is hereby granted without fee, provided that
* the above copyright notice appear in all copies and that both that
* copyright notice and this permission notice appear in supporting
* documentation. No representations are made about the suitability of this
* software for any purpose. It is provided "as is" without express or
* implied warranty.
*/
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cassert>
#include "export.h"
#include <chrono>
#include <iostream>
#include "common.h"
#if defined(__linux__)
#define LIBTEST_MAIN
#endif
#ifdef LIBTEST_MAIN // add LIBTEST #define to compile the test with static library
int main (int argc, char *argv[])
{
int bits = DEFAULT_MAX_BITS;
bool bits_set = false;
char inputFile [PATH_MAX] = { 0 };
char compressedFile [PATH_MAX] = { 0 };
char outputFile [PATH_MAX] = { 0 };
if (2 == argc)
{
strcpy_s (inputFile, PATH_MAX, argv[1]);
}
else if (3 == argc)
{
if (0 == strncmp (argv[1], "-b", 2))
{
char *ptr = argv[1];
bits = atoi (ptr + 2);
bits_set = true;
strcpy_s(inputFile, PATH_MAX, argv[2]);
}
}
if (strlen (inputFile) == 0 || (bits < 9 || bits > SUPPORTED_MAX_BITS))
{
printf ("Usage: %s [-b{12-16}] fileToCompress\n", argv[0]);
return EXIT_FAILURE;
}
printf ("Testing compression on: %s\n", inputFile);
tmpnam_s (compressedFile, sizeof(compressedFile));
tmpnam_s (outputFile, sizeof(outputFile));
assert (strlen(compressedFile) > 0 && strlen (outputFile) > 0);
std::chrono::high_resolution_clock::time_point start;
start = std::chrono::high_resolution_clock::now();
int ret = 0;
if (!bits_set)
ret = Compress (inputFile, compressedFile, VERBOSE_OUTPUT);
else
ret = Compress2 (inputFile, compressedFile, VERBOSE_OUTPUT, bits);
printf ("Compression %s.\n", ret ? "successful" : "failed");
if (!ret)
return EXIT_FAILURE;
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
std::cout << duration.count() << " microsecs\n";
start = std::chrono::high_resolution_clock::now();
ret = Decompress (compressedFile, outputFile, VERBOSE_OUTPUT);
printf ("Decompression %s.\n", ret ? "successful" : "failed");
if (!ret)
return EXIT_FAILURE;
end = std::chrono::high_resolution_clock::now();
duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
std::cout << duration.count() << " microsecs\n";
remove (compressedFile);
remove (outputFile);
return EXIT_SUCCESS;
}
#endif // LIBTEST_MAIN