-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunit_test.cpp
171 lines (133 loc) · 4.4 KB
/
unit_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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
//
// Created by wuyuchun on 16-6-22.
//
//!测试文件
#include "unit_test.h"
#include <cstdlib>
#include <iostream>
#include <streambuf>
#include <math.h>
#include <opencv2/opencv.hpp>
#include "GreedyAlgo.h"
#include "chineseAndnumber.h"
#include "zipzap.h"
#include "imgHash.h"
#include "utility.h"
#include "algorithms.h"
//!测试三种策略策略时候的场景
void testFunc1()
{
KNAPSACK_PROBLEM problem;
OBJECT obj1(35,10,0), obj2(30,40,0), obj3(60,30,0), obj4(50,50,0),
obj5(40,35,0), obj6(10,40,0), obj7(25,30,0);
problem.totalc = 150;
problem.objs.push_back(obj1);
problem.objs.push_back(obj2);
problem.objs.push_back(obj3);
problem.objs.push_back(obj4);
problem.objs.push_back(obj5);
problem.objs.push_back(obj6);
problem.objs.push_back(obj7);
std::cout << "------------------------第一种情况---------------------------" << std::endl;
GreadyAlgo(&problem, Choosefunc1);
std::cout << "------------------------第二种情况---------------------------" << std::endl;
for (int i = 0; i < problem.objs.size(); ++i) { //初始化
problem.objs[i].status = 0;
}
GreadyAlgo(&problem, Choosefunc2);
std::cout << "------------------------第三种情况---------------------------" << std::endl;
for (int i = 0; i < problem.objs.size(); ++i) { //初始化
problem.objs[i].status = 0;
}
GreadyAlgo(&problem, Choosefunc3);
}
void testFunc2(){
int number(10000000);
std::string chineseNumber;
NumberToChinese(number, chineseNumber);
std::cout << "中文数字: " << chineseNumber << std::endl;
}
void testFunc3(){
union {
float f;
unsigned int u;
} p;
p.f = -13.0;
unsigned int sign = (p.u >> 31) & 1;
unsigned int exp = (p.u >> 23) & 0xff;
unsigned int coef_mask = (1 << 23) -1;
unsigned int coef = p.u & coef_mask;
std::cout << "sign: " << sign << "\nexp:" << exp << "\ncoef: " << coef << std::endl;
}
double get_seconds(){
struct timespec ts;
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);
return ts.tv_sec + ts.tv_nsec / 1e9;
}
void testFunc4(){
#if 0
int zz = int_to_zigzag(-1);
int rr = zigzag_to_int(zz);
#endif
int a[]={(int)(-pow(2L,32)),-1000, -1, 0, 1, 1000, (int)(pow(2L,31)-1L)};
for (int i = 0; i < sizeof(a) / sizeof(int) ; ++i) {
int n = a[i];
char str[INT_BIT_SIZE+INT_BIT_SIZE/BYTE_BITS+1];
int_to_binary_str(n,str,sizeof(str));
int zz = int_to_zigzag(n);
char str_zz[INT_BIT_SIZE+INT_BIT_SIZE/BYTE_BITS+1];
int_to_binary_str(zz,str_zz,sizeof(str_zz));
byte write_buffer[5];
int to_write_size = write_to_buffer(zz,write_buffer,sizeof(write_buffer));
char str_write[to_write_size * BYTE_BITS+ to_write_size +1];
bytes_to_binary_str(write_buffer,to_write_size,str_write, sizeof(str_write));
int read_num = read_from_buffer(write_buffer,to_write_size);
int last_num =zigzag_to_int(read_num);
printf("%11d [%s] ==to-zigzag==> %6d [%s] ====to-buf===> %s\n",n,str,zz,str_zz,str_write);
}
}
//!计算图像的相似度
void testFunc5(){
cv::Mat img = cv::imread("../image/1.jpg");
cv::Mat img2 = cv::imread("../image/2.jpeg");
if( img.empty() || img2.empty() ){
std::cout << "open img Err!" << std::endl;
}
imgHash test(img);
unsigned long lRet(0);
lRet = test.getHashValue();
std::cout << "img Hash Value: " << lRet << std::endl;
imgHash test2(img2);
unsigned long lRet2(0);
lRet2 = test2.getHashValue();
std::cout << "img2 Hash Value: " << lRet2 << std::endl;
std::cout << "相似度: " << lRet2 - lRet << std::endl;
}
void testFunc6(){
int iTestNum[] = { -24,-6,-4,0,3,8,28 };
for (int i = 0; i < sizeof(iTestNum)/sizeof(iTestNum[0]); ++i) {
adjust8Mult(iTestNum[i]);
}
}
void testFunc7(){
calcDicePro();
std::cout << std::endl;
}
void testFunc8(){
outbuf ob;
std::ostream out(&ob);
out << "31 heaxdecimal: " << std::endl;
}
void testFunc9(){
float val = 4.0;
float ret = FastInvSqrt(val);
std::cout << "快速计算倒平方根的值: " << ret << std::endl;
}
void testFunc10(){
char sz[]="abcdef";
std::cout << "origin sz:“" << sz << std::endl;
ratorWord(sz, 6, 3);
std::cout << "new sz: " << sz << std::endl;
LeftRotateString(sz,6,3);
std::cout << "rote: " << sz << std::endl;
}