-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.c
143 lines (111 loc) · 2.73 KB
/
test.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
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
/**
* Copyright (C) 2017 Yuzhong Wen <[email protected]>
*
* basehan test cases
*
* Distributed under terms of the MIT license.
*/
#include <string.h>
#include <stdio.h>
ssize_t get_encode_len(ssize_t in)
{
return ((in + 7) / 8) * 15;
}
ssize_t get_decode_len(ssize_t out)
{
return (out / 15) * 8;
}
void test_000()
{
char in[1024]="这是一个测试项,用来测试utf8";
char out[1024];
ssize_t len_in = strlen(in);
printf("\n==Testing UTF-8 encoding/decoding ===\n");
printf("%s\n", in);
memset(out, 0, 1024);
len_in = cjk_mapping_encode(in, len_in, out, get_encode_len(len_in));
printf("%s\n", out);
memset(in, 0, 1024);
cjk_mapping_decode(out, len_in, in, get_decode_len(len_in));
printf("%s\n", in);
}
void test_001()
{
char in[1024]="lel";
char out[1024];
ssize_t len_in = strlen(in);
printf("\n==Testing ASCII encoding/decoding ===\n");
printf("%s\n", in);
memset(out, 0, 1024);
len_in = cjk_mapping_encode(in, len_in, out, get_encode_len(len_in));
printf("%s\n", out);
memset(in, 0, 1024);
cjk_mapping_decode(out, len_in, in, get_decode_len(len_in));
printf("%s\n", in);
}
void test_002()
{
char in[1024]="llellellellellellellellellellellellellellellellellellelel";
char out[1024];
ssize_t len_in = strlen(in);
printf("\n==Testing Long ASCII encoding/decoding ===\n");
printf("%s\n", in);
memset(out, 0, 1024);
len_in = cjk_mapping_encode(in, len_in, out, get_encode_len(len_in));
printf("%s\n", out);
memset(in, 0, 1024);
cjk_mapping_decode(out, len_in, in, get_decode_len(len_in));
printf("%s\n", in);
}
void test_003()
{
char in[1024]={0x2, 0x3, 0x4, 0x5, 0x6, 0x7};
char out[1024];
int i;
ssize_t len_in = 6;
printf("\n==Testing binary encoding/decoding ===\n");
for (i = 0; i < 6; i++)
printf("%x ", in[i]);
printf("\n");
memset(out, 0, 1024);
len_in = cjk_mapping_encode(in, len_in, out, get_encode_len(len_in));
printf("%s\n", out);
memset(in, 0, 1024);
cjk_mapping_decode(out, len_in, in, get_decode_len(len_in));
for (i = 0; i < 6; i++)
printf("%x ", in[i]);
printf("\n");
printf("=====\n");
}
void test_004()
{
char in[1024]={0x2, 0x3, 0x4, 0x5,
0x6, 0x7, 0x3, 0x4,
0x5, 0x6, 0x7, 0x3,
0x4, 0x5, 0x6, 0x7};
char out[1024];
int i;
ssize_t len_in = 16;
printf("\n==Testing long binary encoding/decoding ===\n");
for (i = 0; i < 16; i++)
printf("%x ", in[i]);
printf("\n");
memset(out, 0, 1024);
len_in = cjk_mapping_encode(in, len_in, out, get_encode_len(len_in));
printf("%s\n", out);
memset(in, 0, 1024);
cjk_mapping_decode(out, len_in, in, get_decode_len(len_in));
for (i = 0; i < 16; i++)
printf("%x ", in[i]);
printf("\n");
printf("=====\n");
}
int main()
{
test_000();
test_001();
test_002();
test_003();
test_004();
return 0;
}