forked from RangeNetworks/CommonLibs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
UtilsTest.cpp
141 lines (121 loc) · 3.67 KB
/
UtilsTest.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
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
#include <string>
#include <stdio.h>
#include "Configuration.h"
#include "Utils.h"
ConfigurationTable gConfig; // Needed for logger.
using namespace std;
struct foo : public RefCntBase {
string id;
foo(string wid) : id(wid) { printf("create foo %s %p\n",id.c_str(),this); }
~foo() { printf("delete foo %s %p\n",id.c_str(),this); }
};
typedef RefCntPointer<foo> fooref;
RefCntPointer<foo> refize(foo*f)
{
fooref ref(f);
return ref;
}
void test1() // Basic test.
{
foo *b = new foo("b");
fooref bref(b);
bref.free(); // Should free b.
}
void test2() // assignment
{
foo *c = new foo("c");
fooref cref = refize(c);
foo *d = new foo("d");
fooref dref = d;
fooref dref2 = d;
dref = c;
printf("here\n");
// d and c deleted when test2 returns.
}
void test3() // assignment
{
foo *c = new foo("c");
fooref cref = refize(c);
foo *d = new foo("d");
fooref dref = d;
dref = c;
printf("here\n");
// d and c deleted when test2 returns.
}
void refcntpointertest()
{
test1();
test2();
test3();
}
// Throw spaces into the string.
string messup(string in)
{
string result;
for (string::iterator it = in.begin(); it != in.end(); it++) {
if (rand() & 1) result.push_back(' ');
result.push_back(*it);
}
return result;
}
void asciiEncoderTest(bool verbose)
{
char tbuf[102]; // binary data to try encoding and then decoding.
int numtests = 0;
printf("asciiEncoderTest...\n");
for (unsigned len = 0; len < 100; len++) { // Length of binary data.
for (unsigned testno = 0; testno < 100; testno++) {
numtests++;
for (unsigned charno = 0; charno < len; charno++) {
tbuf[charno] = rand();
}
string binaryinput = string(tbuf,len);
for (unsigned type = 0; type < 2; type++) {
const char *method = type ? "base64" : "hex";
string errorMessage;
string encoded = encodeToString(tbuf,len,method,errorMessage);
if (testno & 1) encoded = messup(encoded);
string decoded = decodeToString(encoded.data(),encoded.size(),method,errorMessage);
// Check our results.
if (decoded.size() != len) { printf("FAIL %s len=%d size=%d encoded=%s\n",method,len,decoded.size(),encoded.c_str()); }
if (binaryinput != decoded) { printf("FAIL %s testno=%d len=%d encoded=%s\n",method,testno,len,encoded.c_str()); }
// Paranoid double check manually:
for (unsigned j = 0; j < len; j++) {
if (tbuf[j] != decoded[j]) { printf("FAIL %s len=%d j=%d\n",method,len,j); }
}
if (errorMessage.size()) {
printf("%s error message=%s\n",method,errorMessage.c_str());
}
if (verbose) printf("TEST %s len=%d test=%d encoded=%s\n",method,len,testno,encoded.c_str());
}
/****
{ // Hex encoder/decoder tests.
string errorMessage = "";
string hexencoded = encodeToString(tbuf,len,"hex",errorMessage);
if (hexencoded.size() != 2 * len) {
printf("FAIL hex encoded size=%d expected=%d\n",hexencoded.size(),2*len);
}
string hexdecoded = decodeToString(hexencoded.data(),hexencoded.size(),"hex",errorMessage);
//printf("input size=%d hexencoded size=%d hexdecoded size=%d %s\n",len,hexencoded.size(),hexdecoded.size(),hexencoded.c_str());
string e2 = encodeToString(hexdecoded.data(),hexdecoded.size(),"hex",errorMessage);
//printf("in=%s out=%s\n",hexencoded.c_str(),e2.c_str());
if (e2 != hexencoded) {
printf("FAIL hex here\n");
}
if (binaryinput != hexdecoded) { printf("FAIL hex testno=%d len=%d\n",testno,len); }
if (errorMessage.size()) {
printf("base64 error message=%s\n",errorMessage.c_str());
}
}
****/
}
}
printf("PASS %d tests\n",numtests);
}
int main(int argc, char **argv)
{
bool verbose = argc > 1;
printf("Utils Test starting.\n");
refcntpointertest();
asciiEncoderTest(verbose);
}